Ejemplo n.º 1
0
        public static void RunUdp(IPEndPoint RemoteEndPoint, SerializationProtocolType ProtocolType, Boolean UseOld)
        {
            if (!(ProtocolType == SerializationProtocolType.Binary || ProtocolType == SerializationProtocolType.Json))
            {
                Console.WriteLine("协议不能识别:" + ProtocolType.ToString());
            }
            IApplicationClient ac;
            IStreamedVirtualTransportClient vtc;
            var bt = new Rc4PacketClientTransformer();

            if (ProtocolType == SerializationProtocolType.Binary)
            {
                var a = new BinarySerializationClientAdapter();
                ac  = a.GetApplicationClient();
                vtc = new BinaryCountPacketClient(a, bt);
            }
            else if (ProtocolType == SerializationProtocolType.Json)
            {
                var a = new JsonSerializationClientAdapter();
                ac  = a.GetApplicationClient();
                vtc = new JsonLinePacketClient(a, bt);
            }
            else
            {
                throw new InvalidOperationException();
            }
            using (var bc = new UdpClient(RemoteEndPoint, vtc, QueueUserWorkItem))
            {
                bc.Connect();
                Console.WriteLine("输入login登录,输入secure启用安全连接。");

                var             Lockee   = new Object();
                Action <Action> DoHandle = a =>
                {
                    lock (Lockee)
                    {
                        a();
                    }
                };
                bc.ReceiveAsync(DoHandle, ex => Console.WriteLine(ex.Message));
                Action <SecureContext> SetSecureContext = c =>
                {
                    bt.SetSecureContext(c);
                    bc.SecureContext = c;
                };
                ReadLineAndSendLoop(ac, SetSecureContext, UseOld, Lockee);
            }
        }
Ejemplo n.º 2
0
        public static void RunHttp(String UrlPrefix, String ServiceVirtualPath, Boolean UseOld)
        {
            IApplicationClient ac;

            Http.IHttpVirtualTransportClient vtc;
            var a = new JsonSerializationClientAdapter();

            ac  = a.GetApplicationClient();
            vtc = new Http.JsonHttpPacketClient(a);
            using (var bc = new Http.HttpClient(UrlPrefix, ServiceVirtualPath, vtc))
            {
                Console.WriteLine("输入login登录。");

                var Lockee = new Object();
                Action <SecureContext> SetSecureContext = c =>
                {
                    throw new InvalidOperationException();
                };
                ReadLineAndSendLoop(ac, SetSecureContext, UseOld, Lockee);
            }
        }
Ejemplo n.º 3
0
        public static void TestHttpForNumUser(String UrlPrefix, String ServiceVirtualPath, int NumUser, String Title, Action <int, int, ClientContext, IApplicationClient, Action> Test, Action <int, int, ClientContext, IApplicationClient, Action> InitializeClientContext = null, Action <ClientContext[]> FinalCheck = null)
        {
            Console.Write("{0}: ", Title);
            Console.Out.Flush();

            var tl         = new List <Task>();
            var bcl        = new List <Http.HttpClient>();
            var ccl        = new List <ClientContext>();
            var vConnected = new LockedVariable <int>(0);
            var vCompleted = new LockedVariable <int>(0);
            var Check      = new AutoResetEvent(false);

            var vError = new LockedVariable <int>(0);

            for (int k = 0; k < NumUser; k += 1)
            {
                var n      = k;
                var Lockee = new Object();
                var a      = new JsonSerializationClientAdapter();
                var ac     = a.GetApplicationClient();
                var vtc    = new Http.JsonHttpPacketClient(a);
                var bc     = new Http.HttpClient(UrlPrefix, ServiceVirtualPath, vtc);
                var cc     = new ClientContext();
                ac.Error += e =>
                {
                    var m = e.Message;
                    Console.WriteLine(m);
                };
                Action Completed;
                if (Test == TestQuit)
                {
                    Completed = () =>
                    {
                        vCompleted.Update(i => i + 1);
                        Check.Set();
                    };
                }
                else
                {
                    Completed = () =>
                    {
                        ac.Quit(new QuitRequest {
                        }).ContinueWith(tt =>
                        {
                            vCompleted.Update(i => i + 1);
                            Check.Set();
                        });
                    };
                }
                if (InitializeClientContext != null)
                {
                    InitializeClientContext(NumUser, n, cc, ac, Completed);
                }
                try
                {
                    ac.ServerTime(new ServerTimeRequest {
                    }).ContinueWith(tt =>
                    {
                        vConnected.Update(i => i + 1);
                        Check.Set();
                    });
                }
                catch (Exception ex)
                {
                    Console.WriteLine(String.Format("{0}:{1}", n, ex.Message));
                    Completed();
                }
                var t = new Task
                        (
                    () =>
                {
                    try
                    {
                        Test(NumUser, n, cc, ac, Completed);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(String.Format("{0}:{1}", n, ex.Message));
                        Completed();
                    }
                }
                        );
                tl.Add(t);
                bcl.Add(bc);
                ccl.Add(cc);
            }

            while (vConnected.Check(i => i != NumUser))
            {
                Check.WaitOne();
            }

            var Time = Environment.TickCount;

            foreach (var t in tl)
            {
                t.Start();
            }

            while (vCompleted.Check(i => i != NumUser))
            {
                Check.WaitOne();
            }

            var TimeDiff = Environment.TickCount - Time;

            Task.WaitAll(tl.ToArray());
            foreach (var t in tl)
            {
                t.Dispose();
            }

            foreach (var bc in bcl)
            {
                bc.Dispose();
            }

            if (FinalCheck != null)
            {
                FinalCheck(ccl.ToArray());
            }

            var NumError = vError.Check(v => v);

            if (NumError > 0)
            {
                Console.WriteLine("{0} Errors", NumError);
            }
            Console.WriteLine("{0} Users, {1} ms", NumUser, TimeDiff);
        }
