public void TestDataReading()
        {
            var data = new List<byte>();
            data.Add(byte.MaxValue);
            data.AddRange(BitConverter.GetBytes(short.MaxValue));
            data.AddRange(BitConverter.GetBytes(int.MaxValue));
            data.AddRange(BitConverter.GetBytes(float.MaxValue));
            data.AddRange(BitConverter.GetBytes(ulong.MaxValue));
            data.AddRange(Encoding.UTF8.GetBytes("test string"));
            data.Add(0x00); // String terminate

            var reader = new PacketReader(data.ToArray());

            Assert.That(reader.ReadByte(), Is.EqualTo(byte.MaxValue));
            Assert.That(reader.ReadShort(), Is.EqualTo(short.MaxValue));
            Assert.That(reader.ReadLong(), Is.EqualTo(int.MaxValue));
            Assert.That(reader.ReadFloat(), Is.EqualTo(float.MaxValue));
            Assert.That(reader.ReadLongLong(), Is.EqualTo(ulong.MaxValue));
            Assert.That(reader.ReadUTFString(), Is.EqualTo("test string"));
        }
Exemple #2
0
        private TCPPacket Parse(byte[] data)
        {
            var result = new TCPPacket();
            var packetReader = new PacketReader(data);
            result.Size = packetReader.ReadLong();
            result.Id = packetReader.ReadLong();
            result.Type = packetReader.ReadLong();
            result.Body = packetReader.ReadASCIIString();
            result.EmptyString = packetReader.ReadASCIIString();

            return result;
        }
Exemple #3
-1
        private async Task<ReceiveDataInfo> ReceiveDataAsync(ServerAddress address, byte[] data, CancellationToken ct, params char[] header)
        {
            var udpClient = new UdpClient();
            var ipAddr = IPAddress.Parse(address.Ip);
            try
            {
                udpClient.Connect(ipAddr, address.Port);

                var sendTime = DateTime.Now;
                await udpClient.SendAsync(data, data.Length);

                Packet packet = null;            
                return await Task.Run(async () =>
                {
                    while (true)
                    {
                        var clientResult = await udpClient.ReceiveAsync().WithCancellation(ct);
                        var remoteIp = clientResult.RemoteEndPoint.Address.ToString();
                        var remotePort = clientResult.RemoteEndPoint.Port;
                        var receiveDate = DateTime.Now;
                        var packetReader = new PacketReader(clientResult.Buffer);
                        var packetheader = packetReader.ReadLong();
                        if (packet == null)
                        {
                            packet = engineFactory.CreatePacket(packetheader);
                        }
                        packet.AddData(clientResult.Buffer);
                        if (packet.IsCompleted)
                        {
                            var messageReader = new PacketReader(packet.Payload);
                            var messageheader = messageReader.ReadByte();
                            var messageHeaderChar = Convert.ToChar(messageheader);
                            if (header.Contains(messageHeaderChar))
                            {
                                var result = new ReceiveDataInfo
                                {
                                    Ping = (int)(DateTime.Now - sendTime).TotalMilliseconds,
                                    Data = packet.Payload,
                                    MessageHeaderChar = messageHeaderChar,
                                };
                                return result;
                            }
                        }
                    }
                });
            }
            catch 
            {
                throw new OperationCanceledException();
            }
            finally
            {
                udpClient.Close();
            }
        }