コード例 #1
0
ファイル: UdpSocketTests.cs プロジェクト: danchart/Ecs
        public void ClientServerSendReceive()
        {
            var logger       = new TestLogger();
            var serverBuffer = new ReceiveBuffer(maxPacketSize: 64, packetQueueCapacity: 4);

            var server = new ServerUdpSocket(logger, serverBuffer);

            server.Start(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 27000));

            var clientBuffer = new ReceiveBuffer(maxPacketSize: 64, packetQueueCapacity: 4);
            var client       = new ClientUdpSocket(logger, clientBuffer);

            client.Start(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 27000));

            // Simulate TCP style SYN > SYN-ACK > ACK handshake...

            // Client->Server SYN

            client.Send(Encoding.ASCII.GetBytes("SYN"));
            client.Send(Encoding.ASCII.GetBytes("SYN 2")); // Just to test multiple packets

            WaitForReceive(serverBuffer, 2);

            Assert.Equal(2, serverBuffer.Count);

            byte[]     data;
            int        offset;
            int        size;
            IPEndPoint clientIpEndPoint;

            serverBuffer.BeginRead(out data, out offset, out size);
            serverBuffer.GetEndPoint(out clientIpEndPoint);
            serverBuffer.EndRead();
            var text0 = Encoding.ASCII.GetString(data, offset, size);

            serverBuffer.BeginRead(out data, out offset, out size);
            serverBuffer.EndRead();
            var text1 = Encoding.ASCII.GetString(data, offset, size);

            Assert.Equal(0, serverBuffer.Count);

            Assert.True((text0 == "SYN" && text1 == "SYN 2") ||
                        (text0 == "SYN 2" && text1 == "SYN"));

            // Server->Client SYN-ACK

            server.SendTo(Encoding.ASCII.GetBytes("SYN-ACK"), clientIpEndPoint);

            WaitForReceive(clientBuffer, 1);

            clientBuffer.BeginRead(out data, out offset, out size);
            clientBuffer.EndRead();
            var syncAckText = Encoding.ASCII.GetString(data, offset, size);

            Assert.Equal("SYN-ACK", syncAckText);

            // Client->Server ACK

            client.Send(Encoding.ASCII.GetBytes("ACK"));

            WaitForReceive(serverBuffer, 1);

            serverBuffer.BeginRead(out data, out offset, out size);
            serverBuffer.EndRead();
            var ackText = Encoding.ASCII.GetString(data, offset, size);

            Assert.Equal("ACK", ackText);
        }
コード例 #2
0
        public void Test()
        {
            var buffer = new ReceiveBuffer(maxPacketSize: 16, packetQueueCapacity: 4);

            Assert.Equal(0, buffer.Count);

            byte[] data;
            int    offset;
            int    size;
            var    ipEndPoint = new IPEndPoint(IPAddress.Any, 0);

            Assert.True(buffer.BeginWrite(out data, out offset, out size));

            data[offset]     = 1;
            data[offset + 1] = 2;
            buffer.EndWrite(2, ipEndPoint);

            Assert.Equal(1, buffer.Count);

            Assert.True(buffer.BeginWrite(out data, out offset, out size));
            data[offset]     = 3;
            data[offset + 1] = 4;
            buffer.EndWrite(2, ipEndPoint);

            Assert.Equal(2, buffer.Count);

            buffer.EndWrite(0, ipEndPoint);
            Assert.Equal(3, buffer.Count);

            buffer.EndWrite(0, ipEndPoint);
            Assert.Equal(4, buffer.Count);

            // Overflow
            Assert.False(buffer.BeginWrite(out data, out offset, out size));
            Assert.Equal(4, buffer.Count);

            Assert.True(buffer.BeginRead(out data, out offset, out size));
            Assert.Equal(2, size);
            Assert.Equal(1, data[offset]);
            Assert.Equal(2, data[offset + 1]);

            buffer.EndRead();

            Assert.Equal(3, buffer.Count);

            Assert.True(buffer.BeginRead(out data, out offset, out size));
            Assert.Equal(2, size);
            Assert.Equal(3, data[offset]);
            Assert.Equal(4, data[offset + 1]);

            buffer.EndRead();

            Assert.Equal(2, buffer.Count);

            Assert.True(buffer.BeginRead(out data, out offset, out size));
            Assert.Equal(0, size);

            buffer.EndRead();

            Assert.Equal(1, buffer.Count);

            Assert.True(buffer.BeginRead(out data, out offset, out size));
            Assert.Equal(0, size);

            buffer.EndRead();

            Assert.Equal(0, buffer.Count);
            Assert.False(buffer.BeginRead(out data, out offset, out size));
        }