Class DatagramPacketEncoder<M>
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelOutboundHandlerAdapter
io.netty.handler.codec.MessageToMessageEncoder<AddressedEnvelope<M, InetSocketAddress>>
io.netty.handler.codec.DatagramPacketEncoder<M>
- Type Parameters:
M- the type of message to be encoded
- All Implemented Interfaces:
ChannelHandler, ChannelOutboundHandler
public class DatagramPacketEncoder<M>
extends MessageToMessageEncoder<AddressedEnvelope<M, InetSocketAddress>>
An encoder that encodes the content in
AddressedEnvelope to DatagramPacket using
the specified message encoder. E.g.,
ChannelPipeline pipeline = ...;
pipeline.addLast("udpEncoder", new DatagramPacketEncoder(new ProtobufEncoder(...));
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.-
Nested Class Summary
Nested classes/interfaces inherited from interface ChannelHandler
ChannelHandler.Sharable -
Constructor Summary
ConstructorsConstructorDescriptionDatagramPacketEncoder(MessageToMessageEncoder<? super M> encoder) Create an encoder that encodes the content inAddressedEnvelopetoDatagramPacketusing the specified message encoder. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturnstrueif the given message should be handled.voidbind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) CallsChannelOutboundInvoker.bind(SocketAddress, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidclose(ChannelHandlerContext ctx, ChannelPromise promise) CallsChannelOutboundInvoker.close(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidconnect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) CallsChannelOutboundInvoker.connect(SocketAddress, SocketAddress, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidderegister(ChannelHandlerContext ctx, ChannelPromise promise) CallsChannelOutboundInvoker.deregister(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voiddisconnect(ChannelHandlerContext ctx, ChannelPromise promise) CallsChannelOutboundInvoker.disconnect(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.protected voidencode(ChannelHandlerContext ctx, AddressedEnvelope<M, InetSocketAddress> msg, List<Object> out) Encode from one message to an other.voidexceptionCaught(ChannelHandlerContext ctx, Throwable cause) CallsChannelHandlerContext.fireExceptionCaught(Throwable)to forward to the nextChannelHandlerin theChannelPipeline.voidCallsChannelHandlerContext.flush()to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidDo nothing by default, sub-classes may override this method.voidDo nothing by default, sub-classes may override this method.booleanReturntrueif the implementation isChannelHandler.Sharableand so can be added to differentChannelPipelines.voidCallsChannelHandlerContext.read()to forward to the nextChannelOutboundHandlerin theChannelPipeline.Methods inherited from class MessageToMessageEncoder
writeMethods inherited from class ChannelHandlerAdapter
ensureNotSharable
-
Constructor Details
-
DatagramPacketEncoder
Create an encoder that encodes the content inAddressedEnvelopetoDatagramPacketusing the specified message encoder.- Parameters:
encoder- the specified message encoder
-
-
Method Details
-
acceptOutboundMessage
Description copied from class:MessageToMessageEncoderReturnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelOutboundHandlerin 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 an other. This method will be called for each written message that can be handled by this encoder.- Specified by:
encodein classMessageToMessageEncoder<AddressedEnvelope<M, InetSocketAddress>>- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToMessageEncoderbelongs tomsg- the message to encode to an other oneout- theListinto which the encoded msg should be added needs to do some kind of aggregation- Throws:
Exception- is thrown if an error occurs
-
bind
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception Description copied from class:ChannelOutboundHandlerAdapterCallsChannelOutboundInvoker.bind(SocketAddress, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
bindin interfaceChannelOutboundHandler- Overrides:
bindin classChannelOutboundHandlerAdapter- Parameters:
ctx- theChannelHandlerContextfor which the bind operation is madelocalAddress- theSocketAddressto which it should boundpromise- theChannelPromiseto notify once the operation completes- Throws:
Exception- thrown if an error occurs
-
connect
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception Description copied from class:ChannelOutboundHandlerAdapterCallsChannelOutboundInvoker.connect(SocketAddress, SocketAddress, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
connectin interfaceChannelOutboundHandler- Overrides:
connectin classChannelOutboundHandlerAdapter- Parameters:
ctx- theChannelHandlerContextfor which the connect operation is maderemoteAddress- theSocketAddressto which it should connectlocalAddress- theSocketAddresswhich is used as source on connectpromise- theChannelPromiseto notify once the operation completes- Throws:
Exception- thrown if an error occurs
-
disconnect
Description copied from class:ChannelOutboundHandlerAdapterCallsChannelOutboundInvoker.disconnect(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
disconnectin interfaceChannelOutboundHandler- Overrides:
disconnectin classChannelOutboundHandlerAdapter- Parameters:
ctx- theChannelHandlerContextfor which the disconnect operation is madepromise- theChannelPromiseto notify once the operation completes- Throws:
Exception- thrown if an error occurs
-
close
Description copied from class:ChannelOutboundHandlerAdapterCallsChannelOutboundInvoker.close(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
closein interfaceChannelOutboundHandler- Overrides:
closein classChannelOutboundHandlerAdapter- Parameters:
ctx- theChannelHandlerContextfor which the close operation is madepromise- theChannelPromiseto notify once the operation completes- Throws:
Exception- thrown if an error occurs
-
deregister
Description copied from class:ChannelOutboundHandlerAdapterCallsChannelOutboundInvoker.deregister(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
deregisterin interfaceChannelOutboundHandler- Overrides:
deregisterin classChannelOutboundHandlerAdapter- Parameters:
ctx- theChannelHandlerContextfor which the close operation is madepromise- theChannelPromiseto notify once the operation completes- Throws:
Exception- thrown if an error occurs
-
read
Description copied from class:ChannelOutboundHandlerAdapterCallsChannelHandlerContext.read()to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
readin interfaceChannelOutboundHandler- Overrides:
readin classChannelOutboundHandlerAdapter- Throws:
Exception
-
flush
Description copied from class:ChannelOutboundHandlerAdapterCallsChannelHandlerContext.flush()to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
flushin interfaceChannelOutboundHandler- Overrides:
flushin classChannelOutboundHandlerAdapter- Parameters:
ctx- theChannelHandlerContextfor which the flush operation is made- Throws:
Exception- thrown if an error occurs
-
handlerAdded
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter- Throws:
Exception
-
handlerRemoved
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerRemovedin interfaceChannelHandler- Overrides:
handlerRemovedin classChannelHandlerAdapter- Throws:
Exception
-
exceptionCaught
Description copied from class:ChannelHandlerAdapterCallsChannelHandlerContext.fireExceptionCaught(Throwable)to forward to the nextChannelHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
exceptionCaughtin interfaceChannelHandler- Overrides:
exceptionCaughtin classChannelHandlerAdapter- Throws:
Exception
-
isSharable
public boolean isSharable()Description copied from class:ChannelHandlerAdapterReturntrueif the implementation isChannelHandler.Sharableand so can be added to differentChannelPipelines.- Overrides:
isSharablein classChannelHandlerAdapter
-