From 60598c5c7314593af8d5f90ba86c95200a45b1a7 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 13 Dec 2012 11:31:14 -0700 Subject: [PATCH] 396518 - Websocket AB Tests should test for which side disconnected and closed.wasClean + adding AbstractWebSocketConnection.onWriteWebSocketClose() to allow for different behavior between client vs server TCP disconnect logic. --- .../jetty/websocket/common/events/EventDriver.java | 14 ++++++++++---- .../common/io/AbstractWebSocketConnection.java | 14 ++++++++++---- .../eclipse/jetty/websocket/common/io/IOState.java | 8 +++----- .../server/WebSocketServerConnection.java | 8 ++++++++ .../server/WebSocketInvalidVersionTest.java | 2 +- .../server/blockhead/BlockheadClient.java | 4 ++++ 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/EventDriver.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/EventDriver.java index f297fd8fdf6..2206c7123f3 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/EventDriver.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/EventDriver.java @@ -103,11 +103,17 @@ public abstract class EventDriver implements IncomingFrames // notify user websocket pojo onClose(close); - // respond - session.close(close.getStatusCode(),close.getReason()); - // process handshake - session.getConnection().getIOState().onCloseHandshake(true,close); + if (session.getConnection().getIOState().onCloseHandshake(true,close)) + { + // handshake resolved, disconnect. + session.getConnection().disconnect(); + } + else + { + // respond + session.close(close.getStatusCode(),close.getReason()); + } return; } diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/io/AbstractWebSocketConnection.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/io/AbstractWebSocketConnection.java index 9c32f9c1eae..8973e532438 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/io/AbstractWebSocketConnection.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/io/AbstractWebSocketConnection.java @@ -169,10 +169,8 @@ public abstract class AbstractWebSocketConnection extends AbstractConnection imp if (frame.getType().getOpCode() == OpCode.CLOSE) { CloseInfo close = new CloseInfo(origPayload,false); - if (ioState.onCloseHandshake(false,close)) - { - disconnect(); - } + // TODO: change into DisconnectWebSocketCallback + onWriteWebSocketClose(close); } getBufferPool().release(origPayload); @@ -580,6 +578,14 @@ public abstract class AbstractWebSocketConnection extends AbstractConnection imp return true; } + public void onWriteWebSocketClose(CloseInfo close) + { + if (ioState.onCloseHandshake(false,close)) + { + disconnect(); + } + } + @Override public Future outgoingFrame(Frame frame) throws IOException { diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/io/IOState.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/io/IOState.java index 420a2b606fd..aa92630ff4a 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/io/IOState.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/io/IOState.java @@ -144,11 +144,9 @@ public class IOState return true; } - if (close.isHarsh()) - { - LOG.debug("Close status code was harsh, disconnecting"); - return true; - } + /* + * if (close.isHarsh()) { LOG.debug("Close status code was harsh, disconnecting"); return true; } + */ return false; } diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerConnection.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerConnection.java index 09d857544ff..48e30aa2e40 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerConnection.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerConnection.java @@ -26,6 +26,7 @@ import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.util.thread.Scheduler; import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.api.extensions.IncomingFrames; +import org.eclipse.jetty.websocket.common.CloseInfo; import org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection; public class WebSocketServerConnection extends AbstractWebSocketConnection @@ -71,6 +72,13 @@ public class WebSocketServerConnection extends AbstractWebSocketConnection super.onOpen(); } + @Override + public void onWriteWebSocketClose(CloseInfo close) + { + // as server, always disconnect if writing close + disconnect(); + } + @Override public void setNextIncomingFrames(IncomingFrames incoming) { diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketInvalidVersionTest.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketInvalidVersionTest.java index 2775dbb45f8..d9b42b19d0c 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketInvalidVersionTest.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketInvalidVersionTest.java @@ -62,7 +62,7 @@ public class WebSocketInvalidVersionTest } finally { - client.close(); + client.disconnect(); } } } diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java index 8c8d1765593..7d46f40f829 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java @@ -624,6 +624,10 @@ public class BlockheadClient implements IncomingFrames, OutgoingFrames public void write(WebSocketFrame frame) throws IOException { + if (!ioState.isOpen()) + { + return; + } LOG.debug("write(Frame->{}) to {}",frame,outgoing); if (LOG.isDebugEnabled()) {