You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by tr...@apache.org on 2008/03/31 17:01:42 UTC

svn commit: r643016 - in /mina: branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/ branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/ trunk/core/src/main/java/org/apache/mina/transport/socket/nio/

Author: trustin
Date: Mon Mar 31 08:01:38 2008
New Revision: 643016

URL: http://svn.apache.org/viewvc?rev=643016&view=rev
Log:
Resolved issue: DIRMINA-561 - Socket.setReceiveBufferSize() called after bind preventing correct TCP receive window scaling
* Made sure Socket.setReceiveBufferSize() is called before bind() or connect() if SocketSessionConfig.receiveBufferSize is 64kb+

Modified:
    mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java
    mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java
    mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java

Modified: mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java
URL: http://svn.apache.org/viewvc/mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java?rev=643016&r1=643015&r2=643016&view=diff
==============================================================================
--- mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java (original)
+++ mina/branches/1.0/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java Mon Mar 31 08:01:38 2008
@@ -6,16 +6,16 @@
  *  to you under the Apache License, Version 2.0 (the
  *  "License"); you may not use this file except in compliance
  *  with the License.  You may obtain a copy of the License at
- *  
+ *
  *    http://www.apache.org/licenses/LICENSE-2.0
- *  
+ *
  *  Unless required by applicable law or agreed to in writing,
  *  software distributed under the License is distributed on an
  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  *  KIND, either express or implied.  See the License for the
  *  specific language governing permissions and limitations
- *  under the License. 
- *  
+ *  under the License.
+ *
  */
 package org.apache.mina.transport.socket.nio;
 
@@ -84,7 +84,7 @@
     private int workerTimeout = 60; // 1 min.
 
     /**
-     * Create a connector with a single processing thread using a NewThreadExecutor 
+     * Create a connector with a single processing thread using a NewThreadExecutor
      */
     public SocketConnector() {
         this(1, new NewThreadExecutor());
@@ -146,19 +146,23 @@
     public ConnectFuture connect(SocketAddress address,
             SocketAddress localAddress, IoHandler handler,
             IoServiceConfig config) {
-        if (address == null)
+        if (address == null) {
             throw new NullPointerException("address");
-        if (handler == null)
+        }
+        if (handler == null) {
             throw new NullPointerException("handler");
+        }
 
-        if (!(address instanceof InetSocketAddress))
+        if (!(address instanceof InetSocketAddress)) {
             throw new IllegalArgumentException("Unexpected address type: "
                     + address.getClass());
+        }
 
         if (localAddress != null
-                && !(localAddress instanceof InetSocketAddress))
+                && !(localAddress instanceof InetSocketAddress)) {
             throw new IllegalArgumentException(
                     "Unexpected local address type: " + localAddress.getClass());
+        }
 
         if (config == null) {
             config = getDefaultConfig();
@@ -169,6 +173,17 @@
         try {
             ch = SocketChannel.open();
             ch.socket().setReuseAddress(true);
+
+            // Receive buffer size must be set BEFORE the socket is connected
+            // in order for the TCP window to be sized accordingly
+            if (config instanceof SocketConnectorConfig) {
+                int receiveBufferSize =
+                    ((SocketSessionConfig) config.getSessionConfig()).getReceiveBufferSize();
+                if (receiveBufferSize > 65535) {
+                    ch.socket().setReceiveBufferSize(receiveBufferSize);
+                }
+            }
+
             if (localAddress != null) {
                 ch.socket().bind(localAddress);
             }
@@ -205,10 +220,10 @@
                 } catch (IOException e2) {
                     ExceptionMonitor.getInstance().exceptionCaught(e2);
                 }
-    
+
                 return DefaultConnectFuture.newFailedFuture(e);
             }
-    
+
             synchronized (connectQueue) {
                 connectQueue.push(request);
             }
@@ -224,7 +239,7 @@
 
     /**
      * Sets the config this connector will use by default.
-     * 
+     *
      * @param defaultConfig the default config.
      * @throws NullPointerException if the specified value is <code>null</code>.
      */
@@ -234,7 +249,7 @@
         }
         this.defaultConfig = defaultConfig;
     }
