コード例 #1
0
ファイル: SslTests.cs プロジェクト: winlith/NetCoreServer
        public void TcpServerMulticastTest()
        {
            string address = "127.0.0.1";
            int    port    = 2223;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            // Disconnect the Echo client
            Assert.True(client1.DisconnectAsync());
            while (client1.IsConnected || client1.IsHandshaked || (server.Clients != 2))
            {
                Thread.Yield();
            }

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

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

            // Disconnect the Echo client
            Assert.True(client2.DisconnectAsync());
            while (client2.IsConnected || client2.IsHandshaked || (server.Clients != 1))
            {
                Thread.Yield();
            }

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

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

            // Disconnect the Echo client
            Assert.True(client3.DisconnectAsync());
            while (client3.IsConnected || client3.IsHandshaked || (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.Handshaked);
            Assert.True(server.Disconnected);
            Assert.True(server.BytesSent == 36);
            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 == 12);
            Assert.True(client2.BytesReceived == 12);
            Assert.True(client3.BytesReceived == 12);
            Assert.True(!client1.Errors);
            Assert.True(!client2.Errors);
            Assert.True(!client3.Errors);
        }
コード例 #2
0
ファイル: SslTests.cs プロジェクト: winlith/NetCoreServer
        public void TcpServerNoClientCertTest()
        {
            string address = "127.0.0.1";
            int    port    = 2222;

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

            serverContext.ClientCertificateRequired = false;

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

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

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

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

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

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

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

            // Disconnect the Echo client
            Assert.True(client.DisconnectAsync());
            while (client.IsConnected || client.IsHandshaked || (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.Handshaked);
            Assert.True(server.Disconnected);
            Assert.True(server.BytesSent == 4);
            Assert.True(server.BytesReceived == 4);
            Assert.True(!server.Errors);

            // Check the Echo client state
            Assert.True(client.Connected);
            Assert.True(client.Handshaked);
            Assert.True(client.Disconnected);
            Assert.True(client.BytesSent == 4);
            Assert.True(client.BytesReceived == 4);
            Assert.True(!client.Errors);
        }