Example #1
0
        public void SockConnectAndReceiveFrom()
        {
            UdpClient udpClient = new UdpClient(23458);

            _sock.Connect(IPAddress.Loopback, 23458);

            udpClient.Send(BitConverter.GetBytes(123456789), 4, (IPEndPoint)_sock.SysSock.LocalEndPoint);

            var receivedPacket = Utils.WaitOnReceive(_sock);

            Assert.AreEqual(123456789, BitConverter.ToInt32(receivedPacket.Buffer, 0));
            Assert.AreEqual(0, receivedPacket.Offset);
            Assert.AreEqual(4, receivedPacket.Length);
            Assert.AreEqual(1, _bufferPool.Rented.Count);
            Assert.AreEqual(0, _bufferPool.Returned.Count, "Buffer should not be returned to pool until processed by client");
        }
Example #2
0
        public void MultiplePacketsSent()
        {
            UdpClient udpClient   = new UdpClient(23450);
            var       receiveTask = udpClient.ReceiveAsync();

            var bufferPool = new ByteBufferPool();
            var sock       = new BareSock(bufferPool, AddressFamily.InterNetwork, new LoggerStub());

            sock.Connect(IPAddress.Loopback, 23450);
            sock.Send(BitConverter.GetBytes(1), 0, 4, false);
            sock.Send(BitConverter.GetBytes(2), 0, 4, false);
            sock.Send(BitConverter.GetBytes(3), 0, 4, false);

            receiveTask.Wait(2000);
            Assert.AreEqual(TaskStatus.RanToCompletion, receiveTask.Status);
            Assert.LessOrEqual(1, BitConverter.ToInt32(receiveTask.Result.Buffer, 0));

            receiveTask = udpClient.ReceiveAsync();
            receiveTask.Wait(2000);
            Assert.AreEqual(TaskStatus.RanToCompletion, receiveTask.Status);
            Assert.LessOrEqual(2, BitConverter.ToInt32(receiveTask.Result.Buffer, 0));

            // This test is flaky...
            receiveTask = udpClient.ReceiveAsync();
            receiveTask.Wait(2000);
            Assert.AreEqual(TaskStatus.RanToCompletion, receiveTask.Status);
            Assert.AreEqual(3, BitConverter.ToInt32(receiveTask.Result.Buffer, 0));

            sock.Close();
        }
Example #3
0
        public void SockConnectAndReceiveFrom()
        {
            UdpClient udpClient = new UdpClient(23448, AddressFamily.InterNetworkV6);

            _sock.Connect(IPAddress.IPv6Loopback, 23448);

            udpClient.Send(BitConverter.GetBytes(123456789), 4, (IPEndPoint)_sock.SysSock.LocalEndPoint);

            var receivedPacket = Utils.WaitOnReceive(_sock);

            Assert.AreEqual(123456789, BitConverter.ToInt32(receivedPacket.Buffer, 0));
            Assert.AreEqual(0, receivedPacket.Offset);
            Assert.AreEqual(4, receivedPacket.Length);
            Assert.AreEqual(1, _bufferPool.Rented.Count);
            Assert.AreEqual(0, _bufferPool.Returned.Count);
        }