You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2015/04/14 22:58:25 UTC

svn commit: r1673555 - /tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java

Author: remm
Date: Tue Apr 14 20:58:24 2015
New Revision: 1673555

URL: http://svn.apache.org/r1673555
Log:
- Remove read/write pending flags since they don't really add anything, use a lot of finally and are still slightly incorrect without additional finally (or similar).
- Simplify, harmonize and cleanups.

Modified:
    tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java?rev=1673555&r1=1673554&r2=1673555&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java Tue Apr 14 20:58:24 2015
@@ -65,8 +65,6 @@ public class SecureNio2Channel extends N
 
     protected boolean closed;
     protected boolean closing;
-    protected volatile boolean readPending;
-    protected volatile boolean writePending;
 
     private CompletionHandler<Integer, SocketWrapperBase<Nio2Channel>> handshakeReadCompletionHandler;
     private CompletionHandler<Integer, SocketWrapperBase<Nio2Channel>> handshakeWriteCompletionHandler;
@@ -123,15 +121,13 @@ public class SecureNio2Channel extends N
         handshakeComplete = false;
         closed = false;
         closing = false;
-        readPending = false;
-        writePending = false;
     }
 
 
     private class FutureFlush implements Future<Boolean> {
         private Future<Integer> integer;
-        protected FutureFlush(Future<Integer> integer) {
-            this.integer = integer;
+        protected FutureFlush() {
+            integer = sc.write(netOutBuffer);
         }
         @Override
         public boolean cancel(boolean mayInterruptIfRunning) {
@@ -148,23 +144,13 @@ public class SecureNio2Channel extends N
         @Override
         public Boolean get() throws InterruptedException,
                 ExecutionException {
-            try {
-                int result = integer.get().intValue();
-                return Boolean.valueOf(result >= 0);
-            } finally {
-                writePending = false;
-            }
+            return Boolean.valueOf(integer.get().intValue() >= 0);
         }
         @Override
         public Boolean get(long timeout, TimeUnit unit)
                 throws InterruptedException, ExecutionException,
                 TimeoutException {
-            try {
-                int result = integer.get(timeout, unit).intValue();
-                return Boolean.valueOf(result >= 0);
-            } finally {
-                writePending = false;
-            }
+            return Boolean.valueOf(integer.get(timeout, unit).intValue() >= 0);
         }
     }
 
@@ -176,12 +162,7 @@ public class SecureNio2Channel extends N
      */
     @Override
     public Future<Boolean> flush() {
-        if (writePending) {
-            throw new WritePendingException();
-        } else {
-            writePending = true;
-        }
-        return new FutureFlush(sc.write(netOutBuffer));
+        return new FutureFlush();
     }
 
     /**
@@ -558,7 +539,6 @@ public class SecureNio2Channel extends N
         }
         @Override
         public boolean cancel(boolean mayInterruptIfRunning) {
-            readPending = false;
             return integer.cancel(mayInterruptIfRunning);
         }
         @Override
@@ -571,21 +551,13 @@ public class SecureNio2Channel extends N
         }
         @Override
         public Integer get() throws InterruptedException, ExecutionException {
-            try {
-                return unwrap(integer.get().intValue());
-            } finally {
-                readPending = false;
-            }
+            return unwrap(integer.get().intValue());
         }
         @Override
         public Integer get(long timeout, TimeUnit unit)
                 throws InterruptedException, ExecutionException,
                 TimeoutException {
-            try {
-                return unwrap(integer.get(timeout, unit).intValue());
-            } finally {
-                readPending = false;
-            }
+            return unwrap(integer.get(timeout, unit).intValue());
         }
         private Integer unwrap(int nRead) throws ExecutionException {
             //are we in the middle of closing or closed?
@@ -647,11 +619,6 @@ public class SecureNio2Channel extends N
         if (!handshakeComplete) {
             throw new IllegalStateException(sm.getString("channel.nio.ssl.incompleteHandshake"));
         }
-        if (readPending) {
-            throw new ReadPendingException();
-        } else {
-            readPending = true;
-        }
         return new FutureRead(dst);
     }
 
@@ -665,13 +632,12 @@ public class SecureNio2Channel extends N
             //are we closing or closed?
             if (closing || closed) {
                 t = new IOException(sm.getString("channel.nio.ssl.closing"));
-                return;
+            } else {
+                wrap();
             }
-            wrap();
         }
         @Override
         public boolean cancel(boolean mayInterruptIfRunning) {
-            writePending = false;
             return integer.cancel(mayInterruptIfRunning);
         }
         @Override
@@ -685,7 +651,6 @@ public class SecureNio2Channel extends N
         @Override
         public Integer get() throws InterruptedException, ExecutionException {
             if (t != null) {
-                writePending = false;
                 throw new ExecutionException(t);
             }
             if (integer.get().intValue() > 0 && written == 0) {
@@ -695,7 +660,6 @@ public class SecureNio2Channel extends N
                 integer = sc.write(netOutBuffer);
                 return get();
             } else {
-                writePending = false;
                 return Integer.valueOf(written);
             }
         }
@@ -704,7 +668,6 @@ public class SecureNio2Channel extends N
                 throws InterruptedException, ExecutionException,
                 TimeoutException {
             if (t != null) {
-                writePending = false;
                 throw new ExecutionException(t);
             }
             if (integer.get(timeout, unit).intValue() > 0 && written == 0) {
@@ -714,7 +677,6 @@ public class SecureNio2Channel extends N
                 integer = sc.write(netOutBuffer);
                 return get(timeout, unit);
             } else {
-                writePending = false;
                 return Integer.valueOf(written);
             }
         }
@@ -747,11 +709,6 @@ public class SecureNio2Channel extends N
      */
     @Override
     public Future<Integer> write(ByteBuffer src) {
-        if (writePending) {
-            throw new WritePendingException();
-        } else {
-            writePending = true;
-        }
         return new FutureWrite(src);
     }
 
@@ -767,11 +724,6 @@ public class SecureNio2Channel extends N
         if (!handshakeComplete) {
             throw new IllegalStateException(sm.getString("channel.nio.ssl.incompleteHandshake"));
         }
-        if (readPending) {
-            throw new ReadPendingException();
-        } else {
-            readPending = true;
-        }
         sc.read(netInBuffer, timeout, unit, attachment, new CompletionHandler<Integer, A>() {
             @Override
             public void completed(Integer nBytes, A attach) {
@@ -812,7 +764,6 @@ public class SecureNio2Channel extends N
                             }
                         } while ((netInBuffer.position() != 0)); //continue to unwrapping as long as the input buffer has stuff
                         // If everything is OK, so complete
-                        readPending = false;
                         handler.completed(Integer.valueOf(read), attach);
                     } catch (Exception e) {
                         failed(e, attach);
@@ -821,7 +772,6 @@ public class SecureNio2Channel extends N
             }
             @Override
             public void failed(Throwable exc, A attach) {
-                readPending = false;
                 handler.failed(exc, attach);
             }
         });
