Author: markt Date: Mon Feb 20 22:03:57 2012 New Revision: 1291480 URL: http://svn.apache.org/viewvc?rev=1291480&view=rev Log: Add close message support.
Modified: tomcat/trunk/java/org/apache/catalina/websocket/StreamInbound.java tomcat/trunk/java/org/apache/catalina/websocket/WsOutbound.java Modified: tomcat/trunk/java/org/apache/catalina/websocket/StreamInbound.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/websocket/StreamInbound.java?rev=1291480&r1=1291479&r2=1291480&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/websocket/StreamInbound.java (original) +++ tomcat/trunk/java/org/apache/catalina/websocket/StreamInbound.java Mon Feb 20 22:03:57 2012 @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.nio.ByteBuffer; import org.apache.coyote.http11.upgrade.UpgradeInbound; import org.apache.coyote.http11.upgrade.UpgradeOutbound; @@ -62,6 +63,9 @@ public abstract class StreamInbound impl InputStreamReader r = new InputStreamReader(wsIs, B2CConverter.UTF_8); onTextData(r); + } else if (opCode == Constants.OPCODE_CLOSE){ + doClose(wsIs); + return SocketState.CLOSED; } else { // TODO i18n throw new IOException("OpCode " + opCode + " not supported"); @@ -69,6 +73,30 @@ public abstract class StreamInbound impl return SocketState.UPGRADED; } + private void doClose(InputStream is) throws IOException { + // Control messages have a max size of 125 bytes + ByteBuffer data = ByteBuffer.allocate(125); + + int status1 = is.read(); + int status2 = 0; + System.out.println("" + status1); + if (status1 != -1) { + status1 = status1 << 8; + status2 = is.read(); + System.out.println("" + status2); + status1 = status1 + status2; + int read = 0; + while (read > -1) { + data.position(data.position() + read); + read = is.read(data.array(), data.position(), data.remaining()); + } + } else { + status1 = 0; + } + data.flip(); + getOutbound().close(status1, data); + } + protected abstract void onBinaryData(InputStream is) throws IOException; protected abstract void onTextData(Reader r) throws IOException; Modified: tomcat/trunk/java/org/apache/catalina/websocket/WsOutbound.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/websocket/WsOutbound.java?rev=1291480&r1=1291479&r2=1291480&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/websocket/WsOutbound.java (original) +++ tomcat/trunk/java/org/apache/catalina/websocket/WsOutbound.java Mon Feb 20 22:03:57 2012 @@ -30,6 +30,7 @@ public class WsOutbound { private UpgradeOutbound upgradeOutbound; private ByteBuffer bb; private CharBuffer cb; + private boolean closed = false; protected Boolean text = null; protected boolean firstFrame = true; @@ -110,16 +111,32 @@ public class WsOutbound { } - public void close() throws IOException { + public void close(int status, ByteBuffer data) throws IOException { + // TODO Think about threading requirements for writing. This is not + // currently thread safe and writing almost certainly needs to be. + if (closed) { + return; + } + closed = true; + doFlush(true); - // TODO: Send a close message + upgradeOutbound.write(0x88); + if (status == 0) { + upgradeOutbound.write(0); + } else { + upgradeOutbound.write(2 + data.limit()); + upgradeOutbound.write(status >>> 8); + upgradeOutbound.write(status); + upgradeOutbound.write(data.array(), 0, data.limit()); + } + upgradeOutbound.flush(); + bb = null; cb = null; upgradeOutbound = null; } - protected void doWriteBinary(ByteBuffer buffer, boolean finalFragment) throws IOException { --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org