From 63c874e283d684e5a05fd4bbbb0f1233118c169f Mon Sep 17 00:00:00 2001 From: markt Date: Sat, 18 Sep 2010 18:09:48 +0000 Subject: [PATCH] Re-factor JIO end point to look more like NIO end point. Moving stuff around, processing order should remain the same. 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 | 47 +++++++++++++--------- java/org/apache/tomcat/util/net/NioEndpoint.java | 5 ++- java/org/apache/tomcat/util/net/SocketWrapper.java | 4 -- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/java/org/apache/tomcat/util/net/JIoEndpoint.java b/java/org/apache/tomcat/util/net/JIoEndpoint.java index ede1de7b3..a24059294 100644 --- a/java/org/apache/tomcat/util/net/JIoEndpoint.java +++ b/java/org/apache/tomcat/util/net/JIoEndpoint.java @@ -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 wrapper = new SocketWrapper(socket); wrapper.setKeepAliveLeft(getMaxKeepAliveRequests()); diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java index b1526c884..c0967d76a 100644 --- a/java/org/apache/tomcat/util/net/NioEndpoint.java +++ b/java/org/apache/tomcat/util/net/NioEndpoint.java @@ -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(); diff --git a/java/org/apache/tomcat/util/net/SocketWrapper.java b/java/org/apache/tomcat/util/net/SocketWrapper.java index ab785b3c2..80df5905a 100644 --- a/java/org/apache/tomcat/util/net/SocketWrapper.java +++ b/java/org/apache/tomcat/util/net/SocketWrapper.java @@ -31,7 +31,6 @@ public class SocketWrapper { 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 { 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;} - } -- 2.11.0