Example #1
0
        public void ReadWriteMultipleTest()
        {
            var packet = new NetPacket();

            const bool   bVal   = true;
            const double dVal   = double.MaxValue / 3 * 2;
            const float  fVal   = float.MinValue / 5;
            const short  sVal   = -12345;
            const int    offset = 113;

            packet.WriteBool(bVal);
            packet.WriteDouble(dVal);
            packet.WriteFloat(fVal);
            packet.WriteShort(sVal);
            Assert.AreEqual(offset, packet.WritePosition);

            packet.ResetRead();
            Assert.AreEqual(bVal, packet.ReadBool());
            Assert.AreEqual(dVal, packet.ReadDouble());
            Assert.AreEqual(fVal, packet.ReadFloat());
            Assert.AreEqual(sVal, packet.ReadShort());
            Assert.AreEqual(offset, packet.ReadPosition);

            packet.Dispose();
        }
        public void RejectHugePacket()
        {
            using (TcpListener listener = new TcpListener(1024))
            {
                listener.Blocking = true;
                listener.Listen(PORT);

                TcpSocket clientSock       = new TcpSocket();
                var       connectionResult = clientSock.ConnectAsync("localhost", PORT).Result;

                var status = listener.Accept(out TcpSocket serverSock);
                Assert.AreEqual(SocketStatus.Done, status);

                var largePacket = new NetPacket();
                largePacket.WriteBytes(new byte[8192], true);

                while (clientSock.Send(largePacket) != SocketStatus.Done)
                {
                    ;
                }

                Assert.AreEqual(SocketStatus.Disconnected, serverSock.Receive(largePacket));

                Assert.IsFalse(serverSock.Connected);

                clientSock.Dispose();
                serverSock.Dispose();
                largePacket.Dispose();
            }
        }
        public void CreateNetPacketFromBufferTest()
        {
            short     shortValue  = _randomizer.Short();
            float     floatValue  = _randomizer.Float();
            const int contentSize = sizeof(short) + sizeof(float);

            byte[] data = BitConverter.GetBytes(contentSize)
                          .Concat(BitConverter.GetBytes((short)shortValue))
                          .Concat(BitConverter.GetBytes((float)floatValue))
                          .ToArray();

            var   packet            = new NetPacket(data);
            int   packetContentSize = packet.Read <int>();
            short shortValuePacket  = packet.Read <short>();
            float floatValuePacket  = packet.Read <float>();

            Assert.Equal(NetPacketStateType.Read, packet.State);
            Assert.NotNull(packet.Buffer);
            Assert.Equal(data, packet.Buffer);
            Assert.Equal(contentSize, BitConverter.ToInt32(packet.Buffer, 0));
            Assert.Equal(contentSize, packetContentSize);

            Assert.Equal(shortValue, BitConverter.ToInt16(packet.Buffer, sizeof(int)));
            Assert.Equal(shortValue, shortValuePacket);

            Assert.Equal(floatValue, BitConverter.ToSingle(packet.Buffer, sizeof(int) + sizeof(short)));
            Assert.Equal(floatValue, floatValuePacket);

            packet.Dispose();
        }
Example #4
0
        public void ExpandTest()
        {
            NetPacket bs = new NetPacket();

            bs.WriteLong(1);
            bs.WriteLong(2);

            Assert.AreEqual(16, bs.Size);
            Assert.AreEqual(16, bs.Capacity);

            bs.WriteLong(3);

            // Expect another resize.
            Assert.AreEqual(32, bs.Capacity);
            Assert.AreEqual(24, bs.Size);


            // Confirm data is still there.
            bs.Mode = SerializationMode.Reading;
            Assert.AreEqual(1, bs.ReadLong());
            Assert.AreEqual(2, bs.ReadLong());
            Assert.AreEqual(3, bs.ReadLong());

            bs.Dispose();
        }
Example #5
0
        public void SizeTest()
        {
            NetPacket bs = new NetPacket();

            bs.WriteInt32(1, 28);
            Assert.AreEqual(4, bs.Size);

            bs.Dispose();
        }
Example #6
0
        public void ResetReadTest1()
        {
            NetPacket bs = CreatePacket(10);

            Assert.AreEqual(10, bs.Size);
            Assert.AreEqual(0, bs.ReadPosition);
            Assert.IsTrue(bs.IsReading);

            bs.Dispose();
        }
Example #7
0
        public void ZeroLargeTest()
        {
            NetPacket bs = new NetPacket();

            bs.Skip(20 << 3);

            Assert.AreEqual(20, bs.Size);
            Assert.AreEqual(24, bs.Capacity);
            bs.Dispose();
        }
