- 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 inAddressedEnvelope
toDatagramPacket
using 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.ChannelPipeline
pipeline = ...; pipeline.addLast("udpEncoder", newDatagramPacketEncoder
(newByteArrayEncoder
(...));
-
-
Constructor Summary
Constructors Constructor Description DatagramPacketEncoder(MessageToMessageEncoder<? super M> encoder)
Create an encoder that encodes the content inAddressedEnvelope
toDatagramPacket
using the specified message encoder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
acceptOutboundMessage(Object msg)
Returnstrue
if 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 void
encode(ChannelHandlerContext ctx, AddressedEnvelope<M,InetSocketAddress> msg, List<Object> out)
Encode from one message to another.void
flush(ChannelHandlerContext ctx)
Called once a flush operation is made.void
handlerAdded(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events.void
handlerRemoved(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was removed from the actual context and it doesn't handle events anymore.boolean
isSharable()
Returnstrue
if this handler is sharable and thus can be added to more than oneChannelPipeline
.void
read(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 inAddressedEnvelope
toDatagramPacket
using the specified message encoder.- Parameters:
encoder
- the specified message encoder
-
-
Method Detail
-
acceptOutboundMessage
public boolean acceptOutboundMessage(Object msg) throws Exception
Description copied from class:MessageToMessageEncoder
Returnstrue
if the given message should be handled. Iffalse
it will be passed to the nextChannelHandler
in theChannelPipeline
.- Overrides:
acceptOutboundMessage
in 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:MessageToMessageEncoder
Encode 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:
encode
in classMessageToMessageEncoder<AddressedEnvelope<M,InetSocketAddress>>
- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToMessageEncoder
belongs to.msg
- the message to encode to another one.out
- theList
into 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:ChannelHandler
Called once a bind operation is made.- Parameters:
ctx
- theChannelHandlerContext
for which the bind operation is madelocalAddress
- theSocketAddress
to which it should bound- Returns:
- the
Future
which will be notified once the operation completes.
-
connect
public Future<Void> connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress)
Description copied from interface:ChannelHandler
Called once a connect operation is made.- Parameters:
ctx
- theChannelHandlerContext
for which the connect operation is maderemoteAddress
- theSocketAddress
to which it should connectlocalAddress
- theSocketAddress
which is used as source on connect- Returns:
- the
Future
which will be notified once the operation completes.
-
disconnect
public Future<Void> disconnect(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a disconnect operation is made.- Parameters:
ctx
- theChannelHandlerContext
for which the disconnect operation is made- Returns:
- the
Future
which will be notified once the operation completes.
-
close
public Future<Void> close(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a close operation is made.- Parameters:
ctx
- theChannelHandlerContext
for which the close operation is made- Returns:
- the
Future
which will be notified once the operation completes.
-
deregister
public Future<Void> deregister(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a deregister operation is made from the current registeredEventLoop
.- Parameters:
ctx
- theChannelHandlerContext
for which the deregister operation is made- Returns:
- the
Future
which will be notified once the operation completes.
-
read
public void read(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
InterceptsChannelHandlerContext.read()
.
-
flush
public void flush(ChannelHandlerContext ctx)
Description copied from interface:ChannelHandler
Called once a flush operation is made. The flush operation will try to flush out all previous written messages that are pending.- Parameters:
ctx
- theChannelHandlerContext
for which the flush operation is made
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Gets called after theChannelHandler
was 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:ChannelHandler
Gets called after theChannelHandler
was removed from the actual context and it doesn't handle events anymore.- Throws:
Exception
-
isSharable
public boolean isSharable()
Description copied from interface:ChannelHandler
Returnstrue
if 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.
-
-