Пример #1
0
        public void TcpServerMulticastTest()
        {
            string address = "127.0.0.1";
            int    port    = 8445;

            // Create and prepare a new SSL server context
            var serverContext = EchoWssServer.CreateContext();

            // Create and start Echo server
            var server = new EchoWssServer(serverContext, IPAddress.Any, port);

            Assert.True(server.Start());
            while (!server.IsStarted)
            {
                Thread.Yield();
            }

            // Create and prepare a new SSL client context
            var clientContext = EchoWssClient.CreateContext();

            // Create and connect Echo client
            var client1 = new EchoWssClient(clientContext, address, port);

            Assert.True(client1.ConnectAsync());
            while (!client1.Connected || (server.Clients != 1))
            {
                Thread.Yield();
            }

            // Multicast some data to all clients
            server.MulticastText("test");

            // Wait for all data processed...
            while (client1.Received != 4)
            {
                Thread.Yield();
            }

            // Create and connect Echo client
            var client2 = new EchoWssClient(clientContext, address, port);

            Assert.True(client2.ConnectAsync());
            while (!client2.Connected || (server.Clients != 2))
            {
                Thread.Yield();
            }

            // Multicast some data to all clients
            server.MulticastText("test");

            // Wait for all data processed...
            while ((client1.Received != 8) || (client2.Received != 4))
            {
                Thread.Yield();
            }

            // Create and connect Echo client
            var client3 = new EchoWssClient(clientContext, address, port);

            Assert.True(client3.ConnectAsync());
            while (!client3.Connected || (server.Clients != 3))
            {
                Thread.Yield();
            }

            // Multicast some data to all clients
            server.MulticastText("test");

            // Wait for all data processed...
            while ((client1.Received != 12) || (client2.Received != 8) || (client3.Received != 4))
            {
                Thread.Yield();
            }

            // Disconnect the Echo client
            Assert.True(client1.CloseAsync(1000));
            while (!client1.Disconnected || (server.Clients != 2))
            {
                Thread.Yield();
            }

            // Multicast some data to all clients
            server.MulticastText("test");

            // Wait for all data processed...
            while ((client1.Received != 12) || (client2.Received != 12) || (client3.Received != 8))
            {
                Thread.Yield();
            }

            // Disconnect the Echo client
            Assert.True(client2.CloseAsync(1000));
            while (!client2.Disconnected || (server.Clients != 1))
            {
                Thread.Yield();
            }

            // Multicast some data to all clients
            server.MulticastText("test");

            // Wait for all data processed...
            while ((client1.Received != 12) || (client2.Received != 12) || (client3.Received != 12))
            {
                Thread.Yield();
            }

            // Disconnect the Echo client
            Assert.True(client3.CloseAsync(1000));
            while (!client3.Disconnected || (server.Clients != 0))
            {
                Thread.Yield();
            }

            // Stop the Echo server
            Assert.True(server.Stop());
            while (server.IsStarted)
            {
                Thread.Yield();
            }

            // Check the Echo server state
            Assert.True(server.Started);
            Assert.True(server.Stopped);
            Assert.True(server.Connected);
            Assert.True(server.Disconnected);
            Assert.True(server.BytesSent > 0);
            Assert.True(server.BytesReceived > 0);
            Assert.True(!server.Errors);

            // Check the Echo client state
            Assert.True(client1.BytesSent > 0);
            Assert.True(client2.BytesSent > 0);
            Assert.True(client3.BytesSent > 0);
            Assert.True(client1.BytesReceived > 0);
            Assert.True(client2.BytesReceived > 0);
            Assert.True(client3.BytesReceived > 0);
            Assert.True(!client1.Errors);
            Assert.True(!client2.Errors);
            Assert.True(!client3.Errors);
        }
