Пример #1
0
        public void SmartSockSendConnected()
        {
            var endPoint = new IPEndPoint(IPAddress.Loopback, 23452);

            _sock.Connect(endPoint.Address, endPoint.Port);
            Utils.SendConnectResponse(_bareSock, endPoint, _bufferPool);
            var receivedPacket = new ReceivedSmartPacket();

            Assert.IsFalse(_sock.Receive(ref receivedPacket));

            var ms = new MemoryStream();

            ms.Write(BitConverter.GetBytes(123456789), 0, 4);
            var buffer = ms.ToArray();

            _sock.Send(buffer, 0, buffer.Length, false);

            Assert.AreEqual(1, _cbs.OnConnectCalls.Count);
            Assert.AreEqual(2, _bareSock.Sends.Count);

            var header       = new PacketHeader();
            var packetToSend = _bareSock.LastSend;

            header.Init(packetToSend.Buffer, packetToSend.Offset);

            Assert.AreEqual(buffer.Length + header.HeaderLength, header.Length);
            Assert.AreEqual(123456789, BitConverter.ToInt32(packetToSend.Buffer, header.HeaderLength));
            Assert.IsTrue(packetToSend.PutBufferToPool, "Unreliable packets should return to pool after send");
        }
Пример #2
0
        public void SendReliable()
        {
            var endPoint = new IPEndPoint(IPAddress.Loopback, 23452);

            Utils.SendConnectRequest(_bareSock, endPoint, _bufferPool);
            var receivedPacket = new ReceivedSmartPacket();

            Assert.IsFalse(_sock.Receive(ref receivedPacket));

            var ms = new MemoryStream();

            ms.Write(BitConverter.GetBytes(123456789), 0, 4);
            var buffer = ms.ToArray();

            _sock.Send(endPoint, buffer, 0, buffer.Length, true);

            Assert.AreEqual(2, _bareSock.Sends.Count);

            var packetToSend = _bareSock.LastSend;
            var header       = new PacketHeader();

            header.Init(packetToSend.Buffer, packetToSend.Offset);
            Assert.AreEqual(buffer.Length + header.HeaderLength, header.Length);
            Assert.AreEqual(123456789, BitConverter.ToInt32(packetToSend.Buffer, header.HeaderLength));
            Assert.AreEqual(0, header.SeqNum);
            Assert.IsTrue(header.GetNeedAck());
            Assert.IsFalse(packetToSend.PutBufferToPool, "Reliable packets should wait for Ack before going to pool");
        }
Пример #3
0
        public void ReSendAfterTimeout()
        {
            _sock.AckTimeout = 1;

            var endPoint = new IPEndPoint(IPAddress.Loopback, 23452);

            Utils.SendConnectRequest(_bareSock, endPoint, _bufferPool);
            var receivedPacket = new ReceivedSmartPacket();

            Assert.IsFalse(_sock.Receive(ref receivedPacket));

            var ms = new MemoryStream();

            ms.Write(BitConverter.GetBytes(123456789), 0, 4);
            var buffer = ms.ToArray();

            _sock.Send(endPoint, buffer, 0, buffer.Length, true);

            Thread.Sleep(20);
            _sock.Tick();

            Assert.AreEqual(3, _bareSock.Sends.Count);
            var packetToSend = _bareSock.LastSend;
            var header       = new PacketHeader();

            header.Init(packetToSend.Buffer, packetToSend.Offset);
            Assert.AreEqual(buffer.Length + header.HeaderLength, header.Length);
            Assert.AreEqual(123456789, BitConverter.ToInt32(packetToSend.Buffer, header.HeaderLength));
            Assert.AreEqual(0, header.SeqNum);
            Assert.IsTrue(header.GetNeedAck());
        }
Пример #4
0
        public void ConnectResponseResent()
        {
            var remoteEndPoint = new IPEndPoint(IPAddress.Loopback, 54321);

            var receivedPacket = new ReceivedSmartPacket();

            for (int i = 1; i < 11; ++i)
            {
                Utils.SendConnectRequest(_bareSock, remoteEndPoint, _bufferPool);

                Assert.AreEqual(PixocketState.NotConnected, _sock.State, "Server is not going to connected state");

                Assert.IsFalse(_sock.Receive(ref receivedPacket));

                Assert.AreEqual(PixocketState.NotConnected, _sock.State, "Server is not going to connected state");

                Assert.AreEqual(i, _bareSock.Sends.Count);
                var header     = new PacketHeader();
                var packetSent = _bareSock.LastSend;
                header.Init(packetSent.Buffer, packetSent.Offset);
                Assert.IsTrue((header.Flags & PacketHeader.Connect) != 0);
                Assert.AreNotEqual(PacketHeader.EmptySessionId, header.SessionId);

                Assert.AreEqual(1, _cbs.OnConnectCalls.Count);
                Assert.AreEqual(remoteEndPoint, _cbs.OnConnectCalls[0]);
                Assert.AreEqual(0, _cbs.OnDisconnectCalls.Count);
            }
        }
