public class ProtobufParser<MessageType extends com.google.protobuf.MessageLite> extends AbstractTimeoutHandler implements StreamParser
Modifier and Type | Class and Description |
---|---|
static interface |
ProtobufParser.Listener<MessageType extends com.google.protobuf.MessageLite>
An interface which can be implemented to handle callbacks as new messages are generated and socket events occur.
|
Constructor and Description |
---|
ProtobufParser(ProtobufParser.Listener<MessageType> handler,
MessageType prototype,
int maxMessageSize,
int timeoutMillis)
Creates a new protobuf handler.
|
Modifier and Type | Method and Description |
---|---|
void |
closeConnection()
Closes this connection, eventually triggering a
connectionClosed() event. |
void |
connectionClosed()
Called when the connection socket is closed
|
void |
connectionOpened()
Called when the connection socket is first opened
|
int |
getMaxMessageSize()
Returns the maximum message size of a message on the socket.
|
int |
receiveBytes(ByteBuffer buff)
Called when new bytes are available from the remote end.
|
void |
setWriteTarget(MessageWriteTarget writeTarget)
Called when this parser is attached to an upstream write target (ie a low-level connection handler).
|
protected void |
timeoutOccurred() |
void |
write(MessageType msg)
Writes the given message to the other side of the connection, prefixing it with the proper 4-byte prefix.
|
resetTimeout, setSocketTimeout, setTimeoutEnabled
public ProtobufParser(ProtobufParser.Listener<MessageType> handler, MessageType prototype, int maxMessageSize, int timeoutMillis)
handler
- The callback listenerprototype
- The default instance of the message type used in both directions of this channel.
This should be the return value from MessageType#getDefaultInstanceForType()
maxMessageSize
- The maximum message size (not including the 4-byte length prefix).
Note that this has an upper bound of Integer.MAX_VALUE
- 4timeoutMillis
- The timeout between messages before the connection is automatically closed. Only enabled
after the connection is established.public void setWriteTarget(MessageWriteTarget writeTarget)
StreamParser
setWriteTarget
in interface StreamParser
public int getMaxMessageSize()
StreamParser
getMaxMessageSize
in interface StreamParser
public void closeConnection()
connectionClosed()
event.protected void timeoutOccurred()
timeoutOccurred
in class AbstractTimeoutHandler
public int receiveBytes(ByteBuffer buff) throws Exception
StreamParser
Called when new bytes are available from the remote end. This should only ever be called by the single writeTarget associated with any given StreamParser, multiple callers will likely confuse implementations.
Implementers/callers must follow the following conventions exactly:receiveBytes
in interface StreamParser
Exception
public void connectionClosed()
StreamParser
connectionClosed
in interface StreamParser
public void connectionOpened()
StreamParser
connectionOpened
in interface StreamParser
public void write(MessageType msg) throws IllegalStateException
Writes the given message to the other side of the connection, prefixing it with the proper 4-byte prefix.
Provides a write-order guarantee.
IllegalStateException
- If the encoded message is larger than the maximum message size.Copyright © 2014. All rights reserved.