Example #1
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 #2
0
        public void DataTraceTest()
        {
            var server = new TcpServer();

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

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

            var client = new TcpClient();

            client.DataReceivedEvent     += Client_DataReceivedEvent;
            client.ReceiveDataTraceEvent += Client_ReceiveDataTraceEvent;
            client.SendDataTraceEvent    += Client_SendDataTraceEvent;
            client.ConnectedEvent        += Client_ConnectedEvent;
            client.DisconnectedEvent     += Client_DisconnectedEvent;

            ClientConnectedEventArgs = null;
            client.Start(ipEp1.EndPoint);
            var waitStart = DateTime.Now;

            while (ClientConnectedEventArgs == null)
            {
                var delta = DateTime.Now - waitStart;
                if (delta.TotalSeconds > 5)
                {
                    Assert.Fail("ClientConnectedEventEventArgs Timeout");
                }
            }

            client.DataTrace = false;
            server.DataTrace = false;

            for (int i = 1; i < 10; i++)
            {
                var data = BitConverter.GetBytes(i);
                ServerDataReceivedEvent.Clear();
                ClientSendDataTraceEvent.Clear();
                Assert.AreEqual(0, ServerDataReceivedEvent.Count);
                Assert.AreEqual(0, ClientSendDataTraceEvent.Count);
                client.SendData(data);
                Thread.Sleep(300);
                Assert.AreEqual(1, ServerDataReceivedEvent.Count);
                Assert.AreEqual(data.Length, ServerDataReceivedEvent[0].Data.Length);
                Assert.AreEqual(0, ClientSendDataTraceEvent.Count);
                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();
                ClientReceiveDataTraceEvent.Clear();
                Assert.AreEqual(0, ClientDataReceivedEvent.Count);
                Assert.AreEqual(0, ClientReceiveDataTraceEvent.Count);
                server.Write(data, client.ExchangeInterface.RemoteEndPoint, client.ExchangeInterface.LocalEndPoint);
                Thread.Sleep(300);
                Assert.AreEqual(1, ClientDataReceivedEvent.Count);
                Assert.AreEqual(data.Length, ClientDataReceivedEvent[0].Data.Length);
                Assert.AreEqual(0, ClientReceiveDataTraceEvent.Count);
                for (int j = 0; j < data.Length; j++)
                {
                    Assert.AreEqual(data[j], ClientDataReceivedEvent[0].Data[j]);
                    Assert.AreEqual(ipEp1.EndPoint, ClientDataReceivedEvent[0].RemoteEndPoint);
                }
            }


            client.DataTrace = true;
            server.DataTrace = true;

            for (int i = 1; i < 10; i++)
            {
                var data = BitConverter.GetBytes(i);
                ServerDataReceivedEvent.Clear();
                ClientSendDataTraceEvent.Clear();
                Assert.AreEqual(0, ServerDataReceivedEvent.Count);
                Assert.AreEqual(0, ClientSendDataTraceEvent.Count);
                client.SendData(data);
                Thread.Sleep(300);
                Assert.AreEqual(1, ServerDataReceivedEvent.Count);
                Assert.AreEqual(data.Length, ServerDataReceivedEvent[0].Data.Length);
                Assert.AreEqual(1, ClientSendDataTraceEvent.Count);
                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);

                    Assert.AreEqual(data[j], ClientSendDataTraceEvent[0].Data[j]);
                }


                Assert.AreEqual(ipEp1.EndPoint, ClientSendDataTraceEvent[0].RemoteEndPoint);
            }

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

                    Assert.AreEqual(data[j], ClientReceiveDataTraceEvent[0].Data[j]);
                }

                Assert.AreEqual(ipEp1.EndPoint, ClientReceiveDataTraceEvent[0].RemoteEndPoint);
            }

            server.Dispose();
        }
