Class ProtobufEncoder

  • All Implemented Interfaces:
    io.netty.channel.ChannelHandler, io.netty.channel.ChannelOutboundHandler

    @Sharable
    public class ProtobufEncoder
    extends MessageToMessageEncoder<com.google.protobuf.MessageLiteOrBuilder>
    Encodes the requested Google Protocol Buffers Message and MessageLite into a ByteBuf. A typical setup for TCP/IP would be:
     ChannelPipeline pipeline = ...;
    
     // Decoders
     pipeline.addLast("frameDecoder",
                      new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4));
     pipeline.addLast("protobufDecoder",
                      new ProtobufDecoder(MyMessage.getDefaultInstance()));
    
     // Encoder
     pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
     pipeline.addLast("protobufEncoder", new ProtobufEncoder());
     
    and then you can use a MyMessage instead of a ByteBuf as a message:
     void channelRead(ChannelHandlerContext ctx, Object msg) {
         MyMessage req = (MyMessage) msg;
         MyMessage res = MyMessage.newBuilder().setText(
                                   "Did you say '" + req.getText() + "'?").build();
         ch.write(res);
     }
     
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler

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

      Constructors 
      Constructor Description
      ProtobufEncoder()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void encode​(io.netty.channel.ChannelHandlerContext ctx, com.google.protobuf.MessageLiteOrBuilder msg, List<Object> out)
      Encode from one message to an other.
      • Methods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter

        bind, close, connect, deregister, disconnect, flush, read
      • Methods inherited from class io.netty.channel.ChannelHandlerAdapter

        ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharable
      • Methods inherited from interface io.netty.channel.ChannelHandler

        exceptionCaught, handlerAdded, handlerRemoved
    • Constructor Detail

      • ProtobufEncoder

        public ProtobufEncoder()
    • Method Detail

      • encode

        protected void encode​(io.netty.channel.ChannelHandlerContext ctx,
                              com.google.protobuf.MessageLiteOrBuilder 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<com.google.protobuf.MessageLiteOrBuilder>
        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