Esempio n. 1
0
        public static void TestTcpForNumUser(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 <TcpClient>();
            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 TcpClient(RemoteEndPoint, vtc, QueueUserWorkItem);
                var    cc         = new ClientContext();
                var    bCompleted = new LockedVariable <Boolean>(false);
                Action Completed  = () =>
                {
                    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));
                    }
                    Completed();
                };
                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;
                    }
                    ac.ServerTime(new ServerTimeRequest {
                    });
                },
                    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);
            }
        }
Esempio n. 2
0
 public void Dispose() => _tcpClient.Dispose();
Esempio n. 3
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);
        }
        private void Connect(string name, string ip, int port)
        {
            header = 0;
            client = new TcpClient();
            // Client connects to the server on the specified ip and port.
            try
            {
                client.Connect(ip, port);
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine(e.Message + "\n" + e.StackTrace);
                MessageBoxResult mbr = MessageBox.Show("IP was empty.");
                return;

                throw;
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message + "\n" + e.StackTrace);
                MessageBoxResult mbr = MessageBox.Show("Port isn't within the allowed range.");
                return;

                throw;
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.Message + "\n" + e.StackTrace);
                MessageBoxResult mbr = MessageBox.Show("Couldn't access the socket.");
                return;

                throw;
            }
            catch (ObjectDisposedException e)
            {
                Console.WriteLine(e.Message + "\n" + e.StackTrace);
                MessageBoxResult mbr = MessageBox.Show("TCP Client is closed.");
                return;

                throw;
            }

            this.Dispatcher.Invoke(() =>
            {
                btn_connect.IsEnabled    = false;
                btn_disconnect.IsEnabled = true;
            });
            // Get client stream for reading and writing.
            // Using is a try finally, if an exception occurs it disposes of the stream.
            using (NetworkStream stream = client.GetStream())
            {
                Send(stream, name, header);
                while (true)
                {
                    if (client.Connected)
                    {
                        Receive(stream);
                    }
                    else
                    {
                        stream.Dispose();
                        client.Close();
                        client.Dispose();
                        Thread.CurrentThread.Join();
                    }
                }
            };
        }
Esempio n. 5
0
 public void Dispose()
 {
     _client?.Dispose();
 }
Esempio n. 6
0
 public void Close()
 {
     comm.GetStream().Close();
     comm.Close();
     comm.Dispose();
 }