Пример #5
0
        public void ReceiveReliableFragmented()
        {
            _sock.Connect(_remoteEndPoint.Address, _remoteEndPoint.Port);
            Utils.SendConnectResponse(_bareSock, _remoteEndPoint, _bufferPool);

            var buffer1 = CreateFirstFragment();

            _bareSock.FakeReceive(buffer1.Array, buffer1.Offset, buffer1.Count, _remoteEndPoint);

            var buffer2 = CreateSecondFragment();

            _bareSock.FakeReceive(buffer2.Array, buffer2.Offset, buffer2.Count, _remoteEndPoint);

            AssertCombinedPacketReceived();

            // Test sending acks back on tick
            _sock.Tick();
            var packetToSend = _bareSock.LastSend;
            var header       = new PacketHeader();

            header.Init(packetToSend.Buffer, packetToSend.Offset);
            Assert.AreEqual(2, header.Acks.Count);
            CollectionAssert.AreEqual(new List <ushort> {
                100, 101
            }, header.Acks);
        }
Пример #6
0
        public void SequenceNumberResetAfterConnectionTimeout()
        {
            _sock.ConnectionTimeout = 1;
            var endPoint = new IPEndPoint(IPAddress.Loopback, 23452);

            Utils.SendConnectRequest(_bareSock, endPoint, _bufferPool);
            var receivedPacket = new ReceivedSmartPacket();

            Assert.IsFalse(_sock.Receive(ref receivedPacket));

            Assert.AreEqual(1, _cbs.OnConnectCalls.Count);

            var ms = new MemoryStream();

            ms.Write(BitConverter.GetBytes(123456789), 0, 4);
            var buffer = ms.ToArray();

            _sock.Send(endPoint, buffer, 0, buffer.Length, false);

            Thread.Sleep(20);
            _sock.Tick();

            _sock.Send(endPoint, buffer, 0, buffer.Length, false);

            // Two sends total
            Assert.AreEqual(2, _bareSock.Sends.Count);

            // Last Sequnce number is zero
            var header = new PacketHeader();

            header.Init(_bareSock.Sends[1].Buffer, _bareSock.Sends[1].Offset);
            Assert.AreEqual(0, header.SeqNum);
        }
Пример #7
0
        public void ServerRepliesWithFilledSessionId()
        {
            var clientEndPoint = new IPEndPoint(IPAddress.Loopback, 54321);

            Utils.SendConnectRequest(_bareSock, clientEndPoint, _bufferPool);

            ushort seqNum = 1;

            FakeSendPacket(0, 123456789, clientEndPoint, ref seqNum);

            var receivedPacket = new ReceivedSmartPacket();

            Assert.IsTrue(_sock.Receive(ref receivedPacket));
            Assert.AreEqual(1, _cbs.OnConnectCalls.Count);
            Assert.AreEqual(0, _cbs.OnDisconnectCalls.Count);

            // Reply from server
            _sock.Send(clientEndPoint, BitConverter.GetBytes(123456789), 0, 4, false);

            Assert.AreEqual(2, _bareSock.Sends.Count);

            var packet = _bareSock.LastSend;
            var header = new PacketHeader();

            header.Init(packet.Buffer, packet.Offset);

            Assert.AreNotEqual(PacketHeader.EmptySessionId, header.SessionId);
        }
