public void ConnectionCountTest()
        {
            var serverName = "TestServer";

            using (X509Certificate serverCert = MakeCert(serverName))
            {
                var server = new TcpServer();
                server.ErrorEvent += Server_ErrorEvent;

                server.ConnectedEvent    += Server_ConnectedEvent;
                server.DisconnectedEvent += Server_DisconnectedEvent;

                var epList = new List <TcpServerLocalEndpoint>();
                var ipEp1  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1111), 1));
                var ipEp2  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2222), 2));
                var ipEp3  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3333), 3));

                epList.Add(ipEp1);
                epList.Add(ipEp2);
                epList.Add(ipEp3);

                server.Start(epList, serverCert, false, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);

                ConnectionTest(ipEp1, serverName, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);
                ConnectionTest(ipEp2, serverName, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);
                ConnectionTest(ipEp3, serverName, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);

                server.Dispose();
            }
        }
Example #2
0
        public void DisconnectClient_Tests()
        {
            var server = new TcpServer();

            server.ErrorEvent += Server_ErrorEvent;

            server.ConnectedEvent    += Server_ConnectedEvent;
            server.DisconnectedEvent += Server_DisconnectedEvent;

            var epList = new List <TcpServerLocalEndpoint>();
            var ipEp1  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7777);
            var ipEp2  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
            var ipEp3  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);

            epList.Add(new TcpServerLocalEndpoint(ipEp1, 1));
            epList.Add(new TcpServerLocalEndpoint(ipEp2, 1));
            epList.Add(new TcpServerLocalEndpoint(ipEp3, 1));

            server.Start(epList);

            var client = new TcpClient();

            client.ConnectedEvent    += Client_ConnectedEvent;
            client.DisconnectedEvent += Client_DisconnectedEvent;

            DisconnectClientTest(client, server, ipEp1);
            DisconnectClientTest(client, server, ipEp2);
            DisconnectClientTest(client, server, ipEp3);

            server.Dispose();

            client.Dispose();
        }
Example #3
0
        public void ConnectionCountTest()
        {
            var server = new TcpServer();

            server.ErrorEvent += Server_ErrorEvent;

            server.ConnectedEvent    += Server_ConnectedEvent;
            server.DisconnectedEvent += Server_DisconnectedEvent;

            var epList = new List <TcpServerLocalEndpoint>();
            var ipEp1  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1111), 1));
            var ipEp2  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2222), 2));
            var ipEp3  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3333), 3));

            epList.Add(ipEp1);
            epList.Add(ipEp2);
            epList.Add(ipEp3);

            server.Start(epList);

            ConnectionTest(ipEp1);
            ConnectionTest(ipEp2);
            ConnectionTest(ipEp3);

            server.Dispose();
        }
Example #4
0
        public void LocalhostServerStartedTests()
        {
            var server = new TcpServer();

            server.ErrorEvent += Server_ErrorEvent;

            server.StartListeningEvent += Server_StartListeningEvent;

            var epList = new List <TcpServerLocalEndpoint>();

            epList.Add(new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4444), 1));
            epList.Add(new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5555), 1));
            epList.Add(new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6666), 1));

            EndPointArgs.Clear();
            Assert.AreEqual(0, EndPointArgs.Count);
            server.Start(epList);
            Assert.AreEqual(3, EndPointArgs.Count);
            Assert.AreEqual(epList[0].EndPoint, EndPointArgs[0].EndPoint);
            Assert.AreEqual(epList[1].EndPoint, EndPointArgs[1].EndPoint);
            Assert.AreEqual(epList[2].EndPoint, EndPointArgs[2].EndPoint);


            Assert.IsTrue(server.IsStarted);
            Assert.IsFalse(server.IsDisposed);
            server.Dispose();
            Assert.IsTrue(server.IsDisposed);
            Assert.IsFalse(server.IsStarted);
        }
