Created by a ThreadedSocketReactor to handle a client connection. Each ClientHandlerThread has a SocketReader which reads from the socket.
Inheritance: 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();
 }
Exemple #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;
         }
     }
 }
 /// <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();
 }
Exemple #5
0
        public SocketReader(TcpClient tcpClient, SocketSettings settings, ClientHandlerThread responder)
        {
            tcpClient_ = tcpClient;
            responder_ = responder;
            stream_    = Transport.StreamFactory.CreateServerStream(tcpClient, settings, responder.GetLog());

            socketSettings_ = settings;
        }
 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);
 }
Exemple #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());
 }
Exemple #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();
        }
Exemple #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;
         }
     }
 }
Exemple #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;
         }
     }
 }
Exemple #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();
        }
 public ExitedEventArgs(ClientHandlerThread clientHandlerThread)
 {
     this.ClientHandlerThread = clientHandlerThread;
 }
 public SocketReader(TcpClient tcpClient, ClientHandlerThread responder)
     : this(tcpClient, new SocketSettings(), responder)
 {
 }
 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;
         }
     }
 }
Exemple #15
0
 public SocketReader(TcpClient tcpClient, ClientHandlerThread responder)
 {
     tcpClient_ = tcpClient;
     responder_ = responder;
 }
Exemple #16
0
 public SocketReader(TcpClient tcpClient, SocketSettings settings, ClientHandlerThread responder)
 {
     tcpClient_ = tcpClient;
     responder_ = responder;
     stream_ = Transport.StreamFactory.CreateServerStream(tcpClient, settings, responder.GetLog());
 }
Exemple #17
0
 public SocketReader(TcpClient tcpClient, ClientHandlerThread responder)
     : this(tcpClient, new SocketSettings(), responder)
 {
 }
Exemple #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();
        }
 public SocketReader(TcpClient tcpClient, SocketSettings settings, ClientHandlerThread responder)
     : this(tcpClient, settings, responder, null)
 {
 }
Exemple #20
0
 public SocketReader(TcpClient tcpClient, ClientHandlerThread responder)
 {
     tcpClient_ = tcpClient;
     responder_ = responder;
 }
Exemple #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();
        }