public class DemuxingProtocolDecoder extends CumulativeProtocolDecoder
ProtocolDecoder
that demultiplexes incoming IoBuffer
decoding requests into an appropriate MessageDecoder
.
MessageDecoder
selectionDemuxingProtocolDecoder
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(Class<? extends MessageDecoder> decoderClass)
Adds a new message decoder class
|
void |
addMessageDecoder(MessageDecoder decoder)
Adds a new message decoder instance
|
void |
addMessageDecoder(MessageDecoderFactory factory)
Adds a new message decoder 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, setTransportMetadataFragmentation
public void addMessageDecoder(Class<? extends MessageDecoder> decoderClass)
decoderClass
- The decoder classpublic void addMessageDecoder(MessageDecoder decoder)
decoder
- The decoder instancepublic void addMessageDecoder(MessageDecoderFactory factory)
factory
- The decoder factoryprotected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception
doDecode
in class CumulativeProtocolDecoder
session
- The current Sessionin
- the cumulative bufferout
- The ProtocolDecoderOutput
that will receive the decoded messageException
- if cannot decode in.public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception
finishDecode
in interface ProtocolDecoder
finishDecode
in class ProtocolDecoderAdapter
session
- The current Sessionout
- The ProtocolDecoderOutput
that contains the decoded messageException
- if the read data violated protocol specificationpublic void dispose(IoSession session) throws Exception
dispose
in interface ProtocolDecoder
dispose
in class CumulativeProtocolDecoder
session
- The current SessionException
- if failed to dispose all resourcesCopyright © 2004–2018 Apache MINA Project. All rights reserved.