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.
构造器和说明 |
---|
DemuxingProtocolDecoder() |
限定符和类型 | 方法和说明 |
---|---|
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
doDecode
在类中 CumulativeProtocolDecoder
session
- The current Sessionin
- the cumulative bufferout
- The ProtocolDecoderOutput
that will receive the decoded messagejava.lang.Exception
- if cannot decode in.public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws java.lang.Exception
finishDecode
在接口中 ProtocolDecoder
finishDecode
在类中 ProtocolDecoderAdapter
session
- The current Sessionout
- The ProtocolDecoderOutput
that contains the decoded messagejava.lang.Exception
- if the read data violated protocol specificationpublic void dispose(IoSession session) throws java.lang.Exception
dispose
在接口中 ProtocolDecoder
dispose
在类中 CumulativeProtocolDecoder
session
- The current Sessionjava.lang.Exception
- if failed to dispose all resources