diff --git a/websocket-sharp/Server/HttpServer.cs b/websocket-sharp/Server/HttpServer.cs index fb8ebdd4..9309e173 100644 --- a/websocket-sharp/Server/HttpServer.cs +++ b/websocket-sharp/Server/HttpServer.cs @@ -775,6 +775,61 @@ namespace WebSocketSharp.Server _receiveThread.Start (); } + private void stop (ushort code, string reason) + { + if (_state == ServerState.Ready) { + _log.Info ("The server is not started."); + return; + } + + if (_state == ServerState.ShuttingDown) { + _log.Info ("The server is shutting down."); + return; + } + + if (_state == ServerState.Stop) { + _log.Info ("The server has already stopped."); + return; + } + + lock (_sync) { + if (_state == ServerState.ShuttingDown) { + _log.Info ("The server is shutting down."); + return; + } + + if (_state == ServerState.Stop) { + _log.Info ("The server has already stopped."); + return; + } + + _state = ServerState.ShuttingDown; + } + + try { + var threw = false; + try { + _services.Stop (code, reason); + } + catch { + threw = true; + throw; + } + finally { + try { + stopReceiving (5000); + } + catch { + if (!threw) + throw; + } + } + } + finally { + _state = ServerState.Stop; + } + } + private void stopReceiving (int millisecondsTimeout) { _listener.Close ();