Author: remm Date: Mon Dec 1 22:35:30 2014 New Revision: 1642773 URL: http://svn.apache.org/r1642773 Log: Move the offending code to the server. Avoid sending a message when nothing has actually been written.
Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java tomcat/trunk/java/org/apache/tomcat/websocket/WsRemoteEndpointImplBase.java Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java?rev=1642773&r1=1642772&r2=1642773&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java Mon Dec 1 22:35:30 2014 @@ -390,10 +390,8 @@ public abstract class WsFrameBase { messageBufferText.toString(), last); } else { // Caller ensures last == true if this branch is used - if (messageBufferText.remaining() > 0) { - ((MessageHandler.Whole<String>) textMsgHandler).onMessage( - messageBufferText.toString()); - } + ((MessageHandler.Whole<String>) textMsgHandler).onMessage( + messageBufferText.toString()); } } catch (Throwable t) { handleThrowableOnSend(t); @@ -585,9 +583,7 @@ public abstract class WsFrameBase { ((MessageHandler.Partial<ByteBuffer>) binaryMsgHandler).onMessage(msg, last); } else { // Caller ensures last == true if this branch is used - if (msg.remaining() > 0) { - ((MessageHandler.Whole<ByteBuffer>) binaryMsgHandler).onMessage(msg); - } + ((MessageHandler.Whole<ByteBuffer>) binaryMsgHandler).onMessage(msg); } } catch(Throwable t) { handleThrowableOnSend(t); Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsRemoteEndpointImplBase.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsRemoteEndpointImplBase.java?rev=1642773&r1=1642772&r2=1642773&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/WsRemoteEndpointImplBase.java (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/WsRemoteEndpointImplBase.java Mon Dec 1 22:35:30 2014 @@ -875,12 +875,13 @@ public abstract class WsRemoteEndpointIm } - private class WsOutputStream extends OutputStream { + private static class WsOutputStream extends OutputStream { private final WsRemoteEndpointImplBase endpoint; private final ByteBuffer buffer = ByteBuffer.allocate(Constants.DEFAULT_BUFFER_SIZE); private final Object closeLock = new Object(); private volatile boolean closed = false; + private volatile boolean used = false; public WsOutputStream(WsRemoteEndpointImplBase endpoint) { this.endpoint = endpoint; @@ -913,6 +914,7 @@ public abstract class WsRemoteEndpointIm throw new IndexOutOfBoundsException(); } + used = true; if (buffer.remaining() == 0) { flush(); } @@ -951,9 +953,11 @@ public abstract class WsRemoteEndpointIm } private void doWrite(boolean last) throws IOException { - buffer.flip(); - endpoint.startMessageBlock(Constants.OPCODE_BINARY, buffer, last); - stateMachine.complete(last); + if (used) { + buffer.flip(); + endpoint.startMessageBlock(Constants.OPCODE_BINARY, buffer, last); + } + endpoint.stateMachine.complete(last); buffer.clear(); } } @@ -965,6 +969,7 @@ public abstract class WsRemoteEndpointIm private final CharBuffer buffer = CharBuffer.allocate(Constants.DEFAULT_BUFFER_SIZE); private final Object closeLock = new Object(); private volatile boolean closed = false; + private volatile boolean used = false; public WsWriter(WsRemoteEndpointImplBase endpoint) { this.endpoint = endpoint; @@ -984,6 +989,7 @@ public abstract class WsRemoteEndpointIm throw new IndexOutOfBoundsException(); } + used = true; if (buffer.remaining() == 0) { flush(); } @@ -1022,9 +1028,13 @@ public abstract class WsRemoteEndpointIm } private void doWrite(boolean last) throws IOException { - buffer.flip(); - endpoint.sendPartialString(buffer, last); - buffer.clear(); + if (used) { + buffer.flip(); + endpoint.sendPartialString(buffer, last); + buffer.clear(); + } else { + endpoint.stateMachine.complete(last); + } } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org