- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.MessageToMessageEncoder<AddressedEnvelope<M,InetSocketAddress>>
-
- io.netty5.handler.codec.DatagramPacketEncoder<M>
-
- Type Parameters:
M- the type of message to be encoded
- All Implemented Interfaces:
ChannelHandler
public class DatagramPacketEncoder<M> extends MessageToMessageEncoder<AddressedEnvelope<M,InetSocketAddress>>
An encoder that encodes the content inAddressedEnvelopetoDatagramPacketusing the specified message encoder. E.g.,
Note: As UDP packets are out-of-order, you should make sure the encoded message size are not greater than the max safe packet size in your particular network path which guarantees no packet fragmentation.ChannelPipelinepipeline = ...; pipeline.addLast("udpEncoder", newDatagramPacketEncoder(newByteArrayEncoder(...));
-
-
Constructor Summary
Constructors Constructor Description DatagramPacketEncoder(MessageToMessageEncoder<? super M> encoder)Create an encoder that encodes the content inAddressedEnvelopetoDatagramPacketusing the specified message encoder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanacceptOutboundMessage(Object msg)Returnstrueif the given message should be handled.Future<Void>bind(ChannelHandlerContext ctx, SocketAddress localAddress)Called once a bind operation is made.Future<Void>close(ChannelHandlerContext ctx)Called once a close operation is made.Future<Void>connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress)Called once a connect operation is made.Future<Void>deregister(ChannelHandlerContext ctx)Called once a deregister operation is made from the current registeredEventLoop.Future<Void>disconnect(ChannelHandlerContext ctx)Called once a disconnect operation is made.protected voidencode(ChannelHandlerContext ctx, AddressedEnvelope<M,InetSocketAddress> msg, List<Object> out)Encode from one message to another.voidflush(ChannelHandlerContext ctx)Called once a flush operation is made.voidhandlerAdded(ChannelHandlerContext ctx)Gets called after theChannelHandlerwas added to the actual context and it's ready to handle events.voidhandlerRemoved(ChannelHandlerContext ctx)Gets called after theChannelHandlerwas removed from the actual context and it doesn't handle events anymore.booleanisSharable()Returnstrueif this handler is sharable and thus can be added to more than oneChannelPipeline.voidread(ChannelHandlerContext ctx)InterceptsChannelHandlerContext.read().-
Methods inherited from class io.netty5.handler.codec.MessageToMessageEncoder
encodeAndClose, write
-
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
channelActive, channelExceptionCaught, channelInactive, channelInboundEvent, channelRead, channelReadComplete, channelRegistered, channelShutdown, channelUnregistered, channelWritabilityChanged, pendingOutboundBytes, register, sendOutboundEvent, shutdown
-
-
-
-
Constructor Detail
-
DatagramPacketEncoder
public DatagramPacketEncoder(MessageToMessageEncoder<? super M> encoder)
Create an encoder that encodes the content inAddressedEnvelopetoDatagramPacketusing the specified message encoder.- Parameters:
encoder- the specified message encoder
-
-
Method Detail
-
acceptOutboundMessage
public boolean acceptOutboundMessage(Object msg) throws Exception
Description copied from class:MessageToMessageEncoderReturnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelHandlerin theChannelPipeline.- Overrides:
acceptOutboundMessagein classMessageToMessageEncoder<AddressedEnvelope<M,InetSocketAddress>>- Throws:
Exception
-
encode
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<M,InetSocketAddress> msg, List<Object> out) throws Exception
Description copied from class:MessageToMessageEncoderEncode from one message to another. This method will be called for each written message that can be handled by this encoder.The message will be disposed of after this call.
Subclasses that wish to sometimes pass messages through, should instead override the
MessageToMessageEncoder.encodeAndClose(ChannelHandlerContext, Object, List)method.- Overrides:
encodein classMessageToMessageEncoder<AddressedEnvelope<M,InetSocketAddress>>- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToMessageEncoderbelongs to.msg- the message to encode to another one.out- theListinto which produced output messages should be added.- Throws:
Exception- is thrown if an error occurs.
-
bind
public Future<Void> bind(ChannelHandlerContext ctx, SocketAddress localAddress)
Description copied from interface:ChannelHandlerCalled once a bind operation is made.- Parameters:
ctx- theChannelHandlerContextfor which the bind operation is madelocalAddress- theSocketAddressto which it should bound- Returns:
- the
Futurewhich will be notified once the operation completes.
-
connect
public Future<Void> connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress)
Description copied from interface:ChannelHandlerCalled once a connect operation is made.- Parameters:
ctx- theChannelHandlerContextfor which the connect operation is maderemoteAddress- theSocketAddressto which it should connectlocalAddress- theSocketAddresswhich is used as source on connect- Returns:
- the
Futurewhich will be notified once the operation completes.
-
disconnect
public Future<Void> disconnect(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandlerCalled once a disconnect operation is made.- Parameters:
ctx- theChannelHandlerContextfor which the disconnect operation is made- Returns:
- the
Futurewhich will be notified once the operation completes.
-
close
public Future<Void> close(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandlerCalled once a close operation is made.- Parameters:
ctx- theChannelHandlerContextfor which the close operation is made- Returns:
- the
Futurewhich will be notified once the operation completes.
-
deregister
public Future<Void> deregister(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandlerCalled once a deregister operation is made from the current registeredEventLoop.- Parameters:
ctx- theChannelHandlerContextfor which the deregister operation is made- Returns:
- the
Futurewhich will be notified once the operation completes.
-
read
public void read(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandlerInterceptsChannelHandlerContext.read().
-
flush
public void flush(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandlerCalled once a flush operation is made. The flush operation will try to flush out all previous written messages that are pending.- Parameters:
ctx- theChannelHandlerContextfor which the flush operation is made
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerGets called after theChannelHandlerwas added to the actual context and it's ready to handle events.- Throws:
Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerGets called after theChannelHandlerwas removed from the actual context and it doesn't handle events anymore.- Throws:
Exception
-
isSharable
public boolean isSharable()
Description copied from interface:ChannelHandlerReturnstrueif this handler is sharable and thus can be added to more than oneChannelPipeline. By default, this method returnsfalse. If this method returnsfalse, you have to create a new handler instance every time you add it to a pipeline because it has unshared state such as member variables.
-
-