Skip to content

Commit

Permalink
- Remove read/write pending flags since they don't really add anythin…
Browse files Browse the repository at this point in the history
…g, use a lot of finally and are still slightly incorrect without additional finally (or similar).

- Simplify, harmonize and cleanups.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1673555 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
rmaucher committed Apr 14, 2015
1 parent e476e2f commit 7677c59
Showing 1 changed file with 15 additions and 90 deletions.
105 changes: 15 additions & 90 deletions java/org/apache/tomcat/util/net/SecureNio2Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ public class SecureNio2Channel extends Nio2Channel {

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;
Expand Down Expand Up @@ -123,15 +121,13 @@ public void reset(AsynchronousSocketChannel channel, SocketWrapperBase<Nio2Chann
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) {
Expand All @@ -148,23 +144,13 @@ public boolean isDone() {
@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);
}
}

Expand All @@ -176,12 +162,7 @@ public Boolean get(long timeout, TimeUnit unit)
*/
@Override
public Future<Boolean> flush() {
if (writePending) {
throw new WritePendingException();
} else {
writePending = true;
}
return new FutureFlush(sc.write(netOutBuffer));
return new FutureFlush();
}

/**
Expand Down Expand Up @@ -558,7 +539,6 @@ private FutureRead(ByteBuffer dst) {
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
readPending = false;
return integer.cancel(mayInterruptIfRunning);
}
@Override
Expand All @@ -571,21 +551,13 @@ public boolean isDone() {
}
@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?
Expand Down Expand Up @@ -647,11 +619,6 @@ public Future<Integer> read(ByteBuffer dst) {
if (!handshakeComplete) {
throw new IllegalStateException(sm.getString("channel.nio.ssl.incompleteHandshake"));
}
if (readPending) {
throw new ReadPendingException();
} else {
readPending = true;
}
return new FutureRead(dst);
}

Expand All @@ -665,13 +632,12 @@ private FutureWrite(ByteBuffer src) {
//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
Expand All @@ -685,7 +651,6 @@ public boolean isDone() {
@Override
public Integer get() throws InterruptedException, ExecutionException {
if (t != null) {
writePending = false;
throw new ExecutionException(t);
}
if (integer.get().intValue() > 0 && written == 0) {
Expand All @@ -695,7 +660,6 @@ public Integer get() throws InterruptedException, ExecutionException {
integer = sc.write(netOutBuffer);
return get();
} else {
writePending = false;
return Integer.valueOf(written);
}
}
Expand All @@ -704,7 +668,6 @@ public Integer get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException,
TimeoutException {
if (t != null) {
writePending = false;
throw new ExecutionException(t);
}
if (integer.get(timeout, unit).intValue() > 0 && written == 0) {
Expand All @@ -714,7 +677,6 @@ public Integer get(long timeout, TimeUnit unit)
integer = sc.write(netOutBuffer);
return get(timeout, unit);
} else {
writePending = false;
return Integer.valueOf(written);
}
}
Expand Down Expand Up @@ -747,11 +709,6 @@ protected void wrap() {
*/
@Override
public Future<Integer> write(ByteBuffer src) {
if (writePending) {
throw new WritePendingException();
} else {
writePending = true;
}
return new FutureWrite(src);
}

Expand All @@ -767,11 +724,6 @@ public <A> void read(final ByteBuffer dst,
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) {
Expand Down Expand Up @@ -812,7 +764,6 @@ public void completed(Integer nBytes, A attach) {
}
} 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);
Expand All @@ -821,7 +772,6 @@ public void completed(Integer nBytes, A attach) {
}
@Override
public void failed(Throwable exc, A attach) {
readPending = false;
handler.failed(exc, attach);
}
});
Expand All @@ -834,16 +784,12 @@ public <A> void read(ByteBuffer[] dsts, int offset, int length,
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
Expand Down Expand Up @@ -885,7 +831,6 @@ public void completed(Integer nBytes, A attach) {
}
} 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);
Expand All @@ -894,7 +839,6 @@ public void completed(Integer nBytes, A attach) {
}
@Override
public void failed(Throwable exc, A attach) {
readPending = false;
handler.failed(exc, attach);
}
});
Expand All @@ -909,12 +853,6 @@ public <A> void write(ByteBuffer src, long timeout, TimeUnit unit,
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();
Expand All @@ -937,26 +875,22 @@ public void completed(Integer nBytes, A attach) {
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);
}
});
} else {
throw new IOException(sm.getString("channel.nio.ssl.wrapFail", result.getStatus()));
}
} catch (Exception e) {
writePending = false;
handler.failed(e, attachment);
}
}
Expand All @@ -965,19 +899,14 @@ public void failed(Throwable exc, A attach) {
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();
Expand All @@ -999,26 +928,22 @@ public void completed(Integer nBytes, A attach) {
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);
}
});
} else {
throw new IOException(sm.getString("channel.nio.ssl.wrapFail", result.getStatus()));
}
} catch (Exception e) {
writePending = false;
handler.failed(e, attachment);
}
}
Expand Down

0 comments on commit 7677c59

Please sign in to comment.