Example #8
0
        public unsafe void CTorTest2()
        {
            NetPacket bs = new NetPacket(8);

            Assert.IsTrue(bs.IsWriting);
            Assert.AreEqual(16, bs.Capacity);
            Assert.AreEqual(0, bs.Size);

            bs.Dispose();
        }
        public void CreateEmptyNetPacketTest()
        {
            var packet = new NetPacket();

            Assert.Equal(NetPacketStateType.Write, packet.State);
            Assert.NotNull(packet.Buffer);
            Assert.Equal(sizeof(int), packet.Buffer.Length);
            Assert.Equal(0, BitConverter.ToInt32(packet.Buffer, 0));

            packet.Dispose();
        }
Example #10
0
        public void SerializeWriteTest(int value)
        {
            var packet = new NetPacket();

            packet.Serialize(ref value);
            Assert.AreEqual(4, packet.Size);

            packet.ResetRead();
            Assert.AreEqual(value, packet.ReadInt32());

            packet.Dispose();
        }
        // [Test]
        public void StressTest()
        {
            TcpListener listener = new TcpListener();

            listener.Listen(PORT);

            TcpSocket clientSock       = new TcpSocket();
            var       connectionResult = clientSock.ConnectAsync("localhost", PORT).Result;

            var status = listener.Accept(out TcpSocket serverSock);

            for (int i = 0; i < 1000000; i++)
            {
                NetPacket packet = new NetPacket();

                var messageTo = Guid.NewGuid().ToString();
                packet.WriteString(messageTo);
                while (serverSock.Send(packet) != SocketStatus.Done)
                {
                    ;
                }

                packet.Clear();

                while (clientSock.Receive(packet) != SocketStatus.Done)
                {
                    ;
                }

                Assert.AreEqual(messageTo, packet.ReadString());
                packet.ResetWrite();

                packet.WriteString($"Message with code {messageTo} received.");
                while (clientSock.Send(packet) != SocketStatus.Done)
                {
                    ;
                }

                packet.Clear();

                while (serverSock.Receive(packet) != SocketStatus.Done)
                {
                    ;
                }

                packet.Dispose();
            }

            listener.Dispose();
            clientSock.Dispose();
            serverSock.Dispose();
        }
Example #12
0
        public void ReadwriteTest(int value)
        {
            var packet = new NetPacket();

            packet.WriteInt32(value);
            Assert.AreEqual(32, packet.WritePosition);

            packet.ResetRead();
            Assert.AreEqual(value, packet.ReadInt32());
            Assert.AreEqual(32, packet.ReadPosition);

            packet.Dispose();
        }
Example #13
0
        public void SerializeReadTest(int value)
        {
            var packet = new NetPacket();

            packet.WriteInt32(value);
            packet.ResetRead();

            int ret = 0;

            packet.Serialize(ref ret);
            Assert.AreEqual(ret, value);
            Assert.AreEqual(4, packet.Size);

            packet.Dispose();
        }
Example #14
0
        public void ReadwriteFloat(float value)
        {
            var packet = new NetPacket();

            int val = (int)value;

            packet.WriteByte(0, 4);
            packet.WriteInt32(val);

            packet.ResetRead();
            packet.ReadByte(4);
            Assert.AreEqual(val, packet.ReadInt32());

            packet.Dispose();
        }
Example #15
0
        public void ReadWriteIntTest()
        {
            var packet = new NetPacket();

            int val = 123456789;

            packet.WriteInt32(val);

            Assert.AreEqual(4, packet.Size);

            packet.ResetRead();

            Assert.AreEqual(val, packet.ReadInt32());

            packet.Dispose();
        }
Example #16
0
        public void SerializeTest(float value)
        {
            var packet = new NetPacket();

            packet.Serialize(ref value);
            Assert.AreEqual(32, packet.WritePosition);

            packet.ResetRead();
            float ret = 0;

            packet.Serialize(ref ret);
            Assert.AreEqual(value, ret);
            Assert.AreEqual(32, packet.ReadPosition);

            packet.Dispose();
        }
Example #17
0
        private void CharArraySmall(BitEncoding encoding)
        {
            packet = new NetPacket();

            char[] rep = new char[4];

            packet.WriteString("TestString", encoding);
            packet.ResetRead();

            int charCount = packet.ReadString(rep, encoding);

            Assert.AreEqual(charCount, rep.Length);
            string repStr = new string(rep, 0, charCount);

            Assert.AreEqual("Test", repStr);

            packet.Dispose();
        }
