// [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();
        }
        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();
            }
        }
Beispiel #3
0
        public void ClearTest()
        {
            var packet = new NetPacket(12);

            // Write some data;
            for (int i = 0; i < 3; i++)
            {
                packet.WriteInt32(i + 1);
            }

            Assert.AreEqual(16, packet.Capacity);

            packet.Clear();

            Assert.AreEqual(16, packet.Capacity);

            // Confirm all values are 0.
            packet.Mode = SerializationMode.Reading;

            Assert.AreEqual(0, packet.ReadLong());
            Assert.AreEqual(0, packet.ReadInt32());
        }
Beispiel #4
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();
        }
Beispiel #5
0
 public void Setup()
 {
     packet.Clear();
     packet.Mode = SerializationMode.Writing;
 }