T
- Message type, that this handler is able to encode and decode.public abstract class BaseMessageHandler<T extends Message> extends Object implements MessageHandler<T>
MessageHandler
implementations, that work with a single message type.Constructor and Description |
---|
BaseMessageHandler() |
Modifier and Type | Method and Description |
---|---|
int |
decode(DecodingContext context,
ByteBufferView buffer)
Tries to decode message from the byte buffer.
|
protected abstract int |
doDecode(DecodingContext context,
ByteBufferView buffer)
Decode the payload of a message (excluding message prefix -- see
StandardBittorrentProtocol.MESSAGE_PREFIX_SIZE )
and place it into the provided context. |
protected abstract boolean |
doEncode(EncodingContext context,
T message,
ByteBuffer buffer)
Encode the payload of a message (excluding message prefix -- see
StandardBittorrentProtocol.MESSAGE_PREFIX_SIZE )
and write it into the provided buffer. |
boolean |
encode(EncodingContext context,
T message,
ByteBuffer buffer)
Tries to encode the provided message and place the result into the byte buffer.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getSupportedTypes, readMessageType
public boolean encode(EncodingContext context, T message, ByteBuffer buffer)
MessageHandler
encode
in interface MessageHandler<T extends Message>
context
- Encoding contextbuffer
- Byte buffer of arbitrary capacity.
Encoded message should be placed into the buffer starting with its current position.
Protocol should check if the buffer has sufficient space available, and return false
if it's not the case.protected abstract boolean doEncode(EncodingContext context, T message, ByteBuffer buffer)
StandardBittorrentProtocol.MESSAGE_PREFIX_SIZE
)
and write it into the provided buffer.context
- Encoding contextbuffer
- Byte buffer to write to.
Encoded message should be placed into the buffer starting with its current position.
This method should check if the buffer has sufficient space available, and return false
if it's not the case.
After this method returns, the buffer's Buffer.position()
is used
to calculate the size of message's payload, which is then specified in the message's prefix.public int decode(DecodingContext context, ByteBufferView buffer)
MessageHandler
context
decode
in interface MessageHandler<T extends Message>
context
- Message context. In case of success the decoded message must be put into this context.buffer
- Byte buffer of arbitrary length containing (a part of) the message.
Decoding should be performed starting with the current position of the buffer.protected abstract int doDecode(DecodingContext context, ByteBufferView buffer)
StandardBittorrentProtocol.MESSAGE_PREFIX_SIZE
)
and place it into the provided context.context
- The context to place the decoded message into.buffer
- Buffer to decode from. Buffer.remaining()
is set
to the declared length of the message.
Message payload starts precisely at buffer's Buffer.position()
.Copyright © 2016–2021. All rights reserved.