/// <summary>
 /// Finds supported SSL protocol from server options
 /// </summary>
 private static SslProtocols GetProtocol(HostListener server)
 {
     return(server.Options.SslProtocol switch
     {
         "tls" => SslProtocols.Tls,
         "tls11" => SslProtocols.Tls11,
         "tls12" => SslProtocols.Tls12,
         _ => SslProtocols.None
     });
예제 #2
0
 public ConnectionInfo(TcpClient client, HostListener server)
 {
     Client      = client;
     Server      = server;
     PlainStream = client.GetStream();
 }
 /// <summary>
 /// Creates new connection handler for listening specified port
 /// </summary>
 public ConnectionHandler(HorseServer server, HostListener listener)
 {
     _server   = server;
     _listener = listener;
 }
예제 #4
0
        /// <summary>
        /// Starts server and listens new connection requests
        /// </summary>
        public void Start()
        {
            if (IsRunning)
            {
                throw new InvalidOperationException("Stop the HttpServer before restart");
            }

            if (Options.Hosts == null || Options.Hosts.Count == 0)
            {
                throw new ArgumentNullException($"Hosts", "There is no host to listen. Add hosts to Horse Options");
            }

            AppDomain.CurrentDomain.ProcessExit += (sender, args) => OnStopped?.Invoke(this);

            if (_timeTimer != null)
            {
                _timeTimer.Stop();
                _timeTimer.Dispose();
            }

            IsRunning = true;
            _handlers = new List <ConnectionHandler>();

            //if websocket ping is activated, starts pinger
            if (Options.PingInterval > 0)
            {
                HeartbeatManager = new HeartbeatManager(this, TimeSpan.FromSeconds(Options.PingInterval));
                HeartbeatManager.Start();
            }

            foreach (HostOptions host in Options.Hosts)
            {
                HostListener server = new HostListener();
                server.Options = host;

                if (host.SslEnabled && !string.IsNullOrEmpty(host.SslCertificate))
                {
                    server.Certificate = string.IsNullOrEmpty(host.CertificateKey)
                                             ? new X509Certificate2(host.SslCertificate)
                                             : new X509Certificate2(host.SslCertificate, host.CertificateKey);
                }

                server.Listener = new TcpListener(IPAddress.Any, host.Port);

                if (Options.MaximumPendingConnections == 0)
                {
                    server.Listener.Start();
                }
                else
                {
                    server.Listener.Start(Options.MaximumPendingConnections);
                }

                ConnectionHandler handler = new ConnectionHandler(this, server);
                server.Handle = new Thread(() => _ = handler.Handle());
                server.Handle.IsBackground = true;
                server.Handle.Priority     = ThreadPriority.Highest;
                server.Handle.Start();
                _handlers.Add(handler);
            }

            IsRunning = true;

            OnStarted?.Invoke(this);
        }