Example #5
0
 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     Server.Stop();
     Server.Dispose();
     AppServer.Stop();
     AppServer.Dispose();
 }
Example #6
0
        public void MultiClientPerfomanceTest()
        {
            var serverName = "TestServer";

            using (X509Certificate serverCert = MakeCert(serverName))
            {
                var clientsCount = 20;

                var server = new TcpServer();
                server.ErrorEvent        += Server_ErrorEvent;
                server.DataReceivedEvent += Server_DataReceivedEvent;

                var epList = new List <TcpServerLocalEndpoint>();
                var ipEp1  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6574), clientsCount));

                epList.Add(ipEp1);
                server.Start(epList, serverCert, false, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);

                for (int i = 0; i < clientsCount; i++)
                {
                    ThreadPool.QueueUserWorkItem(CallBack, server);
                }

                Thread.Sleep(30000);

                server.Dispose();
            }
        }
Example #7
0
        public void PollTest()
        {
            var server = new TcpServer();
            var epList = new List <TcpServerLocalEndpoint>();
            var ipEp1  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0192), 1));

            epList.Add(ipEp1);
            server.DataReceivedEvent += Server_DataReceivedEvent;
            server.Start(epList);

            var client = new TcpClientTestClass();

            client.ErrorEvent        += Client_ErrorEvent;
            client.DisconnectedEvent += Client_DisconnectedEvent;
            client.ConnectedEvent    += Client_ConnectedEvent;
            client.Start(ipEp1.EndPoint);

            Thread.Sleep(200);

            CheckPollInterval(client, server, 100);
            CheckPollInterval(client, server, 500);
            CheckPollInterval(client, server, 1000);
            CheckPollInterval(client, server, 2000);

            CheckPollFailCount(client, server, 1);
            CheckPollFailCount(client, server, 3);
            CheckPollFailCount(client, server, 5);
            CheckPollFailCount(client, server, 10);

            client.Dispose();
            server.Dispose();
        }
        public void EncryptedServerVsUnencryptedClientConnectDisconnectTests()
        {
            var serverName = "TestServer";

            using (X509Certificate serverCert = MakeCert(serverName))
            {
                var server = new TcpServer();
                server.ErrorEvent += Server_NonFailErrorEvent;

                server.ConnectedEvent    += Server_ConnectedEvent;
                server.DisconnectedEvent += Server_DisconnectedEvent;

                var epList = new List <TcpServerLocalEndpoint>();
                var ipEp1  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7483);

                epList.Add(new TcpServerLocalEndpoint(ipEp1, 1));

                server.Start(epList, serverCert, false, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);

                var client = new TcpClient();
                client.ConnectedEvent      += Client_ConnectedEvent;
                client.DisconnectedEvent   += Client_DisconnectedEvent;
                client.ConnectionFailEvent += Client_ConnectionFailEvent;

                ServerConnectedEventArgs      = null;
                ServerDisconnectedEventArgs   = null;
                ClientConnectedEventEventArgs = null;
                ServerErrorEvent        = null;
                ConnectionFailEventArgs = null;
                Assert.IsNull(ServerConnectedEventArgs);
                Assert.IsNull(ServerDisconnectedEventArgs);
                Assert.IsNull(ClientConnectedEventEventArgs);
                Assert.IsNull(ServerErrorEvent);
                Assert.IsNull(ConnectionFailEventArgs);
                Assert.IsFalse(ClientDisconnectedEventColled);

                client.Start(ipEp1);

                var waitStart = DateTime.Now;
                while (ServerErrorEvent == null)
                {
                    var delta = DateTime.Now - waitStart;
                    if (delta.TotalSeconds > 20)
                    {
                        Assert.Fail("Wait Timeout");
                    }
                }

                Assert.IsNotNull(ServerErrorEvent);
                Assert.IsNull(ConnectionFailEventArgs);
                Assert.IsNotNull(ClientConnectedEventEventArgs);
                Assert.IsTrue(ClientDisconnectedEventColled);
                Assert.IsNull(ServerConnectedEventArgs);
                Assert.IsNull(ServerDisconnectedEventArgs);

                server.Dispose();
                client.Dispose();
            }
        }
