IdT
- type of the unique identifier of this stream.public abstract class AbstractStream<IdT> extends Object implements Stream
Stream
implementations.Modifier and Type | Class and Description |
---|---|
protected static class |
AbstractStream.Phase
Indicates the phase of the GRPC stream in one direction.
|
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_ONREADY_THRESHOLD
The default number of queued bytes for a given stream, below which
StreamListener.onReady() will be called. |
Modifier and Type | Method and Description |
---|---|
boolean |
canReceive()
Returns
true if the stream can receive data from its remote peer. |
boolean |
canSend()
Returns
true if the stream can send data to its remote peer. |
protected void |
closeDeframer()
Closes this deframer and frees any resources.
|
protected void |
deframe(ReadableBuffer frame,
boolean endOfStream)
Called to parse a received frame and attempt delivery of any completed
messages.
|
protected abstract void |
deframeFailed(Throwable cause)
Called when a
deframe(ReadableBuffer, boolean) operation failed. |
void |
dispose()
Frees any resources associated with this stream.
|
void |
flush()
Flushes any internally buffered messages to the remote end-point.
|
int |
getOnReadyThreshold()
The number of queued bytes for a given stream, below which
StreamListener.onReady()
will be called. |
abstract IdT |
id()
Returns the internal ID for this stream.
|
protected abstract void |
inboundDeliveryPaused()
Handles the event that the deframer has no pending deliveries.
|
protected abstract void |
internalSendFrame(WritableBuffer frame,
boolean endOfStream,
boolean flush)
Sends an outbound frame to the remote end point.
|
boolean |
isClosed()
Whether the stream is fully closed.
|
protected boolean |
isDeframerStalled()
Indicates whether delivery is currently stalled, pending receipt of more data.
|
boolean |
isReady()
If
true , indicates that the transport is capable of sending additional messages
without requiring excessive buffering internally. |
protected abstract StreamListener |
listener()
Override this method to provide a stream listener.
|
protected void |
onSendingBytes(int numBytes)
Event handler to be called by the subclass when a number of bytes are being queued for sending
to the remote endpoint.
|
protected void |
onSentBytes(int numBytes)
Event handler to be called by the subclass when a number of bytes has been sent to the remote
endpoint.
|
protected void |
onStreamAllocated()
Event handler to be called by the subclass when the stream's headers have passed any connection
flow control (i.e., MAX_CONCURRENT_STREAMS).
|
protected abstract void |
receiveMessage(InputStream is)
Handles a message that was just deframed.
|
protected abstract void |
remoteEndClosed()
Handles the event that the deframer has reached end of stream.
|
protected void |
requestMessagesFromDeframer(int numMessages)
Called to request the given number of messages from the deframer.
|
protected abstract void |
returnProcessedBytes(int processedBytes)
Returns the given number of processed bytes back to inbound flow control to enable receipt of
more data.
|
String |
toString() |
protected Objects.ToStringHelper |
toStringHelper() |
void |
writeMessage(InputStream message)
Writes a message payload to the remote end-point.
|
public static final int DEFAULT_ONREADY_THRESHOLD
StreamListener.onReady()
will be called.protected abstract StreamListener listener()
@Nullable public abstract IdT id()
null
for client streams
as the transport may defer creating the stream to the remote side until it has a payload or
metadata to send.public int getOnReadyThreshold()
StreamListener.onReady()
will be called. Defaults to DEFAULT_ONREADY_THRESHOLD
.public void writeMessage(InputStream message)
Stream
KnownLength
to improve efficiency. This method will always return immediately
and will not wait for the write to complete.
When the write is "accepted" by the transport, the given callback (if provided) will be called. The definition of what it means to be "accepted" is up to the transport implementation, but this is a general indication that the transport is capable of handling more out-bound data on the stream. If the stream/connection is closed for any reason before the write could be accepted, the callback will never be invoked.
writeMessage
in interface Stream
message
- stream containing the serialized message to be sentpublic final void flush()
Stream
public final boolean isReady()
Stream
true
, indicates that the transport is capable of sending additional messages
without requiring excessive buffering internally. This event is
just a suggestion and the application is free to ignore it, however doing so may
result in excessive buffering within the transport.public void dispose()
NOTE: Can be called by both the transport thread and the application thread. Transport threads need to dispose when the remote side has terminated the stream. Application threads will dispose when the application decides to close the stream as part of normal processing.
protected abstract void internalSendFrame(WritableBuffer frame, boolean endOfStream, boolean flush)
frame
- a buffer containing the chunk of data to be sent.endOfStream
- if true
indicates that no more data will be sent on the stream by
this endpoint.flush
- true
if more data may not be arriving soonprotected abstract void receiveMessage(InputStream is)
is
- the stream containing the messageprotected abstract void inboundDeliveryPaused()
protected abstract void remoteEndClosed()
protected abstract void returnProcessedBytes(int processedBytes)
protected abstract void deframeFailed(Throwable cause)
deframe(ReadableBuffer, boolean)
operation failed.cause
- the actual failureprotected final void closeDeframer()
protected final void deframe(ReadableBuffer frame, boolean endOfStream)
protected final boolean isDeframerStalled()
protected final void requestMessagesFromDeframer(int numMessages)
protected final void onStreamAllocated()
StreamListener.onReady()
handler if appropriate. This must be called from the transport
thread, since the listener may be called back directly.protected final void onSendingBytes(int numBytes)
numBytes
- the number of bytes being sent.protected final void onSentBytes(int numBytes)
StreamListener.onReady()
handler if appropriate.
This must be called from the transport thread, since the listener may be called back directly.numBytes
- the number of bytes that were sent.public boolean canReceive()
true
if the stream can receive data from its remote peer.public boolean canSend()
true
if the stream can send data to its remote peer.public boolean isClosed()
inboundPhase
and outboundPhase
are mutated in different threads. Tests must account
for thread coordination when calling.protected Objects.ToStringHelper toStringHelper()