private void ListenForClients() { ManualResetEvent acceptDone = new ManualResetEvent(false); bool running = true; Task.Run(() => { while (running) { acceptDone.Reset(); var task = ListenerSocket.Accept( s => { running = (s != null); acceptDone.Set(); OnClientConnect(s); }, e => { FleckLog.Error("Error while listening for new clients", e); if (RestartAfterListenError) { TryRestart(); } running = false; acceptDone.Set(); } ); task.ContinueWith((t) => FleckLog.Warn("Error during client connect", t.Exception), TaskContinuationOptions.OnlyOnFaulted); acceptDone.WaitOne(); } }); }
public void Start(Action <IWebSocketConnection> config) { if (CheckIfPortAvailable(Port)) { var ipLocal = new IPEndPoint(_locationIP, Port); ListenerSocket.Bind(ipLocal); ListenerSocket.Listen(100); Port = ((IPEndPoint)ListenerSocket.LocalEndPoint).Port; FleckLog.Info(string.Format("Server started at {0} (actual port {1})", Location, Port)); if (_scheme == "wss") { if (Certificate == null) { FleckLog.Error("Scheme cannot be 'wss' without a Certificate"); return; } if (EnabledSslProtocols == SslProtocols.None) { EnabledSslProtocols = SslProtocols.Tls; FleckLog.Debug("Using default TLS 1.0 security protocol."); } } ListenForClients(); _config = config; } else { FleckLog.Error(string.Format("Server cannot start port {0}, port already in use", Port)); throw new Exception("Error: listen EADDRINUSE: address already in use :::" + Port); } }
private void HandleReadError(Exception e) { if (e is AggregateException) { var agg = e as AggregateException; HandleReadError(agg.InnerException); return; } if (e is ObjectDisposedException) { FleckLog.Debug("Swallowing ObjectDisposedException", e); return; } OnError(e); if (e is WebSocketException) { FleckLog.Debug("Error while reading", e); Close(((WebSocketException)e).StatusCode); } else if (e is IOException) { FleckLog.Debug("Error while reading", e); Close(WebSocketStatusCodes.AbnormalClosure); } else { FleckLog.Error("Application Error", e); Close(WebSocketStatusCodes.InternalServerError); } }
private void HandleReadError(Exception e) { if (e is AggregateException) { var agg = e as AggregateException; HandleReadError(agg.InnerException); return; } else if (e is ObjectDisposedException) { FleckLog.Debug("Swallowing ObjectDisposedException", e); return; } OnError(e); if (e is HandshakeException) { FleckLog.Debug("Error while reading", e); } else if (e is WebSocketException) { FleckLog.Debug("Error while reading", e); Close(WebSocketStatusCodes.ProtocolError); } else { FleckLog.Error("Application Error", e); Close(WebSocketStatusCodes.ApplicationError); } }
private void ListenForClients() { ListenerSocket.Accept( OnClientConnect, e => { FleckLog.Error("Listener socket is closed", e); if (RestartAfterListenError) { FleckLog.Info("Listener socket restarting"); try { ListenerSocket.Dispose(); var socket = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP); ListenerSocket = new SocketWrapper(socket); Start(_config); FleckLog.Info("Listener socket restarted"); } catch (Exception ex) { FleckLog.Error("Listener could not be restarted", ex); } } } ); }
public void Start(Action <IWebSocketConnection> config) { var ipLocal = new IPEndPoint(_locationIP, Port); ListenerSocket.Bind(ipLocal); ListenerSocket.Listen(100); Port = ((IPEndPoint)ListenerSocket.LocalEndPoint).Port; FleckLog.Info(string.Format("Server started at {0} (actual port {1})", Location, Port)); if (_scheme == "wss") { if (Certificate == null) { FleckLog.Error("Scheme cannot be 'wss' without a Certificate"); return; } if (EnabledSslProtocols == SslProtocols.None) { EnabledSslProtocols = SslProtocols.Tls; FleckLog.Debug("Using default TLS 1.0 security protocol."); } } ListenForClients(); _config = config; }
//when new ts file generated public void OnFileHasCome(object sender, FileInfoEventArg arg) { var arrayBits = arg.GetfileBytes(); Socket.Send(arrayBits, () => { FleckLog.Info("send " + arrayBits.Length + " bytes"); }, e => { FleckLog.Error("send error" + e.Message); }); }
private void TryRestart() { FleckLog.Info("Listener socket restarting"); try { ListenerSocket.Dispose(); var socket = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP); ListenerSocket = new SocketWrapper(socket); Start(_config); FleckLog.Info("Listener socket restarted"); } catch (Exception ex) { FleckLog.Error("Listener socket 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) { FleckLog.Debug("Swallowing ObjectDisposedException", e); CloseSocket(); return; } OnError(e); if (e is HandshakeException) { FleckLog.Debug("Error while reading", e); // why no close here?? TL //Close(WebSocketStatusCodes.ProtocolError); CloseSocket(); } else if (e is WebSocketException) { FleckLog.Debug("Error while reading", e); //Close(((WebSocketException)e).StatusCode); CloseSocket(); } else if (e is SubProtocolNegotiationFailureException) { FleckLog.Debug(e.Message); CloseSocket(); //Close(WebSocketStatusCodes.ProtocolError); } else if (e is IOException) { FleckLog.Debug("Error while reading", e); CloseSocket(); //Close(WebSocketStatusCodes.AbnormalClosure); } else { FleckLog.Error("Application Error", e); CloseSocket(); //Close(WebSocketStatusCodes.InternalServerError); } }
public void Start(Action <IWebSocketConnection> config) { var ipLocal = new HostName(Location); ListenerSocket.Bind(ipLocal, Port.ToString()); ListenerSocket.Listen(100); FleckLog.Info("Server started at " + Location); if (_scheme == "wss") { FleckLog.Error("Scheme cannot be 'wss' without a Certificate"); return; } ListenForClients(); _config = config; }
private void HandleReadError(Exception e) { if (e is AggregateException) { var agg = e as AggregateException; HandleReadError(agg.InnerException); return; } if (e is ObjectDisposedException) { FleckLog.Debug("Swallowing ObjectDisposedException", e); Log.Insert(DateTime.Now, "WebSocketConnection.cs", string.Format("Swallowing ObjectDisposedException: {0}", e), "white"); return; } OnError(e); if (e is HandshakeException) { FleckLog.Debug("Error while reading", e); Log.Insert(DateTime.Now, "WebSocketConnection.cs", string.Format("Error while reading: {0}", e), "white"); } else if (e is WebSocketException) { FleckLog.Debug("Error while reading", e); Log.Insert(DateTime.Now, "WebSocketConnection.cs", string.Format("Error while reading: {0}", e), "white"); Close(((WebSocketException)e).StatusCode); } else if (e is SubProtocolNegotiationFailureException) { FleckLog.Debug(e.Message); Log.Insert(DateTime.Now, "WebSocketConnection.cs", string.Format("e.Message: {0}", e.Message), "white"); Close(WebSocketStatusCodes.ProtocolError); } else if (e is IOException) { FleckLog.Debug("Error while reading", e); Log.Insert(DateTime.Now, "WebSocketConnection.cs", string.Format("Error while reading: {0}", e), "white"); Close(WebSocketStatusCodes.AbnormalClosure); } else { FleckLog.Error("Application Error", e); Log.Insert(DateTime.Now, "WebSocketConnection.cs", string.Format("Application error: {0}", e), "white"); Close(WebSocketStatusCodes.InternalServerError); } }
public void Start(Action <IWebSocketConnection> config) { var ipLocal = new IPEndPoint(IPAddress.Any, Port); ListenerSocket.Bind(ipLocal); ListenerSocket.Listen(100); FleckLog.Info("Server started at " + Location); if (_scheme == "wss") { if (Certificate == null) { FleckLog.Error("Scheme cannot be 'wss' without a Certificate"); return; } } ListenForClients(); _config = config; }
public void Start(Action <IWebSocketConnection> config) { var ipLocal = new IPEndPoint(IPAddress.Any, Port); int actualPort = ListenerSocket.Bind(ipLocal); ListenerSocket.Listen(100); FleckLog.Info(_scheme + " server started at " + Location + (actualPort != Port ? (" (port " + actualPort + ")") : "")); SocketBound(this, "Web Socket Server listening on port " + actualPort + " (" + _scheme + ")"); if (_scheme == "wss") { if (Certificate == null) { FleckLog.Error("Scheme cannot be 'wss' without a Certificate"); return; } } ListenForClients(); _config = config; }
public void Start(Action <IWebSocketConnection> config) { var ipLocal = new IPEndPoint(_locationIP, Port); ListenerSocket.Bind(ipLocal); ListenerSocket.Listen(100); Port = ((IPEndPoint)ListenerSocket.LocalEndPoint).Port; FleckLog.Info($"Server started at {Location} (actual port {Port})"); if (_scheme == "wss") { if (Certificate == null) { FleckLog.Error("Scheme cannot be 'wss' without a Certificate"); return; } } ListenForClients(); _config = config; }
private Task SendBytes(byte[] bytes, Action callback = null) { return(Socket.Send(bytes, () => { FleckLog.Trace("Sent " + bytes.Length + " bytes"); callback?.Invoke(); }, e => { if (e is IOException) { FleckLog.Error("Failed to send. Disconnecting.", e); } else { FleckLog.Error("Failed to send. Disconnecting.", e); } CloseSocket(); })); }
public SocketConnection(ISocket socket) { Socket = socket; OnOpen = () => { }; OnClose = () => { }; OnMessage = (x) => { }; OnError = (x) => { }; // default read handler OnRead = (data, readBytes) => { OnMessage(Encoding.UTF8.GetString(readBytes.ToArray <byte>())); }; // default read error handler OnReadError = (e) => { FleckLog.Error("Error while reading", e); OnError(e); Close(); }; }
private void OnClientConnect(ISocket clientSocket) { if (clientSocket == null) { return; // socket closed } FleckLog.Trace($"Client connected from {clientSocket.RemoteIpAddress}:{clientSocket.RemotePort}"); ListenForClients(); WebSocketConnection connection = null; connection = new WebSocketConnection( clientSocket, _config, bytes => RequestParser.Parse(bytes, _scheme), r => HandlerFactory.BuildHandler(r, s => connection.OnMessage(s), connection.Close, b => connection.OnBinary(b), b => connection.OnPing(b), b => connection.OnPong(b)), s => SubProtocolNegotiator.Negotiate(SupportedSubProtocols, s)); if (IsSecure) { FleckLog.Debug("Attempting to secure connection..."); clientSocket.Authenticate(Certificate, EnabledSslProtocols, connection.StartReceiving, e => FleckLog.Error($"Cannot secure the connection: {e.Message}", e)); } else { connection.StartReceiving(); } }
private void ListenForClients() { ListenerSocket.Accept(OnClientConnect, e => FleckLog.Error("Listener socket is closed", e)); }
private void OnClientConnect(ISocket clientSocket) { FleckLog.Debug(String.Format("Client connected from {0}:{1}", clientSocket.RemoteIpAddress, clientSocket.RemotePort.ToString())); ListenForClients(); WebSocketConnection connection = null; connection = new WebSocketConnection( clientSocket, _config, bytes => RequestParser.Parse(bytes, _scheme), r => HandlerFactory.BuildHandler(r, s => connection.OnMessage(s), connection.Close, b => connection.OnBinary(b), b => connection.OnPing(b), b => connection.OnPong(b)), s => SubProtocolNegotiator.Negotiate(SupportedSubProtocols, s)); // Determine whether this is a ws or wss connection try { // Wait up to 5 seconds for the first handshake byte // (Only peek, so it's still in the buffer for the actual handshake handler) byte[] buffer = new byte[1]; clientSocket.Socket.ReceiveTimeout = 5000; int BytesRead = clientSocket.Socket.Receive(buffer, 1, SocketFlags.Peek); clientSocket.Socket.ReceiveTimeout = 0; if (BytesRead == 1) { if ((buffer[0] == 0x16) || (buffer[0] == 0x80)) { // wss connection, ensure we have a certificate if (IsSecure) { FleckLog.Info("Accepting wss:// Connection"); clientSocket .Authenticate(Certificate, connection.StartReceiving, e => { FleckLog.Warn("Failed to Authenticate", e); connection.Close(); }); } else { FleckLog.Warn("Rejecting wss:// connection (no certificate)"); connection.Close(); } } else { // ws connection FleckLog.Info("Accepting ws:// Connection"); connection.StartReceiving(); } } else { connection.Close(); } } catch (Exception ex) { FleckLog.Error("Unable to read handshake byte from client", ex); connection.Close(); } }