Example #9
0
        public void IsRunning()
        {
            var tcpServer = new TcpServer();

            tcpServer.Start(Port);
            Assert.IsTrue(tcpServer.IsRunning);
            tcpServer.Dispose();
        }
Example #10
0
 public void TearDown()
 {
     connectedSession?.Dispose();
     connectedSession = null;
     listener.Stop();
     listener.Dispose();
     listener = null;
 }
 private void Application_ApplicationExit(object sender, EventArgs e)
 {
     ((MainForm)_mainForm).PrepareShutdown();
     _mainForm.Close();
     _mainForm.Dispose();
     _server.Dispose();
     _notifyIcon.Dispose();
 }
        public override void Dispose()
        {
            Logger.Debug("Disposing ServerManager");

            TcpServer?.Dispose();
            UdpServer?.Dispose();

            Logger.Debug("Disposed ServerManager");
        }
Example #13
0
 public void Dispose()
 {
     if (m_server != null)
     {
         m_server.Dispose();
         m_server = null;
     }
     GC.SuppressFinalize(this);
 }
Example #14
0
        private void Q_TagPublish_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Server == null)
            {
                return;
            }

            Server.Dispose();
            Server = null;
        }
Example #15
0
        public void SendReceiveTest()
        {
            var server = new TcpServer();

            server.ErrorEvent += Server_ErrorEvent;

            server.DataReceivedEvent += Server_DataReceivedEvent;

            var epList = new List <TcpServerLocalEndpoint>();
            var ipEp1  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234), 1));

            epList.Add(ipEp1);
            server.Start(epList);

            var client = new TcpClient();

            client.DataReceivedEvent += Client_DataReceivedEvent;
            client.Start(ipEp1.EndPoint);
            Thread.Sleep(200);

            for (int i = 1; i < 10; i++)
            {
                var data = BitConverter.GetBytes(i);
                ServerDataReceivedEvent.Clear();
                Assert.AreEqual(0, ServerDataReceivedEvent.Count);
                client.SendData(data);
                Thread.Sleep(100);
                Assert.AreEqual(1, ServerDataReceivedEvent.Count);
                Assert.AreEqual(data.Length, ServerDataReceivedEvent[0].Data.Length);
                for (int j = 0; j < data.Length; j++)
                {
                    Assert.AreEqual(data[j], ServerDataReceivedEvent[0].Data[j]);
                    Assert.AreEqual(client.ExchangeInterface.LocalEndPoint, ServerDataReceivedEvent[0].RemoteEndPoint);
                    Assert.AreEqual(client.ExchangeInterface.RemoteEndPoint, ServerDataReceivedEvent[0].LocalEndPoint);
                }
            }

            for (int i = 1; i < 10; i++)
            {
                var data = BitConverter.GetBytes(i);
                ClientDataReceivedEvent.Clear();
                Assert.AreEqual(0, ClientDataReceivedEvent.Count);
                server.Write(data, client.ExchangeInterface.RemoteEndPoint, client.ExchangeInterface.LocalEndPoint);
                Thread.Sleep(100);
                Assert.AreEqual(1, ClientDataReceivedEvent.Count);
                Assert.AreEqual(data.Length, ClientDataReceivedEvent[0].Data.Length);
                for (int j = 0; j < data.Length; j++)
                {
                    Assert.AreEqual(data[j], ClientDataReceivedEvent[0].Data[j]);
                    Assert.AreEqual(ipEp1.EndPoint, ClientDataReceivedEvent[0].RemoteEndPoint);
                }
            }

            server.Dispose();
        }
