Correctly respect isAutoRead() and make it consistent across OIO/NIO

This commit is contained in:
Norman Maurer 2014-02-11 18:08:40 +01:00
parent 7041a9238e
commit 80e6f9adf4
3 changed files with 76 additions and 48 deletions

View File

@ -108,9 +108,6 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel {
if (allocHandle == null) {
this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle();
}
if (!config.isAutoRead()) {
removeReadOp();
}
ByteBuf byteBuf = null;
int messages = 0;
@ -139,6 +136,12 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel {
}
totalReadAmount += localReadAmount;
// stop reading
if (!config.isAutoRead()) {
break;
}
if (localReadAmount < writable) {
// Read less than what the buffer can hold,
// which might mean we drained the recv buffer completely.
@ -155,6 +158,10 @@ public abstract class AbstractNioByteChannel extends AbstractNioChannel {
}
} catch (Throwable t) {
handleReadException(pipeline, byteBuf, t, close);
} finally {
if (!config.isAutoRead()) {
removeReadOp();
}
}
}
}

View File

@ -59,13 +59,10 @@ public abstract class AbstractNioMessageChannel extends AbstractNioChannel {
@Override
public void read() {
assert eventLoop().inEventLoop();
if (!config().isAutoRead()) {
removeReadOp();
}
final ChannelConfig config = config();
try {
final int maxMessagesPerRead = config.getMaxMessagesPerRead();
final boolean autoRead = config.isAutoRead();
final ChannelPipeline pipeline = pipeline();
boolean closed = false;
Throwable exception = null;
@ -80,7 +77,12 @@ public abstract class AbstractNioMessageChannel extends AbstractNioChannel {
break;
}
if (readBuf.size() >= maxMessagesPerRead | !autoRead) {
// stop reading and remove op
if (!config.isAutoRead()) {
break;
}
if (readBuf.size() >= maxMessagesPerRead) {
break;
}
}
@ -110,6 +112,11 @@ public abstract class AbstractNioMessageChannel extends AbstractNioChannel {
close(voidPromise());
}
}
} finally {
if (!config().isAutoRead()) {
removeReadOp();
}
}
}
}

View File

@ -16,6 +16,7 @@
package io.netty.channel.oio;
import io.netty.channel.Channel;
import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelPipeline;
import java.io.IOException;
@ -37,11 +38,24 @@ public abstract class AbstractOioMessageChannel extends AbstractOioChannel {
protected void doRead() {
final ChannelPipeline pipeline = pipeline();
boolean closed = false;
final ChannelConfig config = config();
final int maxMessagesPerRead = config.getMaxMessagesPerRead();
Throwable exception = null;
try {
int localReadAmount = doReadMessages(readBuf);
if (localReadAmount < 0) {
for (;;) {
int localRead = doReadMessages(readBuf);
if (localRead == 0) {
break;
}
if (localRead < 0) {
closed = true;
break;
}
if (readBuf.size() >= maxMessagesPerRead || !config.isAutoRead()) {
break;
}
}
} catch (Throwable t) {
exception = t;