public class DemuxingProtocolDecoder extends CumulativeProtocolDecoder
ProtocolDecoder that demultiplexes incoming IoBuffer
decoding requests into an appropriate MessageDecoder.
MessageDecoder selection
DemuxingProtocolDecoder iterates the list of candidate
MessageDecoders and calls MessageDecoder.decodable(IoSession, IoBuffer).
Initially, all registered MessageDecoders are candidates.MessageDecoderResult.NOT_OK is returned, it is removed from the candidate
list.MessageDecoderResult.NEED_DATA is returned, it is retained in the candidate
list, and its MessageDecoder.decodable(IoSession, IoBuffer) will be invoked
again when more data is received.MessageDecoderResult.OK is returned, DemuxingProtocolDecoder
found the right MessageDecoder.DemuxingProtocolDecoder will keep iterating the candidate list.
IoBuffer
in MessageDecoder.decodable(IoSession, IoBuffer) will be reverted back to its
original value.
Once a MessageDecoder is selected, DemuxingProtocolDecoder calls
MessageDecoder.decode(IoSession, IoBuffer, ProtocolDecoderOutput) continuously
reading its return value:
MessageDecoderResult.NOT_OK - protocol violation; ProtocolDecoderException
is raised automatically.MessageDecoderResult.NEED_DATA - needs more data to read the whole message;
MessageDecoder.decode(IoSession, IoBuffer, ProtocolDecoderOutput)
will be invoked again when more data is received.MessageDecoderResult.OK - successfully decoded a message; the candidate list will
be reset and the selection process will start over.MessageDecoderFactory,
MessageDecoder| Constructor and Description |
|---|
DemuxingProtocolDecoder() |
| Modifier and Type | Method and Description |
|---|---|
void |
addMessageDecoder(Class<? extends MessageDecoder> decoderClass) |
void |
addMessageDecoder(MessageDecoder decoder) |
void |
addMessageDecoder(MessageDecoderFactory factory) |
void |
dispose(IoSession session)
Releases the cumulative buffer used by the specified session.
|
protected boolean |
doDecode(IoSession session,
IoBuffer in,
ProtocolDecoderOutput out)
Implement this method to consume the specified cumulative buffer and
decode its content into message(s).
|
void |
finishDecode(IoSession session,
ProtocolDecoderOutput out)
Override this method to deal with the closed connection.
|
decodepublic void addMessageDecoder(Class<? extends MessageDecoder> decoderClass)
public void addMessageDecoder(MessageDecoder decoder)
public void addMessageDecoder(MessageDecoderFactory factory)
protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception
doDecode in class CumulativeProtocolDecoderin - the cumulative bufferException - if cannot decode in.public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception
finishDecode in interface ProtocolDecoderfinishDecode in class ProtocolDecoderAdapterException - if the read data violated protocol specificationpublic void dispose(IoSession session) throws Exception
dispose in interface ProtocolDecoderdispose in class CumulativeProtocolDecoderException - if failed to dispose all resources