Ejemplo n.º 4
0
        public static void TestUdpForNumUser(IPEndPoint RemoteEndPoint, SerializationProtocolType ProtocolType, int NumUser, String Title, Action <int, int, ClientContext, IApplicationClient, Action> Test, Action <int, int, ClientContext, IApplicationClient, Action> InitializeClientContext = null, Action <ClientContext[]> FinalCheck = null)
        {
            Console.Write("{0}: ", Title);
            Console.Out.Flush();

            var tl         = new List <Task>();
            var bcl        = new List <UdpClient>();
            var ccl        = new List <ClientContext>();
            var tmrl       = new List <Timer>();
            var vConnected = new LockedVariable <int>(0);
            var vCompleted = new LockedVariable <int>(0);
            var Check      = new AutoResetEvent(false);

            var bAbondon = new LockedVariable <Boolean>(false);

            var vError = new LockedVariable <int>(0);

            for (int k = 0; k < NumUser; k += 1)
            {
                var n      = k;
                var Lockee = new Object();
                IApplicationClient ac;
                IStreamedVirtualTransportClient vtc;
                if (ProtocolType == SerializationProtocolType.Binary)
                {
                    var a = new BinarySerializationClientAdapter();
                    ac  = a.GetApplicationClient();
                    vtc = new BinaryCountPacketClient(a);
                }
                else if (ProtocolType == SerializationProtocolType.Json)
                {
                    var a = new JsonSerializationClientAdapter();
                    ac  = a.GetApplicationClient();
                    vtc = new JsonLinePacketClient(a);
                }
                else
                {
                    throw new InvalidOperationException();
                }
                var    bc         = new UdpClient(RemoteEndPoint, vtc, QueueUserWorkItem);
                var    cc         = new ClientContext();
                var    bCompleted = new LockedVariable <Boolean>(false);
                Action Completed;
                Action FaultedCompleted;
                if (Test == TestQuit)
                {
                    Completed = () =>
                    {
                        bCompleted.Update(b => true);
                        vCompleted.Update(i => i + 1);
                        Check.Set();
                    };
                    FaultedCompleted = Completed;
                }
                else
                {
                    Completed = () =>
                    {
                        ac.Quit(new QuitRequest {
                        }).ContinueWith(tt =>
                        {
                            bCompleted.Update(b => true);
                            vCompleted.Update(i => i + 1);
                            Check.Set();
                        });
                    };
                    FaultedCompleted = () =>
                    {
                        bCompleted.Update(b => true);
                        vCompleted.Update(i => i + 1);
                        Check.Set();
                    };
                }

                ac.Error += e =>
                {
                    var m = e.Message;
                    Console.WriteLine(m);
                };
                if (InitializeClientContext != null)
                {
                    InitializeClientContext(NumUser, n, cc, ac, Completed);
                }
                bc.Connect();
                Action <Exception> UnknownFaulted = ex =>
                {
                    int OldValue = 0;
                    vError.Update(v =>
                    {
                        OldValue = v;
                        return(v + 1);
                    });
                    if (OldValue <= 10)
                    {
                        Console.WriteLine(String.Format("{0}:{1}", n, ex.Message));
                    }
                    FaultedCompleted();
                };
                bc.ReceiveAsync
                (
                    a =>
                {
                    a();
                },
                    UnknownFaulted
                );
                ac.ServerTime(new ServerTimeRequest {
                }).ContinueWith(tt =>
                {
                    vConnected.Update(i => i + 1);
                    Check.Set();
                });
                var t = new Task
                        (
                    () =>
                {
                    Test(NumUser, n, cc, ac, Completed);
                }
                        );
                var tmr = new Timer
                          (
                    o =>
                {
                    if (!bAbondon.Check(b => b))
                    {
                        return;
                    }
                    if (bCompleted.Check(b => b))
                    {
                        return;
                    }
                    int OldValue = 0;
                    vError.Update(v =>
                    {
                        OldValue = v;
                        return(v + 1);
                    });
                    if (OldValue <= 10)
                    {
                        Console.WriteLine(String.Format("{0}:{1}", n, "Timedout"));
                    }
                    FaultedCompleted();
                },
                    null,
                    10000,
                    10000
                          );
                tl.Add(t);
                bcl.Add(bc);
                ccl.Add(cc);
                tmrl.Add(tmr);
            }

            while (vConnected.Check(i => i != NumUser))
            {
                Check.WaitOne();
            }

            var Time = Environment.TickCount;

            foreach (var t in tl)
            {
                t.Start();
            }

            while (vCompleted.Check(i => i != NumUser))
            {
                if (!Check.WaitOne(10000))
                {
                    if (vCompleted.Check(i => i > 0))
                    {
                        bAbondon.Update(b => true);
                        break;
                    }
                }
            }

            var NumMutualWaiting = NumUser - vCompleted.Check(i => i);

            while (vCompleted.Check(i => i != NumUser))
            {
                Check.WaitOne();
            }
            foreach (var tmr in tmrl)
            {
                tmr.Dispose();
            }

            var TimeDiff = Environment.TickCount - Time;

            Task.WaitAll(tl.ToArray());
            foreach (var t in tl)
            {
                t.Dispose();
            }

            foreach (var bc in bcl)
            {
                bc.Dispose();
            }

            if (FinalCheck != null)
            {
                FinalCheck(ccl.ToArray());
            }

            var NumError = vError.Check(v => v);

            if (NumError > 0)
            {
                Console.WriteLine("{0} Errors", NumError);
            }
            if (NumMutualWaiting > 0)
            {
                Console.WriteLine("{0} Users, {1} ms, {2} MutualWaiting", NumUser, TimeDiff, NumMutualWaiting);
            }
            else
            {
                Console.WriteLine("{0} Users, {1} ms", NumUser, TimeDiff);
            }
        }
