public void ReadDisconnectPayloadTest() { var buffer = ByteBufferAllocator.NewBuffer(DataSerializer.Endian); var detailMessage = "Invalid CommandType"; var srcDetail = Encoding.UTF8.GetBytes(detailMessage); buffer.WriteInt((int)DisconnectReason.InvalidDataFormat); buffer.WriteInt(srcDetail.Length); buffer.WriteBytes(srcDetail); byte[] payload = buffer.ToArray(); int offset = 0; DisconnectReason disconnectType = (DisconnectReason)ByteRead.GetInt(payload, ref offset); int detailLength = ByteRead.GetInt(payload, ref offset); byte[] dstDetail = new byte[detailLength]; ByteRead.ReadBytes(payload, dstDetail, offset, 0, detailLength); Assert.AreEqual(DisconnectReason.InvalidDataFormat, disconnectType); Assert.AreEqual(srcDetail.Length, detailLength); Assert.AreEqual(detailMessage, Encoding.UTF8.GetString(dstDetail)); }
/// <summary> /// Process a received command. /// (A ack command is processed by a receive thread, other commands is dispatched in a rendering thread.) /// </summary> private void ExecuteReceiveCommand(IncomingCommand command) { if (_udpSocket.State != SocketState.Connected) { return; } switch (command.Type) { case CommandType.Acknowledge: // Already process a acknowledge command in a receive thread. return; case CommandType.Disconnect: byte[] payload = command.GetPayload(); int offset = 0; DisconnectReason disconnectType = (DisconnectReason)ByteRead.GetInt(payload, ref offset); var detailMessage = ByteRead.GetString <int>(payload, ref offset, Encoding.UTF8); Log(LogLevel.Error, "Disconnect this client[{0}] : {1}", disconnectType, detailMessage); Disconnect(disconnectType, false); return; case CommandType.Reliable: case CommandType.Unreliable: EnqueueIncomingCommand(command); return; case CommandType.Fragmented: if (command.FragmentNumber > command.FragmentCount || command.FragmentOffset >= command.TotalLength || command.FragmentOffset + command.GetPayload().Length > command.TotalLength) { Log(LogLevel.Error, "Received fragment has bad size: {0}", command); return; } if (EnqueueIncomingCommand(command)) { UdpChannel udpChannel = _channels[command.Channel]; ReliableReceiveQueue reliableReceiveQueue = udpChannel.ReliableReceiveQueue as ReliableReceiveQueue; if (reliableReceiveQueue != null) { reliableReceiveQueue.ReceiveFragmentCommand(command); } } return; default: Log(LogLevel.Error, "Unknown command received {0}", command.Type); return; } }