Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 2
0
 protected override Task ExecuteAsync(CancellationToken stoppingToken)
 {
     using (var server = new RUDP <UDPSocket> (new UDPSocket("ServerSocket"))) {
         server.ConnetionRequested += (e, d) => {
             _logger.LogInformation($"{e} Connected. {Encoding.ASCII.GetString (d)}");
             return(true);
         };
         server.DataReceived += (e, d) => {
             string message = Encoding.UTF8.GetString(d);
             _logger.LogInformation($"{e}: {message} ");
             server.SendTo(e, Channel.ReliableInOrder, d);
             return(true);
         };
         server.Start(8001);
         _logger.LogInformation("Running.");
         while (!stoppingToken.IsCancellationRequested)
         {
             Thread.Sleep(10);
         }
         _logger.LogInformation("Shutting Down");
         server.Disconnect();
     }
     return(Task.CompletedTask);
 }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Host IP");
            string    remoteIp        = Console.ReadLine();
            IPAddress remoteIpAddress = IPAddress.Parse(remoteIp);

            Console.WriteLine("Enter Host Port");
            string remotePort = Console.ReadLine();
            int    port       = int.Parse(remotePort);

            using (var client = new RUDP <UDPSocket> (new UDPSocket("ClientSocket"))) {
                client.ConnetionRequested += (e, d) => {
                    Console.WriteLine($"{e} Connected. {Encoding.ASCII.GetString (d)}");
                    return(true);
                };
                client.DataReceived += (e, d) => {
                    Console.WriteLine($"{e}: {Encoding.UTF8.GetString (d)} ");
                    return(true);
                };
                client.Start(8001);
                Console.WriteLine("Connecting...");
                client.Connect(remoteIpAddress.ToString(), port);
                EndPoint ep = new IPEndPoint(remoteIpAddress, port);
                Console.WriteLine("Connected.");

                bool   done    = false;
                string message = string.Empty;
                while (!done)
                {
                    if (Console.KeyAvailable)
                    {
                        var info = Console.ReadKey();
                        if (info.Key == ConsoleKey.Q && info.Modifiers.HasFlag(ConsoleModifiers.Control))
                        {
                            break;
                        }
                        if (info.Key == ConsoleKey.Enter)
                        {
                            client.SendTo(ep, Channel.ReliableInOrder, Encoding.UTF8.GetBytes(message));
                            message = string.Empty;
                            continue;
                        }
                        if (info.Key == ConsoleKey.P && info.Modifiers.HasFlag(ConsoleModifiers.Control))
                        {
                            client.Ping();
                            continue;
                        }
                        message += info.KeyChar;
                    }
                    Thread.Sleep(10);
                }
                Console.WriteLine("Shutting Down");
                client.Disconnect();
            }
            //     int serverPort = 8002;
            //     int clientPort = 8003;
            //     bool done = false;
            //     List<Task> tasks = new List<Task> ();
            //     System.Threading.CancellationTokenSource cts = new System.Threading.CancellationTokenSource ();
            //     using (var server = new UDPSocket ("Server")) {
            //         server.Initialize ();
            //         server.Listen (serverPort);
            //         tasks.Add (Task.Run (async () => {
            //             int i = 50000;
            //             try {
            //                 while (!done) {
            //                     var data = await server.ReceiveFrom (new IPEndPoint(IPAddress.Any, serverPort), cts.Token);
            //                     if (data.length > 0) {
            //                         Console.WriteLine ( $"SERVER: Got {Encoding.ASCII.GetString (data.data)} from {data.remote}");
            //                         server.ReturnBuffer (data.data);
            //                         await server.SendTo (data.remote, Encoding.ASCII.GetBytes ($"ACK{i++}"), cts.Token);
            //                     }
            //                 }
            //             } catch (Exception ex) {
            //                 Console.WriteLine(ex);
            //             } finally {
            //                 Console.WriteLine ($"Server {i-50000}");
            //             }
            //         }));
            //         tasks.Add (Task.Run ( async () => {
            //             int i = 20000;
            //             try {
            //                 using (var socket = new UDPSocket ($"Client 1")) {
            //                     socket.Initialize ();
            //                     socket.Listen (clientPort+1);
            //                     var local = new IPEndPoint (IPAddress.Parse ("127.0.0.1"), serverPort);

            //                     int c = 1000;

            //                     while (!done) {
            //                         var data = await socket.ReceiveFrom (local, cts.Token);
            //                         if (data.length > 0) {
            //                             Console.WriteLine ( $"CLIENT: Got {Encoding.ASCII.GetString (data.data)} from {data.remote}");
            //                             socket.ReturnBuffer (data.data);
            //                             //await socket.SendTo (data.remote, Encoding.ASCII.GetBytes ($"ACK{i++}"), cts.Token);
            //                         }
            //                         c--;
            //                         if (c < 0)
            //                             await socket.SendTo (local, Encoding.ASCII.GetBytes ("Hello"), cts.Token);
            //                     }
            //                 }
            //             } catch (Exception ex) {
            //                 Console.WriteLine(ex);
            //             } finally {
            //                 Console.WriteLine ($"Client 1 {i-20000}");
            //             }
            //         }));

            //         TimeSpan ts = TimeSpan.FromSeconds (5);
            //         while (!done) {
            //             System.Threading.Thread.Sleep (10);
            //             ts = ts.Add (-TimeSpan.FromMilliseconds (10));
            //             if (ts.TotalMilliseconds < 0) {
            //                 done = true;
            //                 cts.Cancel ();
            //                 System.Threading.Thread.Sleep(100);
            //                 try {
            //                 Task.WaitAll (tasks.ToArray ());
            //                 } catch {
            //                     // ignore we will get a cancelled error
            //                 }
            //                 Console.WriteLine ("Done");
            //             }
            //         }
            //     }
        }