コード例 #1
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();
            }
        }
コード例 #2
0
ファイル: ConnectionHandler.cs プロジェクト: ciker/twino
 /// <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
     });
コード例 #3
0
ファイル: ConnectionInfo.cs プロジェクト: ciker/twino
 public ConnectionInfo(TcpClient client, HostListener server)
 {
     Client      = client;
     Server      = server;
     PlainStream = client.GetStream();
 }
コード例 #4
0
ファイル: ConnectionHandler.cs プロジェクト: ciker/twino
 /// <summary>
 /// Creates new connection handler for listening specified port
 /// </summary>
 public ConnectionHandler(TwinoServer server, HostListener listener)
 {
     _server   = server;
     _listener = listener;
 }