/** * Start both the reading thread and the writing thread. * * <p> * The reading thread will call {@link #onReadingThreadStarted()} * as its first step. Likewise, the writing thread will call * {@link #onWritingThreadStarted()} as its first step. After * both the threads have started, {@link #onThreadsStarted()} is * called. * </p> */ private void startThreads() { ReadingThread readingThread = new ReadingThread(this); WritingThread writingThread = new WritingThread(this); synchronized (mThreadsLock) { mReadingThread = readingThread; mWritingThread = writingThread; } // Execute onThreadCreated of the listeners. readingThread.callOnThreadCreated(); writingThread.callOnThreadCreated(); readingThread.start(); writingThread.start(); }
private boolean handleBinaryFrame(WebSocketFrame frame) { // Notify the listeners that a binary frame was received. callOnBinaryFrame(frame); // If the frame indicates the start of fragmentation. if (frame.getFin() == false) { // Start a continuation sequence. mContinuation.add(frame); // Keep reading. return true; } // Get the payload of the frame. Decompression is performed // when necessary. byte[] payload = getMessage(frame); // Notify the listeners that a binary message was received. callOnBinaryMessage(payload); // Keep reading. return true; }
private boolean handleContinuationFrame(WebSocketFrame frame) callOnContinuationFrame(frame); byte[] data = getMessage(mContinuation); callOnTextMessage(data); callOnBinaryMessage(data);
private boolean handleFrame(WebSocketFrame frame) { // Notify the listeners that a frame was received. callOnFrame(frame); // Dispatch based on the opcode. switch (frame.getOpcode()) { case CONTINUATION: return handleContinuationFrame(frame); case TEXT: return handleTextFrame(frame); case BINARY: return handleBinaryFrame(frame); case CLOSE: return handleCloseFrame(frame); case PING: return handlePingFrame(frame); case PONG: return handlePongFrame(frame); default: // Ignore the frame whose opcode is unknown. Keep reading. return true; } }
verifyFrame(frame); if (mStopRequested && isInterrupted()) callOnError(wse); callOnFrameError(wse, frame); WebSocketFrame closeFrame = createCloseFrame(wse);
/** * Call {@link WebSocketListener#onTextMessage(WebSocket, String) * onTextMessage} method of the listeners. */ private void callOnTextMessage(byte[] data) { if (mWebSocket.isDirectTextMessage()) { mWebSocket.getListenerManager().callOnTextMessage(data); return; } try { // Interpret the byte array as a string. // OutOfMemoryError may happen when the size of data is too big. String message = Misc.toStringUTF8(data); // Call onTextMessage() method of the listeners. callOnTextMessage(message); } catch (Throwable t) { // Failed to convert payload data into a string. WebSocketException wse = new WebSocketException( WebSocketError.TEXT_MESSAGE_CONSTRUCTION_ERROR, "Failed to convert payload data into a string: " + t.getMessage(), t); // Notify the listeners that text message construction failed. callOnError(wse); callOnTextMessageError(wse, data); } }
private byte[] decompress(byte[] input) { WebSocketException wse; try { // Decompress the message. return mPMCE.decompress(input); } catch (WebSocketException e) { wse = e; } // Notify the listeners that decompression failed. callOnError(wse); callOnMessageDecompressionError(wse, input); // Create a close frame with a close code of 1003 which // indicates that the message cannot be accepted. WebSocketFrame frame = WebSocketFrame .createCloseFrame(WebSocketCloseCode.UNACCEPTABLE, wse.getMessage()); // Send the close frame. mWebSocket.sendFrame(frame); // Failed to construct a message. return null; }
callOnError(wse); callOnMessageError(wse, frames);
new InterruptionThread().start(); new ReadingThread().start();
private boolean handleFrame(WebSocketFrame frame) { // Notify the listeners that a frame was received. callOnFrame(frame); // Dispatch based on the opcode. switch (frame.getOpcode()) { case CONTINUATION: return handleContinuationFrame(frame); case TEXT: return handleTextFrame(frame); case BINARY: return handleBinaryFrame(frame); case CLOSE: return handleCloseFrame(frame); case PING: return handlePingFrame(frame); case PONG: return handlePongFrame(frame); default: // Ignore the frame whose opcode is unknown. Keep reading. return true; } }
verifyFrame(frame); if (mStopRequested && isInterrupted()) callOnError(wse); callOnFrameError(wse, frame); WebSocketFrame closeFrame = createCloseFrame(wse);
/** * Call {@link WebSocketListener#onTextMessage(WebSocket, String) * onTextMessage} method of the listeners. */ private void callOnTextMessage(byte[] data) { if (mWebSocket.isDirectTextMessage()) { mWebSocket.getListenerManager().callOnTextMessage(data); return; } try { // Interpret the byte array as a string. // OutOfMemoryError may happen when the size of data is too big. String message = Misc.toStringUTF8(data); // Call onTextMessage() method of the listeners. callOnTextMessage(message); } catch (Throwable t) { // Failed to convert payload data into a string. WebSocketException wse = new WebSocketException( WebSocketError.TEXT_MESSAGE_CONSTRUCTION_ERROR, "Failed to convert payload data into a string: " + t.getMessage(), t); // Notify the listeners that text message construction failed. callOnError(wse); callOnTextMessageError(wse, data); } }
private byte[] decompress(byte[] input) { WebSocketException wse; try { // Decompress the message. return mPMCE.decompress(input); } catch (WebSocketException e) { wse = e; } // Notify the listeners that decompression failed. callOnError(wse); callOnMessageDecompressionError(wse, input); // Create a close frame with a close code of 1003 which // indicates that the message cannot be accepted. WebSocketFrame frame = WebSocketFrame .createCloseFrame(WebSocketCloseCode.UNACCEPTABLE, wse.getMessage()); // Send the close frame. mWebSocket.sendFrame(frame); // Failed to construct a message. return null; }
callOnError(wse); callOnMessageError(wse, frames);
/** * Start both the reading thread and the writing thread. * * <p> * The reading thread will call {@link #onReadingThreadStarted()} * as its first step. Likewise, the writing thread will call * {@link #onWritingThreadStarted()} as its first step. After * both the threads have started, {@link #onThreadsStarted()} is * called. * </p> */ private void startThreads() { ReadingThread readingThread = new ReadingThread(this); WritingThread writingThread = new WritingThread(this); synchronized (mThreadsLock) { mReadingThread = readingThread; mWritingThread = writingThread; } // Execute onThreadCreated of the listeners. readingThread.callOnThreadCreated(); writingThread.callOnThreadCreated(); readingThread.start(); writingThread.start(); }
private boolean handleContinuationFrame(WebSocketFrame frame) callOnContinuationFrame(frame); byte[] data = getMessage(mContinuation); callOnTextMessage(data); callOnBinaryMessage(data);
private boolean handleBinaryFrame(WebSocketFrame frame) { // Notify the listeners that a binary frame was received. callOnBinaryFrame(frame); // If the frame indicates the start of fragmentation. if (frame.getFin() == false) { // Start a continuation sequence. mContinuation.add(frame); // Keep reading. return true; } // Get the payload of the frame. Decompression is performed // when necessary. byte[] payload = getMessage(frame); // Notify the listeners that a binary message was received. callOnBinaryMessage(payload); // Keep reading. return true; }