/// <summary> /// Runs a client disconnect 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 RunClientDisconnectTest(NetworkConnectionListener listener, Connection connection) { 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(); }
/// <summary> /// Ensures a client sends a disconnect packet to the server on Dispose. /// </summary> /// <param name="listener">The listener to test.</param> /// <param name="connection">The connection to test.</param> internal static void RunClientDisconnectOnDisposeTest(NetworkConnectionListener listener, Connection connection) { 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(); if (!mutex.WaitOne(TimeSpan.FromSeconds(1))) { Assert.Fail("Timeout waiting for client connection"); } connection.Dispose(); if (!mutex2.WaitOne(TimeSpan.FromSeconds(1))) { Assert.Fail("Timeout waiting for client disconnect packet"); } }
/// <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(NetworkConnectionListener 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); }
/// <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(NetworkConnectionListener 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); }