Add combined codec classes and related utility method
This commit is contained in:
parent
e5da7b53dd
commit
e846505ceb
@ -0,0 +1,51 @@
|
|||||||
|
package io.netty.handler.codec;
|
||||||
|
|
||||||
|
import io.netty.channel.ChannelBufferHolder;
|
||||||
|
import io.netty.channel.ChannelFuture;
|
||||||
|
import io.netty.channel.ChannelHandlerAdapter;
|
||||||
|
import io.netty.channel.ChannelInboundHandlerContext;
|
||||||
|
import io.netty.channel.ChannelOutboundHandlerContext;
|
||||||
|
|
||||||
|
public abstract class MessageToMessageCodec<INBOUND_IN, INBOUND_OUT, OUTBOUND_IN, OUTBOUND_OUT>
|
||||||
|
extends ChannelHandlerAdapter<INBOUND_IN, OUTBOUND_IN> {
|
||||||
|
|
||||||
|
private final MessageToMessageEncoder<OUTBOUND_IN, OUTBOUND_OUT> encoder =
|
||||||
|
new MessageToMessageEncoder<OUTBOUND_IN, OUTBOUND_OUT>() {
|
||||||
|
@Override
|
||||||
|
public OUTBOUND_OUT encode(ChannelOutboundHandlerContext<OUTBOUND_IN> ctx, OUTBOUND_IN msg) throws Exception {
|
||||||
|
return MessageToMessageCodec.this.encode(ctx, msg);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private final MessageToMessageDecoder<INBOUND_IN, INBOUND_OUT> decoder =
|
||||||
|
new MessageToMessageDecoder<INBOUND_IN, INBOUND_OUT>() {
|
||||||
|
@Override
|
||||||
|
public INBOUND_OUT decode(ChannelInboundHandlerContext<INBOUND_IN> ctx, INBOUND_IN msg) throws Exception {
|
||||||
|
return MessageToMessageCodec.this.decode(ctx, msg);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChannelBufferHolder<INBOUND_IN> newInboundBuffer(ChannelInboundHandlerContext<INBOUND_IN> ctx) throws Exception {
|
||||||
|
return decoder.newInboundBuffer(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void inboundBufferUpdated(
|
||||||
|
ChannelInboundHandlerContext<INBOUND_IN> ctx) throws Exception {
|
||||||
|
decoder.inboundBufferUpdated(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChannelBufferHolder<OUTBOUND_IN> newOutboundBuffer(ChannelOutboundHandlerContext<OUTBOUND_IN> ctx) throws Exception {
|
||||||
|
return encoder.newOutboundBuffer(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void flush(ChannelOutboundHandlerContext<OUTBOUND_IN> ctx, ChannelFuture future) throws Exception {
|
||||||
|
encoder.flush(ctx, future);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract OUTBOUND_OUT encode(ChannelOutboundHandlerContext<OUTBOUND_IN> ctx, OUTBOUND_IN msg) throws Exception;
|
||||||
|
public abstract INBOUND_OUT decode(ChannelInboundHandlerContext<INBOUND_IN> ctx, INBOUND_IN msg) throws Exception;
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
package io.netty.handler.codec;
|
||||||
|
|
||||||
|
import io.netty.buffer.ChannelBuffer;
|
||||||
|
import io.netty.channel.ChannelBufferHolder;
|
||||||
|
import io.netty.channel.ChannelFuture;
|
||||||
|
import io.netty.channel.ChannelHandlerAdapter;
|
||||||
|
import io.netty.channel.ChannelInboundHandlerContext;
|
||||||
|
import io.netty.channel.ChannelOutboundHandlerContext;
|
||||||
|
|
||||||
|
public abstract class StreamToMessageCodec<INBOUND_OUT, OUTBOUND_IN>
|
||||||
|
extends ChannelHandlerAdapter<Byte, OUTBOUND_IN> {
|
||||||
|
|
||||||
|
private final MessageToStreamEncoder<OUTBOUND_IN> encoder =
|
||||||
|
new MessageToStreamEncoder<OUTBOUND_IN>() {
|
||||||
|
@Override
|
||||||
|
public void encode(
|
||||||
|
ChannelOutboundHandlerContext<OUTBOUND_IN> ctx,
|
||||||
|
OUTBOUND_IN msg, ChannelBuffer out) throws Exception {
|
||||||
|
StreamToMessageCodec.this.encode(ctx, msg, out);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private final StreamToMessageDecoder<INBOUND_OUT> decoder =
|
||||||
|
new StreamToMessageDecoder<INBOUND_OUT>() {
|
||||||
|
@Override
|
||||||
|
public INBOUND_OUT decode(
|
||||||
|
ChannelInboundHandlerContext<Byte> ctx, ChannelBuffer in) throws Exception {
|
||||||
|
return StreamToMessageCodec.this.decode(ctx, in);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChannelBufferHolder<Byte> newInboundBuffer(
|
||||||
|
ChannelInboundHandlerContext<Byte> ctx) throws Exception {
|
||||||
|
return decoder.newInboundBuffer(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void inboundBufferUpdated(ChannelInboundHandlerContext<Byte> ctx) throws Exception {
|
||||||
|
decoder.inboundBufferUpdated(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChannelBufferHolder<OUTBOUND_IN> newOutboundBuffer(
|
||||||
|
ChannelOutboundHandlerContext<OUTBOUND_IN> ctx) throws Exception {
|
||||||
|
return encoder.newOutboundBuffer(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void flush(
|
||||||
|
ChannelOutboundHandlerContext<OUTBOUND_IN> ctx, ChannelFuture future) throws Exception {
|
||||||
|
encoder.flush(ctx, future);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void encode(
|
||||||
|
ChannelOutboundHandlerContext<OUTBOUND_IN> ctx,
|
||||||
|
OUTBOUND_IN msg, ChannelBuffer out) throws Exception;
|
||||||
|
|
||||||
|
public abstract INBOUND_OUT decode(
|
||||||
|
ChannelInboundHandlerContext<Byte> ctx, ChannelBuffer in) throws Exception;
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package io.netty.handler.codec;
|
||||||
|
|
||||||
|
import io.netty.buffer.ChannelBuffer;
|
||||||
|
import io.netty.channel.ChannelBufferHolder;
|
||||||
|
import io.netty.channel.ChannelFuture;
|
||||||
|
import io.netty.channel.ChannelHandlerAdapter;
|
||||||
|
import io.netty.channel.ChannelInboundHandlerContext;
|
||||||
|
import io.netty.channel.ChannelOutboundHandlerContext;
|
||||||
|
|
||||||
|
public abstract class StreamToStreamCodec extends ChannelHandlerAdapter<Byte, Byte> {
|
||||||
|
|
||||||
|
private final StreamToStreamEncoder encoder = new StreamToStreamEncoder() {
|
||||||
|
@Override
|
||||||
|
public void encode(
|
||||||
|
ChannelOutboundHandlerContext<Byte> ctx,
|
||||||
|
ChannelBuffer in, ChannelBuffer out) throws Exception {
|
||||||
|
StreamToStreamCodec.this.encode(ctx, in, out);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private final StreamToStreamDecoder decoder = new StreamToStreamDecoder() {
|
||||||
|
@Override
|
||||||
|
public void decode(
|
||||||
|
ChannelInboundHandlerContext<Byte> ctx,
|
||||||
|
ChannelBuffer in, ChannelBuffer out) throws Exception {
|
||||||
|
StreamToStreamCodec.this.decode(ctx, in, out);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChannelBufferHolder<Byte> newInboundBuffer(
|
||||||
|
ChannelInboundHandlerContext<Byte> ctx) throws Exception {
|
||||||
|
return decoder.newInboundBuffer(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void inboundBufferUpdated(ChannelInboundHandlerContext<Byte> ctx) throws Exception {
|
||||||
|
decoder.inboundBufferUpdated(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChannelBufferHolder<Byte> newOutboundBuffer(
|
||||||
|
ChannelOutboundHandlerContext<Byte> ctx) throws Exception {
|
||||||
|
return encoder.newOutboundBuffer(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void flush(
|
||||||
|
ChannelOutboundHandlerContext<Byte> ctx, ChannelFuture future) throws Exception {
|
||||||
|
encoder.flush(ctx, future);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void encode(
|
||||||
|
ChannelOutboundHandlerContext<Byte> ctx,
|
||||||
|
ChannelBuffer in, ChannelBuffer out) throws Exception;
|
||||||
|
|
||||||
|
public abstract void decode(
|
||||||
|
ChannelInboundHandlerContext<Byte> ctx,
|
||||||
|
ChannelBuffer in, ChannelBuffer out) throws Exception;
|
||||||
|
}
|
@ -3,6 +3,146 @@ package io.netty.channel;
|
|||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
|
|
||||||
public abstract class ChannelHandlerAdapter<I, O> implements ChannelInboundHandler<I>, ChannelOutboundHandler<O> {
|
public abstract class ChannelHandlerAdapter<I, O> implements ChannelInboundHandler<I>, ChannelOutboundHandler<O> {
|
||||||
|
|
||||||
|
public static <I, O> ChannelHandlerAdapter<I, O> combine(
|
||||||
|
ChannelInboundHandler<I> inboundHandler, ChannelOutboundHandler<O> outboundHandler) {
|
||||||
|
if (inboundHandler == null) {
|
||||||
|
throw new NullPointerException("inboundHandler");
|
||||||
|
}
|
||||||
|
if (outboundHandler == null) {
|
||||||
|
throw new NullPointerException("outboundHandler");
|
||||||
|
}
|
||||||
|
|
||||||
|
final ChannelInboundHandler<I> in = inboundHandler;
|
||||||
|
final ChannelOutboundHandler<O> out = outboundHandler;
|
||||||
|
return new ChannelHandlerAdapter<I, O>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChannelBufferHolder<I> newInboundBuffer(
|
||||||
|
ChannelInboundHandlerContext<I> ctx) throws Exception {
|
||||||
|
return in.newInboundBuffer(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChannelBufferHolder<O> newOutboundBuffer(
|
||||||
|
ChannelOutboundHandlerContext<O> ctx) throws Exception {
|
||||||
|
return out.newOutboundBuffer(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
try {
|
||||||
|
in.beforeAdd(ctx);
|
||||||
|
} finally {
|
||||||
|
out.beforeAdd(ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterAdd(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
try {
|
||||||
|
in.afterAdd(ctx);
|
||||||
|
} finally {
|
||||||
|
out.afterAdd(ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void beforeRemove(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
try {
|
||||||
|
in.beforeRemove(ctx);
|
||||||
|
} finally {
|
||||||
|
out.beforeRemove(ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterRemove(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
try {
|
||||||
|
in.afterRemove(ctx);
|
||||||
|
} finally {
|
||||||
|
out.afterRemove(ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelRegistered(ChannelInboundHandlerContext<I> ctx) throws Exception {
|
||||||
|
in.channelRegistered(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelUnregistered(ChannelInboundHandlerContext<I> ctx) throws Exception {
|
||||||
|
in.channelUnregistered(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelActive(ChannelInboundHandlerContext<I> ctx) throws Exception {
|
||||||
|
in.channelActive(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelInactive(ChannelInboundHandlerContext<I> ctx) throws Exception {
|
||||||
|
in.channelInactive(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void exceptionCaught(
|
||||||
|
ChannelInboundHandlerContext<I> ctx, Throwable cause) throws Exception {
|
||||||
|
in.exceptionCaught(ctx, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void userEventTriggered(
|
||||||
|
ChannelInboundHandlerContext<I> ctx, Object evt) throws Exception {
|
||||||
|
in.userEventTriggered(ctx, evt);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void inboundBufferUpdated(ChannelInboundHandlerContext<I> ctx) throws Exception {
|
||||||
|
in.inboundBufferUpdated(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void bind(
|
||||||
|
ChannelOutboundHandlerContext<O> ctx,
|
||||||
|
SocketAddress localAddress, ChannelFuture future) throws Exception {
|
||||||
|
out.bind(ctx, localAddress, future);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void connect(
|
||||||
|
ChannelOutboundHandlerContext<O> ctx,
|
||||||
|
SocketAddress remoteAddress, SocketAddress localAddress,
|
||||||
|
ChannelFuture future) throws Exception {
|
||||||
|
out.connect(ctx, remoteAddress, localAddress, future);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void disconnect(
|
||||||
|
ChannelOutboundHandlerContext<O> ctx, ChannelFuture future) throws Exception {
|
||||||
|
out.disconnect(ctx, future);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close(
|
||||||
|
ChannelOutboundHandlerContext<O> ctx, ChannelFuture future) throws Exception {
|
||||||
|
out.close(ctx, future);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deregister(
|
||||||
|
ChannelOutboundHandlerContext<O> ctx, ChannelFuture future) throws Exception {
|
||||||
|
out.deregister(ctx, future);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void flush(
|
||||||
|
ChannelOutboundHandlerContext<O> ctx, ChannelFuture future) throws Exception {
|
||||||
|
out.flush(ctx, future);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
|
public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
|
||||||
// Do nothing by default.
|
// Do nothing by default.
|
||||||
|
Loading…
Reference in New Issue
Block a user