Module io.netty5.codec
Package io.netty5.handler.codec
Class MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN>
- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN>
-
- All Implemented Interfaces:
ChannelHandler
- Direct Known Subclasses:
Http2StreamFrameToHttpObjectCodec,HttpContentEncoder
public abstract class MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN> extends ChannelHandlerAdapter
A Codec for on-the-fly encoding/decoding of message. This can be thought of as a combination ofMessageToMessageDecoderandMessageToMessageEncoder. Here is an example of aMessageToMessageCodecwhich just decode fromIntegertoLongand encode fromLongtoInteger.public class NumberCodec extendsBe aware that you need to callMessageToMessageCodec<Integer,Long> {@OverridepublicLongdecode(ChannelHandlerContextctx,Integermsg, List<Object> out) throwsException{ out.add(msg.longValue()); }@OverridepublicIntegerencode(ChannelHandlerContextctx,Longmsg, List<Object> out) throwsException{ out.add(msg.intValue()); } }ReferenceCounted.retain()on messages that are just passed through if they are of typeReferenceCounted. This is needed as theMessageToMessageCodecwill callReferenceCounted.release()on encoded / decoded messages.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedMessageToMessageCodec()Create a new instance which will try to detect the types to decode and encode out of the type parameter of the class.protectedMessageToMessageCodec(Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType)Create a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanacceptInboundMessage(Object msg)Returnstrueif and only if the specified message can be decoded by this codec.booleanacceptOutboundMessage(Object msg)Returnstrueif and only if the specified message can be encoded by this codec.voidchannelRead(ChannelHandlerContext ctx, Object msg)Invoked when the currentChannelhas read a message from the peer.protected voiddecode(ChannelHandlerContext ctx, INBOUND_IN msg)protected voiddecodeAndClose(ChannelHandlerContext ctx, INBOUND_IN msg)protected voidencode(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out)protected voidencodeAndClose(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out)Future<Void>write(ChannelHandlerContext ctx, Object msg)Called once a write operation is made.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelActive, channelExceptionCaught, channelInactive, channelInboundEvent, channelReadComplete, channelRegistered, channelShutdown, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, flush, handlerAdded, handlerRemoved, isSharable, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown
-
-
-
-
Constructor Detail
-
MessageToMessageCodec
protected MessageToMessageCodec()
Create a new instance which will try to detect the types to decode and encode out of the type parameter of the class.
-
MessageToMessageCodec
protected MessageToMessageCodec(Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType)
Create a new instance.- Parameters:
inboundMessageType- The type of messages to decodeoutboundMessageType- The type of messages to encode
-
-
Method Detail
-
channelRead
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
Description copied from interface:ChannelHandlerInvoked when the currentChannelhas read a message from the peer.- Throws:
Exception
-
write
public Future<Void> write(ChannelHandlerContext ctx, Object msg)
Description copied from interface:ChannelHandlerCalled once a write operation is made. The write operation will write the messages through theChannelPipeline. Those are then ready to be flushed to the actualChannelonceChannel.flush()is called.- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to write- Returns:
- the
Futurewhich will be notified once the operation completes.
-
acceptInboundMessage
public boolean acceptInboundMessage(Object msg) throws Exception
Returnstrueif and only if the specified message can be decoded by this codec.- Parameters:
msg- the message- Throws:
Exception
-
acceptOutboundMessage
public boolean acceptOutboundMessage(Object msg) throws Exception
Returnstrueif and only if the specified message can be encoded by this codec.- Parameters:
msg- the message- Throws:
Exception
-
encode
protected void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out) throws Exception
- Throws:
Exception- See Also:
MessageToMessageEncoder.encode(ChannelHandlerContext, Object, List)
-
encodeAndClose
protected void encodeAndClose(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out) throws Exception
-
decode
protected void decode(ChannelHandlerContext ctx, INBOUND_IN msg) throws Exception
- Throws:
Exception- See Also:
MessageToMessageDecoder.decode(ChannelHandlerContext, Object)
-
decodeAndClose
protected void decodeAndClose(ChannelHandlerContext ctx, INBOUND_IN msg) throws Exception
-
-