Search in sources :

Example 1 with Opcode

use of org.java_websocket.framing.Framedata.Opcode 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 2 with Opcode

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

the class Draft_10 method translateSingleFrame.

public Framedata translateSingleFrame(ByteBuffer buffer) throws IncompleteException, InvalidDataException {
    int maxpacketsize = buffer.remaining();
    int realpacketsize = 2;
    if (maxpacketsize < realpacketsize) {
        throw new IncompleteException(realpacketsize);
    }
    byte b1 = buffer.get();
    boolean FIN = b1 >> 8 != 0;
    byte rsv = (byte) ((b1 & ~(byte) 128) >> 4);
    if (rsv != 0) {
        throw new InvalidFrameException("bad rsv " + rsv);
    }
    byte b2 = buffer.get();
    boolean MASK = (b2 & -128) != 0;
    int payloadlength = (byte) (b2 & ~(byte) 128);
    Opcode optcode = toOpcode((byte) (b1 & 15));
    if (!FIN) {
        if (optcode == Opcode.PING || optcode == Opcode.PONG || optcode == Opcode.CLOSING) {
            throw new InvalidFrameException("control frames may no be fragmented");
        }
    }
    if (payloadlength >= 0 && payloadlength <= 125) {
    } else {
        if (optcode == Opcode.PING || optcode == Opcode.PONG || optcode == Opcode.CLOSING) {
            throw new InvalidFrameException("more than 125 octets");
        }
        if (payloadlength == 126) {
            // additional length bytes
            realpacketsize += 2;
            if (maxpacketsize < realpacketsize) {
                throw new IncompleteException(realpacketsize);
            }
            byte[] sizebytes = new byte[3];
            sizebytes[1] = buffer.get();
            sizebytes[2] = buffer.get();
            payloadlength = new BigInteger(sizebytes).intValue();
        } else {
            // additional length bytes
            realpacketsize += 8;
            if (maxpacketsize < realpacketsize) {
                throw new IncompleteException(realpacketsize);
            }
            byte[] bytes = new byte[8];
            for (int i = 0; i < 8; i++) {
                bytes[i] = buffer.get();
            }
            long length = new BigInteger(bytes).longValue();
            if (length > Integer.MAX_VALUE) {
                throw new LimitExedeedException("Payloadsize is to big...");
            } else {
                payloadlength = (int) length;
            }
        }
    }
    // int maskskeystart = foff + realpacketsize;
    realpacketsize += (MASK ? 4 : 0);
    // int payloadstart = foff + realpacketsize;
    realpacketsize += payloadlength;
    if (maxpacketsize < realpacketsize) {
        throw new IncompleteException(realpacketsize);
    }
    ByteBuffer payload = ByteBuffer.allocate(checkAlloc(payloadlength));
    if (MASK) {
        byte[] maskskey = new byte[4];
        buffer.get(maskskey);
        for (int i = 0; i < payloadlength; i++) {
            payload.put((byte) ((byte) buffer.get() ^ (byte) maskskey[i % 4]));
        }
    } else {
        payload.put(buffer.array(), buffer.position(), payload.limit());
        buffer.position(buffer.position() + payload.limit());
    }
    FrameBuilder frame;
    if (optcode == Opcode.CLOSING) {
        frame = new CloseFrameBuilder();
    } else {
        frame = new FramedataImpl1();
        frame.setFin(FIN);
        frame.setOptcode(optcode);
    }
    payload.flip();
    frame.setPayload(payload);
    return frame;
}
Also used : Opcode(org.java_websocket.framing.Framedata.Opcode) CloseFrameBuilder(org.java_websocket.framing.CloseFrameBuilder) InvalidFrameException(org.java_websocket.exceptions.InvalidFrameException) ByteBuffer(java.nio.ByteBuffer) LimitExedeedException(org.java_websocket.exceptions.LimitExedeedException) BigInteger(java.math.BigInteger) CloseFrameBuilder(org.java_websocket.framing.CloseFrameBuilder) FrameBuilder(org.java_websocket.framing.FrameBuilder) FramedataImpl1(org.java_websocket.framing.FramedataImpl1)

Aggregations

Opcode (org.java_websocket.framing.Framedata.Opcode)2 BigInteger (java.math.BigInteger)1 ByteBuffer (java.nio.ByteBuffer)1 InvalidDataException (org.java_websocket.exceptions.InvalidDataException)1 InvalidFrameException (org.java_websocket.exceptions.InvalidFrameException)1 LimitExedeedException (org.java_websocket.exceptions.LimitExedeedException)1 CloseFrame (org.java_websocket.framing.CloseFrame)1 CloseFrameBuilder (org.java_websocket.framing.CloseFrameBuilder)1 FrameBuilder (org.java_websocket.framing.FrameBuilder)1 Framedata (org.java_websocket.framing.Framedata)1 FramedataImpl1 (org.java_websocket.framing.FramedataImpl1)1