From 0428490f67ad5978815c584582eabf3d4b9a9bf4 Mon Sep 17 00:00:00 2001 From: fhanik Date: Fri, 8 Jun 2007 10:18:21 +0000 Subject: [PATCH] straightened out buffer handling git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@545469 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/coyote/http11/InternalNioOutputBuffer.java | 16 ++++++++++------ java/org/apache/tomcat/util/net/NioBlockingSelector.java | 12 ++++++++++-- java/org/apache/tomcat/util/net/NioSelectorPool.java | 4 ---- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/java/org/apache/coyote/http11/InternalNioOutputBuffer.java b/java/org/apache/coyote/http11/InternalNioOutputBuffer.java index df5dc7155..7499294e8 100644 --- a/java/org/apache/coyote/http11/InternalNioOutputBuffer.java +++ b/java/org/apache/coyote/http11/InternalNioOutputBuffer.java @@ -406,7 +406,7 @@ public class InternalNioOutputBuffer if (!committed) { //Socket.send(socket, Constants.ACK_BYTES, 0, Constants.ACK_BYTES.length) < 0 ByteBuffer buf = ByteBuffer.wrap(Constants.ACK_BYTES,0,Constants.ACK_BYTES.length); - writeToSocket(buf,false,true); + writeToSocket(buf,true); } } @@ -419,9 +419,12 @@ public class InternalNioOutputBuffer * @throws IOException * @todo Fix non blocking write properly */ - private synchronized int writeToSocket(ByteBuffer bytebuffer, boolean flip, boolean block) throws IOException { - //int limit = bytebuffer.position(); - if ( flip ) bytebuffer.flip(); + private synchronized int writeToSocket(ByteBuffer bytebuffer, boolean block) throws IOException { + if (socket.getBufHandler().getWriteBuffer() != bytebuffer) { + socket.getBufHandler().getWriteBuffer().put(bytebuffer); + bytebuffer = socket.getBufHandler().getWriteBuffer(); + } + int written = 0; NioEndpoint.KeyAttachment att = (NioEndpoint.KeyAttachment)socket.getAttachment(false); if ( att == null ) throw new IOException("Key must be cancelled"); @@ -441,7 +444,7 @@ public class InternalNioOutputBuffer }finally { if ( selector != null ) getSelectorPool().put(selector); } - if ( block ) socket.getBufHandler().getWriteBuffer().clear(); //only clear + if ( block ) bytebuffer.clear(); //only clear this.total = 0; return written; } @@ -762,7 +765,8 @@ public class InternalNioOutputBuffer //write to the socket, if there is anything to write if (socket.getBufHandler().getWriteBuffer().position() > 0) { - writeToSocket(socket.getBufHandler().getWriteBuffer(),true,true); + socket.getBufHandler().getWriteBuffer().flip(); + writeToSocket(socket.getBufHandler().getWriteBuffer(),true); } } diff --git a/java/org/apache/tomcat/util/net/NioBlockingSelector.java b/java/org/apache/tomcat/util/net/NioBlockingSelector.java index 5b06b6739..d5a0f3638 100644 --- a/java/org/apache/tomcat/util/net/NioBlockingSelector.java +++ b/java/org/apache/tomcat/util/net/NioBlockingSelector.java @@ -206,7 +206,11 @@ public class NioBlockingSelector { public void add(final KeyAttachment key, final int ops) { Runnable r = new Runnable() { public void run() { - SocketChannel ch = key.getChannel().getIOChannel(); + if ( key == null ) return; + NioChannel nch = key.getChannel(); + if ( nch == null ) return; + SocketChannel ch = nch.getIOChannel(); + if ( ch == null ) return; SelectionKey sk = ch.keyFor(selector); try { if (sk == null) { @@ -230,7 +234,11 @@ public class NioBlockingSelector { public void remove(final KeyAttachment key, final int ops) { Runnable r = new Runnable() { public void run() { - SocketChannel ch = key.getChannel().getIOChannel(); + if ( key == null ) return; + NioChannel nch = key.getChannel(); + if ( nch == null ) return; + SocketChannel ch = nch.getIOChannel(); + if ( ch == null ) return; SelectionKey sk = ch.keyFor(selector); try { if (sk == null) { diff --git a/java/org/apache/tomcat/util/net/NioSelectorPool.java b/java/org/apache/tomcat/util/net/NioSelectorPool.java index b3b8cbbda..aa61ffd96 100644 --- a/java/org/apache/tomcat/util/net/NioSelectorPool.java +++ b/java/org/apache/tomcat/util/net/NioSelectorPool.java @@ -152,10 +152,6 @@ public class NioSelectorPool { public int write(ByteBuffer buf, NioChannel socket, Selector selector, long writeTimeout, boolean block,MutableInteger lastWrite) throws IOException { - if (socket.getBufHandler().getWriteBuffer() != buf) { - socket.getBufHandler().getWriteBuffer().put(buf); - buf = socket.getBufHandler().getWriteBuffer(); - } if ( SHARED && block ) { return blockingSelector.write(buf,socket,writeTimeout,lastWrite); } -- 2.11.0