public void Send(int connectionId, ArraySegment <byte> segment, KcpChannel channel) { if (connections.TryGetValue(connectionId, out KcpServerConnection connection)) { connection.SendData(segment, channel); } }
public void TimeoutIsResetByMessage(KcpChannel channel) { server.Start(Port); ConnectClientBlocking(); // do nothing for 'Timeout / 2' seconds int firstSleep = Timeout / 2; Thread.Sleep(firstSleep); // send one reliable message client.Send(new ArraySegment <byte>(new byte[1]), channel); // update UpdateSeveralTimes(); // do nothing for exactly the remaining timeout time + 1 to be sure Thread.Sleep(Timeout - firstSleep + 1); // now update UpdateSeveralTimes(); // should still be connected Assert.That(client.connected, Is.True); Assert.That(server.connections.Count, Is.EqualTo(1)); }
public void SendData(ArraySegment <byte> data, KcpChannel channel) { // sending empty segments is not allowed. // nobody should ever try to send empty data. // it means that something went wrong, e.g. in Mirror/DOTSNET. // let's make it obvious so it's easy to debug. if (data.Count == 0) { // pass error to user callback. no need to log it manually. OnError(ErrorCode.InvalidSend, "KcpConnection: tried sending empty message. This should never happen. Disconnecting."); Disconnect(); return; } switch (channel) { case KcpChannel.Reliable: SendReliable(KcpHeader.Data, data); break; case KcpChannel.Unreliable: SendUnreliable(data); break; } }
private void Send(ArraySegment <byte> buffer, KcpChannel channel) { if (!IsConnected) { return; } client.SendData(buffer, channel); Interlocked.Increment(ref benchmarkStatistics.MessagesClientSent); }
public void Send(ArraySegment <byte> segment, KcpChannel channel) { if (connected) { connection.SendData(segment, channel); } else { Log.Warning("KCP: can't send because client not connected!"); } }
private void Broadcast(ArraySegment <byte> message, KcpChannel channel) { foreach (var connection in server.connections.Values) { connection.SendData(message, channel); } var messagesSent = server.connections.Count; Interlocked.Add(ref benchmarkStatistics.MessagesServerSent, messagesSent); }
public void ClientToServerMessage(KcpChannel channel) { server.Start(Port); ConnectClientBlocking(); byte[] message = { 0x01, 0x02 }; SendClientToServerBlocking(new ArraySegment <byte>(message), channel); Assert.That(serverReceived.Count, Is.EqualTo(1)); Assert.That(serverReceived[0].data.SequenceEqual(message), Is.True); Assert.That(serverReceived[0].channel, Is.EqualTo(channel)); }
public void SendData(ArraySegment <byte> data, KcpChannel channel) { switch (channel) { case KcpChannel.Reliable: SendReliable(KcpHeader.Data, data); break; case KcpChannel.Unreliable: SendUnreliable(data); break; } }
public void ClientToServerEmptyMessage(KcpChannel channel) { server.Start(Port); ConnectClientBlocking(); // sending empty messages is not allowed #if UNITY_2018_3_OR_NEWER UnityEngine.TestTools.LogAssert.Expect(UnityEngine.LogType.Warning, "KcpConnection: tried sending empty message. This should never happen. Disconnecting."); #endif byte[] message = new byte[0]; SendClientToServerBlocking(new ArraySegment <byte>(message), channel); Assert.That(serverReceived.Count, Is.EqualTo(0)); }
public EchoServer(Configuration config, BenchmarkStatistics benchmarkStatistics) : base(config) { this.config = config; this.benchmarkStatistics = benchmarkStatistics; communicationChannel = Kcp2kBenchmark.GetChannel(config.Transmission); var interval = (uint)Utilities.CalculateTimeout(config.ServerTickRate); server = new KcpServer(OnConnected, OnReceiveMessage, OnDisconnected, DualMode, NoDelay, interval); serverThread = new Thread(TickLoop); serverThread.Name = "Kcp2k Server"; serverThread.Priority = ThreadPriority.AboveNormal; }
public override void ServiceSend(KcpChannel channelId, ArraySegment <byte> segment, int connectionId = 0) { // switch to kcp channel. // unreliable or reliable. // default to reliable just to be sure. switch (channelId) { case KcpChannel.Unreliable: client.Send(segment, KcpChannel.Unreliable); break; default: client.Send(segment, KcpChannel.Reliable); break; } }
public void ClientToServerSlightlySmallerThanMTUSizedMessage(KcpChannel channel) { server.Start(Port); ConnectClientBlocking(); byte[] message = new byte[Kcp.MTU_DEF - 5]; for (int i = 0; i < message.Length; ++i) { message[i] = (byte)(i & 0xFF); } Log.Info($"Sending {message.Length} bytes = {message.Length / 1024} KB message"); SendClientToServerBlocking(new ArraySegment <byte>(message), channel); Assert.That(serverReceived.Count, Is.EqualTo(1)); Assert.That(serverReceived[0].data.SequenceEqual(message), Is.True); Assert.That(serverReceived[0].channel, Is.EqualTo(channel)); }
public void ServerToClientSlightlySmallerThanMTUSizedMessage(KcpChannel channel) { server.Start(Port); ConnectClientBlocking(); int connectionId = ServerFirstConnectionId(); byte[] message = new byte[Kcp.MTU_DEF - 5]; for (int i = 0; i < message.Length; ++i) { message[i] = (byte)(i & 0xFF); } SendServerToClientBlocking(connectionId, new ArraySegment <byte>(message), channel); Assert.That(clientReceived.Count, Is.EqualTo(1)); Assert.That(clientReceived[0].data.SequenceEqual(message), Is.True); Assert.That(clientReceived[0].channel, Is.EqualTo(channel)); }
private void UpdateKcp() { uint currMs = (uint)stopwatch.ElapsedMilliseconds; foreach (var e in kcpChannels) { byte channel = e.Key; KcpChannel kcpChannel = e.Value; kcpChannel.kcp.Update(currMs); int peekSize = 0; while ((peekSize = kcpChannel.kcp.PeekSize()) > 0) { byte[] buff = new byte[peekSize]; kcpChannel.kcp.Recv(buff); Packet p = new Packet(buff, buff.Length, channel); pendingRecvBuff.Enqueue(p); } } }
public void ServerToClientTwoMessages(KcpChannel channel) { server.Start(Port); ConnectClientBlocking(); int connectionId = ServerFirstConnectionId(); byte[] message = { 0x03, 0x04 }; SendServerToClientBlocking(connectionId, new ArraySegment <byte>(message), channel); byte[] message2 = { 0x05, 0x06 }; SendServerToClientBlocking(connectionId, new ArraySegment <byte>(message2), channel); Assert.That(clientReceived.Count, Is.EqualTo(2)); Assert.That(clientReceived[0].data.SequenceEqual(message), Is.True); Assert.That(clientReceived[0].channel, Is.EqualTo(channel)); Assert.That(clientReceived[1].data.SequenceEqual(message2), Is.True); Assert.That(clientReceived[1].channel, Is.EqualTo(channel)); client.Disconnect(); server.Stop(); }
public EchoClient(int id, Configuration config, BenchmarkStatistics benchmarkStatistics) : base(config) { this.id = id; this.config = config; this.benchmarkStatistics = benchmarkStatistics; noDelay = true; communicationChannel = Kcp2kBenchmark.GetChannel(config.Transmission); client = new KcpClientConnection(); client.OnAuthenticated = OnPeerConnected; client.OnData = OnNetworkReceive; client.OnDisconnected = OnPeerDisconnected; tickThread = new Thread(TickLoop); tickThread.Name = $"Kcp2k Client {id}"; tickThread.IsBackground = true; isConnected = false; isDisposed = false; }
private void HandShake() { while (state == CompState.handshake) { uint currMs = (uint)stopwatch.ElapsedMilliseconds; if (lastSendHandShakeTS + 100 <= currMs) { byte[] tokenBuff = Encoding.UTF8.GetBytes(token); Packet packet = new Packet(tokenBuff, tokenBuff.Length, 0); byte[] buff = packet.toBuff(); _SendBuff(buff, buff.Length); lastSendHandShakeTS = currMs; } Thread.Sleep(100); while (udpClient.Available > 0) { byte[] buff = udpClient.Receive(ref remoteEndPoint); Packet packet = Packet.fromBuff(buff); string conn_id = Encoding.UTF8.GetString(packet.data); Debug.Log("Handshake recv buff " + packet.channel + ", " + dumpBuff(packet.data, packet.length)); if (packet.channel == 0) { this.conn_id = uint.Parse(conn_id); byte[] channels = new byte[kcpChannels.Keys.Count]; kcpChannels.Keys.CopyTo(channels, 0); foreach (var channel in channels) { uint conv = this.conn_id * 0x100 + channel; kcpChannels[channel] = new KcpChannel(channel, conv, _SendBuff); } state = CompState.data; break; } } } }
private void Send(int connectionId, ArraySegment <byte> message, KcpChannel channel) { server.Send(connectionId, message, channel); Interlocked.Increment(ref benchmarkStatistics.MessagesServerSent); }
void SendServerToClientBlocking(int connectionId, ArraySegment <byte> message, KcpChannel channel) { server.Send(connectionId, message, channel); UpdateSeveralTimes(); }
void SendClientToServerBlocking(ArraySegment <byte> message, KcpChannel channel) { client.Send(message, channel); UpdateSeveralTimes(); }
public Message(byte[] data, KcpChannel channel) { this.data = data; this.channel = channel; }
public virtual void ServiceSend(KcpChannel channelId, ArraySegment <byte> segment, int connectionId) { }
// translate Kcp <-> Mirror channels static int FromKcpChannel(KcpChannel channel) => channel == KcpChannel.Reliable ? Channels.Reliable : Channels.Unreliable;
public void SendNetworkMessage(KcpChannel channelId, ArraySegment <byte> segment, int connectionId) { kcpServerService.ServiceSend(channelId, segment, connectionId); }
protected void ServerOnData(int connectionId, ArraySegment <byte> message, KcpChannel channel) { byte[] copy = new byte[message.Count]; Buffer.BlockCopy(message.Array, message.Offset, copy, 0, message.Count); serverReceived.Add(new Message(copy, channel)); }
// setup /////////////////////////////////////////////////////////////// protected void ClientOnData(ArraySegment <byte> message, KcpChannel channel) { byte[] copy = new byte[message.Count]; Buffer.BlockCopy(message.Array, message.Offset, copy, 0, message.Count); clientReceived.Add(new Message(copy, channel)); }