コード例 #1
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");
        }
コード例 #2
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());
        }
コード例 #3
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);
        }
コード例 #4
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");
        }
コード例 #5
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);
            }
        }
コード例 #6
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);
        }
コード例 #7
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");
        }