-    
+
     private Selector getSelector() {
         synchronized (lock) {
             return this.selector;
@@ -252,8 +267,9 @@
     }
 
     private void registerNew() {
-        if (connectQueue.isEmpty())
+        if (connectQueue.isEmpty()) {
             return;
+        }
 
         Selector selector = getSelector();
         for (;;) {
@@ -262,8 +278,9 @@
                 req = (ConnectionRequest) connectQueue.pop();
             }
 
-            if (req == null)
+            if (req == null) {
                 break;
+            }
 
             SocketChannel ch = req.channel;
             try {
@@ -285,8 +302,9 @@
         while (it.hasNext()) {
             SelectionKey key = (SelectionKey) it.next();
 
-            if (!key.isConnectable())
+            if (!key.isConnectable()) {
                 continue;
+            }
 
             SocketChannel ch = (SocketChannel) key.channel();
             ConnectionRequest entry = (ConnectionRequest) key.attachment();
@@ -322,8 +340,9 @@
         while (it.hasNext()) {
             SelectionKey key = (SelectionKey) it.next();
 
-            if (!key.isValid())
+            if (!key.isValid()) {
                 continue;
+            }
 
             ConnectionRequest entry = (ConnectionRequest) key.attachment();
 

Modified: mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java
URL: http://svn.apache.org/viewvc/mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java?rev=643016&r1=643015&r2=643016&view=diff
==============================================================================
--- mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java (original)
+++ mina/branches/1.1/core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java Mon Mar 31 08:01:38 2008
@@ -140,19 +140,23 @@
     public ConnectFuture connect(SocketAddress address,
             SocketAddress localAddress, IoHandler handler,
             IoServiceConfig config) {
-        if (address == null)
+        if (address == null) {
             throw new NullPointerException("address");
-        if (handler == null)
+        }
+        if (handler == null) {
             throw new NullPointerException("handler");
+        }
 
-        if (!(address instanceof InetSocketAddress))
+        if (!(address instanceof InetSocketAddress)) {
             throw new IllegalArgumentException("Unexpected address type: "
                     + address.getClass());
+        }
 
         if (localAddress != null
-                && !(localAddress instanceof InetSocketAddress))
+                && !(localAddress instanceof InetSocketAddress)) {
             throw new IllegalArgumentException(
                     "Unexpected local address type: " + localAddress.getClass());
+        }
 
         if (config == null) {
             config = getDefaultConfig();
@@ -163,6 +167,17 @@
         try {
             ch = SocketChannel.open();
             ch.socket().setReuseAddress(true);
+
+            // Receive buffer size must be set BEFORE the socket is connected
+            // in order for the TCP window to be sized accordingly
+            if (config instanceof SocketConnectorConfig) {
+                int receiveBufferSize =
+                    ((SocketSessionConfig) config.getSessionConfig()).getReceiveBufferSize();
+                if (receiveBufferSize > 65535) {
+                    ch.socket().setReceiveBufferSize(receiveBufferSize);
+                }
+            }
+
             if (localAddress != null) {
                 ch.socket().bind(localAddress);
             }
@@ -199,10 +214,10 @@
                 } catch (IOException e2) {
                     ExceptionMonitor.getInstance().exceptionCaught(e2);
                 }
-    
+
                 return DefaultConnectFuture.newFailedFuture(e);
             }
-    
+
             connectQueue.add(request);
             selector.wakeup();
         }
@@ -238,15 +253,17 @@
     }
 
     private void registerNew() {
-        if (connectQueue.isEmpty())
+        if (connectQueue.isEmpty()) {
             return;
+        }
 
         Selector selector = this.selector;
         for (;;) {
             ConnectionRequest req = connectQueue.poll();
 
-            if (req == null)
+            if (req == null) {
                 break;
+            }
 
             SocketChannel ch = req.channel;
             try {
@@ -264,8 +281,9 @@
 
     private void processSessions(Set<SelectionKey> keys) {
         for (SelectionKey key : keys) {
-            if (!key.isConnectable())
+            if (!key.isConnectable()) {
                 continue;
+            }
 
             SocketChannel ch = (SocketChannel) key.channel();
             ConnectionRequest entry = (ConnectionRequest) key.attachment();
@@ -298,8 +316,9 @@
         long currentTime = System.currentTimeMillis();
 
         for (SelectionKey key : keys) {
-            if (!key.isValid())
+            if (!key.isValid()) {
                 continue;
+            }
 
             ConnectionRequest entry = (ConnectionRequest) key.attachment();
 

Modified: mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java?rev=643016&r1=643015&r2=643016&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/transport/socket/nio/NioSocketConnector.java Mon Mar 31 08:01:38 2008
@@ -67,12 +67,12 @@
         super(new DefaultSocketSessionConfig(), executor, processor);
         ((DefaultSocketSessionConfig) getSessionConfig()).init(this);
     }
-    
+
     @Override
     protected void init() throws Exception {
         this.selector = Selector.open();
     }
-    
+
     @Override
     protected void destroy() throws Exception {
         if (selector != null) {
@@ -93,7 +93,7 @@
     public InetSocketAddress getDefaultRemoteAddress() {
         return (InetSocketAddress) super.getDefaultRemoteAddress();
     }
-    
+
     public void setDefaultRemoteAddress(InetSocketAddress defaultRemoteAddress) {
         super.setDefaultRemoteAddress(defaultRemoteAddress);
     }
@@ -115,7 +115,7 @@
         if (key == null) {
             return null;
         }
-        
+
         return (ConnectionRequest) key.attachment();
     }
 
@@ -137,7 +137,7 @@
             }
             return true;
         }
-        
+
         return false;
     }
 
@@ -145,6 +145,13 @@
     protected SocketChannel newHandle(SocketAddress localAddress)
             throws Exception {
         SocketChannel ch = SocketChannel.open();
+
+        int receiveBufferSize =
+            (getSessionConfig()).getReceiveBufferSize();
+        if (receiveBufferSize > 65535) {
+            ch.socket().setReceiveBufferSize(receiveBufferSize);
+        }
+
         if (localAddress != null) {
             ch.socket().bind(localAddress);
         }
@@ -177,11 +184,11 @@
     protected void wakeup() {
         selector.wakeup();
     }
-    
+
     private static class SocketChannelIterator implements Iterator<SocketChannel> {
-        
+
         private final Iterator<SelectionKey> i;
-        
+
         private SocketChannelIterator(Collection<SelectionKey> selectedKeys) {
             this.i = selectedKeys.iterator();
         }