@@ -834,16 +784,12 @@ public class SecureNio2Channel extends N
         if (offset < 0 || dsts == null || (offset + length) > dsts.length) {
             throw new IllegalArgumentException();
         }
-        ByteBuffer dst = null;
-        // Find the first buffer with space
-        for (int i = 0; i < length; i++) {
-            ByteBuffer current = dsts[offset + i];
-            if (current.position() < current.limit()) {
-                dst = current;
-            }
+        if (closing || closed) {
+            handler.completed(Long.valueOf(-1), attachment);
+            return;
         }
-        if (dst == null) {
-            throw new IllegalArgumentException();
+        if (!handshakeComplete) {
+            throw new IllegalStateException(sm.getString("channel.nio.ssl.incompleteHandshake"));
         }
         sc.read(netInBuffer, timeout, unit, attachment, new CompletionHandler<Integer, A>() {
             @Override
@@ -885,7 +831,6 @@ public class SecureNio2Channel extends N
                             }
                         } while ((netInBuffer.position() != 0)); //continue to unwrapping as long as the input buffer has stuff
                         // If everything is OK, so complete
-                        readPending = false;
                         handler.completed(Long.valueOf(read), attach);
                     } catch (Exception e) {
                         failed(e, attach);
@@ -894,7 +839,6 @@ public class SecureNio2Channel extends N
             }
             @Override
             public void failed(Throwable exc, A attach) {
-                readPending = false;
                 handler.failed(exc, attach);
             }
         });
@@ -909,12 +853,6 @@ public class SecureNio2Channel extends N
             handler.failed(new IOException(sm.getString("channel.nio.ssl.closing")), attachment);
             return;
         }
-        if (writePending) {
-            throw new WritePendingException();
-        } else {
-            writePending = true;
-        }
-
         try {
             // Prepare the output buffer
             netOutBuffer.clear();
@@ -937,18 +875,15 @@ public class SecureNio2Channel extends N
                             sc.write(netOutBuffer, timeout, unit, attachment, this);
                         } else if (written == 0) {
                             // Special case, start over to avoid code duplication
-                            writePending = false;
                             write(src, timeout, unit, attachment, handler);
                         } else {
                             // Call the handler completed method with the
                             // consumed bytes number
-                            writePending = false;
                             handler.completed(Integer.valueOf(written), attach);
                         }
                     }
                     @Override
                     public void failed(Throwable exc, A attach) {
-                        writePending = false;
                         handler.failed(exc, attach);
                     }
                 });
@@ -956,7 +891,6 @@ public class SecureNio2Channel extends N
                 throw new IOException(sm.getString("channel.nio.ssl.wrapFail", result.getStatus()));
             }
         } catch (Exception e) {
-            writePending = false;
             handler.failed(e, attachment);
         }
     }
@@ -965,19 +899,14 @@ public class SecureNio2Channel extends N
     public <A> void write(ByteBuffer[] srcs, int offset, int length,
             long timeout, TimeUnit unit, A attachment,
             CompletionHandler<Long, ? super A> handler) {
-        // Check state
         if ((offset < 0) || (length < 0) || (offset > srcs.length - length)) {
             throw new IndexOutOfBoundsException();
         }
+        // Check state
         if (closing || closed) {
             handler.failed(new IOException(sm.getString("channel.nio.ssl.closing")), attachment);
             return;
         }
-        if (writePending) {
-            throw new WritePendingException();
-        } else {
-            writePending = true;
-        }
         try {
              // Prepare the output buffer
             netOutBuffer.clear();
@@ -999,18 +928,15 @@ public class SecureNio2Channel extends N
                             sc.write(netOutBuffer, timeout, unit, attachment, this);
                         } else if (written == 0) {
                             // Special case, start over to avoid code duplication
-                            writePending = false;
                             write(srcs, offset, length, timeout, unit, attachment, handler);
                         } else {
                             // Call the handler completed method with the
                             // consumed bytes number
-                            writePending = false;
                             handler.completed(Long.valueOf(written), attach);
                         }
                     }
                     @Override
                     public void failed(Throwable exc, A attach) {
-                        writePending = false;
                         handler.failed(exc, attach);
                     }
                 });
@@ -1018,7 +944,6 @@ public class SecureNio2Channel extends N
                 throw new IOException(sm.getString("channel.nio.ssl.wrapFail", result.getStatus()));
             }
         } catch (Exception e) {
-            writePending = false;
             handler.failed(e, attachment);
         }
    }



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