public void ClientDisconnectTest()
        {
            using (ThreadLimitedUdpConnectionListener listener = new ThreadLimitedUdpConnectionListener(2, new IPEndPoint(IPAddress.Any, 4296), new NullLogger()))
                using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296)))
                {
                    ManualResetEvent mutex  = new ManualResetEvent(false);
                    ManualResetEvent mutex2 = new ManualResetEvent(false);

                    listener.NewConnection += delegate(NewConnectionEventArgs args)
                    {
                        args.Connection.Disconnected += delegate(object sender2, DisconnectedEventArgs args2)
                        {
                            mutex2.Set();
                        };

                        mutex.Set();
                    };

                    listener.Start();

                    connection.Connect();

                    mutex.WaitOne();

                    connection.Disconnect("Testing");

                    mutex2.WaitOne();
                }
        }
Ejemplo n.º 2
0
        public void PingDisconnectClientTest()
        {
#if DEBUG
            using (UdpConnectionListener listener = new UdpConnectionListener(new IPEndPoint(IPAddress.Any, 4296)))
                using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296)))
                {
                    listener.Start();

                    connection.Connect();

                    // After connecting, quietly stop responding to all messages to fake connection loss.
                    Thread.Sleep(10);
                    listener.TestDropRate = 1;

                    connection.KeepAliveInterval = 100;

                    Thread.Sleep(1050); //Enough time for ~10 keep alive packets

                    Assert.AreEqual(ConnectionState.NotConnected, connection.State);
                    Assert.AreEqual(3 * connection.MissingPingsUntilDisconnect + 4, connection.Statistics.TotalBytesSent); // + 4 for connecting overhead
                }
#else
            Assert.Inconclusive("Only works in DEBUG");
#endif
        }
Ejemplo n.º 3
0
        public void ClientServerDisposeDisconnectsTest()
        {
            IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 4296);

            bool serverConnected    = false;
            bool serverDisconnected = false;
            bool clientDisconnected = false;

            using (UdpConnectionListener listener = new UdpConnectionListener(new IPEndPoint(IPAddress.Any, 4296)))
                using (UdpConnection connection = new UdpClientConnection(ep))
                {
                    listener.NewConnection += (evt) =>
                    {
                        serverConnected              = true;
                        evt.Connection.Disconnected += (o, et) => serverDisconnected = true;
                    };

                    connection.Disconnected += (o, et) => clientDisconnected = true;

                    listener.Start();
                    connection.Connect();

                    Thread.Sleep(100); // Gotta wait for the server to set up the events.
                    connection.Dispose();

                    Thread.Sleep(100);

                    Assert.IsTrue(serverConnected);
                    Assert.IsTrue(serverDisconnected);
                    Assert.IsFalse(clientDisconnected);
                }
        }
Ejemplo n.º 4
0
        public void KeepAliveServerTest()
        {
            ManualResetEvent mutex = new ManualResetEvent(false);

            using (UdpConnectionListener listener = new UdpConnectionListener(new IPEndPoint(IPAddress.Any, 4296)))
                using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296)))
                {
                    UdpConnection client = null;
                    listener.NewConnection += delegate(NewConnectionEventArgs args)
                    {
                        client = (UdpConnection)args.Connection;
                        client.KeepAliveInterval = 100;

                        Thread.Sleep(1050); //Enough time for ~10 keep alive packets

                        mutex.Set();
                    };

                    listener.Start();

                    connection.Connect();

                    mutex.WaitOne();

                    Assert.AreEqual(ConnectionState.Connected, client.State);

                    Assert.IsTrue(
                        client.Statistics.TotalBytesSent >= 27 &&
                        client.Statistics.TotalBytesSent <= 50,
                        "Sent: " + client.Statistics.TotalBytesSent
                        );
                }
        }
