Beispiel #1
0
        /// <summary>
        /// Tear down the server and dispose of background workers.
        /// Do not use the object after disposal.
        /// </summary>
        /// <param name="disposing">Indicate if resources should be disposed.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                Events.HandleServerDisposing(this, EventArgs.Empty);

                if (_TcpServer != null)
                {
                    if (_TcpServer.IsListening)
                    {
                        Stop();
                    }

                    _TcpServer.Dispose();
                    _TcpServer = null;
                }

                if (_TokenSource != null && !_Token.IsCancellationRequested)
                {
                    _TokenSource.Cancel();
                }

                _Statistics = null;
                _Settings   = null;
                _Routes     = null;
                _Events     = null;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Instantiate the webserver without SSL listening on localhost port 8080 and using the specified default route.
        /// </summary>
        /// <param name="defaultRoute">Default route.</param>
        public Webserver(Func <HttpContext, Task> defaultRoute)
        {
            if (defaultRoute == null)
            {
                throw new ArgumentNullException(nameof(defaultRoute));
            }

            _Routes = new WebserverRoutes(defaultRoute);

            InitializeServer();
        }
Beispiel #3
0
        /// <summary>
        /// Instantiate the webserver without SSL.
        /// </summary>
        /// <param name="hostname">Hostname or IP address on which to listen.</param>
        /// <param name="port">TCP port on which to listen.</param>
        /// <param name="defaultRoute">Default route.</param>
        public Webserver(string hostname, int port, Func <HttpContext, Task> defaultRoute)
        {
            if (String.IsNullOrEmpty(hostname))
            {
                hostname = "localhost";
            }
            if (port < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }
            if (defaultRoute == null)
            {
                throw new ArgumentNullException(nameof(defaultRoute));
            }

            _Settings = new WebserverSettings(hostname, port);
            _Routes   = new WebserverRoutes(defaultRoute);

            InitializeServer();
        }
Beispiel #4
0
        /// <summary>
        /// Instantiate the webserver with or without SSL.
        /// </summary>
        /// <param name="hostname">Hostname or IP address on which to listen.</param>
        /// <param name="port">TCP port on which to listen.</param>
        /// <param name="ssl">Enable or disable SSL.</param>
        /// <param name="pfxCertFilename">For SSL, the PFX certificate filename.</param>
        /// <param name="pfxCertPassword">For SSL, the PFX certificate password.</param>
        /// <param name="defaultRoute">Default route.</param>
        public Webserver(string hostname, int port, bool ssl, string pfxCertFilename, string pfxCertPassword, Func <HttpContext, Task> defaultRoute)
        {
            if (String.IsNullOrEmpty(hostname))
            {
                hostname = "localhost";
            }
            if (port < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }
            if (defaultRoute == null)
            {
                throw new ArgumentNullException(nameof(defaultRoute));
            }

            _Settings                            = new WebserverSettings(hostname, port);
            _Settings.Ssl.Enable                 = ssl;
            _Settings.Ssl.PfxCertificateFile     = pfxCertFilename;
            _Settings.Ssl.PfxCertificatePassword = pfxCertPassword;

            _Routes = new WebserverRoutes(defaultRoute);

            InitializeServer();
        }