Package io.netty.handler.codec.protobuf
Class ProtobufDecoder
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.handler.codec.MessageToMessageDecoder<io.netty.buffer.ByteBuf>
-
- io.netty.handler.codec.protobuf.ProtobufDecoder
-
- All Implemented Interfaces:
io.netty.channel.ChannelHandler,io.netty.channel.ChannelInboundHandler
@Sharable public class ProtobufDecoder extends io.netty.handler.codec.MessageToMessageDecoder<io.netty.buffer.ByteBuf>Decodes a receivedByteBufinto a Google Protocol BuffersMessageandMessageLite. Please note that this decoder must be used with a properByteToMessageDecodersuch asProtobufVarint32FrameDecoderorLengthFieldBasedFrameDecoderif you are using a stream-based transport such as TCP/IP. A typical setup for TCP/IP would be:
and then you can use aChannelPipelinepipeline = ...; // Decoders pipeline.addLast("frameDecoder", newLengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4)); pipeline.addLast("protobufDecoder", newProtobufDecoder(MyMessage.getDefaultInstance())); // Encoder pipeline.addLast("frameEncoder", newLengthFieldPrepender(4)); pipeline.addLast("protobufEncoder", newProtobufEncoder());MyMessageinstead of aByteBufas a message:void channelRead(
ChannelHandlerContextctx, Object msg) { MyMessage req = (MyMessage) msg; MyMessage res = MyMessage.newBuilder().setText( "Did you say '" + req.getText() + "'?").build(); ch.write(res); }
-
-
Constructor Summary
Constructors Constructor Description ProtobufDecoder(com.google.protobuf.MessageLite prototype)Creates a new instance.ProtobufDecoder(com.google.protobuf.MessageLite prototype, com.google.protobuf.ExtensionRegistry extensionRegistry)ProtobufDecoder(com.google.protobuf.MessageLite prototype, com.google.protobuf.ExtensionRegistryLite extensionRegistry)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voiddecode(io.netty.channel.ChannelHandlerContext ctx, io.netty.buffer.ByteBuf msg, List<Object> out)-
Methods inherited from class io.netty.handler.codec.MessageToMessageDecoder
acceptInboundMessage, channelRead, channelReadComplete
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharable
-
-
-
-
Constructor Detail
-
ProtobufDecoder
public ProtobufDecoder(com.google.protobuf.MessageLite prototype)
Creates a new instance.
-
ProtobufDecoder
public ProtobufDecoder(com.google.protobuf.MessageLite prototype, com.google.protobuf.ExtensionRegistry extensionRegistry)
-
ProtobufDecoder
public ProtobufDecoder(com.google.protobuf.MessageLite prototype, com.google.protobuf.ExtensionRegistryLite extensionRegistry)
-
-