public class ProtobufConnection<MessageType extends com.google.protobuf.MessageLite> extends AbstractTimeoutHandler implements StreamConnection
A handler which is used in NioServer
and NioClient
to split up incoming data streams
into protobufs and provide an interface for writing protobufs to the connections.
Messages are encoded with a 4-byte signed integer (big endian) prefix to indicate their length followed by the serialized protobuf
(Used to be called ProtobufParser)
Modifier and Type | Class and Description |
---|---|
static interface |
ProtobufConnection.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 |
---|
ProtobufConnection(ProtobufConnection.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(java.nio.ByteBuffer buff)
Called when new bytes are available from the remote end.
|
void |
setWriteTarget(MessageWriteTarget writeTarget)
Called when this connection 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 ProtobufConnection(ProtobufConnection.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 MessageLiteOrBuilder.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)
StreamConnection
setWriteTarget
in interface StreamConnection
public int getMaxMessageSize()
StreamConnection
getMaxMessageSize
in interface StreamConnection
public void closeConnection()
connectionClosed()
event.protected void timeoutOccurred()
timeoutOccurred
in class AbstractTimeoutHandler
public int receiveBytes(java.nio.ByteBuffer buff) throws java.lang.Exception
StreamConnection
Called when new bytes are available from the remote end. This should only ever be called by the single writeTarget associated with any given StreamConnection, multiple callers will likely confuse implementations.
Implementers/callers must follow the following conventions exactly:receiveBytes
in interface StreamConnection
java.lang.Exception
public void connectionClosed()
StreamConnection
connectionClosed
in interface StreamConnection
public void connectionOpened()
StreamConnection
connectionOpened
in interface StreamConnection
public void write(MessageType msg) throws java.lang.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.
java.lang.IllegalStateException
- If the encoded message is larger than the maximum message size.