From 3fa89ea5774390e4e74038b14d5a8a87afded3ae Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Fri, 29 Jun 2012 13:00:08 -0700 Subject: [PATCH] Fixing buffer flip logic --- .../eclipse/jetty/websocket/api/EventMethod.java | 3 ++- .../jetty/websocket/generator/Generator.java | 5 +++++ .../websocket/server/AsyncWebSocketConnection.java | 14 +++++++++++++- .../websocket/server/WebSocketServerFactory.java | 11 +++++------ .../server/blockhead/BlockheadClient.java | 10 +++++++++- .../src/test/resources/jetty-logging.properties | 1 + 6 files changed, 35 insertions(+), 9 deletions(-) diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethod.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethod.java index a43bf1aff11..8cb030d7143 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethod.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/EventMethod.java @@ -100,7 +100,8 @@ public class EventMethod } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - LOG.warn("Cannot call method {} on {} with {}",method,pojo,args,e); + LOG.warn("Cannot call method {} on {} with {}",method,pojo,args); + LOG.warn(e); } } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/Generator.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/Generator.java index 2f6fa7b02c3..9a894fb570e 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/Generator.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/Generator.java @@ -3,6 +3,7 @@ package org.eclipse.jetty.websocket.generator; import java.nio.ByteBuffer; import java.util.EnumMap; +import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.websocket.api.OpCode; @@ -52,6 +53,10 @@ public class Generator { "unchecked", "rawtypes" }) public ByteBuffer generate(ByteBuffer buffer, BaseFrame frame) { + if (LOG.isDebugEnabled()) + { + LOG.debug("Buffer: {}",BufferUtil.toDetailString(buffer)); + } FrameGenerator generator = generators.get(frame.getOpCode()); LOG.debug(generator.getClass().getSimpleName() + " active"); return generator.generate(buffer,frame); diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/AsyncWebSocketConnection.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/AsyncWebSocketConnection.java index cea66b0432b..8ea3f48e841 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/AsyncWebSocketConnection.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/AsyncWebSocketConnection.java @@ -240,9 +240,15 @@ public class AsyncWebSocketConnection extends AbstractAsyncConnection implements LOG.debug("write(BaseFrame->{})",frame); } - ByteBuffer raw = ByteBuffer.allocate(frame.getPayloadLength() + FrameGenerator.OVERHEAD); + ByteBuffer raw = bufferPool.acquire(frame.getPayloadLength() + FrameGenerator.OVERHEAD,false); + BufferUtil.clearToFill(raw); generator.generate(raw,frame); + BufferUtil.flipToFlush(raw,0); Callback nop = new FutureCallback<>(); // TODO: add buffer release callback? + if (LOG.isDebugEnabled()) + { + LOG.debug("Raw Buffer: {}",BufferUtil.toDetailString(raw)); + } getEndPoint().write(null,nop,raw); } @@ -273,8 +279,10 @@ public class AsyncWebSocketConnection extends AbstractAsyncConnection implements for (int i = 0; i < len; i++) { raw[i] = bufferPool.acquire(buffers[i].remaining() + FrameGenerator.OVERHEAD,false); + BufferUtil.clearToFill(raw[i]); BinaryFrame frame = new BinaryFrame(buffers[i]); generator.generate(raw[i],frame); + BufferUtil.flipToFlush(raw[i],0); } Callback nop = new FutureCallback<>(); // TODO: add buffer release callback? getEndPoint().write(null,nop,raw); @@ -297,7 +305,9 @@ public class AsyncWebSocketConnection extends AbstractAsyncConnection implements for (int i = 0; i < len; i++) { raw[i] = bufferPool.acquire(frames[i].getPayloadLength() + FrameGenerator.OVERHEAD,false); + BufferUtil.clearToFill(raw[i]); generator.generate(raw[i],frames[i]); + BufferUtil.flipToFlush(raw[i],0); } getEndPoint().write(context,callback,raw); } @@ -319,8 +329,10 @@ public class AsyncWebSocketConnection extends AbstractAsyncConnection implements for (int i = 0; i < len; i++) { raw[i] = bufferPool.acquire(buffers[i].remaining() + FrameGenerator.OVERHEAD,false); + BufferUtil.clearToFill(raw[i]); BinaryFrame frame = new BinaryFrame(buffers[i]); generator.generate(raw[i],frame); + BufferUtil.flipToFlush(raw[i],0); } getEndPoint().write(context,callback,raw); } diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java index cda156c4573..dbc57091bca 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java @@ -342,11 +342,8 @@ public class WebSocketServerFactory extends AbstractLifeCycle implements WebSock endp.setAsyncConnection(connection); connection.getParser().addListener(websocket); - LOG.debug("EndPoint: {}",endp); - LOG.debug("Connection: {}",connection); - - // Notify POJO of connection - websocket.setConnection(connection); + LOG.debug("HttpConnection: {}",http); + LOG.debug("AsyncWebSocketConnection: {}",connection); // Initialize / Negotiate Extensions List extensions = initExtensions(request.getExtensions()); @@ -355,12 +352,14 @@ public class WebSocketServerFactory extends AbstractLifeCycle implements WebSock LOG.debug("Handshake Response: {}",handshaker); handshaker.doHandshakeResponse(request,response,extensions); connection.fillInterested(); + LOG.debug("EndPoint: {}",endp); LOG.debug("Handshake Complete: {}",connection); // Add connection addConnection(connection); - // Notify POJO of connect + // Notify POJO of connection + websocket.setConnection(connection); websocket.onConnect(); // Tell jetty about the new connection 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 e3baf65dc73..1ee6fa493ef 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 @@ -174,6 +174,7 @@ public class BlockheadClient implements Parser.Listener @Override public void onFrame(BaseFrame frame) { + LOG.debug("onFrame({})",frame); if (!incomingFrameQueue.offerLast(frame)) { throw new RuntimeException("Unable to queue incoming frame: " + frame); @@ -204,12 +205,17 @@ public class BlockheadClient implements Parser.Listener long expireOn = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(timeoutDuration,timeoutUnit); ByteBuffer buf = bufferPool.acquire(policy.getBufferSize(),false); + BufferUtil.clearToFill(buf); try { + int len = 0; while (incomingFrameQueue.size() < (startCount + expectedCount)) { - if (read(buf) > 0) + len = read(buf); + if (len > 0) { + LOG.debug("Read {} bytes",len); + BufferUtil.flipToFlush(buf,0); parser.parse(buf); } if (System.currentTimeMillis() > expireOn) @@ -318,6 +324,7 @@ public class BlockheadClient implements Parser.Listener public void write(BaseFrame frame) throws IOException { + LOG.debug("write(BaseFrame->{})",frame); ByteBuffer buf = bufferPool.acquire(policy.getBufferSize(),false); try { @@ -334,6 +341,7 @@ public class BlockheadClient implements Parser.Listener public void write(String str) throws IOException { + LOG.debug("write(String->{})",str); out.write(StringUtil.getBytes(str,StringUtil.__ISO_8859_1)); } } diff --git a/jetty-websocket/websocket-server/src/test/resources/jetty-logging.properties b/jetty-websocket/websocket-server/src/test/resources/jetty-logging.properties index 97212a44cd0..d3f0374b858 100644 --- a/jetty-websocket/websocket-server/src/test/resources/jetty-logging.properties +++ b/jetty-websocket/websocket-server/src/test/resources/jetty-logging.properties @@ -1,3 +1,4 @@ org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog org.eclipse.jetty.io.LEVEL=DEBUG +org.eclipse.jetty.io.SelectorManager.LEVEL=INFO org.eclipse.jetty.websocket.LEVEL=DEBUG \ No newline at end of file