Пример #2
0
        public void TcpServerRandomTest()
        {
            string address = "127.0.0.1";
            int    port    = 8446;

            // Create and prepare a new SSL server context
            var serverContext = EchoWssServer.CreateContext();

            // Create and start Echo server
            var server = new EchoWssServer(serverContext, IPAddress.Any, port);

            Assert.True(server.Start());
            while (!server.IsStarted)
            {
                Thread.Yield();
            }

            // Test duration in seconds
            int duration = 10;

            // Create and prepare a new SSL client context
            var clientContext = EchoWssClient.CreateContext();

            // Clients collection
            var clients = new List <EchoWssClient>();

            // Start random test
            var rand  = new Random();
            var start = DateTime.UtcNow;

            while ((DateTime.UtcNow - start).TotalSeconds < duration)
            {
                // Disconnect all clients
                if ((rand.Next() % 1000) == 0)
                {
                    server.CloseAll(1000);
                }
                // Create a new client and connect
                else if ((rand.Next() % 100) == 0)
                {
                    if (clients.Count < 100)
                    {
                        // Create and connect Echo client
                        var client = new EchoWssClient(clientContext, address, port);
                        clients.Add(client);
                        client.ConnectAsync();
                        while (!client.IsHandshaked)
                        {
                            Thread.Yield();
                        }
                    }
                }
                // Connect/Disconnect the random client
                else if ((rand.Next() % 100) == 0)
                {
                    if (clients.Count > 0)
                    {
                        int index  = rand.Next() % clients.Count;
                        var client = clients[index];
                        if (client.IsHandshaked)
                        {
                            client.CloseAsync(1000);
                            while (client.IsConnected)
                            {
                                Thread.Yield();
                            }
                        }
                        else if (!client.IsConnected)
                        {
                            client.ConnectAsync();
                            while (!client.IsHandshaked)
                            {
                                Thread.Yield();
                            }
                        }
                    }
                }
                // Reconnect the random client
                else if ((rand.Next() % 100) == 0)
                {
                    if (clients.Count > 0)
                    {
                        int index  = rand.Next() % clients.Count;
                        var client = clients[index];
                        if (client.IsHandshaked)
                        {
                            client.ReconnectAsync();
                            while (!client.IsHandshaked)
                            {
                                Thread.Yield();
                            }
                        }
                    }
                }
                // Multicast a message to all clients
                else if ((rand.Next() % 10) == 0)
                {
                    server.MulticastText("test");
                }
                // Send a message from the random client
                else if ((rand.Next() % 1) == 0)
                {
                    if (clients.Count > 0)
                    {
                        int index  = rand.Next() % clients.Count;
                        var client = clients[index];
                        if (client.IsHandshaked)
                        {
                            client.SendTextAsync("test");
                        }
                    }
                }

                // Sleep for a while...
                Thread.Sleep(1);
            }

            // Disconnect clients
            foreach (var client in clients)
            {
                client.CloseAsync(1000);
                while (client.IsConnected)
                {
                    Thread.Yield();
                }
            }

            // Stop the Echo server
            Assert.True(server.Stop());
            while (server.IsStarted)
            {
                Thread.Yield();
            }

            // Check the Echo server state
            Assert.True(server.Started);
            Assert.True(server.Stopped);
            Assert.True(server.Connected);
            Assert.True(server.Disconnected);
            Assert.True(server.BytesSent > 0);
            Assert.True(server.BytesReceived > 0);
            Assert.True(!server.Errors);
        }
Пример #3
0
        public void WssServerTest()
        {
            string address = "127.0.0.1";
            int    port    = 8444;

            // Create and prepare a new SSL server context
            var serverContext = EchoWssServer.CreateContext();

            // Create and start Echo server
            var server = new EchoWssServer(serverContext, IPAddress.Any, port);

            Assert.True(server.Start());
            while (!server.IsStarted)
            {
                Thread.Yield();
            }

            // Create and prepare a new SSL client context
            var clientContext = EchoWssClient.CreateContext();

            // Create and connect Echo client
            var client = new EchoWssClient(clientContext, address, port);

            Assert.True(client.ConnectAsync());
            while (!client.Connected || (server.Clients != 1))
            {
                Thread.Yield();
            }

            // Send a message to the Echo server
            client.SendTextAsync("test");

            // Wait for all data processed...
            while (client.Received != 4)
            {
                Thread.Yield();
            }

            // Disconnect the Echo client
            Assert.True(client.CloseAsync(1000));
            while (!client.Disconnected || (server.Clients != 0))
            {
                Thread.Yield();
            }

            // Stop the Echo server
            Assert.True(server.Stop());
            while (server.IsStarted)
            {
                Thread.Yield();
            }

            // Check the Echo server state
            Assert.True(server.Started);
            Assert.True(server.Stopped);
            Assert.True(server.Connected);
            Assert.True(server.Disconnected);
            Assert.True(server.BytesSent > 0);
            Assert.True(server.BytesReceived > 0);
            Assert.True(!server.Errors);

            // Check the Echo client state
            Assert.True(client.Connected);
            Assert.True(client.Disconnected);
            Assert.True(client.BytesSent > 0);
            Assert.True(client.BytesReceived > 0);
            Assert.True(!client.Errors);
        }