use of org.java_websocket.framing.CloseFrameBuilder in project quorrabot by GloriousEggroll.
the class WebSocketImpl method close.
private void close(int code, String message, boolean remote) {
if (readystate != READYSTATE.CLOSING && readystate != READYSTATE.CLOSED) {
if (readystate == READYSTATE.OPEN) {
if (code == CloseFrame.ABNORMAL_CLOSE) {
assert (remote == false);
readystate = READYSTATE.CLOSING;
flushAndClose(code, message, false);
return;
}
if (draft.getCloseHandshakeType() != CloseHandshakeType.NONE) {
try {
if (!remote) {
try {
wsl.onWebsocketCloseInitiated(this, code, message);
} catch (RuntimeException e) {
wsl.onWebsocketError(this, e);
}
}
sendFrame(new CloseFrameBuilder(code, message));
} catch (InvalidDataException e) {
wsl.onWebsocketError(this, e);
flushAndClose(CloseFrame.ABNORMAL_CLOSE, "generated frame is invalid", false);
}
}
flushAndClose(code, message, remote);
} else if (code == CloseFrame.FLASHPOLICY) {
assert (remote);
flushAndClose(CloseFrame.FLASHPOLICY, message, true);
} else {
flushAndClose(CloseFrame.NEVER_CONNECTED, message, false);
}
if (// this endpoint found a PROTOCOL_ERROR
code == CloseFrame.PROTOCOL_ERROR) {
flushAndClose(code, message, remote);
}
readystate = READYSTATE.CLOSING;
tmpHandshakeBytes = null;
return;
}
}
use of org.java_websocket.framing.CloseFrameBuilder 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.CloseFrameBuilder 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;
}
}
Aggregations