The stream and stream state as used by the application. Must only be called from the sending application thread.
| 36 | * application thread. |
| 37 | */ |
| 38 | public abstract class AbstractStream implements Stream { |
| 39 | private static final Logger log = Logger.getLogger(AbstractStream.class.getName()); |
| 40 | |
| 41 | /** The framer to use for sending messages. */ |
| 42 | protected abstract Framer framer(); |
| 43 | |
| 44 | /** |
| 45 | * Obtain the transport state corresponding to this stream. Each stream must have its own unique |
| 46 | * transport state. |
| 47 | */ |
| 48 | protected abstract TransportState transportState(); |
| 49 | |
| 50 | @Override |
| 51 | public void optimizeForDirectExecutor() { |
| 52 | transportState().optimizeForDirectExecutor(); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public final void setMessageCompression(boolean enable) { |
| 57 | framer().setMessageCompression(enable); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public final void request(int numMessages) { |
| 62 | transportState().requestMessagesFromDeframer(numMessages); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public final void writeMessage(InputStream message) { |
| 67 | checkNotNull(message, "message"); |
| 68 | try { |
| 69 | if (!framer().isClosed()) { |
| 70 | framer().writePayload(message); |
| 71 | } |
| 72 | } finally { |
| 73 | GrpcUtil.closeQuietly(message); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public final void flush() { |
| 79 | if (!framer().isClosed()) { |
| 80 | framer().flush(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * A hint to the stream that specifies how many bytes must be queued before |
| 86 | * {@link #isReady()} will return false. A stream may ignore this property if |
| 87 | * unsupported. This may only be set during stream initialization before |
| 88 | * any messages are set. |
| 89 | * |
| 90 | * @param numBytes The number of bytes that must be queued. Must be a |
| 91 | * positive integer. |
| 92 | */ |
| 93 | protected void setOnReadyThreshold(int numBytes) { |
| 94 | transportState().setOnReadyThreshold(numBytes); |
| 95 | } |