public class DemuxingProtocolDecoder extends CumulativeProtocolDecoder
ProtocolDecoder
that demultiplexes incoming IoBuffer
decoding requests into an appropriate MessageDecoder
.
MessageDecoder
selection
DemuxingProtocolDecoder
iterates the list of candidate
MessageDecoder
s and calls MessageDecoder.decodable(IoSession, IoBuffer)
.
Initially, all registered MessageDecoder
s 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(java.lang.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.
|
decode
public void addMessageDecoder(java.lang.Class<? extends MessageDecoder> decoderClass)
public void addMessageDecoder(MessageDecoder decoder)
public void addMessageDecoder(MessageDecoderFactory factory)
protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws java.lang.Exception
CumulativeProtocolDecoder
doDecode
in class CumulativeProtocolDecoder
in
- the cumulative bufferjava.lang.Exception
- if cannot decode in.public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws java.lang.Exception
ProtocolDecoderAdapter
finishDecode
in interface ProtocolDecoder
finishDecode
in class ProtocolDecoderAdapter
java.lang.Exception
- if the read data violated protocol specificationpublic void dispose(IoSession session) throws java.lang.Exception
CumulativeProtocolDecoder
dispose
in interface ProtocolDecoder
dispose
in class CumulativeProtocolDecoder
java.lang.Exception
- if failed to dispose all resourcesCopyright © 2004-2019 Apache MINA Project. All Rights Reserved.