Search in sources :

Example 6 with FramedataImpl1

use of org.java_websocket.framing.FramedataImpl1 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 7 with FramedataImpl1

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

the class Draft_75 method createFrames.

@Override
public List<Framedata> createFrames(String text, boolean mask) {
    FrameBuilder frame = new FramedataImpl1();
    try {
        frame.setPayload(ByteBuffer.wrap(Charsetfunctions.utf8Bytes(text)));
    } catch (InvalidDataException e) {
        throw new NotSendableException(e);
    }
    frame.setFin(true);
    frame.setOptcode(Opcode.TEXT);
    frame.setTransferemasked(mask);
    return Collections.singletonList((Framedata) frame);
}
Also used : InvalidDataException(org.java_websocket.exceptions.InvalidDataException) FrameBuilder(org.java_websocket.framing.FrameBuilder) NotSendableException(org.java_websocket.exceptions.NotSendableException) FramedataImpl1(org.java_websocket.framing.FramedataImpl1)

Aggregations

FramedataImpl1 (org.java_websocket.framing.FramedataImpl1)7 FrameBuilder (org.java_websocket.framing.FrameBuilder)5 InvalidDataException (org.java_websocket.exceptions.InvalidDataException)4 NotSendableException (org.java_websocket.exceptions.NotSendableException)3 CloseFrameBuilder (org.java_websocket.framing.CloseFrameBuilder)3 InvalidFrameException (org.java_websocket.exceptions.InvalidFrameException)2 BigInteger (java.math.BigInteger)1 ByteBuffer (java.nio.ByteBuffer)1 LimitExedeedException (org.java_websocket.exceptions.LimitExedeedException)1 Framedata (org.java_websocket.framing.Framedata)1 Opcode (org.java_websocket.framing.Framedata.Opcode)1