Created by a ThreadedSocketReactor to handle a client connection. Each ClientHandlerThread has a SocketReader which reads from the socket.
Наследование: IResponder, IDisposable
 /// <summary>
 /// TODO apply networking options, e.g. NO DELAY, LINGER, etc.
 /// </summary>
 public void Run()
 {
     tcpListener_.Start();
     while (State.RUNNING == ReactorState)
     {
         try
         {
             TcpClient client = tcpListener_.AcceptTcpClient();
             ApplySocketOptions(client, socketSettings_);
             ClientHandlerThread t = new ClientHandlerThread(client, nextClientId_++);
             lock (sync_)
             {
                 clientThreads_.AddLast(t);
             }
             // FIXME set the client thread's exception handler here
             t.Log("connected");
             t.Start();
         }
         catch (System.Exception e)
         {
             if (State.RUNNING == ReactorState)
             {
                 this.Log("Error accepting connection: " + e.Message);
             }
         }
     }
     ShutdownClientHandlerThreads();
 }
Пример #2
0
 internal SocketReader(TcpClient tcpClient, SocketSettings settings, ClientHandlerThread responder, AcceptorSocketDescriptor acceptorDescriptor)
 {
     tcpClient_          = tcpClient;
     responder_          = responder;
     acceptorDescriptor_ = acceptorDescriptor;
     stream_             = Transport.StreamFactory.CreateServerStream(tcpClient, settings, responder.GetLog());
 }
 private void ShutdownClientHandlerThreads()
 {
     lock (sync_)
     {
         if (State.SHUTDOWN_COMPLETE != state_)
         {
             this.Log("shutting down...");
             while (clientThreads_.Count > 0)
             {
                 ClientHandlerThread t = clientThreads_.First.Value;
                 clientThreads_.RemoveFirst();
                 t.Shutdown("reactor is shutting down");
                 try
                 {
                     t.Join();
                 }
                 catch (System.Exception e)
                 {
                     t.Log("Error shutting down: " + e.Message);
                 }
             }
             state_ = State.SHUTDOWN_COMPLETE;
         }
     }
 }
Пример #4
0
 /// <summary>
 /// TODO apply networking options, e.g. NO DELAY, LINGER, etc.
 /// </summary>
 public void Run()
 {
     tcpListener_.Start();
     while (State.RUNNING == ReactorState)
     {
         try
         {
             TcpClient client = tcpListener_.AcceptTcpClient();
             ApplySocketOptions(client);
             ClientHandlerThread t = new ClientHandlerThread(client, nextClientId_++);
             lock (sync_)
             {
                 clientThreads_.AddLast(t);
             }
             // FIXME set the client thread's exception handler here
             t.Log("connected");
             t.Start();
         }
         catch (System.Exception e)
         {
             if (State.RUNNING == ReactorState)
                 this.Log("Error accepting connection: " + e.Message);
         }
     }
     ShutdownClientHandlerThreads();
 }
Пример #5
0
        public SocketReader(TcpClient tcpClient, SocketSettings settings, ClientHandlerThread responder)
        {
            tcpClient_ = tcpClient;
            responder_ = responder;
            stream_    = Transport.StreamFactory.CreateServerStream(tcpClient, settings, responder.GetLog());

            socketSettings_ = settings;
        }
Пример #6
0
 public SocketReader(TcpClient tcpClient, SocketSettings settings, ClientHandlerThread responder, Encoding encoding)
 {
     tcpClient_       = tcpClient;
     responder_       = responder;
     stream_          = Transport.StreamFactory.CreateServerStream(tcpClient, settings, responder.GetLog());
     _messageEncoding = encoding;
     parser_          = new Parser(encoding);
 }
Пример #7
0
 public SocketReader(TcpClient tcpClient, SocketSettings settings, ClientHandlerThread responder)
 {
     parser_    = new Parser(settings.Encoding);
     encoding_  = settings.Encoding;
     tcpClient_ = tcpClient;
     responder_ = responder;
     stream_    = Transport.StreamFactory.CreateServerStream(tcpClient, settings, responder.GetLog());
 }
