Search in sources :

Example 1 with Framedata

use of org.java_websocket.framing.Framedata in project quorrabot by GloriousEggroll.

the class Draft_10 method translateFrame.

@Override
public List<Framedata> translateFrame(ByteBuffer buffer) throws LimitExedeedException, InvalidDataException {
    List<Framedata> frames = new LinkedList<Framedata>();
    Framedata cur;
    if (incompleteframe != null) {
        // complete an incomplete frame
        while (true) {
            try {
                buffer.mark();
                // The number of bytes received
                int available_next_byte_count = buffer.remaining();
                // The number of bytes to complete the incomplete frame
                int expected_next_byte_count = incompleteframe.remaining();
                if (expected_next_byte_count > available_next_byte_count) {
                    // did not receive enough bytes to complete the frame
                    incompleteframe.put(buffer.array(), buffer.position(), available_next_byte_count);
                    buffer.position(buffer.position() + available_next_byte_count);
                    return Collections.emptyList();
                }
                incompleteframe.put(buffer.array(), buffer.position(), expected_next_byte_count);
                buffer.position(buffer.position() + expected_next_byte_count);
                cur = translateSingleFrame((ByteBuffer) incompleteframe.duplicate().position(0));
                frames.add(cur);
                incompleteframe = null;
                // go on with the normal frame receival
                break;
            } catch (IncompleteException e) {
                // extending as much as suggested
                int oldsize = incompleteframe.limit();
                ByteBuffer extendedframe = ByteBuffer.allocate(checkAlloc(e.getPreferedSize()));
                assert (extendedframe.limit() > incompleteframe.limit());
                incompleteframe.rewind();
                extendedframe.put(incompleteframe);
                incompleteframe = extendedframe;
                return translateFrame(buffer);
            }
        }
    }
    while (buffer.hasRemaining()) {
        // Read as much as possible full frames
        buffer.mark();
        try {
            cur = translateSingleFrame(buffer);
            frames.add(cur);
        } catch (IncompleteException e) {
            // remember the incomplete data
            buffer.reset();
            int pref = e.getPreferedSize();
            incompleteframe = ByteBuffer.allocate(checkAlloc(pref));
            incompleteframe.put(buffer);
            break;
        }
    }
    return frames;
}
Also used : Framedata(org.java_websocket.framing.Framedata) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList)

Example 2 with Framedata

use of org.java_websocket.framing.Framedata in project quorrabot by GloriousEggroll.

the class WebSocketImpl method decodeFrames.

private void decodeFrames(ByteBuffer socketBuffer) {
    List<Framedata> frames;
    try {
        frames = draft.translateFrame(socketBuffer);
        for (Framedata f : frames) {
            if (DEBUG) {
                System.out.println("matched frame: " + f);
            }
            Opcode curop = f.getOpcode();
            boolean fin = f.isFin();
            if (curop == Opcode.CLOSING) {
                int code = CloseFrame.NOCODE;
                String reason = "";
                if (f instanceof CloseFrame) {
                    CloseFrame cf = (CloseFrame) f;
                    code = cf.getCloseCode();
                    reason = cf.getMessage();
                }
                if (readystate == READYSTATE.CLOSING) {
                    // complete the close handshake by disconnecting
                    closeConnection(code, reason, true);
                } else // echo close handshake
                if (draft.getCloseHandshakeType() == CloseHandshakeType.TWOWAY) {
                    close(code, reason, true);
                } else {
                    flushAndClose(code, reason, false);
                }
                continue;
            } else if (curop == Opcode.PING) {
                wsl.onWebsocketPing(this, f);
                continue;
            } else if (curop == Opcode.PONG) {
                wsl.onWebsocketPong(this, f);
                continue;
            } else if (!fin || curop == Opcode.CONTINUOUS) {
                if (curop != Opcode.CONTINUOUS) {
                    if (current_continuous_frame_opcode != null) {
                        throw new InvalidDataException(CloseFrame.PROTOCOL_ERROR, "Previous continuous frame sequence not completed.");
                    }
                    current_continuous_frame_opcode = curop;
                } else if (fin) {
                    if (current_continuous_frame_opcode == null) {
                        throw new InvalidDataException(CloseFrame.PROTOCOL_ERROR, "Continuous frame sequence was not started.");
                    }
                    current_continuous_frame_opcode = null;
                } else if (current_continuous_frame_opcode == null) {
                    throw new InvalidDataException(CloseFrame.PROTOCOL_ERROR, "Continuous frame sequence was not started.");
                }
                try {
                    wsl.onWebsocketMessageFragment(this, f);
                } catch (RuntimeException e) {
                    wsl.onWebsocketError(this, e);
                }
            } else if (current_continuous_frame_opcode != null) {
                throw new InvalidDataException(CloseFrame.PROTOCOL_ERROR, "Continuous frame sequence not completed.");
            } else if (curop == Opcode.TEXT) {
                try {
                    wsl.onWebsocketMessage(this, Charsetfunctions.stringUtf8(f.getPayloadData()));
                } catch (RuntimeException e) {
                    wsl.onWebsocketError(this, e);
                }
            } else if (curop == Opcode.BINARY) {
                try {
                    wsl.onWebsocketMessage(this, f.getPayloadData());
                } catch (RuntimeException e) {
                    wsl.onWebsocketError(this, e);
                }
            } else {
                throw new InvalidDataException(CloseFrame.PROTOCOL_ERROR, "non control or continious frame expected");
            }
        }
    } catch (InvalidDataException e1) {
        wsl.onWebsocketError(this, e1);
        close(e1);
        return;
    }
}
Also used : Framedata(org.java_websocket.framing.Framedata) InvalidDataException(org.java_websocket.exceptions.InvalidDataException) Opcode(org.java_websocket.framing.Framedata.Opcode) CloseFrame(org.java_websocket.framing.CloseFrame)

