@ChannelHandler.Sharable public class ProtobufEncoder extends MessageToMessageEncoder<Object,ByteBuf>
Message and MessageLite into a
ByteBuf. 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());
MyMessage instead of a ByteBuf
as a message:
void messageReceived(ChannelHandlerContextctx,MessageEvente) { MyMessage req = (MyMessage) e.getMessage(); 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 |
|---|---|
ByteBuf |
encode(ChannelHandlerContext ctx,
Object msg) |
boolean |
isEncodable(Object msg)
Returns
true if and only if the specified message can be encoded by this encoder. |
flushnewOutboundBufferafterAdd, afterRemove, beforeAdd, beforeRemove, bind, close, connect, deregister, disconnect, exceptionCaught, userEventTriggeredclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitbind, close, connect, deregister, disconnectafterAdd, afterRemove, beforeAdd, beforeRemove, exceptionCaught, userEventTriggeredpublic boolean isEncodable(Object msg) throws Exception
MessageToMessageEncodertrue if and only if the specified message can be encoded by this encoder.isEncodable in class MessageToMessageEncoder<Object,ByteBuf>msg - the messageExceptionpublic ByteBuf encode(ChannelHandlerContext ctx, Object msg) throws Exception
encode in class MessageToMessageEncoder<Object,ByteBuf>ExceptionCopyright © 2008-2012 The Netty Project. All Rights Reserved.