Class SimpleChannelDuplexHandler<I,O>
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.SimpleChannelInboundHandler<I>
org.drasyl.peer.connection.handler.SimpleChannelDuplexHandler<I,O>
- All Implemented Interfaces:
io.netty.channel.ChannelHandler,io.netty.channel.ChannelInboundHandler,io.netty.channel.ChannelOutboundHandler
- Direct Known Subclasses:
SignatureHandler,ThreeWayHandshakeClientHandler,ThreeWayHandshakeServerHandler
public abstract class SimpleChannelDuplexHandler<I,O>
extends io.netty.channel.SimpleChannelInboundHandler<I>
implements io.netty.channel.ChannelOutboundHandler
ChannelDuplexHandler which allows to explicit only handle a specific type of messages.
For example here is an implementation which only handle String messages.
public class StringHandler extends
SimpleChannelDuplexHandler<String, String> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String message)
throws Exception {
System.out.println(message);
}
@Override
protected void channelWrite0(ChannelHandlerContext ctx, String message)
throws Exception {
System.out.println(message);
}
}
Be aware that depending of the constructor parameters it will release all handled messages by
passing them to ReferenceCountUtil.release(Object) and fulfills the corresponding ChannelPromise in the channelWrite0(io.netty.channel.ChannelHandlerContext, O, io.netty.channel.ChannelPromise) method. In this case you may need to use ReferenceCountUtil.retain(Object) if you pass the object to the next handler in the ChannelPipeline.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
io.netty.channel.ChannelHandler.Sharable -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedseeSimpleChannelDuplexHandler(boolean, boolean, boolean)withtrueas boolean parameters.protectedSimpleChannelDuplexHandler(boolean inboundAutoRelease, boolean outboundAutoRelease, boolean autoFulfillPromise) Create a new instance which will try to detect the types to match out of the types parameter of the class.protectedSimpleChannelDuplexHandler(Class<? extends I> inboundMessageType, Class<? extends O> outboundMessageType) seeSimpleChannelDuplexHandler(Class, Class, boolean, boolean, boolean)withtrueas boolean values.protectedSimpleChannelDuplexHandler(Class<? extends I> inboundMessageType, Class<? extends O> outboundMessageType, boolean inboundAutoRelease, boolean outboundAutoRelease, boolean autoFulfillPromise) Create a new instance -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturnstrueif the given message should be handled.voidbind(io.netty.channel.ChannelHandlerContext ctx, SocketAddress localAddress, io.netty.channel.ChannelPromise promise) protected abstract voidchannelRead0(io.netty.channel.ChannelHandlerContext ctx, I msg) Is called for each message of typeSimpleChannelDuplexHandleron inbound channel.protected abstract voidchannelWrite0(io.netty.channel.ChannelHandlerContext ctx, O msg, io.netty.channel.ChannelPromise promise) Is called for each message of typeSimpleChannelDuplexHandleron the outbound channel.voidclose(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) voidconnect(io.netty.channel.ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, io.netty.channel.ChannelPromise promise) voidderegister(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) voiddisconnect(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) voidflush(io.netty.channel.ChannelHandlerContext ctx) voidread(io.netty.channel.ChannelHandlerContext ctx) voidwrite(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise) Methods inherited from class io.netty.channel.SimpleChannelInboundHandler
acceptInboundMessage, channelReadMethods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredMethods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharableMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.netty.channel.ChannelHandler
handlerAdded, handlerRemoved
-
Constructor Details
-
SimpleChannelDuplexHandler
protected SimpleChannelDuplexHandler()seeSimpleChannelDuplexHandler(boolean, boolean, boolean)withtrueas boolean parameters. -
SimpleChannelDuplexHandler
protected SimpleChannelDuplexHandler(boolean inboundAutoRelease, boolean outboundAutoRelease, boolean autoFulfillPromise) Create a new instance which will try to detect the types to match out of the types parameter of the class.- Parameters:
inboundAutoRelease-trueif inbound handled messages should be released automatically by passing them toReferenceCountUtil.release(Object).outboundAutoRelease-trueif outbound handled messages should be released automatically by passing them toReferenceCountUtil.release(Object).autoFulfillPromise-trueif outboundChannelPromiseshould be fulfilled automatically
-
SimpleChannelDuplexHandler
protected SimpleChannelDuplexHandler(Class<? extends I> inboundMessageType, Class<? extends O> outboundMessageType) seeSimpleChannelDuplexHandler(Class, Class, boolean, boolean, boolean)withtrueas boolean values. -
SimpleChannelDuplexHandler
protected SimpleChannelDuplexHandler(Class<? extends I> inboundMessageType, Class<? extends O> outboundMessageType, boolean inboundAutoRelease, boolean outboundAutoRelease, boolean autoFulfillPromise) Create a new instance- Parameters:
inboundMessageType- The type of messages to matchoutboundMessageType- The type of messages to matchinboundAutoRelease-trueif inbound handled messages should be released automatically by passing them toReferenceCountUtil.release(Object).outboundAutoRelease-trueif outbound handled messages should be released automatically by passing them toReferenceCountUtil.release(Object).autoFulfillPromise-trueif outboundChannelPromiseshould be fulfilled automatically
-
-
Method Details
-
channelRead0
protected abstract void channelRead0(io.netty.channel.ChannelHandlerContext ctx, I msg) throws Exception Is called for each message of typeSimpleChannelDuplexHandleron inbound channel.- Specified by:
channelRead0in classio.netty.channel.SimpleChannelInboundHandler<I>- Parameters:
ctx- theChannelHandlerContextwhich thisSimpleChannelDuplexHandlerbelongs tomsg- the message to handle- Throws:
Exception- is thrown if an error occurred
-
bind
public void bind(io.netty.channel.ChannelHandlerContext ctx, SocketAddress localAddress, io.netty.channel.ChannelPromise promise) - Specified by:
bindin interfaceio.netty.channel.ChannelOutboundHandler
-
connect
public void connect(io.netty.channel.ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, io.netty.channel.ChannelPromise promise) - Specified by:
connectin interfaceio.netty.channel.ChannelOutboundHandler
-
disconnect
public void disconnect(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) - Specified by:
disconnectin interfaceio.netty.channel.ChannelOutboundHandler
-
close
public void close(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) throws Exception - Specified by:
closein interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
deregister
public void deregister(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) - Specified by:
deregisterin interfaceio.netty.channel.ChannelOutboundHandler
-
read
public void read(io.netty.channel.ChannelHandlerContext ctx) - Specified by:
readin interfaceio.netty.channel.ChannelOutboundHandler
-
write
public void write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise) throws Exception - Specified by:
writein interfaceio.netty.channel.ChannelOutboundHandler- Throws:
Exception
-
acceptOutboundMessage
Returnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelOutboundHandlerin theChannelPipeline. -
channelWrite0
protected abstract void channelWrite0(io.netty.channel.ChannelHandlerContext ctx, O msg, io.netty.channel.ChannelPromise promise) throws Exception Is called for each message of typeSimpleChannelDuplexHandleron the outbound channel.- Parameters:
ctx- theChannelHandlerContextwhich thisSimpleChannelDuplexHandlerbelongs tomsg- the message to handlepromise- the corresponding promise- Throws:
Exception- is thrown if an error occurred
-
flush
public void flush(io.netty.channel.ChannelHandlerContext ctx) - Specified by:
flushin interfaceio.netty.channel.ChannelOutboundHandler
-