Ejemplo n.º 5
0
        public void ServerExtraDataDisconnectTest()
        {
            using (UdpConnectionListener listener = new UdpConnectionListener(new IPEndPoint(IPAddress.Any, 4296)))
                using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296)))
                {
                    MessageReader    received = null;
                    ManualResetEvent mutex    = new ManualResetEvent(false);

                    connection.Disconnected += delegate(object sender, DisconnectedEventArgs args)
                    {
                        received = args.Message;
                        mutex.Set();
                    };

                    listener.NewConnection += delegate(NewConnectionEventArgs args)
                    {
                        MessageWriter writer = MessageWriter.Get(SendOption.None);
                        writer.Write("Goodbye");
                        args.Connection.Disconnect("Testing", writer);
                    };

                    listener.Start();

                    connection.Connect();

                    mutex.WaitOne();

                    Assert.IsNotNull(received);
                    Assert.AreEqual("Goodbye", received.ReadString());
                }
        }
        public void KeepAliveServerTest()
        {
            ManualResetEvent mutex = new ManualResetEvent(false);
            UdpConnection    listenerConnectionToClient = null;

            using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296)))
                using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296)))
                {
                    listener.NewConnection += delegate(object sender, NewConnectionEventArgs args)
                    {
                        listenerConnectionToClient = (UdpConnection)args.Connection;
                        listenerConnectionToClient.KeepAliveInterval = 100;

                        Thread.Sleep(1050); //Enough time for ~10 keep alive packets

                        mutex.Set();
                    };

                    listener.Start();

                    connection.Connect();

                    mutex.WaitOne();

                    Assert.IsNotNull(listenerConnectionToClient);
                    Assert.IsTrue(
                        listenerConnectionToClient.Statistics.TotalBytesSent >= 30 &&
                        listenerConnectionToClient.Statistics.TotalBytesSent <= 50,
                        "Sent: " + listenerConnectionToClient.Statistics.TotalBytesSent
                        );
                }
        }
Ejemplo n.º 7
0
        public void UdpUnreliableMessageSendTest()
        {
            byte[] TestData = new byte[] { 1, 2, 3, 4, 5, 6 };
            using (UdpConnectionListener listener = new UdpConnectionListener(new IPEndPoint(IPAddress.Any, 4296)))
                using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296)))
                {
                    MessageReader output = null;
                    listener.NewConnection += delegate(NewConnectionEventArgs e)
                    {
                        e.Connection.DataReceived += delegate(DataReceivedEventArgs evt)
                        {
                            output = evt.Message;
                        };
                    };

                    listener.Start();
                    connection.Connect();

                    for (int i = 0; i < 4; ++i)
                    {
                        var msg = MessageWriter.Get(SendOption.None);
                        msg.Write(TestData);
                        connection.Send(msg);
                        msg.Recycle();
                    }

                    Thread.Sleep(10);
                    for (int i = 0; i < TestData.Length; ++i)
                    {
                        Assert.AreEqual(TestData[i], output.ReadByte());
                    }
                }
        }
Ejemplo n.º 8
0
        public void KeepAliveServerTest()
        {
            ManualResetEvent mutex = new ManualResetEvent(false);

            using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296))
                using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296)))
                {
                    listener.NewConnection += delegate(object sender, NewConnectionEventArgs args)
                    {
                        ((UdpConnection)args.Connection).KeepAliveInterval = 100;

                        Thread.Sleep(1100); //Enough time for ~10 keep alive packets

                        Assert.IsTrue(
                            args.Connection.Statistics.TotalBytesSent >= 27 &&
                            args.Connection.Statistics.TotalBytesSent <= 33,
                            "Received: " + args.Connection.Statistics.TotalBytesSent
                            );

                        mutex.Set();
                    };

                    listener.Start();

                    connection.Connect();

                    mutex.WaitOne();
                }
        }
        public void UdpIPv4ConnectionTest()
        {
            using (ThreadLimitedUdpConnectionListener listener = new ThreadLimitedUdpConnectionListener(2, new IPEndPoint(IPAddress.Any, 4296), new NullLogger()))
                using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296)))
                {
                    listener.Start();

                    connection.Connect();
                }
        }
        public void UdpIPv4ConnectionTest()
        {
            using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296, IPMode.IPv4)))
                using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4)))
                {
                    listener.Start();

                    connection.Connect();
                }
        }
