@ChannelHandler.Sharable public class ProtobufEncoder extends MessageToMessageEncoder<Object,io.netty.buffer.ByteBuf>
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 messageReceived(ChannelHandlerContext
ctx,MessageEvent
e) { MyMessage req = (MyMessage) e.getMessage(); MyMessage res = MyMessage.newBuilder().setText( "Did you say '" + req.getText() + "'?").build(); ch.write(res); }
Constructor and Description |
---|
ProtobufEncoder() |
Modifier and Type | Method and Description |
---|---|
io.netty.buffer.ByteBuf |
encode(io.netty.channel.ChannelHandlerContext ctx,
Object msg) |
boolean |
isEncodable(Object msg)
Returns
true if and only if the specified message can be encoded by this encoder. |
flush
newOutboundBuffer
afterAdd, afterRemove, beforeAdd, beforeRemove, bind, close, connect, deregister, disconnect, exceptionCaught, userEventTriggered
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
public boolean isEncodable(Object msg) throws Exception
MessageToMessageEncoder
true
if and only if the specified message can be encoded by this encoder.isEncodable
in class MessageToMessageEncoder<Object,io.netty.buffer.ByteBuf>
msg
- the messageException
Copyright © 2008-2012 The Netty Project. All Rights Reserved.