public void Start(Action <IWebSocketConnection> config) { var ipLocal = new IPEndPoint(_locationIP, Port); ListenerSocket.Bind(ipLocal); ListenerSocket.Listen(100); Port = ((IPEndPoint)ListenerSocket.LocalEndPoint).Port; NTWebSocketLog.Info(string.Format("Server started at {0} (actual port {1})", Location, Port)); if (_scheme == "wss") { if (Certificate == null) { NTWebSocketLog.Error("Scheme cannot be 'wss' without a Certificate"); return; } if (EnabledSslProtocols == SslProtocols.None) { EnabledSslProtocols = SslProtocols.Tls; NTWebSocketLog.Debug("Using default TLS 1.0 security protocol."); } } ListenForClients(); _config = config; }
private void ListenForClients() { ListenerSocket.Accept(OnClientConnect, e => { NTWebSocketLog.Error("Listener socket is closed", e); if (RestartAfterListenError) { NTWebSocketLog.Info("Listener socket restarting"); try { ListenerSocket.Dispose(); var socket = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP); ListenerSocket = new SocketWrapper(socket); Start(_config); NTWebSocketLog.Info("Listener socket restarted"); } catch (Exception ex) { NTWebSocketLog.Error("Listener could not be restarted", ex); } } }); }
private void HandleReadError(Exception e) { if (e is AggregateException) { var agg = e as AggregateException; HandleReadError(agg.InnerException); return; } if (e is ObjectDisposedException) { NTWebSocketLog.Debug("Swallowing ObjectDisposedException", e); return; } OnError(e); if (e is WebSocketException) { NTWebSocketLog.Debug("Error while reading", e); Close(((WebSocketException)e).StatusCode); } else if (e is SubProtocolNegotiationFailureException) { NTWebSocketLog.Debug(e.Message); Close(WebSocketStatusCodes.ProtocolError); } else if (e is IOException) { NTWebSocketLog.Debug("Error while reading", e); Close(WebSocketStatusCodes.AbnormalClosure); } else { NTWebSocketLog.Error("Application Error", e); Close(WebSocketStatusCodes.InternalServerError); } }