@ChannelHandler.Sharable public class ProtobufEncoder extends MessageToMessageEncoder<com.google.protobuf.MessageLiteOrBuilder>
Message
and MessageLite
into a
ByteBuf
. A typical setup for TCP/IP would be:
and then you can use aChannelPipeline
pipeline = ...; // 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
());
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);
}
ChannelHandler.Sharable
Constructor and Description |
---|
ProtobufEncoder() |
Modifier and Type | Method and Description |
---|---|
protected void |
encode(ChannelHandlerContext ctx,
com.google.protobuf.MessageLiteOrBuilder msg,
List<Object> out)
Encode from one message to an other.
|
acceptOutboundMessage, write
bind, close, connect, deregister, disconnect, flush, read
ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
exceptionCaught, handlerAdded, handlerRemoved
protected void encode(ChannelHandlerContext ctx, com.google.protobuf.MessageLiteOrBuilder msg, List<Object> out) throws Exception
MessageToMessageEncoder
encode
in class MessageToMessageEncoder<com.google.protobuf.MessageLiteOrBuilder>
ctx
- the ChannelHandlerContext
which this MessageToMessageEncoder
belongs tomsg
- the message to encode to an other oneout
- the List
into which the encoded msg should be added
needs to do some kind of aggregationException
- is thrown if an error occursCopyright © 2008–2024 The Netty Project. All rights reserved.