Пример #1
0
 protected void RemoveConnection(HttpConnection connection)
 {
     _ = connection ?? throw new ArgumentNullException(nameof(connection));
     if (KeepAliveConnections.Contains(connection))
     {
         KeepAliveConnections.Remove(connection);
     }
     AllConnections.Remove(connection);
     connection.NetworkClient?.Close();
 }
Пример #2
0
        protected virtual async Task ClientStartListen(HttpConnection connection)
        {
            connection.LastWorkTime = -1;
            if (connection.NetworkClient != null && connection.NetworkClient.Connected)
            {
                WebServerLog.Add(ServerLogType.Information, GetType(), "Connection", "Listen to Connection {0}",
                                 connection.NetworkClient?.Client.RemoteEndPoint);
                var task = PrepairProgressTask(connection);
                if (task == null)
                {
                    WebServerLog.Add(ServerLogType.Information, GetType(), "Connection",
                                     $"Cannot establish data stream to {connection.Ip}");
                    RemoveConnection(connection);
                    return;
                }

                var start = task.Monitor.Enabled ? DateTime.UtcNow : DateTime.MinValue;

                try
                {
                    await ExecuteTaskChain(task).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    task.Monitor.Current.Log("Unhandled exception: {0}", e);
                    WebServerLog.Add(ServerLogType.Error, GetType(), "runtime exception", $"unhandled exception: {e}");
                    throw;
                }
                finally
                {
                    if (Settings.MonitoringOutputDirectory is string monitorOut && task.Monitor.Enabled)
                    {
                        await task.Monitor.Save(monitorOut, start, task);
                    }
                }

                if (task.SwitchProtocolHandler != null)
                {
                    KeepAliveConnections.Remove(connection);
                    AllConnections.Remove(connection);
                    task.Dispose();
                    _ = task.SwitchProtocolHandler();
                    return;
                }

                if (task.Request.FieldConnection == HttpConnectionType.KeepAlive)
                {
                    if (!KeepAliveConnections.Contains(connection))
                    {
                        KeepAliveConnections.Add(connection);
                    }
                }
                else
                {
                    RemoveConnection(connection);
                }

                connection.LastWorkTime = Environment.TickCount;
                task.Dispose();
            }
            else
            {
                RemoveConnection(connection);
            }
        }