Example #18
0
        public void ReadWriteSizeTest()
        {
            var packet = new NetPacket();

            const byte bVal    = 100;
            const int  iVal    = -100;
            const byte bitSize = 7;

            packet.WriteByte(bVal, bitSize);
            packet.WriteInt32(iVal, bitSize + 1);
            Assert.AreEqual(bitSize * 2 + 1, packet.WritePosition);

            packet.ResetRead();
            Assert.AreEqual(bVal, packet.ReadByte(bitSize));
            Assert.AreEqual(iVal, packet.ReadInt32(bitSize + 1));
            Assert.AreEqual(bitSize * 2 + 1, packet.ReadPosition);

            packet.Dispose();
        }
        public void SendReceiveTest()
        {
            const string serverMessage = "HelloFromServer";
            const string clientMessage = "ResponseFromClient";

            using (TcpListener listener = new TcpListener())
            {
                listener.Blocking = true;
                listener.Listen(PORT);

                TcpSocket clientSock       = new TcpSocket();
                var       connectionResult = clientSock.ConnectAsync("localhost", PORT).Result;

                var status = listener.Accept(out TcpSocket serverSock);

                NetPacket packet       = new NetPacket();
                NetPacket clientPacket = new NetPacket();

                packet.WriteString(serverMessage);

                // Send message to client.
                Assert.AreEqual(SocketStatus.Done, serverSock.Send(packet));

                // Read message from server.
                Assert.AreEqual(SocketStatus.Done, clientSock.Receive(clientPacket));
                Assert.AreEqual(serverMessage, clientPacket.ReadString());

                // Send message back to server.
                clientPacket.Clear(SerializationMode.Writing);
                clientPacket.WriteString(clientMessage);
                Assert.AreEqual(SocketStatus.Done, clientSock.Send(clientPacket));

                // Read message from client.
                Assert.AreEqual(SocketStatus.Done, serverSock.Receive(packet));
                Assert.AreEqual(clientMessage, packet.ReadString());

                clientSock.Dispose();
                serverSock.Dispose();
                packet.Dispose();
                clientPacket.Dispose();
            }
        }
Example #20
0
        public void ReadInvalidateTest()
        {
            NetPacket bs = CreatePacket(4);

            Assert.IsTrue(bs.IsValid);

            bs.ReadInt32();
            Assert.IsTrue(bs.IsValid);

            bs.ReadInt32();
            Assert.IsFalse(bs.IsValid);

            // Confirm offset hasn't increased.
            Assert.AreEqual(4 * 8, bs.ReadPosition);

            bs.ResetRead();
            Assert.IsTrue(bs.IsValid);

            bs.Dispose();
        }
Example #21
0
        public void SendReceiveTest()
        {
            const string serverMessage = "HelloFromServer";
            const string clientMessage = "ResponseFromClient";

            IPEndPoint ep       = new IPEndPoint(IPAddress.Any, 666);
            IPEndPoint serverEp = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 666);
            UdpSocket  client   = new UdpSocket();
            UdpSocket  server   = new UdpSocket();

            server.Bind(666);
            Assert.AreEqual(SocketStatus.Done, client.Connect(serverEp));

            NetPacket packet       = new NetPacket();
            NetPacket clientPacket = new NetPacket();

            packet.WriteString(serverMessage);

            // Send message to client.
            Assert.AreEqual(SocketStatus.Done, server.Send(packet, client.LocalEndpoint));

            // Read message from server.
            Assert.AreEqual(SocketStatus.Done, client.Receive(clientPacket, ref ep));
            Assert.AreEqual(serverMessage, clientPacket.ReadString());

            // Send message back to server.
            clientPacket.Clear(SerializationMode.Writing);
            clientPacket.WriteString(clientMessage);
            Assert.AreEqual(SocketStatus.Done, client.Send(clientPacket));

            // Read message from client.
            Assert.AreEqual(SocketStatus.Done, server.Receive(packet, ref ep));
            Assert.AreEqual(clientMessage, packet.ReadString());

            client.Dispose();
            server.Dispose();
            packet.Dispose();
            clientPacket.Dispose();
        }
Example #22
0
        public void WriteResizeTest()
        {
            NetPacket bs = new NetPacket(4);

            bs.WriteULong(123);
            Assert.IsTrue(bs.IsValid);

            Assert.AreEqual(8, bs.Size);

            bs.WriteInt32(321);
            Assert.IsTrue(bs.IsValid);

            Assert.AreEqual(12, bs.Size);
            Assert.AreEqual(16, bs.Capacity);

            // Confirm offset has increased.
            Assert.AreEqual(12, bs.Size);

            Assert.IsTrue(bs.IsValid);

            bs.Dispose();
        }
        public void CreateNetPacketTest()
        {
            const int HeaderSize = sizeof(int);
            const int DataSize   = sizeof(short) + sizeof(float);
            short     shortValue = _randomizer.Short();
            float     floatValue = _randomizer.Float();

            var packet = new NetPacket();

            packet.Write <short>(shortValue);
            packet.Write <float>(floatValue);

            Assert.Equal(NetPacketStateType.Write, packet.State);
            Assert.NotNull(packet.Buffer);
            Assert.Equal(HeaderSize + DataSize, packet.Buffer.Length);
            Assert.Equal(DataSize, packet.ContentLength);
            Assert.Equal(DataSize, BitConverter.ToInt32(packet.Buffer, 0));
            Assert.Equal(shortValue, BitConverter.ToInt16(packet.Buffer, HeaderSize));
            Assert.Equal(floatValue, BitConverter.ToSingle(packet.Buffer, HeaderSize + sizeof(short)));

            packet.Dispose();
        }
Example #24
0
 public void TearDown()
 {
     packet.Dispose();
 }