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));
        }
示例#2
0
 private void RedirectedHandling()
 {
     // For cases in which input has been redirected from a file
     while (true)
     {
         ByteRead?.Invoke(Console.Read());
     }
 }
        /// <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;
            }
        }
        public void ReadStringTest()
        {
            var buffer = ByteBufferAllocator.NewBuffer(DataSerializer.Endian);

            var detailMessage = "Hello,World!!!";
            var detailBytes   = Encoding.UTF8.GetBytes(detailMessage);

            buffer.WriteInt(detailBytes.Length);
            buffer.WriteBytes(detailBytes);

            int offset = 0;
            var result = ByteRead.GetString <int>(buffer.ToArray(), ref offset, Encoding.UTF8);

            Assert.AreEqual(detailMessage, result);
            Assert.AreEqual(sizeof(int) + detailMessage.Length, offset);
        }
        private void HandleInput()
        {
            var mappings = new Dictionary <ConsoleKey, byte[]>()
            {
                { ConsoleKey.Enter, new [] { (byte)'\n' } },
                { ConsoleKey.UpArrow, new [] { ESCCode, CSICode, (byte)'A' } },
                { ConsoleKey.DownArrow, new [] { ESCCode, CSICode, (byte)'B' } },
                { ConsoleKey.RightArrow, new [] { ESCCode, CSICode, (byte)'C' } },
                { ConsoleKey.LeftArrow, new [] { ESCCode, CSICode, (byte)'D' } },
                { ConsoleKey.Delete, new [] { ESCCode, CSICode, (byte)'3', (byte)'~' } },
                { ConsoleKey.Tab, new [] { (byte)'\t' } },
                { ConsoleKey.Backspace, new [] { (byte)127 } }
            };

            while (true)
            {
                var key = Console.ReadKey(true);
                // this is to support Home/End keys
                if (key.Key == (ConsoleKey)0 && (key.Modifiers & ConsoleModifiers.Alt) != 0)
                {
                    ByteRead?.Invoke(ESCCode);
                    ByteRead?.Invoke(CSICode);
                    continue;
                }

                if (mappings.TryGetValue(key.Key, out var sequence))
                {
                    foreach (var b in sequence)
                    {
                        ByteRead?.Invoke(b);
                    }
                }
                else
                {
                    foreach (var b in checker.UTF8Encoder.GetBytes(new [] { key.KeyChar }))
                    {
                        ByteRead?.Invoke(b);
                    }
                }
            }
        }