Search in sources :

Example 1 with ProtocolViolation

use of io.crossbar.autobahn.websocket.messages.ProtocolViolation in project autobahn-java by crossbario.

the class WebSocketConnection method createHandler.

/**
 * Create master message handler.
 */
private void createHandler() {
    mMasterHandler = new Handler(Looper.getMainLooper()) {

        public void handleMessage(Message msg) {
            // anything received after that.
            if (onCloseCalled) {
                LOGGER.d("onClose called already, ignore message.");
                return;
            }
            if (msg.obj instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) msg.obj;
                if (mWsHandler != null) {
                    mWsHandler.onMessage(textMessage.mPayload);
                } else {
                    LOGGER.d("could not call onTextMessage() .. handler already NULL");
                }
            } else if (msg.obj instanceof RawTextMessage) {
                RawTextMessage rawTextMessage = (RawTextMessage) msg.obj;
                if (mWsHandler != null) {
                    mWsHandler.onMessage(rawTextMessage.mPayload, false);
                } else {
                    LOGGER.d("could not call onRawTextMessage() .. handler already NULL");
                }
            } else if (msg.obj instanceof BinaryMessage) {
                BinaryMessage binaryMessage = (BinaryMessage) msg.obj;
                if (mWsHandler != null) {
                    mWsHandler.onMessage(binaryMessage.mPayload, true);
                } else {
                    LOGGER.d("could not call onBinaryMessage() .. handler already NULL");
                }
            } else if (msg.obj instanceof Ping) {
                Ping ping = (Ping) msg.obj;
                LOGGER.d("WebSockets Ping received");
                if (ping.mPayload == null) {
                    mWsHandler.onPing();
                } else {
                    mWsHandler.onPing(ping.mPayload);
                }
            } else if (msg.obj instanceof Pong) {
                Pong pong = (Pong) msg.obj;
                if (pong.mPayload == null) {
                    mWsHandler.onPong();
                } else {
                    mWsHandler.onPong(pong.mPayload);
                }
                LOGGER.d("WebSockets Pong received");
            } else if (msg.obj instanceof Close) {
                Close close = (Close) msg.obj;
                final int crossbarCloseCode = (close.mCode == 1000) ? IWebSocketConnectionHandler.CLOSE_NORMAL : IWebSocketConnectionHandler.CLOSE_CONNECTION_LOST;
                if (close.mIsReply) {
                    LOGGER.d("WebSockets Close received (" + close.mCode + " - " + close.mReason + ")");
                    closeAndCleanup();
                    onClose(crossbarCloseCode, close.mReason);
                } else if (mActive) {
                    // We have received a close frame, lets clean.
                    closeReaderThread(false);
                    mWriter.forward(new Close(1000, true));
                    mActive = false;
                } else {
                    LOGGER.d("WebSockets Close received (" + close.mCode + " - " + close.mReason + ")");
                    // we've initiated disconnect, so ready to close the channel
                    closeAndCleanup();
                    onClose(crossbarCloseCode, close.mReason);
                }
            } else if (msg.obj instanceof ServerHandshake) {
                ServerHandshake serverHandshake = (ServerHandshake) msg.obj;
                LOGGER.d("opening handshake received");
                if (serverHandshake.mSuccess) {
                    if (mWsHandler != null) {
                        mExecutor.scheduleAtFixedRate(mAutoPinger, 0, mOptions.getAutoPingInterval(), TimeUnit.SECONDS);
                        String protocol = getOrDefault(serverHandshake.headers, "Sec-WebSocket-Protocol", null);
                        mWsHandler.setConnection(WebSocketConnection.this);
                        mWsHandler.onConnect(new ConnectionResponse(protocol));
                        mWsHandler.onOpen();
                        LOGGER.d("onOpen() called, ready to rock.");
                    } else {
                        LOGGER.d("could not call onOpen() .. handler already NULL");
                    }
                }
            } else if (msg.obj instanceof CannotConnect) {
                CannotConnect cannotConnect = (CannotConnect) msg.obj;
                failConnection(IWebSocketConnectionHandler.CLOSE_CANNOT_CONNECT, cannotConnect.reason);
            } else if (msg.obj instanceof ConnectionLost) {
                ConnectionLost connnectionLost = (ConnectionLost) msg.obj;
                failConnection(IWebSocketConnectionHandler.CLOSE_CONNECTION_LOST, connnectionLost.reason);
            } else if (msg.obj instanceof ProtocolViolation) {
                @SuppressWarnings("unused") ProtocolViolation protocolViolation = (ProtocolViolation) msg.obj;
                failConnection(IWebSocketConnectionHandler.CLOSE_PROTOCOL_ERROR, "WebSockets protocol violation");
            } else if (msg.obj instanceof Error) {
                Error error = (Error) msg.obj;
                failConnection(IWebSocketConnectionHandler.CLOSE_INTERNAL_ERROR, "WebSockets internal error (" + error.mException.toString() + ")");
            } else if (msg.obj instanceof ServerError) {
                ServerError error = (ServerError) msg.obj;
                failConnection(IWebSocketConnectionHandler.CLOSE_SERVER_ERROR, "Server error " + error.mStatusCode + " (" + error.mStatusMessage + ")");
            } else {
                processAppMessage(msg.obj);
            }
        }
    };
}
Also used : ServerHandshake(io.crossbar.autobahn.websocket.messages.ServerHandshake) BinaryMessage(io.crossbar.autobahn.websocket.messages.BinaryMessage) Message(android.os.Message) TextMessage(io.crossbar.autobahn.websocket.messages.TextMessage) RawTextMessage(io.crossbar.autobahn.websocket.messages.RawTextMessage) ServerError(io.crossbar.autobahn.websocket.messages.ServerError) CannotConnect(io.crossbar.autobahn.websocket.messages.CannotConnect) Handler(android.os.Handler) IWebSocketConnectionHandler(io.crossbar.autobahn.websocket.interfaces.IWebSocketConnectionHandler) ServerError(io.crossbar.autobahn.websocket.messages.ServerError) Error(io.crossbar.autobahn.websocket.messages.Error) ConnectionResponse(io.crossbar.autobahn.websocket.types.ConnectionResponse) BinaryMessage(io.crossbar.autobahn.websocket.messages.BinaryMessage) ProtocolViolation(io.crossbar.autobahn.websocket.messages.ProtocolViolation) Ping(io.crossbar.autobahn.websocket.messages.Ping) RawTextMessage(io.crossbar.autobahn.websocket.messages.RawTextMessage) Close(io.crossbar.autobahn.websocket.messages.Close) ConnectionLost(io.crossbar.autobahn.websocket.messages.ConnectionLost) TextMessage(io.crossbar.autobahn.websocket.messages.TextMessage) RawTextMessage(io.crossbar.autobahn.websocket.messages.RawTextMessage) Pong(io.crossbar.autobahn.websocket.messages.Pong)