Пример #8
0
        public void SendReliableFragmented()
        {
            _sock.MaxPayload = 3;

            Utils.SendConnectRequest(_bareSock, _remoteEndPoint, _bufferPool);
            var receivedPacket = new ReceivedSmartPacket();

            Assert.IsFalse(_sock.Receive(ref receivedPacket));

            var ms = new MemoryStream();

            ms.Write(BitConverter.GetBytes((ushort)12345), 0, 2);
            ms.Write(new byte[] { 77 }, 0, 1);
            ms.Write(BitConverter.GetBytes((ushort)23456), 0, 2);
            var buffer = ms.ToArray();

            _sock.Send(_remoteEndPoint, buffer, 0, buffer.Length, true);

            Assert.AreEqual(3, _bareSock.Sends.Count);  // 2 for payload + connect response

            var packetToSend = _bareSock.Sends[1];
            var header       = new PacketHeader();

            header.Init(packetToSend.Buffer, packetToSend.Offset);

            Assert.AreEqual(_sock.MaxPayload + header.HeaderLength, header.Length);
            Assert.AreEqual(12345, BitConverter.ToInt16(packetToSend.Buffer, header.HeaderLength));
            Assert.AreEqual(0, header.SeqNum);
            Assert.IsTrue(header.GetNeedAck());
            Assert.IsFalse(packetToSend.PutBufferToPool, "Reliable packets should wait for Ack before going to pool");

            packetToSend = _bareSock.Sends[2];
            header       = new PacketHeader();
            header.Init(packetToSend.Buffer, packetToSend.Offset);

            Assert.AreEqual(buffer.Length - _sock.MaxPayload + header.HeaderLength, header.Length);
            Assert.AreEqual(23456, BitConverter.ToInt16(packetToSend.Buffer, header.HeaderLength));
            Assert.AreEqual(1, header.SeqNum);
            Assert.IsTrue(header.GetNeedAck());
            Assert.IsFalse(packetToSend.PutBufferToPool, "Reliable packets should wait for Ack before going to pool");

            // Ack buffers
            var header1 = new PacketHeader();

            header1.AddAck(0);
            header1.AddAck(1);
            header1.SetSeqNum(1);
            header1.Length = (ushort)header1.HeaderLength;
            var buffer1 = _bufferPool.Get(header1.Length);

            header1.WriteTo(buffer1, 0);

            _bareSock.FakeReceive(buffer1, 0, header1.Length, _remoteEndPoint);

            var receivedPackets = Utils.ReceiveAll(_sock);
        }
Пример #9
0
        private void TestGarbage(byte[] buffer)
        {
            var header = new PacketHeader();

            header.Init(buffer, 0);

            Assert.AreEqual(0, header.Length);
            Assert.AreEqual(0, header.Flags);
            Assert.AreEqual(0, header.SeqNum);
            Assert.AreEqual(0, header.FragId);
            Assert.AreEqual(0, header.FragNum);
            Assert.AreEqual(0, header.FragCount);
            Assert.AreEqual(0, header.SessionId);
        }
Пример #10
0
        public void SendFragmented()
        {
            var endPoint = new IPEndPoint(IPAddress.Loopback, 23452);

            _sock.Connect(endPoint.Address, endPoint.Port);
            Utils.SendConnectResponse(_bareSock, endPoint, _bufferPool);
            var receivedPacket = new ReceivedSmartPacket();

            Assert.IsFalse(_sock.Receive(ref receivedPacket));

            _sock.MaxPayload = 3;

            var ms = new MemoryStream();

            ms.Write(BitConverter.GetBytes((ushort)12345), 0, 2);
            ms.Write(new byte[] { 77 }, 0, 1);
            ms.Write(BitConverter.GetBytes((ushort)23456), 0, 2);
            var buffer = ms.ToArray();

            _sock.Send(endPoint, buffer, 0, buffer.Length, false);

            Assert.AreEqual(3, _bareSock.Sends.Count);

            var packetToSend = _bareSock.Sends[1];
            var header       = new PacketHeader();

            header.Init(packetToSend.Buffer, packetToSend.Offset);

            Assert.AreEqual(_sock.MaxPayload + header.HeaderLength, header.Length);
            Assert.AreEqual(12345, BitConverter.ToInt16(packetToSend.Buffer, header.HeaderLength));
            Assert.AreEqual(0, header.SeqNum);
            Assert.IsFalse(header.GetNeedAck());
            Assert.IsTrue(packetToSend.PutBufferToPool, "Unreliable packets should return to pool after send");

            packetToSend = _bareSock.Sends[2];
            header       = new PacketHeader();
            header.Init(packetToSend.Buffer, packetToSend.Offset);

            Assert.AreEqual(buffer.Length - _sock.MaxPayload + header.HeaderLength, header.Length);
            Assert.AreEqual(23456, BitConverter.ToInt16(packetToSend.Buffer, header.HeaderLength));
            Assert.AreEqual(1, header.SeqNum);
            Assert.IsFalse(header.GetNeedAck());
            Assert.IsTrue(packetToSend.PutBufferToPool, "Unreliable packets should return to pool after send");
        }
