Пример #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);
            }
        }
Пример #3
0
        protected virtual void ServerMainTask()
        {
            if (Listener == null)
            {
                return;
            }
            WebServerLog.Add(ServerLogType.Information, GetType(), "StartUp", "Server successfully started");
            var watch = new Stopwatch();

            while (ServerExecution)
            {
                watch.Restart();
                //request pending connections
                int step = 0;
                while (step < 10)
                {
                    if (!Listener.Pending())
                    {
                        break;
                    }
                    step++;
                    ClientConnected(Listener.AcceptTcpClient());
                }
                //request keep alive connections
                for (int i = 0; i < KeepAliveConnections.Count; ++i)
                {
                    HttpConnection kas;
                    try { kas = KeepAliveConnections[i]; }
                    catch { continue; }
                    if (kas == null)
                    {
                        continue;
                    }

                    if ((kas.NetworkClient != null && !kas.NetworkClient.Connected) ||
                        (kas.LastWorkTime != -1 &&
                         kas.LastWorkTime + Settings.ConnectionTimeout < Environment.TickCount
                        )
                        )
                    {
                        kas.NetworkClient?.Close();
                        kas.NetworkStream?.Dispose();
                        AllConnections.Remove(kas);
                        KeepAliveConnections.Remove(kas);
                        --i;
                        continue;
                    }

                    if (kas.NetworkClient != null && kas.NetworkClient.Available > 0 &&
                        kas.LastWorkTime != -1
                        )
                    {
                        _ = Task.Run(() => SafeClientStartListen(kas));
                    }
                }

                //Warten
                if (Listener.Pending())
                {
                    continue;
                }
                var delay = Settings.ConnectionDelay;
                if (delay > TimeSpan.Zero)
                {
                    var time = delay - watch.Elapsed;
                    if (time <= TimeSpan.Zero)
                    {
                        time = delay;
                    }
                    Thread.Sleep(time);
                }
            }
            watch.Stop();
            Listener.Stop();
            for (int i = 0; i < AllConnections.Count; ++i)
            {
                AllConnections[i].NetworkClient?.Close();
            }
            AllConnections.Clear();
            KeepAliveConnections.Clear();
            WebServerLog.Add(ServerLogType.Information, GetType(), "StartUp", "Server successfully stopped");
        }