Пример #8
0
        /// <summary>
        /// TODO apply networking options, e.g. NO DELAY, LINGER, etc.
        /// </summary>
        public void Run()
        {
            lock (sync_)
            {
                if (State.SHUTDOWN_REQUESTED != state_)
                {
                    try
                    {
                        tcpListener_.Start();
                    }
                    catch (Exception e)
                    {
                        this.Log("Error starting listener: " + e.Message);
                        throw;
                    }
                }
            }

            while (State.RUNNING == ReactorState)
            {
                try
                {
                    TcpClient client = tcpListener_.AcceptTcpClient();
                    if (State.RUNNING == ReactorState)
                    {
                        ApplySocketOptions(client, socketSettings_);
                        ClientHandlerThread t =
                            new ClientHandlerThread(client, nextClientId_++, sessionDict_, socketSettings_, acceptorDescriptor_);
                        t.Exited += OnClientHandlerThreadExited;
                        lock (sync_)
                        {
                            clientThreads_.Add(t.Id, t);
                        }

                        // FIXME set the client thread's exception handler here
                        t.Log("connected");
                        t.Start();
                    }
                    else
                    {
                        client.Dispose();
                    }
                }
                catch (System.Exception e)
                {
                    if (State.RUNNING == ReactorState)
                    {
                        this.Log("Error accepting connection: " + e.Message);
                    }
                }
            }
            tcpListener_.Server.Close();
            tcpListener_.Stop();
            ShutdownClientHandlerThreads();
        }
Пример #9
0
 internal void OnClientHandlerThreadExited(object sender, ClientHandlerThread.ExitedEventArgs e)
 {
     lock (GdaxPrototyping.Common.Core.Threading.TThreadingHelpers.MainLockable)
     {
         ClientHandlerThread t = null;
         if (clientThreads_.TryGetValue(e.ClientHandlerThread.Id, out t))
         {
             clientThreads_.Remove(t.Id);
             t.Dispose();
             t = null;
         }
     }
 }
Пример #10
0
 internal void OnClientHandlerThreadExited(object sender, ClientHandlerThread.ExitedEventArgs e)
 {
     lock (sync_)
     {
         ClientHandlerThread t = null;
         if (clientThreads_.TryGetValue(e.ClientHandlerThread.Id, out t))
         {
             clientThreads_.Remove(t.Id);
             t.Dispose();
             t = null;
         }
     }
 }
Пример #11
0
        /// <summary>
        /// TODO apply networking options, e.g. NO DELAY, LINGER, etc.
        /// </summary>
        public void Run()
        {
            lock (GdaxPrototyping.Common.Core.Threading.TThreadingHelpers.MainLockable)
            {
                if (State.SHUTDOWN_REQUESTED != state_)
                {
                    tcpListener_.Start();
                }
            }

            while (State.RUNNING == ReactorState)
            {
                try
                {
                    TcpClient client = tcpListener_.AcceptTcpClient();
                    ApplySocketOptions(client, socketSettings_);
                    ClientHandlerThread t = new ClientHandlerThread(client, nextClientId_++, sessionDict_, socketSettings_);
                    t.Exited += OnClientHandlerThreadExited;
                    lock (GdaxPrototyping.Common.Core.Threading.TThreadingHelpers.MainLockable)
                    {
                        clientThreads_.Add(t.Id, t);
                    }
                    // FIXME set the client thread's exception handler here
                    t.Log("connected");
                    t.Start();
                }
                catch (System.Exception e)
                {
                    if (State.RUNNING == ReactorState)
                    {
                        this.Log("Error accepting connection: " + e.Message);
                    }
                }
            }
            ShutdownClientHandlerThreads();
        }
Пример #12
0
 public ExitedEventArgs(ClientHandlerThread clientHandlerThread)
 {
     this.ClientHandlerThread = clientHandlerThread;
 }
Пример #13
0
 public SocketReader(TcpClient tcpClient, ClientHandlerThread responder)
     : this(tcpClient, new SocketSettings(), responder)
 {
 }
Пример #14
0
 internal void OnClientHandlerThreadExited(object sender, ClientHandlerThread.ExitedEventArgs e)
 {
     lock(sync_)
     {
         ClientHandlerThread t = null;
         if(clientThreads_.TryGetValue(e.ClientHandlerThread.Id, out t))
         {
             clientThreads_.Remove(t.Id);
             t.Dispose();
             t = null;
         }
     }
 }