Пример #11
0
        public void SerializationTest()
        {
            for (int i = 0; i < 16; ++i)
            {
                ushort sessionId = (ushort)_rnd.Next(ushort.MaxValue);
                ushort seqNum    = (ushort)_rnd.Next(ushort.MaxValue);
                bool   needAck   = _rnd.Next(2) == 0;
                bool   haveFrag  = _rnd.Next(2) == 0;

                var header1 = new PacketHeader();
                header1.SetSessionId(sessionId);
                header1.SetSeqNum(seqNum);
                if (needAck)
                {
                    header1.SetNeedAck();
                }
                if (haveFrag)
                {
                    ushort fragId    = (ushort)_rnd.Next(ushort.MaxValue);
                    ushort fragCount = (ushort)(_rnd.Next(ushort.MaxValue) + 1);
                    ushort fragNum   = (ushort)_rnd.Next(fragCount);
                    header1.SetFrag(fragId, fragNum, fragCount);
                }
                ushort payloadLength = (ushort)(_rnd.Next(ushort.MaxValue) + 1);
                header1.Length = (ushort)(header1.HeaderLength + payloadLength);

                int offset = _rnd.Next(16);

                var buffer = new byte[header1.HeaderLength + offset];
                header1.WriteTo(buffer, offset);

                var header2 = new PacketHeader();
                header2.Init(buffer, offset);

                Assert.AreEqual(header1.Length, header2.Length);
                Assert.AreEqual(header1.Flags, header2.Flags);
                Assert.AreEqual(header1.SessionId, header2.SessionId);
                Assert.AreEqual(header1.GetNeedAck(), header2.GetNeedAck());
                Assert.AreEqual(header1.HeaderLength, header2.HeaderLength);
            }
        }
Пример #12
0
        public void AcksSentWithPayload()
        {
            var endPoint = new IPEndPoint(IPAddress.Loopback, 23452);

            Utils.SendConnectRequest(_bareSock, endPoint, _bufferPool);

            var buffer1 = FakeSentPacket(234);

            _bareSock.FakeReceive(buffer1, 0, buffer1.Length, endPoint);

            var buffer2 = FakeSentPacket(235);

            _bareSock.FakeReceive(buffer2, 0, buffer2.Length, endPoint);

            var receivedPacket = new ReceivedSmartPacket();

            Assert.IsTrue(_sock.Receive(ref receivedPacket));
            Assert.IsTrue(_sock.Receive(ref receivedPacket));

            // Ack not sent yet, just connect response
            Assert.AreEqual(1, _bareSock.Sends.Count);

            var payload = BitConverter.GetBytes(987654321);

            _sock.Send(endPoint, payload, 0, payload.Length, true);
            // Msg with acks sent
            Assert.AreEqual(2, _bareSock.Sends.Count);

            // Make sure ack sent
            var ackHeader = new PacketHeader();
            var packet    = _bareSock.LastSend;

            ackHeader.Init(packet.Buffer, packet.Offset);
            Assert.GreaterOrEqual(packet.Buffer.Length, ackHeader.Length);
            Assert.GreaterOrEqual(packet.Buffer.Length, ackHeader.HeaderLength);
            Assert.Contains(234, ackHeader.Acks);
            Assert.Contains(235, ackHeader.Acks);
            Assert.IsTrue(ackHeader.GetNeedAck());
            Assert.IsTrue((ackHeader.Flags & PacketHeader.ContainsAck) != 0);
        }
Пример #13
0
        public void ReceiveReliable()
        {
            var endPoint = new IPEndPoint(IPAddress.Loopback, 23452);

            Utils.SendConnectRequest(_bareSock, endPoint, _bufferPool);

            var buffer1 = FakeSentPacket(123);

            _bareSock.FakeReceive(buffer1, 0, buffer1.Length, endPoint);

            var buffer2 = FakeSentPacket(124);

            _bareSock.FakeReceive(buffer2, 0, buffer2.Length, endPoint);

            var receivedPacket = new ReceivedSmartPacket();

            Assert.IsTrue(_sock.Receive(ref receivedPacket));
            Assert.IsTrue(_sock.Receive(ref receivedPacket));

            // Ack not sent yet, just connect response
            Assert.AreEqual(1, _bareSock.Sends.Count);
            _sock.Tick();
            // Ack sent
            Assert.AreEqual(2, _bareSock.Sends.Count);

            // Make sure ack sent
            var ackHeader = new PacketHeader();
            var packet    = _bareSock.LastSend;

            ackHeader.Init(packet.Buffer, packet.Offset);
            Assert.GreaterOrEqual(packet.Buffer.Length, ackHeader.Length);
            Assert.GreaterOrEqual(packet.Buffer.Length, ackHeader.HeaderLength);
            Assert.Contains(123, ackHeader.Acks);
            Assert.Contains(124, ackHeader.Acks);
            Assert.IsFalse(ackHeader.GetNeedAck());
            Assert.IsTrue((ackHeader.Flags & PacketHeader.ContainsAck) != 0);
            Assert.IsFalse((ackHeader.Flags & PacketHeader.ContainsSeq) != 0, "No need to write SeqNum in pure Acknowledgement packet");
            Assert.IsTrue(ackHeader.SessionId != PacketHeader.EmptySessionId, "Acks should already have Non-Empty SessionId");
        }
