public void UdpUnreliableServerToClientTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { TestHelper.RunServerToClientTest(listener, connection, 10, SendOption.None); } }
public void UdpReliableClientToServerTest() { using (ThreadLimitedUdpConnectionListener listener = new ThreadLimitedUdpConnectionListener(2, new IPEndPoint(IPAddress.Any, 4296), new NullLogger())) using (UdpConnection connection = new UdpClientConnection(new IPEndPoint(IPAddress.Loopback, 4296))) { TestHelper.RunClientToServerTest(listener, connection, 10, SendOption.Reliable); } }
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 UdpIPv6ConnectionTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.IPv6Any, 4296), new TestLogger(), IPMode.IPv6)) { listener.Start(); using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4296), new TestLogger(), IPMode.IPv6)) { connection.Connect(); } } }
public void DtlsIPv4ConnectionTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { listener.Start(); connection.Connect(); Assert.AreEqual(ConnectionState.Connected, connection.State); } }
public void UdpIPv4ConnectionTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { listener.Start(); connection.Connect(); Assert.AreEqual(ConnectionState.Connected, connection.State); Console.Write($"Client sent {connection.Statistics.TotalBytesSent} bytes "); } }
/// <summary> /// Runs a general test on the given listener and connection. /// </summary> /// <param name="listener">The listener to test.</param> /// <param name="connection">The connection to test.</param> internal static void RunServerToClientTest(ThreadLimitedUdpConnectionListener listener, Connection connection, int dataSize, SendOption sendOption) { //Setup meta stuff byte[] data = BuildData(dataSize); ManualResetEvent mutex = new ManualResetEvent(false); //Setup listener listener.NewConnection += delegate(NewConnectionEventArgs ncArgs) { ncArgs.Connection.SendBytes(data, sendOption); }; listener.Start(); DataReceivedEventArgs?args = null; //Setup conneciton connection.DataReceived += delegate(DataReceivedEventArgs a) { Trace.WriteLine("Data was received correctly."); try { args = a; } finally { mutex.Set(); } }; connection.Connect(); //Wait until data is received mutex.WaitOne(); Assert.AreEqual(data.Length, args.Value.Message.Length); for (int i = 0; i < data.Length; i++) { Assert.AreEqual(data[i], args.Value.Message.ReadByte()); } Assert.AreEqual(sendOption, args.Value.SendOption); }
/// <summary> /// Runs a general test on the given listener and connection. /// </summary> /// <param name="listener">The listener to test.</param> /// <param name="connection">The connection to test.</param> internal static void RunClientToServerTest(ThreadLimitedUdpConnectionListener listener, Connection connection, int dataSize, SendOption sendOption) { //Setup meta stuff byte[] data = BuildData(dataSize); ManualResetEvent mutex = new ManualResetEvent(false); ManualResetEvent mutex2 = new ManualResetEvent(false); //Setup listener DataReceivedEventArgs?result = null; listener.NewConnection += delegate(NewConnectionEventArgs args) { args.Connection.DataReceived += delegate(DataReceivedEventArgs innerArgs) { Trace.WriteLine("Data was received correctly."); result = innerArgs; mutex2.Set(); }; mutex.Set(); }; listener.Start(); //Connect connection.Connect(); mutex.WaitOne(); connection.SendBytes(data, sendOption); //Wait until data is received mutex2.WaitOne(); Assert.AreEqual(data.Length, result.Value.Message.Length); for (int i = 0; i < data.Length; i++) { Assert.AreEqual(data[i], result.Value.Message.ReadByte()); } Assert.AreEqual(sendOption, result.Value.SendOption); }
public void UdpFieldTest() { IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 4296); using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(ep, new TestLogger())) { listener.Start(); connection.Connect(); //Connection fields Assert.AreEqual(ep, connection.EndPoint); //UdpConnection fields Assert.AreEqual(new IPEndPoint(IPAddress.Loopback, 4296), connection.EndPoint); Assert.AreEqual(1, connection.Statistics.DataBytesSent); Assert.AreEqual(0, connection.Statistics.DataBytesReceived); } }
public virtual void KeepAliveClientTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { 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 ); } }
public void ServerDisconnectTest() { using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { SemaphoreSlim mutex = new SemaphoreSlim(0, 100); ManualResetEventSlim serverMutex = new ManualResetEventSlim(false); connection.Disconnected += delegate(object sender, DisconnectedEventArgs args) { mutex.Release(); }; listener.NewConnection += delegate(NewConnectionEventArgs args) { mutex.Release(); // This has to be on a new thread because the client will go straight from Connecting to NotConnected ThreadPool.QueueUserWorkItem(_ => { serverMutex.Wait(500); args.Connection.Disconnect("Testing"); }); }; listener.Start(); connection.Connect(); mutex.Wait(500); Assert.AreEqual(ConnectionState.Connected, connection.State); serverMutex.Set(); mutex.Wait(500); Assert.AreEqual(ConnectionState.NotConnected, connection.State); } }
public void UdpHandshakeTest() { byte[] TestData = new byte[] { 1, 2, 3, 4, 5, 6 }; using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { listener.Start(); MessageReader output = null; listener.NewConnection += delegate(NewConnectionEventArgs e) { output = e.HandshakeData.Duplicate(); }; connection.Connect(TestData); Thread.Sleep(10); for (int i = 0; i < TestData.Length; ++i) { Assert.AreEqual(TestData[i], output.ReadByte()); } } }
public void KeepAliveServerTest() { ManualResetEvent mutex = new ManualResetEvent(false); using (ThreadLimitedUdpConnectionListener listener = this.CreateListener(2, new IPEndPoint(IPAddress.Any, 4296), new TestLogger())) using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Loopback, 4296), new TestLogger())) { UdpConnection client = null; listener.NewConnection += delegate(NewConnectionEventArgs args) { client = (UdpConnection)args.Connection; client.KeepAliveInterval = 100; Thread timeoutThread = new Thread(() => { Thread.Sleep(1050); //Enough time for ~10 keep alive packets mutex.Set(); }); timeoutThread.Start(); }; 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 ); } }
public void MixedConnectionTest() { using (ThreadLimitedUdpConnectionListener listener2 = this.CreateListener(4, new IPEndPoint(IPAddress.IPv6Any, 4296), new ConsoleLogger(true), IPMode.IPv6)) { listener2.Start(); listener2.NewConnection += (evt) => { Console.WriteLine($"Connection: {evt.Connection.EndPoint}"); }; using (UdpConnection connection = this.CreateConnection(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4296), new TestLogger())) { connection.Connect(); Assert.AreEqual(ConnectionState.Connected, connection.State); } using (UdpConnection connection2 = this.CreateConnection(new IPEndPoint(IPAddress.IPv6Loopback, 4296), new TestLogger(), IPMode.IPv6)) { connection2.Connect(); Assert.AreEqual(ConnectionState.Connected, connection2.State); } } }
public void ServerDisconnectTest() { 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); connection.Disconnected += delegate(object sender, DisconnectedEventArgs args) { mutex.Set(); }; listener.NewConnection += delegate(NewConnectionEventArgs args) { args.Connection.Disconnect("Testing"); }; listener.Start(); connection.Connect(); mutex.WaitOne(); } }