private void Read(List <byte> data, byte[] buffer) { if (!IsAvailable) { return; } Socket.Receive(buffer, r => { if (r <= 0) { FleckLog.Debug("0 bytes read. Closing."); CloseSocket(); return; } FleckLog.Debug(r + " bytes read"); var readBytes = buffer.Take(r); if (Handler != null) { Handler.Receive(readBytes); } else { data.AddRange(readBytes); CreateHandler(data); } Read(data, buffer); }, HandleReadError); }
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; }
private Task SendBytes(byte[] bytes, Action callback = null) { return(Socket.Send(bytes, () => { FleckLog.Debug("Sent " + bytes.Length + " bytes"); callback?.Invoke(); }, e => { if (e is IOException) { FleckLog.Debug("Failed to send. Disconnecting.", e); } else { FleckLog.Info("Failed to send. Disconnecting.", e); } CloseSocket(); })); }
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 SubProtocolNegotiationFailureException) { FleckLog.Debug(e.Message); Close(WebSocketStatusCodes.ProtocolError); } 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 OnClientConnect(ISocket clientSocket) { if (clientSocket == null) { return; // socket closed } 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)); if (IsSecure) { FleckLog.Debug("Authenticating Secure Connection"); clientSocket .Authenticate(Certificate, EnabledSslProtocols, connection.StartReceiving, e => FleckLog.Warn("Failed to Authenticate", e)); } else { connection.StartReceiving(); } }