Ejemplo n.º 1
0
        public static void Run_Test_Concurrency()
        {
            var localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5150);
            var maxClients    = 10;
            var clients       = new ClientConnection[maxClients];

            using (var server = new W.Net.TcpServer <W.Net.BlockingSocketClient>(15))
            {
                server.ClientConnected += (sc) =>
                {
                    //sc.UseCompression = useCompression;
                    sc.MessageReady += (c) =>
                    {
                        while (c.MessageCount > 0)
                        {
                            var msg  = c.GetNextMessage();
                            var echo = msg.AsString().ToUpper().AsBytes();
                            c.Send(echo);
                        }
                    };
                };
                //W.Threading.Thread.Sleep(10);
                server.Start(localEndPoint);

                //allocate and configure the clients
                for (int t = 0; t < maxClients; t++)
                {
                    clients[t] = new ClientConnection();
                    clients[t].Start(localEndPoint);
                }

                //display status and wait for exit
                Console.CursorVisible = false;
                while (!Console.KeyAvailable)
                {
                    Console.SetCursorPosition(0, 0);

                    for (int t = 0; t < maxClients; t++)
                    {
                        clients[t].Status.WriteFullConsoleLine();
                    }
                    string.Format("Press Any Key To Return").WriteFullConsoleLine();
                    W.Threading.Thread.Sleep(1);
                }
                Console.ReadKey(true);
                Console.CursorVisible = true;

                //cleanup
                for (int t = 0; t < maxClients; t++)
                {
                    clients[t].Stop();
                }
            }
        }
Ejemplo n.º 2
0
        public static void Run()
        {
            var useCompression = true;
            var localEndPoint  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5150);

            using (var server = new W.Net.TcpServer <W.Net.BlockingSocketClient>(1000))
            {
                server.ClientConnected += (sc) =>
                {
                    //sc.UseCompression = useCompression;
                    sc.MessageReady += (c) =>
                    {
                        while (c.MessageCount > 0)
                        {
                            var msg  = c.GetNextMessage();
                            var echo = msg.AsString().ToUpper().AsBytes();
                            c.Send(echo);
                        }
                    };
                };
                W.Threading.Thread.Sleep(10);
                server.Start(localEndPoint);

                var mre     = new ManualResetEventSlim(false);
                var proceed = new Lockable <bool>();
                int t       = 0;
                while (!Console.KeyAvailable)
                {
                    Console.SetCursorPosition(0, 0);
                    using (var client = new W.Net.BlockingSocketClient())
                    {
                        string request = string.Empty;
                        var    sw      = System.Diagnostics.Stopwatch.StartNew();
                        //client.UseCompression = useCompression;
                        client.MessageReady += (c) =>
                        {
                            "client.MessageReady".WriteFullConsoleLine();
                            while (c.MessageCount > 0)
                            {
                                var response = client.GetNextMessage();
                                if (response == null)
                                {
                                    System.Diagnostics.Debugger.Break();
                                }

                                var message = response.AsString();
                                string.Format("Received {0}", response.AsString()).WriteFullConsoleLine();

                                if (message != request.ToUpper())
                                {
                                    System.Diagnostics.Debugger.Break();
                                }
                            }
                            mre.Set();
                            proceed.Value = true;
                        };
                        //client.Disconnected += (c, ep, e) =>
                        //{
                        //    if (e != null)
                        //        System.Diagnostics.Debug.WriteLine("{0} Disconnected: {1}", ep, e);
                        //    else
                        //        System.Diagnostics.Debug.WriteLine("{0} Disconnected", ep);
                        //};
                        client.ConnectAsync(localEndPoint).Wait();
                        if (client.IsConnected)
                        {
                            request       = string.Format("Message: {0}", t);
                            proceed.Value = false;
                            mre.Reset();
                            client.Send(request.AsBytes());
                            while (!mre.Wait(1000))
                            {
                                //while (!proceed.Value)
                                W.Threading.Thread.Sleep(1);
                            }

                            client.Disconnect();
                            string.Format("Elapsed Connection Time (ms): " + sw.ElapsedMilliseconds).WriteFullConsoleLine();
                        }
                        client.Dispose();
                        string.Format("Total Elapsed Time (ms): " + sw.ElapsedMilliseconds).WriteFullConsoleLine();
                    }

                    t += 1;
                }
            }

            Console.WriteLine("Press Any Key To Return");
            Console.ReadKey(true);
        }