Exemplo n.º 1
0
        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 HandshakeException)
            {
                FleckLog.Debug("Error while reading", e);
            }
            else 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);
            }
        }
Exemplo n.º 2
0
        private Task Send <T>(T message, Func <T, byte[]> createFrame)
        {
            if (Handler == null)
            {
                throw new InvalidOperationException("Cannot send before handshake");
            }

            if (!IsAvailable)
            {
                const string errorMessage = "Data sent while closing or after close. Ignoring.";
                FleckLog.Warn(errorMessage);

                var taskForException = new TaskCompletionSource <object>();
                taskForException.SetException(new ConnectionNotAvailableException(errorMessage));
                return(taskForException.Task);
            }

            var bytes = createFrame(message);

            return(SendBytes(bytes));
        }
Exemplo n.º 3
0
 private Task SendBytes(byte[] bytes, Action callback = null)
 {
     return(Socket.Send(bytes, () =>
     {
         FleckLog.Debug("Sent " + bytes.Length + " bytes");
         if (callback != null)
         {
             callback();
         }
     },
                        e =>
     {
         if (e is IOException)
         {
             FleckLog.Debug("Failed to send. Disconnecting.", e);
         }
         else
         {
             FleckLog.Info("Failed to send. Disconnecting.", e);
         }
         CloseSocket();
     }));
 }
Exemplo n.º 4
0
        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();
            }
        }
Exemplo n.º 5
0
 private void ListenForClients()
 {
     ListenerSocket.Accept(OnClientConnect, e => FleckLog.Error("Listener socket is closed", e));
 }