/// <summary>
        /// Starts the server.
        /// </summary>
        /// <returns>A task that will complete when the server has started.</returns>
        public async Task StartAsync()
        {
            HConsole.WriteLine(this, $"Start server at {_endpoint}");

            _listener = new ServerSocketListener(_endpoint, _hcname)
            {
                OnAcceptConnection = AcceptConnection, OnShutdown = ListenerShutdown
            };

            _open = true;
            await _listener.StartAsync().CfAwait();

            HConsole.WriteLine(this, "Server started");
        }
Exemple #2
0
        /// <summary>
        /// Starts the server.
        /// </summary>
        /// <returns>A task that will complete when the server has started.</returns>
        public async Task StartAsync()
        {
            HConsole.WriteLine(this, $"Start server at {_endpoint}");

            _listener = new ServerSocketListener(_endpoint)
            {
                OnAcceptConnection = AcceptConnection, OnShutdown = ListenerShutdown
            };
            HConsole.Configure(_listener, config => config.SetIndent(24).SetPrefix("LISTENER"));
            _open = true;
            await _listener.StartAsync().CAF();

            HConsole.WriteLine(this, "Server started");
        }
        /// <summary>
        /// Stops the server.
        /// </summary>
        /// <returns>A task that will complete when the server has stopped.</returns>
        public async Task StopAsync()
        {
            var listener = _listener;

            _listener = null;

            if (listener == null)
            {
                return;
            }

            HConsole.WriteLine(this, "Stop server");

            // stop accepting new connections
            await listener.StopAsync().CfAwait();

            await listener.DisposeAsync().CfAwait();

            HConsole.WriteLine(this, "Server stopped");
        }
        private async ValueTask ListenerShutdown(ServerSocketListener arg)
        {
            HConsole.WriteLine(this, "Listener is down");

            lock (_openLock)
            {
                _open = false;
            }

            // shutdown all existing connections
            foreach (var connection in _connections.Values.ToList())
            {
                try
                {
                    await connection.DisposeAsync().CfAwait();
                }
                catch { /* ignore */ }
            }

            HConsole.WriteLine(this, "Connections are down");
        }