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

View File

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

View File

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