tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1445556 - in /tomcat/trunk/java/org/apache/tomcat/websocket: Constants.java WsFrameBase.java WsFrameClient.java WsSession.java WsWebSocketContainer.java server/WsFrameServer.java server/WsProtocolHandler.java
Date Wed, 13 Feb 2013 11:24:16 GMT
Author: markt
Date: Wed Feb 13 11:24:16 2013
New Revision: 1445556

URL: http://svn.apache.org/r1445556
Log:
Implement per session message buffer size limits and check them on every new message.

Modified:
    tomcat/trunk/java/org/apache/tomcat/websocket/Constants.java
    tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java
    tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameClient.java
    tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java
    tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
    tomcat/trunk/java/org/apache/tomcat/websocket/server/WsFrameServer.java
    tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/Constants.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/Constants.java?rev=1445556&r1=1445555&r2=1445556&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/Constants.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/Constants.java Wed Feb 13 11:24:16 2013
@@ -36,6 +36,9 @@ public class Constants {
     // Always set bit 4 so these will be treated as control codes
     static final byte INTERNAL_OPCODE_FLUSH = 0x18;
 
+    // Buffers
+    static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
+
     // Client connection
     public static final String HOST_HEADER_NAME = "Host";
     public static final String UPGRADE_HEADER_NAME = "Upgrade";

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=1445556&r1=1445555&r2=1445556&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java Wed Feb 13 11:24:16 2013
@@ -51,9 +51,6 @@ public abstract class WsFrameBase {
     private final CharBuffer controlBufferText = CharBuffer.allocate(125);
 
     // Attributes of the current message
-    // TODO: May need a new ByteBuffer per message
-    private final ByteBuffer messageBufferBinary;
-    private final CharBuffer messageBufferText;
     private final CharsetDecoder utf8DecoderControl = new Utf8Decoder().
             onMalformedInput(CodingErrorAction.REPORT).
             onUnmappableCharacter(CodingErrorAction.REPORT);
@@ -62,6 +59,9 @@ public abstract class WsFrameBase {
             onUnmappableCharacter(CodingErrorAction.REPORT);
     private boolean continuationExpected = false;
     private boolean textMessage = false;
+    // TODO: May need a new ByteBuffer per message
+    private ByteBuffer messageBufferBinary;
+    private CharBuffer messageBufferText;
 
     // Attributes of the current frame
     private boolean fin = false;
@@ -77,12 +77,13 @@ public abstract class WsFrameBase {
     private int readPos = 0;
     protected int writePos = 0;
 
-    public WsFrameBase(int binaryMessageBufferSize, int textMessageBufferSize,
-            WsSession wsSession) {
+    public WsFrameBase(WsSession wsSession) {
 
-        inputBuffer = new byte[binaryMessageBufferSize];
-        messageBufferBinary = ByteBuffer.allocate(binaryMessageBufferSize);
-        messageBufferText = CharBuffer.allocate(textMessageBufferSize);
+        inputBuffer = new byte[Constants.DEFAULT_BUFFER_SIZE];
+        messageBufferBinary =
+                ByteBuffer.allocate(wsSession.getMaxBinaryMessageBufferSize());
+        messageBufferText =
+                CharBuffer.allocate(wsSession.getMaxTextMessageBufferSize());
         this.wsSession = wsSession;
     }
 
@@ -151,9 +152,19 @@ public abstract class WsFrameBase {
                 }
             } else {
                 if (opCode == Constants.OPCODE_BINARY) {
+                    // New binary message
                     textMessage = false;
+                    int size = wsSession.getMaxBinaryMessageBufferSize();
+                    if (size != messageBufferBinary.capacity()) {
+                        messageBufferBinary = ByteBuffer.allocate(size);
+                    }
                 } else if (opCode == Constants.OPCODE_TEXT) {
+                    // New text message
                     textMessage = true;
+                    int size = wsSession.getMaxTextMessageBufferSize();
+                    if (size != messageBufferText.capacity()) {
+                        messageBufferText = CharBuffer.allocate(size);
+                    }
                 } else {
                     throw new WsIOException(new CloseReason(
                             CloseCodes.PROTOCOL_ERROR,

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameClient.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameClient.java?rev=1445556&r1=1445555&r2=1445556&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameClient.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameClient.java Wed Feb 13 11:24:16 2013
@@ -31,9 +31,8 @@ public class WsFrameClient extends WsFra
     private final CompletionHandler<Integer,Void> handler;
 
     public WsFrameClient(ByteBuffer response, AsynchronousSocketChannel channel,
-            int binaryMessageBufferSize, int textMessageBufferSize,
             WsSession wsSession) {
-        super(binaryMessageBufferSize, textMessageBufferSize, wsSession);
+        super(wsSession);
         this.response = response;
         this.channel = channel;
         this.handler = new WsFrameClientCompletionHandler();

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java?rev=1445556&r1=1445555&r2=1445556&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java Wed Feb 13 11:24:16 2013
@@ -59,6 +59,10 @@ public class WsSession implements Sessio
     private volatile boolean open = true;
     private final Object closeLock = new Object();
     private Map<String,Object> userProperties = new ConcurrentHashMap<>();
+    private volatile int maxBinaryMessageBufferSize =
+            Constants.DEFAULT_BUFFER_SIZE;
+    private volatile int maxTextMessageBufferSize =
+            Constants.DEFAULT_BUFFER_SIZE;
 
 
     /**
@@ -79,6 +83,10 @@ public class WsSession implements Sessio
         applicationClassLoader = Thread.currentThread().getContextClassLoader();
         wsRemoteEndpoint.setAsyncSendTimeout(
                 webSocketContainer.getDefaultAsyncSendTimeout());
+        this.maxBinaryMessageBufferSize =
+                webSocketContainer.getDefaultMaxBinaryMessageBufferSize();
+        this.maxTextMessageBufferSize =
+                webSocketContainer.getDefaultMaxTextMessageBufferSize();
     }
 
 
@@ -206,33 +214,27 @@ public class WsSession implements Sessio
     }
 
 
-
-
     @Override
     public void setMaxBinaryMessageBufferSize(int max) {
-        // TODO Auto-generated method stub
-
+        this.maxBinaryMessageBufferSize = max;
     }
 
 
     @Override
     public int getMaxBinaryMessageBufferSize() {
-        // TODO Auto-generated method stub
-        return 0;
+        return maxBinaryMessageBufferSize;
     }
 
 
     @Override
     public void setMaxTextMessageBufferSize(int max) {
-        // TODO Auto-generated method stub
-
+        this.maxTextMessageBufferSize = max;
     }
 
 
     @Override
     public int getMaxTextMessageBufferSize() {
-        // TODO Auto-generated method stub
-        return 0;
+        return maxTextMessageBufferSize;
     }
 
 
@@ -326,6 +328,20 @@ public class WsSession implements Sessio
 
 
     @Override
+    public String getId() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+
+    @Override
+    public Principal getUserPrincipal() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+
+    @Override
     public Map<String,Object> getUserProperties() {
         return userProperties;
     }
@@ -416,18 +432,4 @@ public class WsSession implements Sessio
             return null;
         }
     }
-
-
-    @Override
-    public String getId() {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-
-    @Override
-    public Principal getUserPrincipal() {
-        // TODO Auto-generated method stub
-        return null;
-    }
 }

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java?rev=1445556&r1=1445555&r2=1445556&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java Wed Feb 13 11:24:16
2013
@@ -52,11 +52,10 @@ public class WsWebSocketContainer implem
     private static final Random random = new Random();
     private static final Charset iso88591 = Charset.forName("ISO-8859-1");
     private static final byte[] crlf = new byte[] {13, 10};
-    private static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
 
     private long defaultAsyncTimeout = -1;
-    private int maxBinaryMessageBufferSize = DEFAULT_BUFFER_SIZE;
-    private int maxTextMessageBufferSize = DEFAULT_BUFFER_SIZE;
+    private int maxBinaryMessageBufferSize = Constants.DEFAULT_BUFFER_SIZE;
+    private int maxTextMessageBufferSize = Constants.DEFAULT_BUFFER_SIZE;
 
     @Override
     public Session connectToServer(Class<?> annotatedEndpointClass, URI path)
@@ -154,7 +153,6 @@ public class WsWebSocketContainer implem
         // Object creation will trigger input processing
         @SuppressWarnings("unused")
         WsFrameClient wsFrameClient = new WsFrameClient(response, channel,
-                maxBinaryMessageBufferSize, maxTextMessageBufferSize,
                 wsSession);
 
         return wsSession;

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/server/WsFrameServer.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/server/WsFrameServer.java?rev=1445556&r1=1445555&r2=1445556&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/server/WsFrameServer.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/server/WsFrameServer.java Wed Feb 13 11:24:16
2013
@@ -30,9 +30,8 @@ public class WsFrameServer extends WsFra
     private final Object connectionReadLock = new Object();
 
 
-    public WsFrameServer(ServletInputStream sis, int binaryMessageBufferSize,
-            int textMessageBufferSize, WsSession wsSession) {
-        super(binaryMessageBufferSize, textMessageBufferSize, wsSession);
+    public WsFrameServer(ServletInputStream sis, WsSession wsSession) {
+        super(wsSession);
         this.sis = sis;
     }
 

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java?rev=1445556&r1=1445555&r2=1445556&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java Wed Feb 13
11:24:16 2013
@@ -88,8 +88,6 @@ public class WsProtocolHandler implement
                     ep, wsRemoteEndpointServer, webSocketContainer);
             WsFrameServer wsFrame = new WsFrameServer(
                     sis,
-                    webSocketContainer.getDefaultMaxBinaryMessageBufferSize(),
-                    webSocketContainer.getDefaultMaxTextMessageBufferSize(),
                     wsSession);
             sis.setReadListener(new WsReadListener(this, wsFrame));
             sos.setWriteListener(



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message