[#983] Force the user to implement an actual ChannelInboundHandler or ChannelOutboundHandler

For this ChannelInboundHandler* and ChannelOutboundHandler* was made package private
This commit is contained in:
Norman Maurer 2013-01-31 15:50:27 +01:00
parent 1f2aca02da
commit ec013bf2d3
10 changed files with 51 additions and 44 deletions

View File

@ -20,7 +20,7 @@ import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundByteHandler;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelInboundMessageHandler;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
@ -143,21 +143,21 @@ public abstract class SpdyOrHttpChooser extends ChannelHandlerAdapter implements
}
/**
* Create the {@link ChannelInboundHandler} that is responsible for handling the http requests
* Create the {@link ChannelInboundMessageHandler} that is responsible for handling the http requests
* when the {@link SelectedProtocol} was {@link SelectedProtocol#HttpVersion1_0} or
* {@link SelectedProtocol#HttpVersion1_1}
*/
protected abstract ChannelInboundHandler createHttpRequestHandlerForHttp();
protected abstract ChannelInboundMessageHandler<?> createHttpRequestHandlerForHttp();
/**
* Create the {@link ChannelInboundHandler} that is responsible for handling the http responses
* Create the {@link ChannelInboundMessageHandler} that is responsible for handling the http responses
* when the {@link SelectedProtocol} was {@link SelectedProtocol#SpdyVersion2} or
* {@link SelectedProtocol#SpdyVersion3}.
*
* Bye default this getMethod will just delecate to {@link #createHttpRequestHandlerForHttp()}, but
* sub-classes may override this to change the behaviour.
*/
protected ChannelInboundHandler createHttpRequestHandlerForSpdy() {
protected ChannelInboundMessageHandler<?> createHttpRequestHandlerForSpdy() {
return createHttpRequestHandlerForHttp();
}
}

View File

