Example #1
0
        public void CheckMessagesAreSent()
        {
            using (var rUDPServer = new RUDP <MockUDPSocket>(serverSocket)) {
                using (var rUDPClient = new RUDP <MockUDPSocket>(clientSocket)) {
                    EndPoint remote       = null;
                    byte[]   dataReceived = null;
                    var      wait         = new ManualResetEvent(false);
                    rUDPServer.ConnetionRequested += (EndPoint e, byte[] data) => {
                        return(true);
                    };
                    rUDPServer.DataReceived = (EndPoint e, byte[] data) => {
                        wait.Set();
                        remote       = e;
                        dataReceived = data;
                        return(true);
                    };
                    rUDPClient.ConnetionRequested += (EndPoint e, byte[] data) => {
                        return(true);
                    };
                    rUDPClient.DataReceived = (EndPoint e, byte [] data) => {
                        wait.Set();
                        remote       = e;
                        dataReceived = data;
                        return(true);
                    };
                    rUDPServer.Start(8000);
                    rUDPClient.Start(8001);
                    Assert.IsTrue(rUDPClient.Connect(serverAny.Address.ToString(), 8000));
                    wait.WaitOne(500);
                    Assert.AreEqual(1, rUDPServer.Remotes.Length);
                    Assert.AreEqual(rUDPClient.EndPoint, rUDPServer.Remotes[0]);

                    Assert.AreEqual(1, rUDPClient.Remotes.Length);
                    Assert.AreEqual(rUDPServer.EndPoint, rUDPClient.Remotes[0]);

                    byte[] message = Encoding.ASCII.GetBytes("Ping");
                    Assert.IsTrue(rUDPClient.SendTo(rUDPServer.EndPoint, Channel.None, message));
                    wait.WaitOne(5000);

                    Assert.AreEqual(message, dataReceived, $"({(string.Join (",", dataReceived ?? new byte[0]))}) != ({(string.Join (",", message))})");
                    Assert.AreEqual(rUDPClient.EndPoint, remote);

                    message      = Encoding.ASCII.GetBytes("Pong");
                    dataReceived = null;
                    remote       = null;
                    wait.Reset();
                    Assert.IsTrue(rUDPServer.SendToAll(Channel.None, message));

                    wait.WaitOne(5000);

                    Assert.AreEqual(message, dataReceived, $"({(string.Join (",", dataReceived ?? new byte[0]))}) != ({(string.Join (",", message))})");
                    Assert.AreEqual(rUDPServer.EndPoint, remote);

                    Assert.IsTrue(rUDPClient.Disconnect());
                    Thread.Sleep(100);
                    Assert.AreEqual(0, rUDPServer.Remotes.Length);
                    Assert.AreEqual(0, rUDPClient.Remotes.Length);
                }
            }
        }
Example #2
0
 static void Main(string[] args)
 {
     using (var server = new RUDP <UDPSocket> (new UDPSocket("ServerSocket"))) {
         server.ConnetionRequested += (e, d) => {
             Console.WriteLine($"{e} Connected. {Encoding.ASCII.GetString (d)}");
             return(true);
         };
         server.DataReceived += (e, d) => {
             string message = Encoding.UTF8.GetString(d);
             Console.WriteLine($"{e}: {message} ");
             server.SendToAll(Channel.ReliableInOrder, d);
             return(true);
         };
         server.Disconnected += (e) => {
             Console.WriteLine($"{e} Disconnected.");
         };
         server.Start(8000);
         Console.WriteLine("Waiting...");
         bool done = false;
         while (!done)
         {
             if (Console.KeyAvailable && Console.ReadKey().Key == ConsoleKey.Q)
             {
                 break;
             }
             Thread.Sleep(10);
         }
         Console.WriteLine("Shutting Down");
         server.Disconnect();
     }
 }