Re-factor JIO end point to look more like NIO end point. Moving stuff around, process...
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sat, 18 Sep 2010 18:09:48 +0000 (18:09 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sat, 18 Sep 2010 18:09:48 +0000 (18:09 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@998509 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/tomcat/util/net/JIoEndpoint.java
java/org/apache/tomcat/util/net/NioEndpoint.java
java/org/apache/tomcat/util/net/SocketWrapper.java

index ede1de7..a240592 100644 (file)
@@ -196,8 +196,19 @@ public class JIoEndpoint extends AbstractEndpoint {
                 try {
                     // Accept the next incoming connection from the server socket
                     Socket socket = serverSocketFactory.acceptSocket(serverSocket);
-                    // Hand this socket off to an appropriate processor
-                    if (!processSocket(socket)) {
+                    
+                    // Configure the socket
+                    if (setSocketOptions(socket)) {
+                        // Hand this socket off to an appropriate processor
+                        if (!processSocket(socket)) {
+                            // Close socket right away
+                            try {
+                                socket.close();
+                            } catch (IOException e) {
+                                // Ignore
+                            }
+                        }
+                    } else {
                         // Close socket right away
                         try {
                             socket.close();
@@ -255,11 +266,17 @@ public class JIoEndpoint extends AbstractEndpoint {
                 }
                 
                 SocketState state = SocketState.OPEN;
-                // Process the request from this socket
-                if ( (!socket.isInitialized()) && (!setSocketOptions(socket.getSocket())) ) { 
+
+                try {
+                    // SSL handshake
+                    serverSocketFactory.handshake(socket.getSocket());
+                } catch (Throwable t) {
+                    if (log.isDebugEnabled()) {
+                        log.debug(sm.getString("endpoint.err.handshake"), t);
+                    }
+                    // Tell to close the socket
                     state = SocketState.CLOSED;
                 }
-                socket.setInitialized(true);
 
                 if ( (state != SocketState.CLOSED) ) {
                     state = (status==null)?handler.process(socket):handler.process(socket,status);
@@ -448,11 +465,11 @@ public class JIoEndpoint extends AbstractEndpoint {
 
 
     /**
-     * Set the options for the current socket.
+     * Configure the socket.
      */
     protected boolean setSocketOptions(Socket socket) {
-        // Process the connection
-        
+        serverSocketFactory.initSocket(socket);
+
         try {
             // 1: Set socket options: timeout, linger, etc
             socketProperties.setProperties(socket);
@@ -468,25 +485,15 @@ public class JIoEndpoint extends AbstractEndpoint {
             // Close the socket
             return false;
         }
-        try {
-            // 2: SSL handshake
-            serverSocketFactory.handshake(socket);
-        } catch (Throwable t) {
-            if (log.isDebugEnabled()) {
-                log.debug(sm.getString("endpoint.err.handshake"), t);
-            }
-            // Tell to close the socket
-            return false;
-        }
         return true;
     }
 
-    
+
     /**
      * Process given socket.
      */
     protected boolean processSocket(Socket socket) {
-        serverSocketFactory.initSocket(socket);
+        // Process the request from this socket
         try {
             SocketWrapper<Socket> wrapper = new SocketWrapper<Socket>(socket);
             wrapper.setKeepAliveLeft(getMaxKeepAliveRequests());
index b1526c8..c0967d7 100644 (file)
@@ -580,6 +580,7 @@ public class NioEndpoint extends AbstractEndpoint {
     /**
      * Stop the endpoint. This will cause all processing threads to stop.
      */
+    @Override
     public void stop() {
         if (!paused) {
             pause();
@@ -643,6 +644,7 @@ public class NioEndpoint extends AbstractEndpoint {
         return selectorPool;
     }
 
+    @Override
     public boolean getUseSendfile() {
         return useSendfile;
     }
@@ -793,7 +795,8 @@ public class NioEndpoint extends AbstractEndpoint {
                     //TODO FIXME - this is currently a blocking call, meaning we will be blocking
                     //further accepts until there is a thread available.
                     if ( running && (!paused) && socket != null ) {
-                        //processSocket(socket);
+                        // setSocketOptions() will add channel to the poller
+                        // if successful
                         if (!setSocketOptions(socket)) {
                             try {
                                 socket.socket().close();
index ab785b3..80df590 100644 (file)
@@ -31,7 +31,6 @@ public class SocketWrapper<E> {
     protected volatile int keepAliveLeft = 100;
     protected boolean async = false;
     protected boolean keptAlive = false;
-    protected boolean initialized = false;
     public AtomicBoolean processing = new AtomicBoolean(false);
     
     public SocketWrapper(E socket) {
@@ -58,7 +57,4 @@ public class SocketWrapper<E> {
     public int decrementKeepAlive() { return (--keepAliveLeft);}
     public boolean isKeptAlive() {return keptAlive;}
     public void setKeptAlive(boolean keptAlive) {this.keptAlive = keptAlive;}
-    public boolean isInitialized() {return initialized;}
-    public void setInitialized(boolean initialized) {this.initialized = initialized;}
-    
 }