Example #16
0
        public void ConnectDisconnectServerTests()
        {
            var server = new TcpServer();
            var epList = new List <TcpServerLocalEndpoint>();
            var ipEp1  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9876);

            epList.Add(new TcpServerLocalEndpoint(ipEp1, 1));

            var client = new TcpClient();

            client.ErrorEvent += Client_ErrorEvent;
            Assert.IsFalse(client.IsStarted);
            Assert.IsFalse(client.IsDisposed);
            Assert.IsFalse(client.IsConnected);

            client.ConnectedEvent    += Client_ConnectedEvent;
            client.DisconnectedEvent += Client_DisconnectedEvent;
            client.ReconnectingEvent += Client_ReconnectingEvent;
            client.Start(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9876));

            ClientReconnectingEventColled = false;
            Assert.IsFalse(ClientReconnectingEventColled);
            Thread.Sleep((int)(1.5 * client.ConnectInterval));
            Assert.IsTrue(ClientReconnectingEventColled);

            ClientConnectedEventArgs = null;
            Assert.IsNull(ClientConnectedEventArgs);
            server.Start(epList);
            Thread.Sleep((int)(1.5 * client.ConnectInterval));
            Assert.IsNotNull(ClientConnectedEventArgs);
            Assert.AreEqual(server.Endpoints[0].EndPoint, ClientConnectedEventArgs.RemoteEndPoint);

            ClientReconnectingEventColled = false;
            Assert.IsFalse(ClientDisconnectedEventCalled);
            client.Stop();
            Thread.Sleep((int)(1.5 * client.PollInterval));
            Assert.IsTrue(ClientDisconnectedEventCalled);

            ClientConnectedEventArgs = null;
            Assert.IsNull(ClientConnectedEventArgs);
            client.Start(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9876));
            Thread.Sleep((int)(1.5 * client.ConnectInterval));
            Assert.IsNotNull(ClientConnectedEventArgs);
            Assert.AreEqual(server.Endpoints[0].EndPoint, ClientConnectedEventArgs.RemoteEndPoint);

            ClientDisconnectedEventCalled = false;
            Assert.IsFalse(ClientDisconnectedEventCalled);
            server.Stop();
            Thread.Sleep(100);
            Assert.IsTrue(ClientDisconnectedEventCalled);

            server.Dispose();
            client.Dispose();
        }
Example #17
0
        static void Main(string[] args)
        {
            Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));

            DataServiceBase service = new TcpDataService(new TcpClient("home.sorenhk.dk"));
            service = new CacheDataService(service);

            var tcpServer = new TcpServer();

            var server = new TcpDataServiceServer(service);
            server.RegisterTo(tcpServer);

            Console.ReadLine();

            tcpServer.Dispose();
            service.Dispose();
        }
Example #18
0
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing"></param>
        void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                if (disposing)
                {
                    threadWriteToHistoryQueueWork.Abort();
                    threadLoginQueueWork.Abort();
                    threadUnsendQueueWork.Abort();
                    threadWaitSendQueueWork.Abort();

                    Server.Dispose();
                }

                m_disposed = true;
            }
        }
Example #19
0
        public void SingleClientPerfomanceTest()
        {
            var server = new TcpServer();

            server.ErrorEvent        += Server_ErrorEvent;
            server.DataReceivedEvent += Server_DataReceivedEvent;

            var epList = new List <TcpServerLocalEndpoint>();
            var ipEp1  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6574), 1));

            epList.Add(ipEp1);
            server.Start(epList);

            TestPerfomance(server, 15000);

            server.Dispose();
        }
Example #20
0
        static void Main(string[] args)
        {
            Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));

            DataServiceBase service = new TcpDataService(new TcpClient("home.sorenhk.dk"));

            service = new CacheDataService(service);

            var tcpServer = new TcpServer();

            var server = new TcpDataServiceServer(service);

            server.RegisterTo(tcpServer);

            Console.ReadLine();

            tcpServer.Dispose();
            service.Dispose();
        }
