Class DatagramPacketEncoder<M>

  • Type Parameters:
    M - the type of message to be encoded
    All Implemented Interfaces:
    io.netty.channel.ChannelHandler, io.netty.channel.ChannelOutboundHandler

    public class DatagramPacketEncoder<M>
    extends MessageToMessageEncoder<io.netty.channel.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 io.netty.channel.ChannelHandler

        io.netty.channel.ChannelHandler.Sharable
    • Constructor Summary

      Constructors 
      Constructor Description
      DatagramPacketEncoder​(MessageToMessageEncoder<? super M> encoder)
      Create an encoder that encodes the content in AddressedEnvelope to DatagramPacket using the specified message encoder.
    • Constructor Detail

      • DatagramPacketEncoder

        public DatagramPacketEncoder​(MessageToMessageEncoder<? super M> encoder)
        Create an encoder that encodes the content in AddressedEnvelope to DatagramPacket using the specified message encoder.
        Parameters:
        encoder - the specified message encoder
    • Method Detail

      • encode

        protected void encode​(io.netty.channel.ChannelHandlerContext ctx,
                              io.netty.channel.AddressedEnvelope<M,​InetSocketAddress> msg,
                              List<Object> out)
                       throws Exception
        Description copied from class: MessageToMessageEncoder
        Encode from one message to an other. This method will be called for each written message that can be handled by this encoder.
        Specified by:
        encode in class MessageToMessageEncoder<io.netty.channel.AddressedEnvelope<M,​InetSocketAddress>>
        Parameters:
        ctx - the ChannelHandlerContext which this MessageToMessageEncoder belongs to
        msg - the message to encode to an other one
        out - the List into 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​(io.netty.channel.ChannelHandlerContext ctx,
                         SocketAddress localAddress,
                         io.netty.channel.ChannelPromise promise)
                  throws Exception
        Specified by:
        bind in interface io.netty.channel.ChannelOutboundHandler
        Overrides:
        bind in class io.netty.channel.ChannelOutboundHandlerAdapter
        Throws:
        Exception
      • connect

        public void connect​(io.netty.channel.ChannelHandlerContext ctx,
                            SocketAddress remoteAddress,
                            SocketAddress localAddress,
                            io.netty.channel.ChannelPromise promise)
                     throws Exception
        Specified by:
        connect in interface io.netty.channel.ChannelOutboundHandler
        Overrides:
        connect in class io.netty.channel.ChannelOutboundHandlerAdapter
        Throws:
        Exception
      • disconnect

        public void disconnect​(io.netty.channel.ChannelHandlerContext ctx,
                               io.netty.channel.ChannelPromise promise)
                        throws Exception
        Specified by:
        disconnect in interface io.netty.channel.ChannelOutboundHandler
        Overrides:
        disconnect in class io.netty.channel.ChannelOutboundHandlerAdapter
        Throws:
        Exception
      • close

        public void close​(io.netty.channel.ChannelHandlerContext ctx,
                          io.netty.channel.ChannelPromise promise)
                   throws Exception
        Specified by:
        close in interface io.netty.channel.ChannelOutboundHandler
        Overrides:
        close in class io.netty.channel.ChannelOutboundHandlerAdapter
        Throws:
        Exception
      • deregister

        public void deregister​(io.netty.channel.ChannelHandlerContext ctx,
                               io.netty.channel.ChannelPromise promise)
                        throws Exception
        Specified by:
        deregister in interface io.netty.channel.ChannelOutboundHandler
        Overrides:
        deregister in class io.netty.channel.ChannelOutboundHandlerAdapter
        Throws:
        Exception
      • read

        public void read​(io.netty.channel.ChannelHandlerContext ctx)
                  throws Exception
        Specified by:
        read in interface io.netty.channel.ChannelOutboundHandler
        Overrides:
        read in class io.netty.channel.ChannelOutboundHandlerAdapter
        Throws:
        Exception
      • flush

        public void flush​(io.netty.channel.ChannelHandlerContext ctx)
                   throws Exception
        Specified by:
        flush in interface io.netty.channel.ChannelOutboundHandler
        Overrides:
        flush in class io.netty.channel.ChannelOutboundHandlerAdapter
        Throws:
        Exception
      • handlerAdded

        public void handlerAdded​(io.netty.channel.ChannelHandlerContext ctx)
                          throws Exception
        Specified by:
        handlerAdded in interface io.netty.channel.ChannelHandler
        Overrides:
        handlerAdded in class io.netty.channel.ChannelHandlerAdapter
        Throws:
        Exception
      • handlerRemoved

        public void handlerRemoved​(io.netty.channel.ChannelHandlerContext ctx)
                            throws Exception
        Specified by:
        handlerRemoved in interface io.netty.channel.ChannelHandler
        Overrides:
        handlerRemoved in class io.netty.channel.ChannelHandlerAdapter
        Throws:
        Exception
      • exceptionCaught

        public void exceptionCaught​(io.netty.channel.ChannelHandlerContext ctx,
                                    Throwable cause)
                             throws Exception
        Specified by:
        exceptionCaught in interface io.netty.channel.ChannelHandler
        Overrides:
        exceptionCaught in class io.netty.channel.ChannelHandlerAdapter
        Throws:
        Exception
      • isSharable

        public boolean isSharable()
        Overrides:
        isSharable in class io.netty.channel.ChannelHandlerAdapter