ByteBuf into a
Google Protocol Buffers
MessageNano. Please note that this decoder must
be used with a proper ByteToMessageDecoder such as LengthFieldBasedFrameDecoder
if 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", newProtobufDecoderNano(MyMessage.getDefaultInstance())); // Encoder pipeline.addLast("frameEncoder", newio.(4)); pipeline.addLast("protobufEncoder", newnetty. handler. codec. LengthFieldPrepender ProtobufEncoderNano());
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);
}
| Modifier and Type | Field and Description |
|---|---|
| private final Class |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected void | decode(ChannelHandlerContext
the ctx, ByteBuf ChannelHandlerContext which this MessageToMessageDecoder belongs tothe message to decode to an other one msg, List<Object> the out)List to which decoded messages should be addedImplements abstract io. |
| clazz | back to summary |
|---|---|
| private final Class<? extends MessageNano> clazz | |
| ProtobufDecoderNano | back to summary |
|---|---|
| public ProtobufDecoderNano(Class<? extends MessageNano> clazz) Creates a new instance. | |
| decode | back to summary |
|---|---|
| protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception Implements abstract io. Doc from io. Decode from one message to an other. This method will be called for each written message that can be handled by this decoder.
| |