use of org.java_websocket.exceptions.InvalidDataException 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;
}
}
use of org.java_websocket.exceptions.InvalidDataException 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);
}
use of org.java_websocket.exceptions.InvalidDataException 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);
}
use of org.java_websocket.exceptions.InvalidDataException in project quorrabot by GloriousEggroll.
the class CloseFrameBuilder method setCodeAndMessage.
private void setCodeAndMessage(int code, String m) throws InvalidDataException {
if (m == null) {
m = "";
}
// CloseFrame.TLS_ERROR is not allowed to be transfered over the wire
if (code == CloseFrame.TLS_ERROR) {
code = CloseFrame.NOCODE;
m = "";
}
if (code == CloseFrame.NOCODE) {
if (0 < m.length()) {
throw new InvalidDataException(PROTOCOL_ERROR, "A close frame must have a closecode if it has a reason");
}
// empty payload
return;
}
byte[] by = Charsetfunctions.utf8Bytes(m);
ByteBuffer buf = ByteBuffer.allocate(4);
buf.putInt(code);
buf.position(2);
ByteBuffer pay = ByteBuffer.allocate(2 + by.length);
pay.put(buf);
pay.put(by);
pay.rewind();
setPayload(pay);
}
use of org.java_websocket.exceptions.InvalidDataException in project quorrabot by GloriousEggroll.
the class Charsetfunctions method stringUtf8.
/*public static String stringUtf8( byte[] bytes, int off, int length ) throws InvalidDataException {
CharsetDecoder decode = Charset.forName( "UTF8" ).newDecoder();
decode.onMalformedInput( codingErrorAction );
decode.onUnmappableCharacter( codingErrorAction );
//decode.replaceWith( "X" );
String s;
try {
s = decode.decode( ByteBuffer.wrap( bytes, off, length ) ).toString();
} catch ( CharacterCodingException e ) {
throw new InvalidDataException( CloseFrame.NO_UTF8, e );
}
return s;
}*/
public static String stringUtf8(ByteBuffer bytes) throws InvalidDataException {
CharsetDecoder decode = Charset.forName("UTF8").newDecoder();
decode.onMalformedInput(codingErrorAction);
decode.onUnmappableCharacter(codingErrorAction);
// decode.replaceWith( "X" );
String s;
try {
bytes.mark();
s = decode.decode(bytes).toString();
bytes.reset();
} catch (CharacterCodingException e) {
throw new InvalidDataException(CloseFrame.NO_UTF8, e);
}
return s;
}
Aggregations