@ -16,11 +16,10 @@
package io.netty.handler.codec;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelHandlerUtil;
import io.netty.channel.ChannelInboundByteHandler;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInboundByteHandlerAdapter;
/**
* {@link ChannelInboundByteHandler} which decodes bytes in a stream-like fashion from one {@link ByteBuf} to an other
@ -40,7 +39,7 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
* </pre>
*/
public abstract class ByteToMessageDecoder
extends ChannelInboundHandlerAdapter implements ChannelInboundByteHandler {
extends ChannelInboundByteHandlerAdapter {
private volatile boolean singleDecode;
private boolean decodeWasNull;
@ -64,19 +63,10 @@ public abstract class ByteToMessageDecoder
public boolean isSingleDecode() {
return singleDecode;
}
@Override
public ByteBuf newInboundBuffer(ChannelHandlerContext ctx) throws Exception {
return ctx.alloc().buffer();
}
@Override
public void discardInboundReadBytes(ChannelHandlerContext ctx) throws Exception {
ctx.inboundByteBuffer().discardSomeReadBytes();
}
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception {
callDecode(ctx);
public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
callDecode(ctx, in);
}
@Override
@ -94,7 +84,7 @@ public abstract class ByteToMessageDecoder
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
ByteBuf in = ctx.inboundByteBuffer();
if (in.readable()) {
callDecode(ctx);
callDecode(ctx, in);
}
try {
@ -112,11 +102,9 @@ public abstract class ByteToMessageDecoder
ctx.fireChannelInactive();
}
protected void callDecode(ChannelHandlerContext ctx) {
protected void callDecode(ChannelHandlerContext ctx, ByteBuf in) {
boolean wasNull = false;
ByteBuf in = ctx.inboundByteBuffer();
boolean decoded = false;
while (in.readable()) {
try {

View File

@ -19,9 +19,9 @@ import io.netty.buffer.MessageBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelHandlerUtil;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInboundMessageHandler;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelStateHandlerAdapter;
/**
* {@link ChannelInboundMessageHandler} which decodes from one message to an other message
@ -46,7 +46,7 @@ import io.netty.channel.ChannelPipeline;
*
*/
public abstract class MessageToMessageDecoder<I>
extends ChannelInboundHandlerAdapter implements ChannelInboundMessageHandler<I> {
extends ChannelStateHandlerAdapter implements ChannelInboundMessageHandler<I> {
private final Class<?>[] acceptedMsgTypes;
@ -141,4 +141,9 @@ public abstract class MessageToMessageDecoder<I>
protected void freeInboundMessage(I msg) throws Exception {
ChannelHandlerUtil.freeMessage(msg);
}
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
ctx.inboundMessageBuffer().free();
}
}

View File

@ -363,7 +363,7 @@ public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder {
replayable.terminate();
ByteBuf in = cumulation;
if (in.readable()) {
callDecode(ctx);
callDecode(ctx, in);
}
try {
@ -385,7 +385,7 @@ public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder {
}
@Override
protected void callDecode(ChannelHandlerContext ctx) {
protected void callDecode(ChannelHandlerContext ctx, ByteBuf buf) {
boolean wasNull = false;
ByteBuf in = cumulation;

View File

@ -22,11 +22,11 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInboundMessageHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelStateHandlerAdapter;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.socket.SocketChannel;
@ -225,7 +225,7 @@ public final class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, Se
}
private static class ServerBootstrapAcceptor
extends ChannelInboundHandlerAdapter implements ChannelInboundMessageHandler<Channel> {
extends ChannelStateHandlerAdapter implements ChannelInboundMessageHandler<Channel> {
private final EventLoopGroup childGroup;
private final ChannelHandler childHandler;
@ -281,6 +281,11 @@ public final class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, Se
}
}
}
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
ctx.inboundMessageBuffer().free();
}
}
@Override

View File

@ -20,7 +20,7 @@ import io.netty.buffer.Buf;
/**
* {@link ChannelStateHandler} which handles inbound data.
*/
public interface ChannelInboundHandler extends ChannelStateHandler {
interface ChannelInboundHandler extends ChannelStateHandler {
/**
* Return the {@link Buf} which will be used for inbound data for the given {@link ChannelHandlerContext}.
*/

View File

@ -21,10 +21,10 @@ import io.netty.buffer.Buf;
/**
* Abstract base class for a {@link ChannelHandler} that handles inbound data.
*
* Most of the times you either want to extend {@link ChannelInboundByteHandlerAdapter} or
* Please either extend {@link ChannelInboundByteHandlerAdapter} or
* {@link ChannelInboundMessageHandlerAdapter}.
*/
public abstract class ChannelInboundHandlerAdapter
abstract class ChannelInboundHandlerAdapter
extends ChannelStateHandlerAdapter implements ChannelInboundHandler {
/**

View File

@ -20,7 +20,7 @@ import io.netty.buffer.Buf;
/**
* {@link ChannelOperationHandler} which handles outbound data.
*/
public interface ChannelOutboundHandler extends ChannelOperationHandler {
interface ChannelOutboundHandler extends ChannelOperationHandler {
/**
* Return the {@link Buf} which will be used for outbound data for the given {@link ChannelHandlerContext}.
*/

View File

@ -21,10 +21,10 @@ import io.netty.buffer.Buf;
/**
* Abstract base class for a {@link ChannelHandler} that handles outbound data.
*
* Most of the times you either want to extend {@link ChannelOutboundByteHandlerAdapter} or
* Please extend {@link ChannelOutboundByteHandlerAdapter} or
* {@link ChannelOutboundMessageHandlerAdapter}.
*/
public abstract class ChannelOutboundHandlerAdapter
abstract class ChannelOutboundHandlerAdapter
extends ChannelOperationHandlerAdapter implements ChannelOutboundHandler {
/**

View File

@ -25,11 +25,11 @@ import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelInboundByteHandler;
import io.netty.channel.ChannelInboundMessageHandler;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.channel.ChannelStateHandlerAdapter;
import io.netty.channel.DefaultChannelConfig;
import io.netty.channel.EventLoop;
import io.netty.logging.InternalLogger;
@ -80,8 +80,10 @@ public abstract class AbstractEmbeddedChannel<O> extends AbstractChannel {
break;
}
nHandlers ++;
p.addLast(h);
if (h instanceof ChannelInboundHandler || h instanceof ChannelOutboundHandler) {
ChannelHandlerContext ctx = p.addLast(h).context(h);
if (ctx.hasInboundByteBuffer() || ctx.hasOutboundByteBuffer()
|| ctx.hasInboundMessageBuffer() || ctx.hasOutboundMessageBuffer()) {
hasBuffer = true;
}
}
@ -324,9 +326,10 @@ public abstract class AbstractEmbeddedChannel<O> extends AbstractChannel {
}
}
private final class LastInboundMessageHandler extends ChannelInboundHandlerAdapter {
private final class LastInboundMessageHandler extends ChannelStateHandlerAdapter
implements ChannelInboundMessageHandler<Object> {
@Override
public Buf newInboundBuffer(ChannelHandlerContext ctx) throws Exception {
public MessageBuf<Object> newInboundBuffer(ChannelHandlerContext ctx) throws Exception {
return lastInboundMessageBuffer;
}
@ -347,12 +350,18 @@ public abstract class AbstractEmbeddedChannel<O> extends AbstractChannel {
}
}
private final class LastInboundByteHandler extends ChannelInboundHandlerAdapter {
private final class LastInboundByteHandler extends ChannelStateHandlerAdapter
implements ChannelInboundByteHandler {
@Override
public Buf newInboundBuffer(ChannelHandlerContext ctx) throws Exception {
public ByteBuf newInboundBuffer(ChannelHandlerContext ctx) throws Exception {
return lastInboundByteBuffer;
}
@Override
public void discardInboundReadBytes(ChannelHandlerContext ctx) throws Exception {
// nothing
}
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
// Do NOT free the buffer.