M
- the type of message to be encodedpublic class DatagramPacketEncoder<M> extends MessageToMessageEncoder<AddressedEnvelope<M,InetSocketAddress>>
AddressedEnvelope
to DatagramPacket
using
the specified message encoder. E.g.,
ChannelPipeline
pipeline = ...;
pipeline.addLast("udpEncoder", new DatagramPacketEncoder
(new ProtobufEncoder
(...));
Note: As UDP packets are out-of-order, you should make sure the encoded message size are not greater than
the max safe packet size in your particular network path which guarantees no packet fragmentation.ChannelHandler.Sharable
Constructor and Description |
---|
DatagramPacketEncoder(MessageToMessageEncoder<? super M> encoder)
Create an encoder that encodes the content in
AddressedEnvelope to DatagramPacket using
the specified message encoder. |
write
ensureNotSharable
public DatagramPacketEncoder(MessageToMessageEncoder<? super M> encoder)
AddressedEnvelope
to DatagramPacket
using
the specified message encoder.encoder
- the specified message encoderpublic boolean acceptOutboundMessage(Object msg) throws Exception
MessageToMessageEncoder
true
if the given message should be handled. If false
it will be passed to the next
ChannelOutboundHandler
in the ChannelPipeline
.acceptOutboundMessage
in class MessageToMessageEncoder<AddressedEnvelope<M,InetSocketAddress>>
Exception
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<M,InetSocketAddress> msg, List<Object> out) throws Exception
MessageToMessageEncoder
encode
in class MessageToMessageEncoder<AddressedEnvelope<M,InetSocketAddress>>
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 occurspublic void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception
ChannelOutboundHandlerAdapter
ChannelOutboundInvoker.bind(SocketAddress, ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.bind
in interface ChannelOutboundHandler
bind
in class ChannelOutboundHandlerAdapter
ctx
- the ChannelHandlerContext
for which the bind operation is madelocalAddress
- the SocketAddress
to which it should boundpromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error occurspublic void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception
ChannelOutboundHandlerAdapter
ChannelOutboundInvoker.connect(SocketAddress, SocketAddress, ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.connect
in interface ChannelOutboundHandler
connect
in class ChannelOutboundHandlerAdapter
ctx
- the ChannelHandlerContext
for which the connect operation is maderemoteAddress
- the SocketAddress
to which it should connectlocalAddress
- the SocketAddress
which is used as source on connectpromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error occurspublic void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception
ChannelOutboundHandlerAdapter
ChannelOutboundInvoker.disconnect(ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.disconnect
in interface ChannelOutboundHandler
disconnect
in class ChannelOutboundHandlerAdapter
ctx
- the ChannelHandlerContext
for which the disconnect operation is madepromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error occurspublic void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception
ChannelOutboundHandlerAdapter
ChannelOutboundInvoker.close(ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.close
in interface ChannelOutboundHandler
close
in class ChannelOutboundHandlerAdapter
ctx
- the ChannelHandlerContext
for which the close operation is madepromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error occurspublic void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception
ChannelOutboundHandlerAdapter
ChannelOutboundInvoker.deregister(ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.deregister
in interface ChannelOutboundHandler
deregister
in class ChannelOutboundHandlerAdapter
ctx
- the ChannelHandlerContext
for which the close operation is madepromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error occurspublic void read(ChannelHandlerContext ctx) throws Exception
ChannelOutboundHandlerAdapter
ChannelHandlerContext.read()
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.read
in interface ChannelOutboundHandler
read
in class ChannelOutboundHandlerAdapter
Exception
public void flush(ChannelHandlerContext ctx) throws Exception
ChannelOutboundHandlerAdapter
ChannelHandlerContext.flush()
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.flush
in interface ChannelOutboundHandler
flush
in class ChannelOutboundHandlerAdapter
ctx
- the ChannelHandlerContext
for which the flush operation is madeException
- thrown if an error occurspublic void handlerAdded(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
handlerAdded
in interface ChannelHandler
handlerAdded
in class ChannelHandlerAdapter
Exception
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
handlerRemoved
in interface ChannelHandler
handlerRemoved
in class ChannelHandlerAdapter
Exception
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.fireExceptionCaught(Throwable)
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.exceptionCaught
in interface ChannelHandler
exceptionCaught
in class ChannelHandlerAdapter
Exception
public boolean isSharable()
ChannelHandlerAdapter
isSharable
in class ChannelHandlerAdapter
Copyright © 2008–2017 The Netty Project. All rights reserved.