Ejemplo n.º 1
0
        /// <summary>
        /// Stops accepting connections.
        /// But does not disconnect connected clients.
        /// In order to disconnect all clients, you need to do it manually
        /// You can use a ClientContainer implementation to do it easily
        /// </summary>
        public void Stop()
        {
            IsRunning = false;

            //stop server time creator timer
            if (_timeTimer != null)
            {
                _timeTimer.Stop();
                _timeTimer.Dispose();
                _timeTimer = null;
            }

            //stop websocket pinger
            if (Pinger != null)
            {
                Pinger.Stop();
                Pinger = null;
            }

            //stop and dispose all listeners (for all ports)
            foreach (ConnectionHandler handler in _handlers)
            {
                handler.Dispose();
            }

            _handlers.Clear();

            OnStopped?.Invoke(this);
        }
Ejemplo n.º 2
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 Twino Options");
            }

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

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

            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;
            //if websocket ping is activated, starts pinger
            if (Options.PingInterval > 0)
            {
                Pinger = new Pinger(this, TimeSpan.FromSeconds(Options.PingInterval));
                Pinger.Start();
            }
        }