Package io.netty.handler.codec
Class DatagramPacketEncoder<M>
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelOutboundHandlerAdapter
-
- io.netty.handler.codec.MessageToMessageEncoder<AddressedEnvelope<M,java.net.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,java.net.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(newProtobufEncoder(...));
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Constructor Summary
Constructors Constructor Description DatagramPacketEncoder(MessageToMessageEncoder<? super M> encoder)Create an encoder that encodes the content inAddressedEnvelopetoDatagramPacketusing the specified message encoder.
-
Method Summary
-
Methods inherited from class io.netty.handler.codec.MessageToMessageEncoder
write
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable
-
-
-
-
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(java.lang.Object msg) throws java.lang.ExceptionDescription copied from class:MessageToMessageEncoderReturnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelOutboundHandlerin theChannelPipeline.- Overrides:
acceptOutboundMessagein classMessageToMessageEncoder<AddressedEnvelope<M,java.net.InetSocketAddress>>- Throws:
java.lang.Exception
-
encode
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<M,java.net.InetSocketAddress> msg, java.util.List<java.lang.Object> out) throws java.lang.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,java.net.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:
java.lang.Exception- is thrown if an error occurs
-
bind
public void bind(ChannelHandlerContext ctx, java.net.SocketAddress localAddress, ChannelPromise promise) throws java.lang.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:
java.lang.Exception- thrown if an error occurs
-
connect
public void connect(ChannelHandlerContext ctx, java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress, ChannelPromise promise) throws java.lang.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:
java.lang.Exception- thrown if an error occurs
-
disconnect
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
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:
java.lang.Exception- thrown if an error occurs
-
close
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
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:
java.lang.Exception- thrown if an error occurs
-
deregister
public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
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:
java.lang.Exception- thrown if an error occurs
-
read
public void read(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
flush
public void flush(ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception- thrown if an error occurs
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter- Throws:
java.lang.Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerRemovedin interfaceChannelHandler- Overrides:
handlerRemovedin classChannelHandlerAdapter- Throws:
java.lang.Exception
-
exceptionCaught
public void exceptionCaught(ChannelHandlerContext ctx, java.lang.Throwable cause) throws java.lang.Exception
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:
java.lang.Exception
-
isSharable
public boolean isSharable()
Description copied from class:ChannelHandlerAdapterReturntrueif the implementation isChannelHandler.Sharableand so can be added to differentChannelPipelines.- Overrides:
isSharablein classChannelHandlerAdapter
-
-