Ejemplo n.º 5
0
        public static void TestTcpForNumUser(IPEndPoint RemoteEndPoint, SerializationProtocolType ProtocolType, int NumRequestPerUser, int NumUser, String Title, Action <int, int, ClientContext, IApplicationClient, Action> Test)
        {
            Console.Write("{0}: ", Title);
            Console.Out.Flush();

            var tll        = new Object();
            var tl         = new List <Task>();
            var bcl        = new List <TcpClient>();
            var ccl        = new List <ClientContext>();
            var vConnected = new LockedVariable <int>(0);
            var vCompleted = new LockedVariable <int>(0);
            var Check      = new AutoResetEvent(false);

            var vError = new LockedVariable <int>(0);

            for (int k = 0; k < NumUser; k += 1)
            {
                Action Completed = null;

                var n      = k;
                var Lockee = new Object();
                IApplicationClient ac;
                IStreamedVirtualTransportClient vtc;
                if (ProtocolType == SerializationProtocolType.Binary)
                {
                    var a = new BinarySerializationClientAdapter();
                    ac  = a.GetApplicationClient();
                    vtc = new BinaryCountPacketClient(a);
                }
                else if (ProtocolType == SerializationProtocolType.Json)
                {
                    var a = new JsonSerializationClientAdapter();
                    ac  = a.GetApplicationClient();
                    vtc = new JsonLinePacketClient(a);
                }
                else
                {
                    throw new InvalidOperationException();
                }
                var bc = new TcpClient(RemoteEndPoint, vtc, QueueUserWorkItem);
                var cc = new ClientContext();
                ac.Error += e =>
                {
                    var m = e.Message;
                    Console.WriteLine(m);
                };
                bc.Connect();
                Action <Exception> UnknownFaulted = ex =>
                {
                    int OldValue = 0;
                    vError.Update(v =>
                    {
                        OldValue = v;
                        return(v + 1);
                    });
                    if (OldValue <= 10)
                    {
                        Console.WriteLine(String.Format("{0}:{1}", n, ex.Message));
                    }
                    vCompleted.Update(i => i + 1);
                    Check.Set();
                };
                bc.ReceiveAsync
                (
                    a =>
                {
                    a();
                },
                    UnknownFaulted
                );
                ac.ServerTime(new ServerTimeRequest {
                }).ContinueWith(tt =>
                {
                    vConnected.Update(i => i + 1);
                    Check.Set();
                });
                Action f = () =>
                {
                    Test(NumUser, n, cc, ac, Completed);
                };
                var t = new Task(f);
                lock (tll)
                {
                    tl.Add(t);
                }
                bcl.Add(bc);
                ccl.Add(cc);

                int RequestCount = NumRequestPerUser;
                Completed = () =>
                {
                    if (RequestCount > 0)
                    {
                        RequestCount -= 1;
                        var tt = new Task(f);
                        lock (tll)
                        {
                            tl.Add(t);
                        }
                        tt.Start();
                        return;
                    }
                    vCompleted.Update(i => i + 1);
                    Check.Set();
                };
            }

            while (vConnected.Check(i => i != NumUser))
            {
                Check.WaitOne();
            }

            var Time = Environment.TickCount;

            lock (tll)
            {
                foreach (var t in tl)
                {
                    t.Start();
                }
            }

            while (vCompleted.Check(i => i != NumUser))
            {
                Check.WaitOne();
            }

            var TimeDiff = Environment.TickCount - Time;

            Task.WaitAll(tl.ToArray());
            foreach (var t in tl)
            {
                t.Dispose();
            }

            foreach (var bc in bcl)
            {
                bc.Dispose();
            }

            var NumError = vError.Check(v => v);

            if (NumError > 0)
            {
                Console.WriteLine("{0} Errors", NumError);
            }
            Console.WriteLine("{0} Users, {1} Request/User, {2} ms", NumUser, NumRequestPerUser, TimeDiff);
        }