Ejemplo n.º 11
0
        public void UdpIPv6ConnectionTest()
        {
            using (UdpConnectionListener listener = new UdpConnectionListener(new IPEndPoint(IPAddress.IPv6Any, 4296), IPMode.IPv6))
            {
                listener.Start();

                using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4296), IPMode.IPv6))
                {
                    connection.Connect();
                }
            }
        }
        public void UdpHandshakeTest()
        {
            using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296, IPMode.IPv4)))
                using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4)))
                {
                    listener.Start();

                    listener.NewConnection += delegate(object sender, NewConnectionEventArgs e)
                    {
                        Assert.IsTrue(Enumerable.SequenceEqual(e.HandshakeData, new byte[] { 1, 2, 3, 4, 5, 6 }));
                    };

                    connection.Connect(new byte[] { 1, 2, 3, 4, 5, 6 });
                }
        }
Ejemplo n.º 13
0
        // [TestMethod]
        public void StressTestOpeningConnections()
        {
            // Start a listener in another process, or even better,
            // adjust the target IP and start listening on another computer.
            var ep = new IPEndPoint(IPAddress.Loopback, 22023);

            Parallel.For(0, 10000,
                         new ParallelOptions {
                MaxDegreeOfParallelism = 64
            },
                         (i) => {
                var connection = new UdpClientConnection(ep);
                connection.KeepAliveInterval = 50;

                connection.Connect(new byte[5]);
            });
        }
Ejemplo n.º 14
0
        public void TcpDualModeConnectionTest()
        {
            using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296, IPMode.IPv4AndIPv6))
            {
                listener.Start();

                using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4)))
                {
                    connection.Connect();
                }

                using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4AndIPv6)))
                {
                    connection.Connect();
                }
            }
        }
        public void KeepAliveClientTest()
        {
            using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296)))
                using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296)))
                {
                    listener.Start();

                    connection.Connect();
                    connection.KeepAliveInterval = 100;

                    System.Threading.Thread.Sleep(1050); //Enough time for ~10 keep alive packets

                    Assert.IsTrue(
                        connection.Statistics.TotalBytesSent >= 30 &&
                        connection.Statistics.TotalBytesSent <= 50,
                        "Sent: " + connection.Statistics.TotalBytesSent
                        );
                }
        }
        public void UdpFieldTest()
        {
            NetworkEndPoint ep = new NetworkEndPoint(IPAddress.Loopback, 4296);

            using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296)))
                using (UdpConnection connection = new UdpClientConnection(ep))
                {
                    listener.Start();

                    connection.Connect();

                    //Connection fields
                    Assert.AreEqual(ep, connection.EndPoint);

                    //UdpConnection fields
                    Assert.AreEqual(new IPEndPoint(IPAddress.Loopback, 4296), connection.RemoteEndPoint);
                    Assert.AreEqual(1, connection.Statistics.DataBytesSent);
                    Assert.AreEqual(0, connection.Statistics.DataBytesReceived);
                }
        }
        public void KeepAliveClientTest()
        {
            using (ThreadLimitedUdpConnectionListener listener = new ThreadLimitedUdpConnectionListener(2, new IPEndPoint(IPAddress.Any, 4296), new NullLogger()))
                using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296)))
                {
                    listener.Start();

                    connection.Connect();
                    connection.KeepAliveInterval = 100;

                    Thread.Sleep(1050); //Enough time for ~10 keep alive packets

                    Assert.AreEqual(ConnectionState.Connected, connection.State);
                    Assert.IsTrue(
                        connection.Statistics.TotalBytesSent >= 30 &&
                        connection.Statistics.TotalBytesSent <= 50,
                        "Sent: " + connection.Statistics.TotalBytesSent
                        );
                }
        }
