Ejemplo n.º 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.");
            }
        }