Ejemplo n.º 6
0
        public static void TestHttpForNumUser(String UrlPrefix, String ServiceVirtualPath, int NumRequestPerUser, int NumUser, String Title, Action <int, int, ClientContext, IApplicationClient, Action> Test)
        {
            Console.Write("{0}: ", Title);
            Console.Out.Flush();

            var tll        = new Object();
            var tl         = new List <Task>();
            var bcl        = new List <Http.HttpClient>();
            var ccl        = new List <ClientContext>();
            var vConnected = new LockedVariable <int>(0);
            var vCompleted = new LockedVariable <int>(0);
            var Check      = new AutoResetEvent(false);

            var vError = new LockedVariable <int>(0);

            for (int k = 0; k < NumUser; k += 1)
            {
                Action Completed = null;

                var n      = k;
                var Lockee = new Object();
                var a      = new JsonSerializationClientAdapter();
                var ac     = a.GetApplicationClient();
                var vtc    = new Http.JsonHttpPacketClient(a);
                var bc     = new Http.HttpClient(UrlPrefix, ServiceVirtualPath, vtc);
                var cc     = new ClientContext();
                ac.Error += e =>
                {
                    var m = e.Message;
                    Console.WriteLine(m);
                };
                Action <Exception> HandleError = ex =>
                {
                    int OldValue = 0;
                    vError.Update(v =>
                    {
                        OldValue = v;
                        return(v + 1);
                    });
                    if (OldValue <= 10)
                    {
                        Console.WriteLine(String.Format("{0}:{1}", n, ex.Message));
                    }
                    vCompleted.Update(i => i + 1);
                    Check.Set();
                };
                try
                {
                    ac.ServerTime(new ServerTimeRequest {
                    }).ContinueWith(tt =>
                    {
                        vConnected.Update(i => i + 1);
                        Check.Set();
                    });
                }
                catch (Exception ex)
                {
                    HandleError(ex);
                }
                Action f = () =>
                {
                    try
                    {
                        Test(NumUser, n, cc, ac, Completed);
                    }
                    catch (Exception ex)
                    {
                        HandleError(ex);
                    }
                };
                var t = new Task(f);
                lock (tll)
                {
                    tl.Add(t);
                }
                bcl.Add(bc);
                ccl.Add(cc);

                int RequestCount = NumRequestPerUser;
                Completed = () =>
                {
                    if (RequestCount > 0)
                    {
                        RequestCount -= 1;
                        var tt = new Task(f);
                        lock (tll)
                        {
                            tl.Add(t);
                        }
                        tt.Start();
                        return;
                    }
                    ac.Quit(new QuitRequest {
                    }).ContinueWith(tt =>
                    {
                        vCompleted.Update(i => i + 1);
                        Check.Set();
                    });
                };
            }

            while (vConnected.Check(i => i != NumUser))
            {
                Check.WaitOne();
            }

            var Time = Environment.TickCount;

            lock (tll)
            {
                foreach (var t in tl)
                {
                    t.Start();
                }
            }

            while (vCompleted.Check(i => i != NumUser))
            {
                Check.WaitOne();
            }

            var TimeDiff = Environment.TickCount - Time;

            Task.WaitAll(tl.ToArray());
            foreach (var t in tl)
            {
                t.Dispose();
            }

            foreach (var bc in bcl)
            {
                bc.Dispose();
            }

            var NumError = vError.Check(v => v);

            if (NumError > 0)
            {
                Console.WriteLine("{0} Errors", NumError);
            }
            Console.WriteLine("{0} Users, {1} Request/User, {2} ms", NumUser, NumRequestPerUser, TimeDiff);
        }