Ejemplo n.º 18
0
        public void ServerExtraDataDisconnectTest()
        {
            using (UdpConnectionListener listener = new UdpConnectionListener(new IPEndPoint(IPAddress.Any, 4296)))
                using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296)))
                {
                    string           received = null;
                    ManualResetEvent mutex    = new ManualResetEvent(false);

                    connection.Disconnected += delegate(object sender, DisconnectedEventArgs args)
                    {
                        // We don't own the message, we have to read the string now
                        received = args.Message.ReadString();
                        mutex.Set();
                    };

                    listener.NewConnection += delegate(NewConnectionEventArgs args)
                    {
                        // As it turns out, the UdpConnectionListener can have an issue on loopback where the disconnect can happen before the hello confirm
                        // Tossing it on a different thread makes this test more reliable. Perhaps something to think about elsewhere though.
                        Task.Run(async() =>
                        {
                            await Task.Delay(1);
                            MessageWriter writer = MessageWriter.Get(SendOption.None);
                            writer.Write("Goodbye");
                            args.Connection.Disconnect("Testing", writer);
                        });
                    };

                    listener.Start();

                    connection.Connect();

                    mutex.WaitOne();

                    Assert.IsNotNull(received);
                    Assert.AreEqual("Goodbye", received);
                }
        }
Ejemplo n.º 19
0
        public void UdpHandshakeTest()
        {
            byte[] TestData = new byte[] { 1, 2, 3, 4, 5, 6 };
            using (UdpConnectionListener listener = new UdpConnectionListener(new IPEndPoint(IPAddress.Any, 4296)))
                using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296)))
                {
                    listener.Start();

                    MessageReader output = null;
                    listener.NewConnection += delegate(NewConnectionEventArgs e)
                    {
                        output = e.HandshakeData;
                    };

                    connection.Connect(TestData);

                    Thread.Sleep(10);
                    for (int i = 0; i < TestData.Length; ++i)
                    {
                        Assert.AreEqual(TestData[i], output.ReadByte());
                    }
                }
        }
Ejemplo n.º 20
0
        public void MixedConnectionTest()
        {
            using (UdpConnectionListener listener2 = new UdpConnectionListener(new IPEndPoint(IPAddress.IPv6Any, 4296), IPMode.IPv6))
            {
                listener2.Start();

                listener2.NewConnection += (evt) =>
                {
                    Console.WriteLine("v6 connection: " + ((NetworkConnection)evt.Connection).GetIP4Address());
                };

                using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4296)))
                {
                    connection.Connect();
                    Assert.AreEqual(ConnectionState.Connected, connection.State);
                }

                using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.IPv6Loopback, 4296), IPMode.IPv6))
                {
                    connection.Connect();
                    Assert.AreEqual(ConnectionState.Connected, connection.State);
                }
            }
        }
        public void MixedConnectionTest()
        {
            using (ThreadLimitedUdpConnectionListener listener2 = new ThreadLimitedUdpConnectionListener(4, new IPEndPoint(IPAddress.IPv6Any, 4296), new ConsoleLogger(), IPMode.IPv6))
            {
                listener2.Start();

                listener2.NewConnection += (evt) =>
                {
                    Console.WriteLine($"Connection: {evt.Connection.EndPoint}");
                };

                using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4296)))
                {
                    connection.Connect();
                    Assert.AreEqual(ConnectionState.Connected, connection.State);
                }

                using (UdpConnection connection2 = new UdpClientConnection(new IPEndPoint(IPAddress.IPv6Loopback, 4296), IPMode.IPv6))
                {
                    connection2.Connect();
                    Assert.AreEqual(ConnectionState.Connected, connection2.State);
                }
            }
        }
Ejemplo n.º 22
0
 public bool Connect(IPAddress address, int port)
 {
     CreateConnection(address, port);
     connection.Connect();
     return(IsConnected);
 }