Skip to main content

KcpServerWrapper

A class in Alta.NetworkTransport.Runtime, Alta.NetworkingTransport.Kcp.

Server side of the KCP transport: wraps a kcp2k KcpServer as an ITransportServer and keeps one KcpServerConnection per connected player.

Constructors​

MemberWhat it is
KcpServerWrapper(KcpTransport transport, KcpSettings settings)Creates a KcpServerWrapper from transport and settings.

Fields​

7 private and internal fields
MemberWhat it is
private readonly Dictionary<int, KcpServerConnection> connectionsCurrent connections keyed by kcp2k connection id.
private static readonly Logger LoggerNLog logger for this class.
private IPacketProcessor processorThe packet processor passed to the last ProcessIncoming call.
private List<byte[]> receiveBuffersCacheDeclared but never used in this build.
private readonly KcpServer serverThe kcp2k server; a KcpServerNonAlloc when KcpSettings.NonAlloc is on.
private readonly KcpSettings settingsDeclared but never assigned; the constructor reads its settings argument directly.
private readonly KcpTransport transportThe KcpTransport this server belongs to, passed to each new connection.

Properties​

MemberWhat it is
int IncomingPacketsTotal { get; private set; }Count of data packets received from all clients since the server wrapper was made.
int OutgoingBytesTotal { get; }Total bytes sent, summed over all current connections.
int OutgoingPacketsTotal { get; }Total packets sent, summed over all current connections.
int OutgoingQueueSize { get; }Messages waiting in kcp2k's send queues, summed over all connections.

Events​

MemberWhat it is
event Action<ITransportConnection> ConnectionCreatedRaised with the new connection when a client connects.
event Action<ITransportConnection> ConnectionDestroyedRaised with the connection after a client disconnects and it is removed from the list.
event Action<AltaNetworkError> OnErrorRaised with the converted error whenever kcp2k reports one for any connection.

Methods​

MemberWhat it is
ArraySegment<byte> CopyDataForCache(ArraySegment<byte> input, int processedBytes = 0)Copies the unprocessed part of a received segment into a pooled 64 KB buffer from MessageReceivePool, so it can be kept after the callback.
INetworkTransportResult ProcessIncoming(IPacketProcessor processor)Stores the processor for HandleData, then ticks kcp2k's incoming side. Always returns success.
INetworkTransportResult ProcessOutgoing()Ticks kcp2k's outgoing side, flushing queued sends. Always returns success.
void Start(ushort port)Starts the kcp2k server listening on the given port.
INetworkTransportResult Terminate()Stops the kcp2k server. Always returns success.
4 private and internal methods
MemberWhat it is
private void HandleConnection(int connectionIdentifier)kcp2k connect callback; creates a KcpServerConnection for the id, stores it and raises ConnectionCreated.
private void HandleData(int connectionId, ArraySegment<byte> data, KcpChannel channel)kcp2k data callback. Reads the first byte as the game's channel and passes the rest to the processor; logs details and rethrows if that fails.
private void HandleDisconnection(int connectionIdentifier)kcp2k disconnect callback; removes the connection, marks it disconnected and raises ConnectionDestroyed.
private void HandleError(int connectionId, ErrorCode error, string message)kcp2k error callback; converts the code to an AltaNetworkError, raises OnError and logs it with the connection id.