use of org.java_websocket.framing.FramedataImpl1 in project quorrabot by GloriousEggroll.
the class Draft method continuousFrame.
public List<Framedata> continuousFrame(Opcode op, ByteBuffer buffer, boolean fin) {
if (op != Opcode.BINARY && op != Opcode.TEXT && op != Opcode.TEXT) {
throw new IllegalArgumentException("Only Opcode.BINARY or Opcode.TEXT are allowed");
}
if (continuousFrameType != null) {
continuousFrameType = Opcode.CONTINUOUS;
} else {
continuousFrameType = op;
}
FrameBuilder bui = new FramedataImpl1(continuousFrameType);
try {
bui.setPayload(buffer);
} catch (InvalidDataException e) {
// can only happen when one builds close frames(Opcode.Close)
throw new RuntimeException(e);
}
bui.setFin(fin);
if (fin) {
continuousFrameType = null;
} else {
continuousFrameType = op;
}
return Collections.singletonList((Framedata) bui);
}
use of org.java_websocket.framing.FramedataImpl1 in project quorrabot by GloriousEggroll.
the class Draft_10 method createFrames.
@Override
public List<Framedata> createFrames(String text, boolean mask) {
FrameBuilder curframe = new FramedataImpl1();
try {
curframe.setPayload(ByteBuffer.wrap(Charsetfunctions.utf8Bytes(text)));
} catch (InvalidDataException e) {
throw new NotSendableException(e);
}
curframe.setFin(true);
curframe.setOptcode(Opcode.TEXT);
curframe.setTransferemasked(mask);
return Collections.singletonList((Framedata) curframe);
}
use of org.java_websocket.framing.FramedataImpl1 in project quorrabot by GloriousEggroll.
the class WebSocketAdapter method onWebsocketPing.
/**
* This default implementation will send a pong in response to the received
* ping. The pong frame will have the same payload as the ping frame.
*
* @see org.java_websocket.WebSocketListener#onWebsocketPing(WebSocket,
* Framedata)
*/
@Override
public void onWebsocketPing(WebSocket conn, Framedata f) {
FramedataImpl1 resp = new FramedataImpl1(f);
resp.setOptcode(Opcode.PONG);
conn.sendFrame(resp);
}
use of org.java_websocket.framing.FramedataImpl1 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;
}
use of org.java_websocket.framing.FramedataImpl1 in project quorrabot by GloriousEggroll.
the class Draft_10 method createFrames.
@Override
public List<Framedata> createFrames(ByteBuffer binary, boolean mask) {
FrameBuilder curframe = new FramedataImpl1();
try {
curframe.setPayload(binary);
} catch (InvalidDataException e) {
throw new NotSendableException(e);
}
curframe.setFin(true);
curframe.setOptcode(Opcode.BINARY);
curframe.setTransferemasked(mask);
return Collections.singletonList((Framedata) curframe);
}
Aggregations