Example 3 with Framedata

use of org.java_websocket.framing.Framedata in project quorrabot by GloriousEggroll.

the class Draft_75 method translateRegularFrame.

protected List<Framedata> translateRegularFrame(ByteBuffer buffer) throws InvalidDataException {
    while (buffer.hasRemaining()) {
        byte newestByte = buffer.get();
        if (newestByte == START_OF_FRAME) {
            // Beginning of Frame
            if (readingState) {
                throw new InvalidFrameException("unexpected START_OF_FRAME");
            }
            readingState = true;
        } else if (newestByte == END_OF_FRAME) {
            // End of Frame
            if (!readingState) {
                throw new InvalidFrameException("unexpected END_OF_FRAME");
            }
            // START_OF_FRAME, thus we will send 'null' as the sent message.
            if (this.currentFrame != null) {
                currentFrame.flip();
                FramedataImpl1 curframe = new FramedataImpl1();
                curframe.setPayload(currentFrame);
                curframe.setFin(true);
                curframe.setOptcode(Opcode.TEXT);
                readyframes.add(curframe);
                this.currentFrame = null;
                buffer.mark();
            }
            readingState = false;
        } else if (readingState) {
            // Regular frame data, add to current frame buffer //TODO This code is very expensive and slow
            if (currentFrame == null) {
                currentFrame = createBuffer();
            } else if (!currentFrame.hasRemaining()) {
                currentFrame = increaseBuffer(currentFrame);
            }
            currentFrame.put(newestByte);
        } else {
            return null;
        }
    }
    // if no error occurred this block will be reached
    /*if( readingState ) {
			checkAlloc(currentFrame.position()+1);
		}*/
    List<Framedata> frames = readyframes;
    readyframes = new LinkedList<Framedata>();
    return frames;
}
Also used : Framedata(org.java_websocket.framing.Framedata) InvalidFrameException(org.java_websocket.exceptions.InvalidFrameException) FramedataImpl1(org.java_websocket.framing.FramedataImpl1)

Example 4 with Framedata

use of org.java_websocket.framing.Framedata in project quorrabot by GloriousEggroll.

the class Draft_76 method translateFrame.

@Override
public List<Framedata> translateFrame(ByteBuffer buffer) throws InvalidDataException {
    buffer.mark();
    List<Framedata> frames = super.translateRegularFrame(buffer);
    if (frames == null) {
        buffer.reset();
        frames = readyframes;
        readingState = true;
        if (currentFrame == null) {
            currentFrame = ByteBuffer.allocate(2);
        } else {
            throw new InvalidFrameException();
        }
        if (buffer.remaining() > currentFrame.remaining()) {
            throw new InvalidFrameException();
        } else {
            currentFrame.put(buffer);
        }
        if (!currentFrame.hasRemaining()) {
            if (Arrays.equals(currentFrame.array(), closehandshake)) {
                frames.add(new CloseFrameBuilder(CloseFrame.NORMAL));
                return frames;
            } else {
                throw new InvalidFrameException();
            }
        } else {
            readyframes = new LinkedList<Framedata>();
            return frames;
        }
    } else {
        return frames;
    }
}
Also used : Framedata(org.java_websocket.framing.Framedata) CloseFrameBuilder(org.java_websocket.framing.CloseFrameBuilder) InvalidFrameException(org.java_websocket.exceptions.InvalidFrameException)

Aggregations

Framedata (org.java_websocket.framing.Framedata)4 InvalidFrameException (org.java_websocket.exceptions.InvalidFrameException)2 ByteBuffer (java.nio.ByteBuffer)1 LinkedList (java.util.LinkedList)1 InvalidDataException (org.java_websocket.exceptions.InvalidDataException)1 CloseFrame (org.java_websocket.framing.CloseFrame)1 CloseFrameBuilder (org.java_websocket.framing.CloseFrameBuilder)1 Opcode (org.java_websocket.framing.Framedata.Opcode)1 FramedataImpl1 (org.java_websocket.framing.FramedataImpl1)1