Example #21
0
        public void StartThenAcceptConnectionsThenStop()
        {
            var notifyTcpClientConnected443  = new AutoResetEvent(false);
            var notifyTcpClientConnected8443 = new AutoResetEvent(false);
            var mockTcpListenerFactory       = new Mock <ITcpListenerFactory>();

            mockTcpListenerFactory
            .Setup(inst => inst.Create(It.IsAny <string>(), 443))
            .Returns <string, int>((addr, port) => MockUpTcpClient(port, notifyTcpClientConnected443));
            mockTcpListenerFactory
            .Setup(inst => inst.Create(It.IsAny <string>(), 8443))
            .Returns <string, int>((addr, port) => MockUpTcpClient(port, notifyTcpClientConnected8443));
            var mockHandler = new Mock <ITcpClientConnectedEventHandler>();

            // Create and start the server
            var server = new TcpServer(
                mockTcpListenerFactory.Object,
                mockHandler.Object
                );

            server.Start(new int[] { 8443, 443 });

            // Wait until we served minimum 10 clients on port 443 and 8443
            WaitForClients(notifyTcpClientConnected443, 10);
            WaitForClients(notifyTcpClientConnected8443, 10);

            // Verify that we got at least 9 connections on port 8443 and 443
            // (The last one may not handled yet)
            VerifyConnectionsOnPort(mockHandler, 8443, Times.AtLeast(9));
            VerifyConnectionsOnPort(mockHandler, 443, Times.AtLeast(9));

            // Now stop the server.
            server.Dispose();

            // Now, we should no longer receive connections on both port
            mockHandler.ResetCalls();
            // Wait to see how many clients keep connecting after the server is stopped.
            // There should be at most one on each port.
            WaitForClients(notifyTcpClientConnected443, 10);
            WaitForClients(notifyTcpClientConnected8443, 10);
            VerifyConnectionsOnPort(mockHandler, 8443, Times.AtMostOnce());
            VerifyConnectionsOnPort(mockHandler, 443, Times.AtMostOnce());
        }
Example #22
0
        public void TcpServerMultipleClients()
        {
            TcpServer server = new TcpServer();

            try
            {
                server.Start(12350);
                TcpDelay();

                for (int x = 0; x < 10; x++)
                {
                    TcpClient client = new TcpClient("127.0.0.1", 12350);
                    client.Connect();
                    TcpDelay();
                }
                TcpDelay();
                Assert.AreEqual(10, server.Clients.Count);
            }
            finally { server.Dispose(); }
        }
Example #23
0
        public void SingleClientPerfomanceTest()
        {
            var serverName = "TestServer";

            using (X509Certificate serverCert = MakeCert(serverName))
            {
                var server = new TcpServer();
                server.ErrorEvent        += Server_ErrorEvent;
                server.DataReceivedEvent += Server_DataReceivedEvent;

                var epList = new List <TcpServerLocalEndpoint>();
                var ipEp1  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6574), 1));

                epList.Add(ipEp1);
                server.Start(epList, serverCert, false, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);

                TestPerfomance(server, 15000, serverName);

                server.Dispose();
            }
        }
        public void DisconnectClient_Tests()
        {
            var serverName = "TestServer";

            using (X509Certificate serverCert = MakeCert(serverName))
            {
                var server = new TcpServer();
                server.ErrorEvent += Server_ErrorEvent;

                server.ConnectedEvent    += Server_ConnectedEvent;
                server.DisconnectedEvent += Server_DisconnectedEvent;

                var epList = new List <TcpServerLocalEndpoint>();
                var ipEp1  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7777);
                var ipEp2  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
                var ipEp3  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);

                epList.Add(new TcpServerLocalEndpoint(ipEp1, 1));
                epList.Add(new TcpServerLocalEndpoint(ipEp2, 1));
                epList.Add(new TcpServerLocalEndpoint(ipEp3, 1));

                server.Start(epList, serverCert, false, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);

                var client = new TcpClient();
                client.ConnectedEvent    += Client_ConnectedEvent;
                client.DisconnectedEvent += Client_DisconnectedEvent;

                X509Certificate clientCert = MakeCert(serverName);

                DisconnectClientTest(client, server, ipEp1, serverName, clientCert, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);
                DisconnectClientTest(client, server, ipEp2, serverName, clientCert, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);
                DisconnectClientTest(client, server, ipEp3, serverName, clientCert, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);

                server.Dispose();

                client.Dispose();

                clientCert.Dispose();
            }
        }
