Add ChannelHandlerContext.hasNext(In|Out)bound(Byte|Message)Buffer

This commit is contained in:
Trustin Lee 2012-06-03 04:35:38 -07:00
parent bbed0602c1
commit d48973b0ff
4 changed files with 90 additions and 26 deletions

View File

@ -52,21 +52,9 @@ public final class ChannelBufferHolder<E> {
case 0: case 0:
return msgBuf != null; return msgBuf != null;
case 1: case 1:
// XXX: Should we introduce hasNext(Inbound|Outbound)(Message|Byte)Buffer() return ctx.hasNextInboundMessageBuffer();
// just because of this?
try {
ctx.nextInboundMessageBuffer();
return true;
} catch (NoSuchBufferException e) {
return false;
}
case 2: case 2:
try { return ctx.hasNextOutboundMessageBuffer();
ctx.nextOutboundMessageBuffer();
return true;
} catch (NoSuchBufferException e) {
return false;
}
default: default:
throw new Error(); throw new Error();
} }
@ -77,19 +65,9 @@ public final class ChannelBufferHolder<E> {
case 0: case 0:
return byteBuf != null; return byteBuf != null;
case 1: case 1:
try { return ctx.hasNextInboundByteBuffer();
ctx.nextInboundByteBuffer();
return true;
} catch (NoSuchBufferException e) {
return false;
}
case 2: case 2:
try { return ctx.hasNextOutboundByteBuffer();
ctx.nextOutboundByteBuffer();
return true;
} catch (NoSuchBufferException e) {
return false;
}
default: default:
throw new Error(); throw new Error();
} }

View File

@ -137,9 +137,13 @@ public interface ChannelHandlerContext
boolean canHandleInbound(); boolean canHandleInbound();
boolean canHandleOutbound(); boolean canHandleOutbound();
boolean hasNextInboundByteBuffer();
boolean hasNextInboundMessageBuffer();
ChannelBuffer nextInboundByteBuffer(); ChannelBuffer nextInboundByteBuffer();
Queue<Object> nextInboundMessageBuffer(); Queue<Object> nextInboundMessageBuffer();
boolean hasNextOutboundByteBuffer();
boolean hasNextOutboundMessageBuffer();
ChannelBuffer nextOutboundByteBuffer(); ChannelBuffer nextOutboundByteBuffer();
Queue<Object> nextOutboundMessageBuffer(); Queue<Object> nextOutboundMessageBuffer();
} }

View File

@ -202,6 +202,26 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
return out; return out;
} }
@Override
public boolean hasNextInboundByteBuffer() {
return DefaultChannelPipeline.hasNextInboundByteBuffer(next);
}
@Override
public boolean hasNextInboundMessageBuffer() {
return DefaultChannelPipeline.hasNextInboundMessageBuffer(next);
}
@Override
public boolean hasNextOutboundByteBuffer() {
return pipeline.hasNextOutboundByteBuffer(prev);
}
@Override
public boolean hasNextOutboundMessageBuffer() {
return pipeline.hasNextOutboundMessageBuffer(prev);
}
@Override @Override
public ChannelBuffer nextInboundByteBuffer() { public ChannelBuffer nextInboundByteBuffer() {
return DefaultChannelPipeline.nextInboundByteBuffer(next); return DefaultChannelPipeline.nextInboundByteBuffer(next);

View File

@ -651,6 +651,32 @@ public class DefaultChannelPipeline implements ChannelPipeline {
return nextOutboundByteBuffer(tail); return nextOutboundByteBuffer(tail);
} }
static boolean hasNextInboundByteBuffer(DefaultChannelHandlerContext ctx) {
for (;;) {
if (ctx == null) {
return false;
}
ChannelBufferHolder<Object> in = ctx.in;
if (in != null && !in.isBypass() && in.hasByteBuffer()) {
return true;
}
ctx = ctx.next;
}
}
static boolean hasNextInboundMessageBuffer(DefaultChannelHandlerContext ctx) {
for (;;) {
if (ctx == null) {
return false;
}
ChannelBufferHolder<Object> in = ctx.inbound();
if (in != null && !in.isBypass() && in.hasMessageBuffer()) {
return true;
}
ctx = ctx.next;
}
}
static ChannelBuffer nextInboundByteBuffer(DefaultChannelHandlerContext ctx) { static ChannelBuffer nextInboundByteBuffer(DefaultChannelHandlerContext ctx) {
for (;;) { for (;;) {
if (ctx == null) { if (ctx == null) {
@ -677,6 +703,42 @@ public class DefaultChannelPipeline implements ChannelPipeline {
} }
} }
boolean hasNextOutboundByteBuffer(DefaultChannelHandlerContext ctx) {
for (;;) {
if (ctx == null) {
if (directOutbound.hasByteBuffer()) {
return true;
} else {
return false;
}
}
ChannelBufferHolder<Object> out = ctx.outbound();
if (out != null && !out.isBypass() && out.hasByteBuffer()) {
return true;
}
ctx = ctx.prev;
}
}
boolean hasNextOutboundMessageBuffer(DefaultChannelHandlerContext ctx) {
for (;;) {
if (ctx == null) {
if (directOutbound.hasMessageBuffer()) {
return true;
} else {
return false;
}
}
ChannelBufferHolder<Object> out = ctx.outbound();
if (out != null && !out.isBypass() && out.hasMessageBuffer()) {
return true;
}
ctx = ctx.prev;
}
}
ChannelBuffer nextOutboundByteBuffer(DefaultChannelHandlerContext ctx) { ChannelBuffer nextOutboundByteBuffer(DefaultChannelHandlerContext ctx) {
for (;;) { for (;;) {
if (ctx == null) { if (ctx == null) {