Пример #15
0
 public SocketReader(TcpClient tcpClient, ClientHandlerThread responder)
 {
     tcpClient_ = tcpClient;
     responder_ = responder;
 }
Пример #16
0
 public SocketReader(TcpClient tcpClient, SocketSettings settings, ClientHandlerThread responder)
 {
     tcpClient_ = tcpClient;
     responder_ = responder;
     stream_ = Transport.StreamFactory.CreateServerStream(tcpClient, settings, responder.GetLog());
 }
Пример #17
0
 public SocketReader(TcpClient tcpClient, ClientHandlerThread responder)
     : this(tcpClient, new SocketSettings(), responder)
 {
 }
Пример #18
0
        /// <summary>
        /// TODO apply networking options, e.g. NO DELAY, LINGER, etc.
        /// </summary>
        public void Run()
        {
            lock (sync_)
            {
                if (State.SHUTDOWN_REQUESTED != state_)
                {
                    try
                    {
                        tcpListener_.Start();
                    }
                    catch (Exception e)
                    {
                        this.Log("Error starting listener: " + e.Message);
                        throw;
                    }
                }
            }

            while (State.RUNNING == ReactorState)
            {
                try
                {
                    TcpClient client = tcpListener_.AcceptTcpClient();
                    if (socketSettings_.SocketCountPerHost.HasValue)
                    {
                        var address = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
                        lock (sync_)
                        {
                            if (clientThreads_.Values.Count(c => c.SourceAddress == address) >= socketSettings_.SocketCountPerHost)
                            {
                                client.Dispose();
                                Log($"Limit of connections per host was reached for '{address}'");
                                continue;
                            }
                        }
                    }


                    if (State.RUNNING == ReactorState)
                    {
                        ApplySocketOptions(client, socketSettings_);
                        ClientHandlerThread t =
                            new ClientHandlerThread(client, nextClientId_++, sessionDict_, socketSettings_, acceptorDescriptor_);
                        t.Exited += OnClientHandlerThreadExited;
                        lock (sync_)
                        {
                            clientThreads_.Add(t.Id, t);
                        }

                        // FIXME set the client thread's exception handler here
                        t.Log("connected");
                        t.Start();
                    }
                    else
                    {
                        client.Dispose();
                    }
                }
                catch (System.Exception e)
                {
                    if (State.RUNNING == ReactorState)
                    {
                        this.Log("Error accepting connection: " + e.Message);
                    }
                }
            }
            tcpListener_.Server.Close();
            tcpListener_.Stop();
            ShutdownClientHandlerThreads();
        }
Пример #19
0
 public SocketReader(TcpClient tcpClient, SocketSettings settings, ClientHandlerThread responder)
     : this(tcpClient, settings, responder, null)
 {
 }
Пример #20
0
 public SocketReader(TcpClient tcpClient, ClientHandlerThread responder)
 {
     tcpClient_ = tcpClient;
     responder_ = responder;
 }
Пример #21
0
 public ExitedEventArgs(ClientHandlerThread clientHandlerThread)
 {
     this.ClientHandlerThread = clientHandlerThread;
 }
        /// <summary>
        /// TODO apply networking options, e.g. NO DELAY, LINGER, etc.
        /// </summary>
        public void Run()
        {
            lock (sync_)
            {
                if (State.SHUTDOWN_REQUESTED != state_)
                    tcpListener_.Start();
            }

            while (State.RUNNING == ReactorState)
            {
                try
                {
                    TcpClient client = tcpListener_.AcceptTcpClient();
                    ApplySocketOptions(client, socketSettings_);
                    ClientHandlerThread t = new ClientHandlerThread(client, nextClientId_++, sessionDict_, socketSettings_);
                    t.Exited += OnClientHandlerThreadExited;
                    lock (sync_)
                    {
                        clientThreads_.Add(t.Id, t);
                    }
                    // FIXME set the client thread's exception handler here
                    t.Log("connected");
                    t.Start();
                }
                catch (System.Exception e)
                {
                    if (State.RUNNING == ReactorState)
                        this.Log("Error accepting connection: " + e.Message);
                }
            }
            ShutdownClientHandlerThreads();
        }