Example #25
0
        public void ServertCertCheckTest()
        {
            var serverName = "TestServer";

            using (X509Certificate serverCert = MakeCert(serverName))
            {
                var server = new TcpServer();

                var epList = new List <TcpServerLocalEndpoint>();
                var ipEp1  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234), 1));

                epList.Add(ipEp1);
                server.Start(epList, serverCert, true, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);
                X509Certificate clientCert = MakeCert(serverName);
                var             client     = new EncryptedTcpClientTestClass();

                client.ServerCertificate = null;
                client.Start(ipEp1.EndPoint, serverName, clientCert, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);
                var waitStart = DateTime.Now;
                while (client.ServerCertificate == null)
                {
                    var delta = DateTime.Now - waitStart;
                    if (delta.TotalSeconds > 5)
                    {
                        Assert.Fail("Wait Timeout");
                    }
                }
                Assert.IsNotNull(client.ServerCertificate);
                Assert.AreEqual(serverCert, client.ServerCertificate);

                client.Stop();
                server.Stop();

                server.Dispose();
                client.Dispose();
                clientCert.Dispose();
            }
        }
Example #26
0
        public void Dispose()
        {
            try
            {
                if (IsConnected)
                {
                    server?.DisconnectClient(client);
                }
            }
            catch (Exception)
            {
                // ignored
            }

            try
            {
                server?.Dispose();
            }
            catch (Exception)
            {
                // ignored
            }
        }
Example #27
0
        public void ClientConnectDisconnect_Event()
        {
            TcpServer server = new TcpServer("TestServer-Event");

            server.Start(12200);
            try
            {
                Client1 = new TcpClient("127.0.0.1", 12200);
                Client1.ConnectionStatusChanged += Client_ConnectionStatusChanged;
                Client1.Connect();
                Thread.Sleep(200);
                Assert.AreEqual(true, ClientConnectFlag);

                server.Stop();
                Thread.Sleep(200);
                Assert.AreEqual(false, ClientConnectFlag);
            }
            finally
            {
                Client1.Disconnect();
                server.Dispose();
            }
        }
Example #28
0
        public void PollTest()
        {
            var serverName = "TestServer";

            using (X509Certificate serverCert = MakeCert(serverName))
            {
                var server = new TcpServer();
                var epList = new List <TcpServerLocalEndpoint>();
                var ipEp1  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0192), 1));
                epList.Add(ipEp1);
                server.DataReceivedEvent += Server_DataReceivedEvent;
                server.Start(epList, serverCert, false, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);

                X509Certificate clientCert = MakeCert(serverName);
                var             client     = new TcpClientTestClass();
                client.ErrorEvent        += Client_ErrorEvent;
                client.DisconnectedEvent += Client_DisconnectedEvent;
                client.ConnectedEvent    += Client_ConnectedEvent;
                client.Start(ipEp1.EndPoint, serverName, clientCert, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);

                Thread.Sleep(200);

                CheckPollInterval(client, server, 100);
                CheckPollInterval(client, server, 500);
                CheckPollInterval(client, server, 1000);
                CheckPollInterval(client, server, 2000);

                CheckPollFailCount(client, server, 1);
                CheckPollFailCount(client, server, 3);
                CheckPollFailCount(client, server, 5);
                CheckPollFailCount(client, server, 10);

                client.Dispose();
                server.Dispose();
                clientCert.Dispose();
            }
        }