Пример #14
0
        public void NotReSendAfterAckAndTimeout()
        {
            _sock.AckTimeout = 1;

            var remoteEndPoint = new IPEndPoint(IPAddress.Loopback, 23452);

            Utils.SendConnectRequest(_bareSock, remoteEndPoint, _bufferPool);
            var receivedPacket = new ReceivedSmartPacket();

            Assert.IsFalse(_sock.Receive(ref receivedPacket));

            var buffer = BitConverter.GetBytes(123456789);

            _sock.Send(remoteEndPoint, buffer, 0, buffer.Length, true);

            var sent       = _bareSock.Sends[1];
            var headerSent = new PacketHeader();

            headerSent.Init(sent.Buffer, sent.Offset);

            var ackHeader = new PacketHeader();

            ackHeader.AddAck(headerSent.SeqNum);
            ackHeader.Length = (ushort)ackHeader.HeaderLength;
            buffer           = _bufferPool.Get(ackHeader.Length);
            ackHeader.WriteTo(buffer, 0);
            _bareSock.FakeReceive(buffer, 0, ackHeader.Length, new IPEndPoint(IPAddress.Loopback, 23452));

            receivedPacket = new ReceivedSmartPacket();
            // Just ack in the packet, no payload
            Assert.IsFalse(_sock.Receive(ref receivedPacket));

            Thread.Sleep(20);
            _sock.Tick();

            Assert.AreEqual(2, _bareSock.Sends.Count);
        }
Пример #15
0
        public void ClientInitiallySendsEmptySessionIdAndConnectFlag()
        {
            // Connect Request
            var endPoint = new IPEndPoint(IPAddress.Loopback, 23452);

            _sock.Connect(endPoint.Address, endPoint.Port);

            Assert.AreEqual(1, _bareSock.Sends.Count);
            Assert.AreEqual(0, _cbs.OnConnectCalls.Count);
            Assert.AreEqual(0, _cbs.OnDisconnectCalls.Count);
            var packet = _bareSock.LastSend;
            var header = new PacketHeader();

            header.Init(packet.Buffer, packet.Offset);

            Assert.AreEqual(PacketHeader.EmptySessionId, header.SessionId);
            Assert.IsTrue((header.Flags & PacketHeader.Connect) != 0);

            // Connect Response
            Utils.SendConnectResponse(_bareSock, endPoint, _bufferPool);
            var receivedPacket = new ReceivedSmartPacket();

            Assert.IsFalse(_sock.Receive(ref receivedPacket));
            Assert.AreEqual(1, _cbs.OnConnectCalls.Count);

            _sock.Send(BitConverter.GetBytes(123456789), 0, 4, false);

            Assert.AreEqual(2, _bareSock.Sends.Count);
            Assert.AreEqual(1, _cbs.OnConnectCalls.Count);
            Assert.AreEqual(0, _cbs.OnDisconnectCalls.Count);

            packet = _bareSock.LastSend;
            header = new PacketHeader();
            header.Init(packet.Buffer, packet.Offset);

            Assert.AreEqual(427, header.SessionId);
        }
Пример #16
0
        public void ClientImbuesSessionId()
        {
            var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 23451);

            _sock.Connect(serverEndPoint.Address, serverEndPoint.Port);

            _sock.Send(BitConverter.GetBytes(123456789), 0, 4, false);
            ushort seqNum = 1;

            FakeSendPacket(223, 1234567, serverEndPoint, ref seqNum);

            var receivedPacket = new ReceivedSmartPacket();

            Assert.IsTrue(_sock.Receive(ref receivedPacket));

            _sock.Send(BitConverter.GetBytes(123456789), 0, 4, false);

            var packet  = _bareSock.LastSend;
            var header2 = new PacketHeader();

            header2.Init(packet.Buffer, packet.Offset);

            Assert.AreEqual(223, header2.SessionId);
        }