Example #3
0
        void TestPerfomance(TcpServer server, int perfomance, string serverName)
        {
            using (X509Certificate clientCert = MakeCert(serverName))
            {
                var client = new TcpClient();
                client.ErrorEvent        += Client_ErrorEvent;
                client.DataReceivedEvent += Client_DataReceivedEvent;
                client.Start(server.Endpoints[0].EndPoint, serverName, clientCert, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);
                Thread.Sleep(500);
                Assert.IsTrue(client.IsConnected);

                var perfomanceList = new List <int>();
                //Client_ > Server Perfomance Test
                for (int j = 0; j < 10; j++)
                {
                    var startTime = DateTime.Now;
                    var now       = DateTime.Now;
                    int value     = 1;

                    if (ClientDataReceivedEvent.ContainsKey(client.ExchangeInterface.LocalEndPoint.ToString()))
                    {
                        ClientDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()].Clear();
                    }
                    if (ServerDataReceivedEvent.ContainsKey(client.ExchangeInterface.LocalEndPoint.ToString()))
                    {
                        ServerDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()].Clear();
                    }
                    var index = 1;

                    while ((now - startTime).TotalSeconds < 1)
                    {
                        Assert.IsTrue(client.SendData(BitConverter.GetBytes(value)));
                        while (!ServerDataReceivedEvent.ContainsKey(client.ExchangeInterface.LocalEndPoint.ToString()))
                        {
                            Assert.IsTrue(client.IsConnected);
                        }
                        while (ServerDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()].Count < 1)
                        {
                            Assert.IsTrue(client.IsConnected);
                        }
                        value++;
                        now = DateTime.Now;
                    }

                    //for (int i = 0; i < value; i ++)
                    //{
                    //    var val = BitConverter.GetBytes(index);
                    //    Assert.AreEqual(val[0], ServerDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()][i].Data[0]);
                    //    Assert.AreEqual(val[1], ServerDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()][i].Data[1]);
                    //    Assert.AreEqual(val[2], ServerDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()][i].Data[2]);
                    //    Assert.AreEqual(val[3], ServerDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()][i].Data[3]);
                    //    index++;
                    //}
                    perfomanceList.Add(value);
                }

                var perfmance = perfomanceList[0];
                //lock (_outputLockObject)
                //{
                //    System.Diagnostics.Trace.WriteLine(
                //        $"Client {client.ExchangeInterface.LocalEndPoint} -> Server Packets per second {perfomanceList[0]}");
                //}
                for (int i = 1; i < perfomanceList.Count; i++)
                {
                    //lock (_outputLockObject)
                    //{
                    //    System.Diagnostics.Trace.WriteLine(
                    //        $"Client {client.ExchangeInterface.LocalEndPoint} -> Server Packets per second {perfomanceList[i]}");
                    //}
                    perfmance = (perfmance + perfomanceList[i]) / 2;
                }
                lock (_outputLockObject)
                {
                    System.Diagnostics.Trace.WriteLine($"Client {client.ExchangeInterface.LocalEndPoint} -> Averange Server Packets per second {perfmance}");
                }
                Assert.IsTrue(perfmance > 500);


                perfomanceList.Clear();
                //Server>Client Perfomance Test
                for (int j = 0; j < 10; j++)
                {
                    var startTime = DateTime.Now;
                    var now       = DateTime.Now;
                    int value     = 1;
                    if (ClientDataReceivedEvent.ContainsKey(client.ExchangeInterface.LocalEndPoint.ToString()))
                    {
                        ClientDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()].Clear();
                    }
                    if (ServerDataReceivedEvent.ContainsKey(client.ExchangeInterface.LocalEndPoint.ToString()))
                    {
                        ServerDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()].Clear();
                    }
                    var index = 1;

                    while ((now - startTime).TotalSeconds < 1)
                    {
                        Assert.IsTrue(server.Write(BitConverter.GetBytes(value), client.ExchangeInterface.RemoteEndPoint, client.ExchangeInterface.LocalEndPoint));
                        while (!ClientDataReceivedEvent.ContainsKey(client.ExchangeInterface.LocalEndPoint.ToString()))
                        {
                            Assert.IsTrue(client.IsConnected);
                        }
                        while (ClientDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()].Count < 1)
                        {
                            Assert.IsTrue(client.IsConnected);
                        }
                        value++;
                        now = DateTime.Now;
                    }

                    //for (int i = 0; i < value; i += sizeof(int))
                    //{
                    //    var val = BitConverter.GetBytes(index);
                    //    Assert.AreEqual(val[0], ClientDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()][i + 0].Data[0]);
                    //    Assert.AreEqual(val[1], ClientDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()][i + 1].Data[0]);
                    //    Assert.AreEqual(val[2], ClientDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()][i + 2].Data[0]);
                    //    Assert.AreEqual(val[3], ClientDataReceivedEvent[client.ExchangeInterface.LocalEndPoint.ToString()][i + 3].Data[0]);
                    //    index++;
                    //}
                    perfomanceList.Add(value);
                }

                perfmance = perfomanceList[0];
                //lock (_outputLockObject)
                //{
                //    System.Diagnostics.Trace.WriteLine($"Server -> Client {client.ExchangeInterface.LocalEndPoint} Packets per second {perfomanceList[0]}");
                //}
                for (int i = 1; i < perfomanceList.Count; i++)
                {
                    //lock (_outputLockObject)
                    //{
                    //    System.Diagnostics.Trace.WriteLine($"Server -> Client {client.ExchangeInterface.LocalEndPoint} Packets per second {perfomanceList[i]}");
                    //}
                    perfmance = (perfmance + perfomanceList[i]) / 2;
                }

                lock (_outputLockObject)
                {
                    System.Diagnostics.Trace.WriteLine($"Server -> Client {client.ExchangeInterface.LocalEndPoint} Averange Packets per second {perfmance}");
                }
                Assert.IsTrue(perfmance > perfomance);

                client.Dispose();
            }
        }
Example #4
0
        public void SendReceiveTest()
        {
            var serverName = "TestServer";

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

                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, serverCert, false, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);

                X509Certificate clientCert = MakeCert(serverName);
                var             client     = new TcpClient();
                client.DataReceivedEvent += Client_DataReceivedEvent;
                client.Start(ipEp1.EndPoint, serverName, clientCert, SslProtocols.None, false, EncryptionPolicy.RequireEncryption);
                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);

                    var waitStart = DateTime.Now;
                    while (ServerDataReceivedEvent.Count == 0)
                    {
                        var delta = DateTime.Now - waitStart;
                        if (delta.TotalMilliseconds > 100)
                        {
                            Assert.Fail("Data not received");
                        }
                    }

                    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);

                    var waitStart = DateTime.Now;
                    while (ClientDataReceivedEvent.Count == 0)
                    {
                        var delta = DateTime.Now - waitStart;
                        if (delta.TotalMilliseconds > 100)
                        {
                            Assert.Fail("Data not received");
                        }
                    }

                    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();
                client.Dispose();
                clientCert.Dispose();
            }
        }