Example 2 with ProtocolViolation

use of io.crossbar.autobahn.websocket.messages.ProtocolViolation in project autobahn-java by crossbario.

the class WebSocketReader method run.

/**
 * Run the background reader thread loop.
 */
@Override
public void run() {
    LOGGER.d("Running");
    try {
        do {
            // blocking read on socket
            int len = mBufferedStream.read(mMessageData, mPosition, mMessageData.length - mPosition);
            mPosition += len;
            if (len > 0) {
                mLastReadTime = System.currentTimeMillis();
                // process buffered data
                while (consumeData()) {
                }
            } else if (mState == STATE_CLOSED) {
                mStopped = true;
            } else if (len < 0) {
                LOGGER.d("run() : ConnectionLost");
                notify(new ConnectionLost(null));
                mStopped = true;
            }
        } while (!mStopped);
    } catch (WebSocketException e) {
        LOGGER.d("run() : WebSocketException (" + e.toString() + ")");
        // wrap the exception and notify master
        notify(new ProtocolViolation(e));
    } catch (SocketException e) {
        // eat the exception if we are already in STATE_CLOSED.
        if (mState != STATE_CLOSED && !mSocket.isClosed()) {
            LOGGER.d("run() : SocketException (" + e.toString() + ")");
            // wrap the exception and notify master
            notify(new ConnectionLost(null));
        }
    } catch (Exception e) {
        LOGGER.d("run() : Exception (" + e.toString() + ")");
        // wrap the exception and notify master
        notify(new Error(e));
    } finally {
        mStopped = true;
    }
    LOGGER.d("Ended");
}
Also used : SocketException(java.net.SocketException) WebSocketException(io.crossbar.autobahn.websocket.exceptions.WebSocketException) WebSocketException(io.crossbar.autobahn.websocket.exceptions.WebSocketException) ProtocolViolation(io.crossbar.autobahn.websocket.messages.ProtocolViolation) ServerError(io.crossbar.autobahn.websocket.messages.ServerError) Error(io.crossbar.autobahn.websocket.messages.Error) ConnectionLost(io.crossbar.autobahn.websocket.messages.ConnectionLost) SocketException(java.net.SocketException) WebSocketException(io.crossbar.autobahn.websocket.exceptions.WebSocketException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

ConnectionLost (io.crossbar.autobahn.websocket.messages.ConnectionLost)2 Error (io.crossbar.autobahn.websocket.messages.Error)2 ProtocolViolation (io.crossbar.autobahn.websocket.messages.ProtocolViolation)2 ServerError (io.crossbar.autobahn.websocket.messages.ServerError)2 Handler (android.os.Handler)1 Message (android.os.Message)1 WebSocketException (io.crossbar.autobahn.websocket.exceptions.WebSocketException)1 IWebSocketConnectionHandler (io.crossbar.autobahn.websocket.interfaces.IWebSocketConnectionHandler)1 BinaryMessage (io.crossbar.autobahn.websocket.messages.BinaryMessage)1 CannotConnect (io.crossbar.autobahn.websocket.messages.CannotConnect)1 Close (io.crossbar.autobahn.websocket.messages.Close)1 Ping (io.crossbar.autobahn.websocket.messages.Ping)1 Pong (io.crossbar.autobahn.websocket.messages.Pong)1 RawTextMessage (io.crossbar.autobahn.websocket.messages.RawTextMessage)1 ServerHandshake (io.crossbar.autobahn.websocket.messages.ServerHandshake)1 TextMessage (io.crossbar.autobahn.websocket.messages.TextMessage)1 ConnectionResponse (io.crossbar.autobahn.websocket.types.ConnectionResponse)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SocketException (java.net.SocketException)1