Exemplo n.º 1
0
        public void ThreadSmartSockReceives()
        {
            var endPoint = new IPEndPoint(IPAddress.Loopback, 23452);

            _threadSmartSock.Connect(endPoint.Address, endPoint.Port);  // NotConnected -> Connecting
            Utils.SendConnectResponse(_bareSock, endPoint, _bufferPool);
            WaitConnect();

            var buffer = Utils.CreatePacket(1234567890);

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

            var receivedPacket = new ReceivedSmartPacket();

            for (int i = 0; i < 1000; i++)
            {
                if (_threadSmartSock.Receive(ref receivedPacket))
                {
                    break;
                }

                Thread.Sleep(1);
            }

            Assert.IsNotNull(receivedPacket.Buffer);
        }
Exemplo n.º 2
0
        public void DisconnectedOnTimeout()
        {
            _sock.ConnectionTimeout = 1;

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

            _sock.Connect(remoteEndPoint.Address, remoteEndPoint.Port);
            Utils.SendConnectResponse(_bareSock, remoteEndPoint, _bufferPool);

            var header = new PacketHeader();

            header.SetSeqNum(1);
            header.Length = (ushort)(header.HeaderLength + 4);
            var ms = new MemoryStream();

            header.WriteTo(ms);
            ms.Write(BitConverter.GetBytes(123456789), 0, 4);  // Payload
            var buffer = Utils.ToBuffer(ms, _bufferPool);

            // Simulate send from UdpClient
            _bareSock.FakeReceive(buffer.Array, buffer.Offset, buffer.Count, remoteEndPoint);

            var receivedPacket = new ReceivedSmartPacket();

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

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

            Assert.AreEqual(1, _cbs.OnConnectCalls.Count);
            Assert.AreEqual(1, _cbs.OnDisconnectCalls.Count);
        }
Exemplo n.º 3
0
        public void ReceiveFragmented()
        {
            var remoteEndPoint = new IPEndPoint(IPAddress.Loopback, 23452);

            Utils.SendConnectRequest(_bareSock, remoteEndPoint, _bufferPool);

            var buffer1 = CreateFirstFragment();

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

            var buffer2 = CreateSecondFragment();

            _bareSock.FakeReceive(buffer2.Array, buffer1.Offset, buffer2.Count, remoteEndPoint);

            AssertCombinedPacketReceived();
        }
Exemplo n.º 4
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");
        }
Exemplo n.º 5
0
        public static void SendConnectRequest(MockSock bareSock, IPEndPoint endPoint, BufferPoolBase bufferPool)
        {
            var header = new PacketHeader();

            header.SetSessionId(PacketHeader.EmptySessionId);
            header.SetConnect();

            var buffer = bufferPool.Get(header.HeaderLength);

            header.Length = (ushort)header.HeaderLength;
            header.WriteTo(buffer, 0);

            bareSock.FakeReceive(buffer, 0, header.HeaderLength, endPoint);
        }
Exemplo n.º 6
0
        public void SmartSockIgnoreReceivedPacketsBeforeConnected()
        {
            var endPoint = new IPEndPoint(IPAddress.Loopback, 23451);

            _sock.Connect(endPoint.Address, endPoint.Port);  // NotConnected -> Connecting

            var header = new PacketHeader();

            header.SetSeqNum(1);
            header.Length = (ushort)(header.HeaderLength + 4);
            var ms = new MemoryStream();

            header.WriteTo(ms);
            ms.Write(BitConverter.GetBytes(123456789), 0, 4);  // Payload
            var buffer = Utils.ToBuffer(ms, _bufferPool);

            // Simulate send from UdpClient
            _bareSock.FakeReceive(buffer.Array, buffer.Offset, buffer.Count, endPoint);

            var receivedPacket = new ReceivedSmartPacket();

            Assert.IsFalse(_sock.Receive(ref receivedPacket));
        }
Exemplo n.º 7
0
        public static void SendDisconnectResponse(MockSock bareSock, IPEndPoint endPoint, BufferPoolBase bufferPool)
        {
            ushort sessionId = 427;
            var    header    = new PacketHeader();

            header.SetSessionId(sessionId);
            header.SetDisconnect();

            var buffer = bufferPool.Get(header.HeaderLength);

            header.Length = (ushort)header.HeaderLength;
            header.WriteTo(buffer, 0);

            bareSock.FakeReceive(buffer, 0, header.HeaderLength, endPoint);
        }
Exemplo n.º 8
0
        private void FakeSendPacket(ushort sessionId, int payload, IPEndPoint serverEndPoint, ref ushort seqNum)
        {
            var header = new PacketHeader();

            header.SetSessionId(sessionId);
            header.SetSeqNum(seqNum++);
            header.Length = (ushort)(header.HeaderLength + 4);
            var ms = new MemoryStream();

            header.WriteTo(ms);
            ms.Write(BitConverter.GetBytes(payload), 0, 4);
            var buffer = Utils.ToBuffer(ms, _bufferPool);

            _bareSock.FakeReceive(buffer.Array, buffer.Offset, buffer.Count, serverEndPoint);
        }
        private void TestWith(PacketHeader header)
        {
            header.Length = (ushort)header.HeaderLength;

            var buffer = _bufferPool.Get(header.Length);

            header.WriteTo(buffer, 0);

            _bareSock.FakeReceive(buffer, 0, header.Length, new IPEndPoint(IPAddress.Loopback, 23452));

            Utils.ReceiveAll(_sock);

            Assert.AreEqual(1, _bufferPool.Rented.Count);
            Assert.AreEqual(1, _bufferPool.Returned.Count);
            Assert.AreEqual(0, _bufferPool.Alien);
        }
Exemplo n.º 10
0
        private void SendPacket(int n)
        {
            var buffer = CreatePacket(n);

            _bareSock.FakeReceive(buffer.Array, buffer.Offset, buffer.Count, _endPoint);
        }
Exemplo n.º 11
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);
        }