Пример #1
0
        private async Task NewConnectionEntryPointAsync(Socket clientSocket, long handlerTaskId, CancellationToken cancellationToken)
        {
            // Start connection servicer on a thread pool thread so the listener thread can get right back to accepting connections.
            // Run any blocking operations asynchronously so that the thread pool thread is yielded.
            // The listener thread isn't going to wait around for this task to complete, so this function
            // is responsible for logging its own errors and not letting exceptions escape, except OperationCanceledException if canceled.
            // This function is responsible for removing its task from m_connectionServicerTasks when complete.

            Logging.Log.Debug("Servicer task entry.");
            try
            {
                using (clientSocket)
                {
                    // TODO: Make these configurable
                    TimeSpan           readTimeout  = TimeSpan.FromSeconds(3);
                    TimeSpan           writeTimeout = TimeSpan.FromSeconds(3);
                    ConnectionServicer servicer     = new ConnectionServicer(clientSocket, m_state, readTimeout, writeTimeout, cancellationToken);
                    await servicer.ServiceConnectionAsync().ConfigureAwait(false);
                }
            }
            catch (Exception ex) when(!(ex is OperationCanceledException))
            {
                Logging.Log.ErrorFormat("Error servicing connection: {0}", ex, ex.Message);
            }
            finally
            {
                lock (m_taskListLock)
                {
                    m_connectionServicerTasks[handlerTaskId].CancellationTokenSource.Dispose();
                    m_connectionServicerTasks.Remove(handlerTaskId);
                }

                Logging.Log.Debug("Servicer task exit.");
            }
        }
Пример #2
0
        private void ConnectionEntryPoint(object clientObj)
        {
            Logging.Log.Debug("Servicer thread entry.");
            using (TcpClient client = (TcpClient)clientObj)
            {
                try
                {
                    const int readTimeout = 3000;
                    const int writeTimeout = 3000;
                    client.ReceiveTimeout = readTimeout;
                    client.SendTimeout = writeTimeout;
                    ConnectionServicer servicer = new ConnectionServicer(client, m_state);
                    servicer.ServiceConnection();
                }
                catch (Exception ex)
                {
                    Logging.Log.ErrorFormat("Error servicing connection: {0}", ex, ex.Message);
                }
            }

            lock(m_syncHandle)
            {
                m_runningTasks.Remove(Task.CurrentId.Value);
            }
        }