1 /* 2 * Copyright 2014 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc.internal; 18 19 import io.grpc.Compressor; 20 import java.io.InputStream; 21 22 /** 23 * A single stream of communication between two end-points within a transport. 24 * 25 * <p>An implementation doesn't need to be thread-safe. All methods are expected to execute quickly. 26 */ 27 public interface Stream { 28 /** 29 * Requests up to the given number of messages from the call to be delivered via 30 * {@link StreamListener#messagesAvailable(StreamListener.MessageProducer)}. No additional 31 * messages will be delivered. If the stream has a {@code start()} method, it must be called 32 * before requesting messages. 33 * 34 * @param numMessages the requested number of messages to be delivered to the listener. 35 */ 36 void request(int numMessages); 37 38 /** 39 * Writes a message payload to the remote end-point. The bytes from the stream are immediately 40 * read by the Transport. Where possible callers should use streams that are 41 * {@link io.grpc.KnownLength} to improve efficiency. This method will always return immediately 42 * and will not wait for the write to complete. If the stream has a {@code start()} method, it 43 * must be called before writing any messages. 44 * 45 * <p>It is recommended that the caller consult {@link #isReady()} before calling this method to 46 * avoid excessive buffering in the transport. 47 * 48 * <p>This method takes ownership of the InputStream, and implementations are responsible for 49 * calling {@link InputStream#close}. 50 * 51 * @param message stream containing the serialized message to be sent 52 */ 53 void writeMessage(InputStream message); 54 55 /** 56 * Flushes any internally buffered messages to the remote end-point. 57 */ 58 void flush(); 59 60 /** 61 * If {@code true}, indicates that the transport is capable of sending additional messages without 62 * requiring excessive buffering internally. Otherwise, {@link StreamListener#onReady()} will be 63 * called when it turns {@code true}. 64 * 65 * <p>This is just a suggestion and the application is free to ignore it, however doing so may 66 * result in excessive buffering within the transport. 67 */ 68 boolean isReady(); 69 70 /** 71 * Sets the compressor on the framer. 72 * 73 * @param compressor the compressor to use 74 */ 75 void setCompressor(Compressor compressor); 76 77 /** 78 * Enables per-message compression, if an encoding type has been negotiated. If no message 79 * encoding has been negotiated, this is a no-op. By default per-message compression is enabled, 80 * but may not have any effect if compression is not enabled on the call. 81 */ 82 void setMessageCompression(boolean enable); 83 } 84