use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.
the class AbstractEventDriver method incomingFrame.
@Override
public void incomingFrame(Frame frame) {
if (LOG.isDebugEnabled()) {
LOG.debug("incomingFrame({})", frame);
}
try {
onFrame(frame);
byte opcode = frame.getOpCode();
switch(opcode) {
case OpCode.CLOSE:
{
boolean validate = true;
CloseFrame closeframe = (CloseFrame) frame;
CloseInfo close = new CloseInfo(closeframe, validate);
// process handshake
session.getConnection().getIOState().onCloseRemote(close);
return;
}
case OpCode.PING:
{
if (LOG.isDebugEnabled()) {
LOG.debug("PING: {}", BufferUtil.toDetailString(frame.getPayload()));
}
ByteBuffer pongBuf;
if (frame.hasPayload()) {
pongBuf = ByteBuffer.allocate(frame.getPayload().remaining());
BufferUtil.put(frame.getPayload().slice(), pongBuf);
BufferUtil.flipToFlush(pongBuf, 0);
} else {
pongBuf = ByteBuffer.allocate(0);
}
onPing(frame.getPayload());
session.getRemote().sendPong(pongBuf);
break;
}
case OpCode.PONG:
{
if (LOG.isDebugEnabled()) {
LOG.debug("PONG: {}", BufferUtil.toDetailString(frame.getPayload()));
}
onPong(frame.getPayload());
break;
}
case OpCode.BINARY:
{
onBinaryFrame(frame.getPayload(), frame.isFin());
return;
}
case OpCode.TEXT:
{
onTextFrame(frame.getPayload(), frame.isFin());
return;
}
case OpCode.CONTINUATION:
{
onContinuationFrame(frame.getPayload(), frame.isFin());
return;
}
default:
{
if (LOG.isDebugEnabled())
LOG.debug("Unhandled OpCode: {}", opcode);
}
}
} catch (NotUtf8Exception e) {
terminateConnection(StatusCode.BAD_PAYLOAD, e.getMessage());
} catch (CloseException e) {
terminateConnection(e.getStatusCode(), e.getMessage());
} catch (Throwable t) {
unhandled(t);
}
}
use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.
the class IOState method onDisconnected.
public void onDisconnected() {
ConnectionState event = null;
synchronized (this) {
if (this.state == ConnectionState.CLOSED) {
// already closed
return;
}
CloseInfo close = new CloseInfo(StatusCode.ABNORMAL, "Disconnected");
this.cleanClose = false;
this.state = ConnectionState.CLOSED;
this.closeInfo = close;
this.inputAvailable = false;
this.outputAvailable = false;
this.closeHandshakeSource = CloseHandshakeSource.ABNORMAL;
event = this.state;
}
notifyStateListeners(event);
}
use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.
the class IOState method onWriteFailure.
/**
* The local endpoint has reached a write failure.
* <p>
* A low level I/O failure, or even a jetty side EndPoint close (from idle timeout) are a few scenarios
* @param t the throwable that caused the write failure
*/
public void onWriteFailure(Throwable t) {
ConnectionState event = null;
synchronized (this) {
if (this.state == ConnectionState.CLOSED) {
// already closed
return;
}
// Build out Close Reason
String reason = "WebSocket Write Failure";
if (t instanceof EOFException) {
reason = "WebSocket Write EOF";
Throwable cause = t.getCause();
if ((cause != null) && (StringUtil.isNotBlank(cause.getMessage()))) {
reason = "EOF: " + cause.getMessage();
}
} else {
if (StringUtil.isNotBlank(t.getMessage())) {
reason = t.getMessage();
}
}
CloseInfo close = new CloseInfo(StatusCode.ABNORMAL, reason);
finalClose.compareAndSet(null, close);
this.cleanClose = false;
this.state = ConnectionState.CLOSED;
this.inputAvailable = false;
this.outputAvailable = false;
this.closeHandshakeSource = CloseHandshakeSource.ABNORMAL;
event = this.state;
}
notifyStateListeners(event);
}
use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.
the class IOState method onReadFailure.
/**
* The local endpoint has reached a read failure.
* <p>
* This could be a normal result after a proper close handshake, or even a premature close due to a connection disconnect.
* @param t the read failure
*/
public void onReadFailure(Throwable t) {
ConnectionState event = null;
synchronized (this) {
if (this.state == ConnectionState.CLOSED) {
// already closed
return;
}
// Build out Close Reason
String reason = "WebSocket Read Failure";
if (t instanceof EOFException) {
reason = "WebSocket Read EOF";
Throwable cause = t.getCause();
if ((cause != null) && (StringUtil.isNotBlank(cause.getMessage()))) {
reason = "EOF: " + cause.getMessage();
}
} else {
if (StringUtil.isNotBlank(t.getMessage())) {
reason = t.getMessage();
}
}
CloseInfo close = new CloseInfo(StatusCode.ABNORMAL, reason);
finalClose.compareAndSet(null, close);
this.cleanClose = false;
this.state = ConnectionState.CLOSED;
this.closeInfo = close;
this.inputAvailable = false;
this.outputAvailable = false;
this.closeHandshakeSource = CloseHandshakeSource.ABNORMAL;
event = this.state;
}
notifyStateListeners(event);
}
use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.
the class TestABCase5 method testCase5_13.
/**
* Send continuation+!fin, then text+fin (framewise)
* @throws Exception on test failure
*/
@Test
public void testCase5_13() throws Exception {
List<WebSocketFrame> send = new ArrayList<>();
send.add(new ContinuationFrame().setPayload("sorry").setFin(false));
send.add(new TextFrame().setPayload("hello, world"));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
try (Fuzzer fuzzer = new Fuzzer(this);
StacklessLogging supress = new StacklessLogging(Parser.class)) {
fuzzer.connect();
fuzzer.setSendMode(Fuzzer.SendMode.PER_FRAME);
fuzzer.sendAndIgnoreBrokenPipe(send);
fuzzer.expect(expect);
}
}
Aggregations