Example #29
0
        public void MultiClientPerfomanceTest()
        {
            var clientsCount = 20;

            var server = new TcpServer();

            server.ErrorEvent        += Server_ErrorEvent;
            server.DataReceivedEvent += Server_DataReceivedEvent;

            var epList = new List <TcpServerLocalEndpoint>();
            var ipEp1  = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6574), clientsCount));

            epList.Add(ipEp1);
            server.Start(epList);

            for (int i = 0; i < clientsCount; i++)
            {
                ThreadPool.QueueUserWorkItem(CallBack, server);
            }

            Thread.Sleep(30000);

            server.Dispose();
        }
        public void WebSocketClient_PollFailed_Test()
        {
            var c = new WebSocketClient(String.Empty);

            c.ConnectedEvent    += C_ConnectedEvent;
            c.DisconnectedEvent += C_DisconnectedEvent;
            c.HandshakeEvent    += C_HandshakeEvent;
            c.ErrorEvent        += C_ErrorEvent;

            var clientsCount = 1;
            var ipep         = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4447);
            var epList       = new List <TcpServerLocalEndpoint>
            {
                new TcpServerLocalEndpoint(ipep, clientsCount)
            };

            var s = new TcpServer();

            s.ConnectedEvent    += S_ConnectedEvent;
            s.DisconnectedEvent += S_DisconnectedEvent;
            s.DataReceivedEvent += tcpS_DataReceivedEvent;
            s.Start(epList);

            _serverConnectionEventArgs     = null;
            _clientConnectionEventArgs     = null;
            _serverHandshakeEventArgs      = null;
            _clientHandshakeEventArgs      = null;
            _tcpServerNetworkDataEventArgs = null;
            _serverDisconnectionEventArgs  = null;
            _clietErrorEventArgs           = null;
            c.Start(ipep);
            var startTime = DateTime.Now;

            while (_serverConnectionEventArgs == null || _clientConnectionEventArgs == null)
            {
                var delta = DateTime.Now - startTime;
                if (delta.TotalSeconds > 5)
                {
                    Assert.Fail();
                }
                Thread.Sleep(1);
            }

            startTime = DateTime.Now;
            while (_tcpServerNetworkDataEventArgs == null)
            {
                var delta = DateTime.Now - startTime;
                if (delta.TotalSeconds > 5)
                {
                    Assert.Fail();
                }
                Thread.Sleep(1);
            }

            var str    = Encoding.UTF8.GetString(_tcpServerNetworkDataEventArgs.Data);
            var strarr = str.Split('\r', '\n');

            Assert.IsTrue(strarr[6].Contains("Sec-WebSocket-Key:"));
            var key = strarr[6].Split(':')[1].Trim();

            var retStr = "HTTP/1.1 101 Switching Protocols\r\n";

            retStr += "Upgrade: websocket\r\n";
            retStr += "Connection: Upgrade\r\n";
            retStr += $"Sec-WebSocket-Accept: {AcceptKeyGenerator.AcceptKey(key)}\r\n\r\n";

            s.Write(Encoding.UTF8.GetBytes(retStr), _tcpServerNetworkDataEventArgs.LocalEndPoint, _tcpServerNetworkDataEventArgs.RemoteEndPoint);

            _clientHandshakeEventArgs = null;
            startTime = DateTime.Now;
            while (_clientHandshakeEventArgs == null)
            {
                var delta = DateTime.Now - startTime;
                if (delta.TotalSeconds > 5)
                {
                    Assert.Fail();
                }
                Thread.Sleep(1);
            }

            _tcpServerNetworkDataEventArgs = null;
            startTime = DateTime.Now;
            while (_tcpServerNetworkDataEventArgs == null)
            {
                var delta = DateTime.Now - startTime;
                if (delta.TotalSeconds > 5)
                {
                    Assert.Fail();
                }
                Thread.Sleep(1);
            }
            Assert.AreEqual(0x89, _tcpServerNetworkDataEventArgs.Data[0]);
            Assert.AreEqual(0x00, _tcpServerNetworkDataEventArgs.Data[1]);

            _serverDisconnectionEventArgs = null;
            startTime = DateTime.Now;
            while (_serverDisconnectionEventArgs == null)
            {
                var delta = DateTime.Now - startTime;
                if (delta.TotalSeconds > 10)
                {
                    Assert.Fail();
                }
                Thread.Sleep(1);
            }

            c.Dispose();
            s.Dispose();
        }
        public void WebSocketClient_HandShake_Fail_Accept_Test()
        {
            var c = new WebSocketClient(String.Empty);

            c.ConnectedEvent    += C_ConnectedEvent;
            c.DisconnectedEvent += C_DisconnectedEvent;
            c.HandshakeEvent    += C_HandshakeEvent;
            c.ErrorEvent        += C_ErrorEvent;

            var clientsCount = 1;
            var ipep         = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4447);
            var epList       = new List <TcpServerLocalEndpoint>
            {
                new TcpServerLocalEndpoint(ipep, clientsCount)
            };

            var s = new TcpServer();

            s.ConnectedEvent    += S_ConnectedEvent;
            s.DisconnectedEvent += S_DisconnectedEvent;
            s.DataReceivedEvent += tcpS_DataReceivedEvent;
            s.Start(epList);

            _serverConnectionEventArgs     = null;
            _clientConnectionEventArgs     = null;
            _serverHandshakeEventArgs      = null;
            _clientHandshakeEventArgs      = null;
            _tcpServerNetworkDataEventArgs = null;
            _clietErrorEventArgs           = null;
            c.Start(ipep);
            var startTime = DateTime.Now;

            while (_serverConnectionEventArgs == null || _clientConnectionEventArgs == null)
            {
                var delta = DateTime.Now - startTime;
                if (delta.TotalSeconds > 5)
                {
                    Assert.Fail();
                }
                Thread.Sleep(1);
            }

            startTime = DateTime.Now;
            while (_tcpServerNetworkDataEventArgs == null)
            {
                var delta = DateTime.Now - startTime;
                if (delta.TotalSeconds > 5)
                {
                    Assert.Fail();
                }
                Thread.Sleep(1);
            }

            var str    = Encoding.UTF8.GetString(_tcpServerNetworkDataEventArgs.Data);
            var strarr = str.Split('\r', '\n');

            Assert.AreEqual("GET  HTTP/1.1", strarr[0]);
            Assert.AreEqual("Upgrade: websocket", strarr[2]);
            Assert.AreEqual("Connection: Upgrade", strarr[4]);
            Assert.IsTrue(strarr[6].Contains("Sec-WebSocket-Key:"));
            Assert.AreEqual("Sec-WebSocket-Version: 13", strarr[8]);

            Assert.IsNull(_clietErrorEventArgs);

            var retStr = "HTTP/1.1 101 Switching Protocols\r\n";

            retStr += "Upgrade: websocket\r\n";
            retStr += "Connection: Upgrade\r\n";
            retStr += $"Sec-WebSocket-Accept: 123\r\n\r\n";

            s.Write(Encoding.UTF8.GetBytes(retStr), _tcpServerNetworkDataEventArgs.LocalEndPoint, _tcpServerNetworkDataEventArgs.RemoteEndPoint);
            startTime = DateTime.Now;
            while (_clietErrorEventArgs == null)
            {
                var delta = DateTime.Now - startTime;
                if (delta.TotalSeconds > 5)
                {
                    Assert.Fail();
                }
                Thread.Sleep(1);
            }
            Assert.AreEqual("Wrong Accept", _clietErrorEventArgs.GetException().Message);

            c.Dispose();
            s.Dispose();
        }