12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133 |
- //
- //
- // Copyright 2015 gRPC authors.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- //
- #ifndef GRPCPP_SUPPORT_ASYNC_STREAM_H
- #define GRPCPP_SUPPORT_ASYNC_STREAM_H
- #include <grpc/grpc.h>
- #include <grpc/support/log.h>
- #include <grpcpp/impl/call.h>
- #include <grpcpp/impl/channel_interface.h>
- #include <grpcpp/impl/service_type.h>
- #include <grpcpp/server_context.h>
- #include <grpcpp/support/status.h>
- namespace grpc {
- namespace internal {
- /// Common interface for all client side asynchronous streaming.
- class ClientAsyncStreamingInterface {
- public:
- virtual ~ClientAsyncStreamingInterface() {}
- /// Start the call that was set up by the constructor, but only if the
- /// constructor was invoked through the "Prepare" API which doesn't actually
- /// start the call
- virtual void StartCall(void* tag) = 0;
- /// Request notification of the reading of the initial metadata. Completion
- /// will be notified by \a tag on the associated completion queue.
- /// This call is optional, but if it is used, it cannot be used concurrently
- /// with or after the \a AsyncReaderInterface::Read method.
- ///
- /// \param[in] tag Tag identifying this request.
- virtual void ReadInitialMetadata(void* tag) = 0;
- /// Indicate that the stream is to be finished and request notification for
- /// when the call has been ended.
- /// Should not be used concurrently with other operations.
- ///
- /// It is appropriate to call this method exactly once when both:
- /// * the client side has no more message to send
- /// (this can be declared implicitly by calling this method, or
- /// explicitly through an earlier call to the <i>WritesDone</i> method
- /// of the class in use, e.g. \a ClientAsyncWriterInterface::WritesDone or
- /// \a ClientAsyncReaderWriterInterface::WritesDone).
- /// * there are no more messages to be received from the server (this can
- /// be known implicitly by the calling code, or explicitly from an
- /// earlier call to \a AsyncReaderInterface::Read that yielded a failed
- /// result, e.g. cq->Next(&read_tag, &ok) filled in 'ok' with 'false').
- ///
- /// The tag will be returned when either:
- /// - all incoming messages have been read and the server has returned
- /// a status.
- /// - the server has returned a non-OK status.
- /// - the call failed for some reason and the library generated a
- /// status.
- ///
- /// Note that implementations of this method attempt to receive initial
- /// metadata from the server if initial metadata hasn't yet been received.
- ///
- /// \param[in] tag Tag identifying this request.
- /// \param[out] status To be updated with the operation status.
- virtual void Finish(grpc::Status* status, void* tag) = 0;
- };
- /// An interface that yields a sequence of messages of type \a R.
- template <class R>
- class AsyncReaderInterface {
- public:
- virtual ~AsyncReaderInterface() {}
- /// Read a message of type \a R into \a msg. Completion will be notified by \a
- /// tag on the associated completion queue.
- /// This is thread-safe with respect to \a Write or \a WritesDone methods. It
- /// should not be called concurrently with other streaming APIs
- /// on the same stream. It is not meaningful to call it concurrently
- /// with another \a AsyncReaderInterface::Read on the same stream since reads
- /// on the same stream are delivered in order.
- ///
- /// \param[out] msg Where to eventually store the read message.
- /// \param[in] tag The tag identifying the operation.
- ///
- /// Side effect: note that this method attempt to receive initial metadata for
- /// a stream if it hasn't yet been received.
- virtual void Read(R* msg, void* tag) = 0;
- };
- /// An interface that can be fed a sequence of messages of type \a W.
- template <class W>
- class AsyncWriterInterface {
- public:
- virtual ~AsyncWriterInterface() {}
- /// Request the writing of \a msg with identifying tag \a tag.
- ///
- /// Only one write may be outstanding at any given time. This means that
- /// after calling Write, one must wait to receive \a tag from the completion
- /// queue BEFORE calling Write again.
- /// This is thread-safe with respect to \a AsyncReaderInterface::Read
- ///
- /// gRPC doesn't take ownership or a reference to \a msg, so it is safe to
- /// to deallocate once Write returns.
- ///
- /// \param[in] msg The message to be written.
- /// \param[in] tag The tag identifying the operation.
- virtual void Write(const W& msg, void* tag) = 0;
- /// Request the writing of \a msg using WriteOptions \a options with
- /// identifying tag \a tag.
- ///
- /// Only one write may be outstanding at any given time. This means that
- /// after calling Write, one must wait to receive \a tag from the completion
- /// queue BEFORE calling Write again.
- /// WriteOptions \a options is used to set the write options of this message.
- /// This is thread-safe with respect to \a AsyncReaderInterface::Read
- ///
- /// gRPC doesn't take ownership or a reference to \a msg, so it is safe to
- /// to deallocate once Write returns.
- ///
- /// \param[in] msg The message to be written.
- /// \param[in] options The WriteOptions to be used to write this message.
- /// \param[in] tag The tag identifying the operation.
- virtual void Write(const W& msg, grpc::WriteOptions options, void* tag) = 0;
- /// Request the writing of \a msg and coalesce it with the writing
- /// of trailing metadata, using WriteOptions \a options with
- /// identifying tag \a tag.
- ///
- /// For client, WriteLast is equivalent of performing Write and
- /// WritesDone in a single step.
- /// For server, WriteLast buffers the \a msg. The writing of \a msg is held
- /// until Finish is called, where \a msg and trailing metadata are coalesced
- /// and write is initiated. Note that WriteLast can only buffer \a msg up to
- /// the flow control window size. If \a msg size is larger than the window
- /// size, it will be sent on wire without buffering.
- ///
- /// gRPC doesn't take ownership or a reference to \a msg, so it is safe to
- /// to deallocate once Write returns.
- ///
- /// \param[in] msg The message to be written.
- /// \param[in] options The WriteOptions to be used to write this message.
- /// \param[in] tag The tag identifying the operation.
- void WriteLast(const W& msg, grpc::WriteOptions options, void* tag) {
- Write(msg, options.set_last_message(), tag);
- }
- };
- } // namespace internal
- template <class R>
- class ClientAsyncReaderInterface
- : public internal::ClientAsyncStreamingInterface,
- public internal::AsyncReaderInterface<R> {};
- namespace internal {
- template <class R>
- class ClientAsyncReaderFactory {
- public:
- /// Create a stream object.
- /// Write the first request out if \a start is set.
- /// \a tag will be notified on \a cq when the call has been started and
- /// \a request has been written out. If \a start is not set, \a tag must be
- /// nullptr and the actual call must be initiated by StartCall
- /// Note that \a context will be used to fill in custom initial metadata
- /// used to send to the server when starting the call.
- template <class W>
- static ClientAsyncReader<R>* Create(grpc::ChannelInterface* channel,
- grpc::CompletionQueue* cq,
- const grpc::internal::RpcMethod& method,
- grpc::ClientContext* context,
- const W& request, bool start, void* tag) {
- grpc::internal::Call call = channel->CreateCall(method, context, cq);
- return new (
- grpc_call_arena_alloc(call.call(), sizeof(ClientAsyncReader<R>)))
- ClientAsyncReader<R>(call, context, request, start, tag);
- }
- };
- } // namespace internal
- /// Async client-side API for doing server-streaming RPCs,
- /// where the incoming message stream coming from the server has
- /// messages of type \a R.
- template <class R>
- class ClientAsyncReader final : public ClientAsyncReaderInterface<R> {
- public:
- // always allocated against a call arena, no memory free required
- static void operator delete(void* /*ptr*/, std::size_t size) {
- GPR_ASSERT(size == sizeof(ClientAsyncReader));
- }
- // This operator should never be called as the memory should be freed as part
- // of the arena destruction. It only exists to provide a matching operator
- // delete to the operator new so that some compilers will not complain (see
- // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
- // there are no tests catching the compiler warning.
- static void operator delete(void*, void*) { GPR_ASSERT(false); }
- void StartCall(void* tag) override {
- GPR_ASSERT(!started_);
- started_ = true;
- StartCallInternal(tag);
- }
- /// See the \a ClientAsyncStreamingInterface.ReadInitialMetadata
- /// method for semantics.
- ///
- /// Side effect:
- /// - upon receiving initial metadata from the server,
- /// the \a ClientContext associated with this call is updated, and the
- /// calling code can access the received metadata through the
- /// \a ClientContext.
- void ReadInitialMetadata(void* tag) override {
- GPR_ASSERT(started_);
- GPR_ASSERT(!context_->initial_metadata_received_);
- meta_ops_.set_output_tag(tag);
- meta_ops_.RecvInitialMetadata(context_);
- call_.PerformOps(&meta_ops_);
- }
- void Read(R* msg, void* tag) override {
- GPR_ASSERT(started_);
- read_ops_.set_output_tag(tag);
- if (!context_->initial_metadata_received_) {
- read_ops_.RecvInitialMetadata(context_);
- }
- read_ops_.RecvMessage(msg);
- call_.PerformOps(&read_ops_);
- }
- /// See the \a ClientAsyncStreamingInterface.Finish method for semantics.
- ///
- /// Side effect:
- /// - the \a ClientContext associated with this call is updated with
- /// possible initial and trailing metadata received from the server.
- void Finish(grpc::Status* status, void* tag) override {
- GPR_ASSERT(started_);
- finish_ops_.set_output_tag(tag);
- if (!context_->initial_metadata_received_) {
- finish_ops_.RecvInitialMetadata(context_);
- }
- finish_ops_.ClientRecvStatus(context_, status);
- call_.PerformOps(&finish_ops_);
- }
- private:
- friend class internal::ClientAsyncReaderFactory<R>;
- template <class W>
- ClientAsyncReader(grpc::internal::Call call, grpc::ClientContext* context,
- const W& request, bool start, void* tag)
- : context_(context), call_(call), started_(start) {
- // TODO(ctiller): don't assert
- GPR_ASSERT(init_ops_.SendMessage(request).ok());
- init_ops_.ClientSendClose();
- if (start) {
- StartCallInternal(tag);
- } else {
- GPR_ASSERT(tag == nullptr);
- }
- }
- void StartCallInternal(void* tag) {
- init_ops_.SendInitialMetadata(&context_->send_initial_metadata_,
- context_->initial_metadata_flags());
- init_ops_.set_output_tag(tag);
- call_.PerformOps(&init_ops_);
- }
- grpc::ClientContext* context_;
- grpc::internal::Call call_;
- bool started_;
- grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata,
- grpc::internal::CallOpSendMessage,
- grpc::internal::CallOpClientSendClose>
- init_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpRecvInitialMetadata>
- meta_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpRecvInitialMetadata,
- grpc::internal::CallOpRecvMessage<R>>
- read_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpRecvInitialMetadata,
- grpc::internal::CallOpClientRecvStatus>
- finish_ops_;
- };
- /// Common interface for client side asynchronous writing.
- template <class W>
- class ClientAsyncWriterInterface
- : public internal::ClientAsyncStreamingInterface,
- public internal::AsyncWriterInterface<W> {
- public:
- /// Signal the client is done with the writes (half-close the client stream).
- /// Thread-safe with respect to \a AsyncReaderInterface::Read
- ///
- /// \param[in] tag The tag identifying the operation.
- virtual void WritesDone(void* tag) = 0;
- };
- namespace internal {
- template <class W>
- class ClientAsyncWriterFactory {
- public:
- /// Create a stream object.
- /// Start the RPC if \a start is set
- /// \a tag will be notified on \a cq when the call has been started (i.e.
- /// initial metadata sent) and \a request has been written out.
- /// If \a start is not set, \a tag must be nullptr and the actual call
- /// must be initiated by StartCall
- /// Note that \a context will be used to fill in custom initial metadata
- /// used to send to the server when starting the call.
- /// \a response will be filled in with the single expected response
- /// message from the server upon a successful call to the \a Finish
- /// method of this instance.
- template <class R>
- static ClientAsyncWriter<W>* Create(grpc::ChannelInterface* channel,
- grpc::CompletionQueue* cq,
- const grpc::internal::RpcMethod& method,
- grpc::ClientContext* context, R* response,
- bool start, void* tag) {
- grpc::internal::Call call = channel->CreateCall(method, context, cq);
- return new (
- grpc_call_arena_alloc(call.call(), sizeof(ClientAsyncWriter<W>)))
- ClientAsyncWriter<W>(call, context, response, start, tag);
- }
- };
- } // namespace internal
- /// Async API on the client side for doing client-streaming RPCs,
- /// where the outgoing message stream going to the server contains
- /// messages of type \a W.
- template <class W>
- class ClientAsyncWriter final : public ClientAsyncWriterInterface<W> {
- public:
- // always allocated against a call arena, no memory free required
- static void operator delete(void* /*ptr*/, std::size_t size) {
- GPR_ASSERT(size == sizeof(ClientAsyncWriter));
- }
- // This operator should never be called as the memory should be freed as part
- // of the arena destruction. It only exists to provide a matching operator
- // delete to the operator new so that some compilers will not complain (see
- // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
- // there are no tests catching the compiler warning.
- static void operator delete(void*, void*) { GPR_ASSERT(false); }
- void StartCall(void* tag) override {
- GPR_ASSERT(!started_);
- started_ = true;
- StartCallInternal(tag);
- }
- /// See the \a ClientAsyncStreamingInterface.ReadInitialMetadata method for
- /// semantics.
- ///
- /// Side effect:
- /// - upon receiving initial metadata from the server, the \a ClientContext
- /// associated with this call is updated, and the calling code can access
- /// the received metadata through the \a ClientContext.
- void ReadInitialMetadata(void* tag) override {
- GPR_ASSERT(started_);
- GPR_ASSERT(!context_->initial_metadata_received_);
- meta_ops_.set_output_tag(tag);
- meta_ops_.RecvInitialMetadata(context_);
- call_.PerformOps(&meta_ops_);
- }
- void Write(const W& msg, void* tag) override {
- GPR_ASSERT(started_);
- write_ops_.set_output_tag(tag);
- // TODO(ctiller): don't assert
- GPR_ASSERT(write_ops_.SendMessage(msg).ok());
- call_.PerformOps(&write_ops_);
- }
- void Write(const W& msg, grpc::WriteOptions options, void* tag) override {
- GPR_ASSERT(started_);
- write_ops_.set_output_tag(tag);
- if (options.is_last_message()) {
- options.set_buffer_hint();
- write_ops_.ClientSendClose();
- }
- // TODO(ctiller): don't assert
- GPR_ASSERT(write_ops_.SendMessage(msg, options).ok());
- call_.PerformOps(&write_ops_);
- }
- void WritesDone(void* tag) override {
- GPR_ASSERT(started_);
- write_ops_.set_output_tag(tag);
- write_ops_.ClientSendClose();
- call_.PerformOps(&write_ops_);
- }
- /// See the \a ClientAsyncStreamingInterface.Finish method for semantics.
- ///
- /// Side effect:
- /// - the \a ClientContext associated with this call is updated with
- /// possible initial and trailing metadata received from the server.
- /// - attempts to fill in the \a response parameter passed to this class's
- /// constructor with the server's response message.
- void Finish(grpc::Status* status, void* tag) override {
- GPR_ASSERT(started_);
- finish_ops_.set_output_tag(tag);
- if (!context_->initial_metadata_received_) {
- finish_ops_.RecvInitialMetadata(context_);
- }
- finish_ops_.ClientRecvStatus(context_, status);
- call_.PerformOps(&finish_ops_);
- }
- private:
- friend class internal::ClientAsyncWriterFactory<W>;
- template <class R>
- ClientAsyncWriter(grpc::internal::Call call, grpc::ClientContext* context,
- R* response, bool start, void* tag)
- : context_(context), call_(call), started_(start) {
- finish_ops_.RecvMessage(response);
- finish_ops_.AllowNoMessage();
- if (start) {
- StartCallInternal(tag);
- } else {
- GPR_ASSERT(tag == nullptr);
- }
- }
- void StartCallInternal(void* tag) {
- write_ops_.SendInitialMetadata(&context_->send_initial_metadata_,
- context_->initial_metadata_flags());
- // if corked bit is set in context, we just keep the initial metadata
- // buffered up to coalesce with later message send. No op is performed.
- if (!context_->initial_metadata_corked_) {
- write_ops_.set_output_tag(tag);
- call_.PerformOps(&write_ops_);
- }
- }
- grpc::ClientContext* context_;
- grpc::internal::Call call_;
- bool started_;
- grpc::internal::CallOpSet<grpc::internal::CallOpRecvInitialMetadata>
- meta_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata,
- grpc::internal::CallOpSendMessage,
- grpc::internal::CallOpClientSendClose>
- write_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpRecvInitialMetadata,
- grpc::internal::CallOpGenericRecvMessage,
- grpc::internal::CallOpClientRecvStatus>
- finish_ops_;
- };
- /// Async client-side interface for bi-directional streaming,
- /// where the client-to-server message stream has messages of type \a W,
- /// and the server-to-client message stream has messages of type \a R.
- template <class W, class R>
- class ClientAsyncReaderWriterInterface
- : public internal::ClientAsyncStreamingInterface,
- public internal::AsyncWriterInterface<W>,
- public internal::AsyncReaderInterface<R> {
- public:
- /// Signal the client is done with the writes (half-close the client stream).
- /// Thread-safe with respect to \a AsyncReaderInterface::Read
- ///
- /// \param[in] tag The tag identifying the operation.
- virtual void WritesDone(void* tag) = 0;
- };
- namespace internal {
- template <class W, class R>
- class ClientAsyncReaderWriterFactory {
- public:
- /// Create a stream object.
- /// Start the RPC request if \a start is set.
- /// \a tag will be notified on \a cq when the call has been started (i.e.
- /// initial metadata sent). If \a start is not set, \a tag must be
- /// nullptr and the actual call must be initiated by StartCall
- /// Note that \a context will be used to fill in custom initial metadata
- /// used to send to the server when starting the call.
- static ClientAsyncReaderWriter<W, R>* Create(
- grpc::ChannelInterface* channel, grpc::CompletionQueue* cq,
- const grpc::internal::RpcMethod& method, grpc::ClientContext* context,
- bool start, void* tag) {
- grpc::internal::Call call = channel->CreateCall(method, context, cq);
- return new (grpc_call_arena_alloc(call.call(),
- sizeof(ClientAsyncReaderWriter<W, R>)))
- ClientAsyncReaderWriter<W, R>(call, context, start, tag);
- }
- };
- } // namespace internal
- /// Async client-side interface for bi-directional streaming,
- /// where the outgoing message stream going to the server
- /// has messages of type \a W, and the incoming message stream coming
- /// from the server has messages of type \a R.
- template <class W, class R>
- class ClientAsyncReaderWriter final
- : public ClientAsyncReaderWriterInterface<W, R> {
- public:
- // always allocated against a call arena, no memory free required
- static void operator delete(void* /*ptr*/, std::size_t size) {
- GPR_ASSERT(size == sizeof(ClientAsyncReaderWriter));
- }
- // This operator should never be called as the memory should be freed as part
- // of the arena destruction. It only exists to provide a matching operator
- // delete to the operator new so that some compilers will not complain (see
- // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
- // there are no tests catching the compiler warning.
- static void operator delete(void*, void*) { GPR_ASSERT(false); }
- void StartCall(void* tag) override {
- GPR_ASSERT(!started_);
- started_ = true;
- StartCallInternal(tag);
- }
- /// See the \a ClientAsyncStreamingInterface.ReadInitialMetadata method
- /// for semantics of this method.
- ///
- /// Side effect:
- /// - upon receiving initial metadata from the server, the \a ClientContext
- /// is updated with it, and then the receiving initial metadata can
- /// be accessed through this \a ClientContext.
- void ReadInitialMetadata(void* tag) override {
- GPR_ASSERT(started_);
- GPR_ASSERT(!context_->initial_metadata_received_);
- meta_ops_.set_output_tag(tag);
- meta_ops_.RecvInitialMetadata(context_);
- call_.PerformOps(&meta_ops_);
- }
- void Read(R* msg, void* tag) override {
- GPR_ASSERT(started_);
- read_ops_.set_output_tag(tag);
- if (!context_->initial_metadata_received_) {
- read_ops_.RecvInitialMetadata(context_);
- }
- read_ops_.RecvMessage(msg);
- call_.PerformOps(&read_ops_);
- }
- void Write(const W& msg, void* tag) override {
- GPR_ASSERT(started_);
- write_ops_.set_output_tag(tag);
- // TODO(ctiller): don't assert
- GPR_ASSERT(write_ops_.SendMessage(msg).ok());
- call_.PerformOps(&write_ops_);
- }
- void Write(const W& msg, grpc::WriteOptions options, void* tag) override {
- GPR_ASSERT(started_);
- write_ops_.set_output_tag(tag);
- if (options.is_last_message()) {
- options.set_buffer_hint();
- write_ops_.ClientSendClose();
- }
- // TODO(ctiller): don't assert
- GPR_ASSERT(write_ops_.SendMessage(msg, options).ok());
- call_.PerformOps(&write_ops_);
- }
- void WritesDone(void* tag) override {
- GPR_ASSERT(started_);
- write_ops_.set_output_tag(tag);
- write_ops_.ClientSendClose();
- call_.PerformOps(&write_ops_);
- }
- /// See the \a ClientAsyncStreamingInterface.Finish method for semantics.
- /// Side effect
- /// - the \a ClientContext associated with this call is updated with
- /// possible initial and trailing metadata sent from the server.
- void Finish(grpc::Status* status, void* tag) override {
- GPR_ASSERT(started_);
- finish_ops_.set_output_tag(tag);
- if (!context_->initial_metadata_received_) {
- finish_ops_.RecvInitialMetadata(context_);
- }
- finish_ops_.ClientRecvStatus(context_, status);
- call_.PerformOps(&finish_ops_);
- }
- private:
- friend class internal::ClientAsyncReaderWriterFactory<W, R>;
- ClientAsyncReaderWriter(grpc::internal::Call call,
- grpc::ClientContext* context, bool start, void* tag)
- : context_(context), call_(call), started_(start) {
- if (start) {
- StartCallInternal(tag);
- } else {
- GPR_ASSERT(tag == nullptr);
- }
- }
- void StartCallInternal(void* tag) {
- write_ops_.SendInitialMetadata(&context_->send_initial_metadata_,
- context_->initial_metadata_flags());
- // if corked bit is set in context, we just keep the initial metadata
- // buffered up to coalesce with later message send. No op is performed.
- if (!context_->initial_metadata_corked_) {
- write_ops_.set_output_tag(tag);
- call_.PerformOps(&write_ops_);
- }
- }
- grpc::ClientContext* context_;
- grpc::internal::Call call_;
- bool started_;
- grpc::internal::CallOpSet<grpc::internal::CallOpRecvInitialMetadata>
- meta_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpRecvInitialMetadata,
- grpc::internal::CallOpRecvMessage<R>>
- read_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata,
- grpc::internal::CallOpSendMessage,
- grpc::internal::CallOpClientSendClose>
- write_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpRecvInitialMetadata,
- grpc::internal::CallOpClientRecvStatus>
- finish_ops_;
- };
- template <class W, class R>
- class ServerAsyncReaderInterface
- : public grpc::internal::ServerAsyncStreamingInterface,
- public internal::AsyncReaderInterface<R> {
- public:
- /// Indicate that the stream is to be finished with a certain status code
- /// and also send out \a msg response to the client.
- /// Request notification for when the server has sent the response and the
- /// appropriate signals to the client to end the call.
- /// Should not be used concurrently with other operations.
- ///
- /// It is appropriate to call this method when:
- /// * all messages from the client have been received (either known
- /// implicitly, or explicitly because a previous
- /// \a AsyncReaderInterface::Read operation with a non-ok result,
- /// e.g., cq->Next(&read_tag, &ok) filled in 'ok' with 'false').
- ///
- /// This operation will end when the server has finished sending out initial
- /// metadata (if not sent already), response message, and status, or if
- /// some failure occurred when trying to do so.
- ///
- /// gRPC doesn't take ownership or a reference to \a msg or \a status, so it
- /// is safe to deallocate once Finish returns.
- ///
- /// \param[in] tag Tag identifying this request.
- /// \param[in] status To be sent to the client as the result of this call.
- /// \param[in] msg To be sent to the client as the response for this call.
- virtual void Finish(const W& msg, const grpc::Status& status, void* tag) = 0;
- /// Indicate that the stream is to be finished with a certain
- /// non-OK status code.
- /// Request notification for when the server has sent the appropriate
- /// signals to the client to end the call.
- /// Should not be used concurrently with other operations.
- ///
- /// This call is meant to end the call with some error, and can be called at
- /// any point that the server would like to "fail" the call (though note
- /// this shouldn't be called concurrently with any other "sending" call, like
- /// \a AsyncWriterInterface::Write).
- ///
- /// This operation will end when the server has finished sending out initial
- /// metadata (if not sent already), and status, or if some failure occurred
- /// when trying to do so.
- ///
- /// gRPC doesn't take ownership or a reference to \a status, so it is safe to
- /// to deallocate once FinishWithError returns.
- ///
- /// \param[in] tag Tag identifying this request.
- /// \param[in] status To be sent to the client as the result of this call.
- /// - Note: \a status must have a non-OK code.
- virtual void FinishWithError(const grpc::Status& status, void* tag) = 0;
- };
- /// Async server-side API for doing client-streaming RPCs,
- /// where the incoming message stream from the client has messages of type \a R,
- /// and the single response message sent from the server is type \a W.
- template <class W, class R>
- class ServerAsyncReader final : public ServerAsyncReaderInterface<W, R> {
- public:
- explicit ServerAsyncReader(grpc::ServerContext* ctx)
- : call_(nullptr, nullptr, nullptr), ctx_(ctx) {}
- /// See \a ServerAsyncStreamingInterface::SendInitialMetadata for semantics.
- ///
- /// Implicit input parameter:
- /// - The initial metadata that will be sent to the client from this op will
- /// be taken from the \a ServerContext associated with the call.
- void SendInitialMetadata(void* tag) override {
- GPR_ASSERT(!ctx_->sent_initial_metadata_);
- meta_ops_.set_output_tag(tag);
- meta_ops_.SendInitialMetadata(&ctx_->initial_metadata_,
- ctx_->initial_metadata_flags());
- if (ctx_->compression_level_set()) {
- meta_ops_.set_compression_level(ctx_->compression_level());
- }
- ctx_->sent_initial_metadata_ = true;
- call_.PerformOps(&meta_ops_);
- }
- void Read(R* msg, void* tag) override {
- read_ops_.set_output_tag(tag);
- read_ops_.RecvMessage(msg);
- call_.PerformOps(&read_ops_);
- }
- /// See the \a ServerAsyncReaderInterface.Read method for semantics
- ///
- /// Side effect:
- /// - also sends initial metadata if not already sent.
- /// - uses the \a ServerContext associated with this call to send possible
- /// initial and trailing metadata.
- ///
- /// Note: \a msg is not sent if \a status has a non-OK code.
- ///
- /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it
- /// is safe to deallocate once Finish returns.
- void Finish(const W& msg, const grpc::Status& status, void* tag) override {
- finish_ops_.set_output_tag(tag);
- if (!ctx_->sent_initial_metadata_) {
- finish_ops_.SendInitialMetadata(&ctx_->initial_metadata_,
- ctx_->initial_metadata_flags());
- if (ctx_->compression_level_set()) {
- finish_ops_.set_compression_level(ctx_->compression_level());
- }
- ctx_->sent_initial_metadata_ = true;
- }
- // The response is dropped if the status is not OK.
- if (status.ok()) {
- finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_,
- finish_ops_.SendMessage(msg));
- } else {
- finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status);
- }
- call_.PerformOps(&finish_ops_);
- }
- /// See the \a ServerAsyncReaderInterface.Read method for semantics
- ///
- /// Side effect:
- /// - also sends initial metadata if not already sent.
- /// - uses the \a ServerContext associated with this call to send possible
- /// initial and trailing metadata.
- ///
- /// gRPC doesn't take ownership or a reference to \a status, so it is safe to
- /// to deallocate once FinishWithError returns.
- void FinishWithError(const grpc::Status& status, void* tag) override {
- GPR_ASSERT(!status.ok());
- finish_ops_.set_output_tag(tag);
- if (!ctx_->sent_initial_metadata_) {
- finish_ops_.SendInitialMetadata(&ctx_->initial_metadata_,
- ctx_->initial_metadata_flags());
- if (ctx_->compression_level_set()) {
- finish_ops_.set_compression_level(ctx_->compression_level());
- }
- ctx_->sent_initial_metadata_ = true;
- }
- finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status);
- call_.PerformOps(&finish_ops_);
- }
- private:
- void BindCall(grpc::internal::Call* call) override { call_ = *call; }
- grpc::internal::Call call_;
- grpc::ServerContext* ctx_;
- grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata>
- meta_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpRecvMessage<R>> read_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata,
- grpc::internal::CallOpSendMessage,
- grpc::internal::CallOpServerSendStatus>
- finish_ops_;
- };
- template <class W>
- class ServerAsyncWriterInterface
- : public grpc::internal::ServerAsyncStreamingInterface,
- public internal::AsyncWriterInterface<W> {
- public:
- /// Indicate that the stream is to be finished with a certain status code.
- /// Request notification for when the server has sent the appropriate
- /// signals to the client to end the call.
- /// Should not be used concurrently with other operations.
- ///
- /// It is appropriate to call this method when either:
- /// * all messages from the client have been received (either known
- /// implicitly, or explicitly because a previous \a
- /// AsyncReaderInterface::Read operation with a non-ok
- /// result (e.g., cq->Next(&read_tag, &ok) filled in 'ok' with 'false'.
- /// * it is desired to end the call early with some non-OK status code.
- ///
- /// This operation will end when the server has finished sending out initial
- /// metadata (if not sent already), response message, and status, or if
- /// some failure occurred when trying to do so.
- ///
- /// gRPC doesn't take ownership or a reference to \a status, so it is safe to
- /// to deallocate once Finish returns.
- ///
- /// \param[in] tag Tag identifying this request.
- /// \param[in] status To be sent to the client as the result of this call.
- virtual void Finish(const grpc::Status& status, void* tag) = 0;
- /// Request the writing of \a msg and coalesce it with trailing metadata which
- /// contains \a status, using WriteOptions options with
- /// identifying tag \a tag.
- ///
- /// WriteAndFinish is equivalent of performing WriteLast and Finish
- /// in a single step.
- ///
- /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it
- /// is safe to deallocate once WriteAndFinish returns.
- ///
- /// \param[in] msg The message to be written.
- /// \param[in] options The WriteOptions to be used to write this message.
- /// \param[in] status The Status that server returns to client.
- /// \param[in] tag The tag identifying the operation.
- virtual void WriteAndFinish(const W& msg, grpc::WriteOptions options,
- const grpc::Status& status, void* tag) = 0;
- };
- /// Async server-side API for doing server streaming RPCs,
- /// where the outgoing message stream from the server has messages of type \a W.
- template <class W>
- class ServerAsyncWriter final : public ServerAsyncWriterInterface<W> {
- public:
- explicit ServerAsyncWriter(grpc::ServerContext* ctx)
- : call_(nullptr, nullptr, nullptr), ctx_(ctx) {}
- /// See \a ServerAsyncStreamingInterface::SendInitialMetadata for semantics.
- ///
- /// Implicit input parameter:
- /// - The initial metadata that will be sent to the client from this op will
- /// be taken from the \a ServerContext associated with the call.
- ///
- /// \param[in] tag Tag identifying this request.
- void SendInitialMetadata(void* tag) override {
- GPR_ASSERT(!ctx_->sent_initial_metadata_);
- meta_ops_.set_output_tag(tag);
- meta_ops_.SendInitialMetadata(&ctx_->initial_metadata_,
- ctx_->initial_metadata_flags());
- if (ctx_->compression_level_set()) {
- meta_ops_.set_compression_level(ctx_->compression_level());
- }
- ctx_->sent_initial_metadata_ = true;
- call_.PerformOps(&meta_ops_);
- }
- void Write(const W& msg, void* tag) override {
- write_ops_.set_output_tag(tag);
- EnsureInitialMetadataSent(&write_ops_);
- // TODO(ctiller): don't assert
- GPR_ASSERT(write_ops_.SendMessage(msg).ok());
- call_.PerformOps(&write_ops_);
- }
- void Write(const W& msg, grpc::WriteOptions options, void* tag) override {
- write_ops_.set_output_tag(tag);
- if (options.is_last_message()) {
- options.set_buffer_hint();
- }
- EnsureInitialMetadataSent(&write_ops_);
- // TODO(ctiller): don't assert
- GPR_ASSERT(write_ops_.SendMessage(msg, options).ok());
- call_.PerformOps(&write_ops_);
- }
- /// See the \a ServerAsyncWriterInterface.WriteAndFinish method for semantics.
- ///
- /// Implicit input parameter:
- /// - the \a ServerContext associated with this call is used
- /// for sending trailing (and initial) metadata to the client.
- ///
- /// Note: \a status must have an OK code.
- ///
- /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it
- /// is safe to deallocate once WriteAndFinish returns.
- void WriteAndFinish(const W& msg, grpc::WriteOptions options,
- const grpc::Status& status, void* tag) override {
- write_ops_.set_output_tag(tag);
- EnsureInitialMetadataSent(&write_ops_);
- options.set_buffer_hint();
- GPR_ASSERT(write_ops_.SendMessage(msg, options).ok());
- write_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status);
- call_.PerformOps(&write_ops_);
- }
- /// See the \a ServerAsyncWriterInterface.Finish method for semantics.
- ///
- /// Implicit input parameter:
- /// - the \a ServerContext associated with this call is used for sending
- /// trailing (and initial if not already sent) metadata to the client.
- ///
- /// Note: there are no restrictions are the code of
- /// \a status,it may be non-OK
- ///
- /// gRPC doesn't take ownership or a reference to \a status, so it is safe to
- /// to deallocate once Finish returns.
- void Finish(const grpc::Status& status, void* tag) override {
- finish_ops_.set_output_tag(tag);
- EnsureInitialMetadataSent(&finish_ops_);
- finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status);
- call_.PerformOps(&finish_ops_);
- }
- private:
- void BindCall(grpc::internal::Call* call) override { call_ = *call; }
- template <class T>
- void EnsureInitialMetadataSent(T* ops) {
- if (!ctx_->sent_initial_metadata_) {
- ops->SendInitialMetadata(&ctx_->initial_metadata_,
- ctx_->initial_metadata_flags());
- if (ctx_->compression_level_set()) {
- ops->set_compression_level(ctx_->compression_level());
- }
- ctx_->sent_initial_metadata_ = true;
- }
- }
- grpc::internal::Call call_;
- grpc::ServerContext* ctx_;
- grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata>
- meta_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata,
- grpc::internal::CallOpSendMessage,
- grpc::internal::CallOpServerSendStatus>
- write_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata,
- grpc::internal::CallOpServerSendStatus>
- finish_ops_;
- };
- /// Server-side interface for asynchronous bi-directional streaming.
- template <class W, class R>
- class ServerAsyncReaderWriterInterface
- : public grpc::internal::ServerAsyncStreamingInterface,
- public internal::AsyncWriterInterface<W>,
- public internal::AsyncReaderInterface<R> {
- public:
- /// Indicate that the stream is to be finished with a certain status code.
- /// Request notification for when the server has sent the appropriate
- /// signals to the client to end the call.
- /// Should not be used concurrently with other operations.
- ///
- /// It is appropriate to call this method when either:
- /// * all messages from the client have been received (either known
- /// implicitly, or explicitly because a previous \a
- /// AsyncReaderInterface::Read operation
- /// with a non-ok result (e.g., cq->Next(&read_tag, &ok) filled in 'ok'
- /// with 'false'.
- /// * it is desired to end the call early with some non-OK status code.
- ///
- /// This operation will end when the server has finished sending out initial
- /// metadata (if not sent already), response message, and status, or if some
- /// failure occurred when trying to do so.
- ///
- /// gRPC doesn't take ownership or a reference to \a status, so it is safe to
- /// to deallocate once Finish returns.
- ///
- /// \param[in] tag Tag identifying this request.
- /// \param[in] status To be sent to the client as the result of this call.
- virtual void Finish(const grpc::Status& status, void* tag) = 0;
- /// Request the writing of \a msg and coalesce it with trailing metadata which
- /// contains \a status, using WriteOptions options with
- /// identifying tag \a tag.
- ///
- /// WriteAndFinish is equivalent of performing WriteLast and Finish in a
- /// single step.
- ///
- /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it
- /// is safe to deallocate once WriteAndFinish returns.
- ///
- /// \param[in] msg The message to be written.
- /// \param[in] options The WriteOptions to be used to write this message.
- /// \param[in] status The Status that server returns to client.
- /// \param[in] tag The tag identifying the operation.
- virtual void WriteAndFinish(const W& msg, grpc::WriteOptions options,
- const grpc::Status& status, void* tag) = 0;
- };
- /// Async server-side API for doing bidirectional streaming RPCs,
- /// where the incoming message stream coming from the client has messages of
- /// type \a R, and the outgoing message stream coming from the server has
- /// messages of type \a W.
- template <class W, class R>
- class ServerAsyncReaderWriter final
- : public ServerAsyncReaderWriterInterface<W, R> {
- public:
- explicit ServerAsyncReaderWriter(grpc::ServerContext* ctx)
- : call_(nullptr, nullptr, nullptr), ctx_(ctx) {}
- /// See \a ServerAsyncStreamingInterface::SendInitialMetadata for semantics.
- ///
- /// Implicit input parameter:
- /// - The initial metadata that will be sent to the client from this op will
- /// be taken from the \a ServerContext associated with the call.
- ///
- /// \param[in] tag Tag identifying this request.
- void SendInitialMetadata(void* tag) override {
- GPR_ASSERT(!ctx_->sent_initial_metadata_);
- meta_ops_.set_output_tag(tag);
- meta_ops_.SendInitialMetadata(&ctx_->initial_metadata_,
- ctx_->initial_metadata_flags());
- if (ctx_->compression_level_set()) {
- meta_ops_.set_compression_level(ctx_->compression_level());
- }
- ctx_->sent_initial_metadata_ = true;
- call_.PerformOps(&meta_ops_);
- }
- void Read(R* msg, void* tag) override {
- read_ops_.set_output_tag(tag);
- read_ops_.RecvMessage(msg);
- call_.PerformOps(&read_ops_);
- }
- void Write(const W& msg, void* tag) override {
- write_ops_.set_output_tag(tag);
- EnsureInitialMetadataSent(&write_ops_);
- // TODO(ctiller): don't assert
- GPR_ASSERT(write_ops_.SendMessage(msg).ok());
- call_.PerformOps(&write_ops_);
- }
- void Write(const W& msg, grpc::WriteOptions options, void* tag) override {
- write_ops_.set_output_tag(tag);
- if (options.is_last_message()) {
- options.set_buffer_hint();
- }
- EnsureInitialMetadataSent(&write_ops_);
- GPR_ASSERT(write_ops_.SendMessage(msg, options).ok());
- call_.PerformOps(&write_ops_);
- }
- /// See the \a ServerAsyncReaderWriterInterface.WriteAndFinish
- /// method for semantics.
- ///
- /// Implicit input parameter:
- /// - the \a ServerContext associated with this call is used
- /// for sending trailing (and initial) metadata to the client.
- ///
- /// Note: \a status must have an OK code.
- //
- /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it
- /// is safe to deallocate once WriteAndFinish returns.
- void WriteAndFinish(const W& msg, grpc::WriteOptions options,
- const grpc::Status& status, void* tag) override {
- write_ops_.set_output_tag(tag);
- EnsureInitialMetadataSent(&write_ops_);
- options.set_buffer_hint();
- GPR_ASSERT(write_ops_.SendMessage(msg, options).ok());
- write_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status);
- call_.PerformOps(&write_ops_);
- }
- /// See the \a ServerAsyncReaderWriterInterface.Finish method for semantics.
- ///
- /// Implicit input parameter:
- /// - the \a ServerContext associated with this call is used for sending
- /// trailing (and initial if not already sent) metadata to the client.
- ///
- /// Note: there are no restrictions are the code of \a status,
- /// it may be non-OK
- //
- /// gRPC doesn't take ownership or a reference to \a status, so it is safe to
- /// to deallocate once Finish returns.
- void Finish(const grpc::Status& status, void* tag) override {
- finish_ops_.set_output_tag(tag);
- EnsureInitialMetadataSent(&finish_ops_);
- finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status);
- call_.PerformOps(&finish_ops_);
- }
- private:
- friend class grpc::Server;
- void BindCall(grpc::internal::Call* call) override { call_ = *call; }
- template <class T>
- void EnsureInitialMetadataSent(T* ops) {
- if (!ctx_->sent_initial_metadata_) {
- ops->SendInitialMetadata(&ctx_->initial_metadata_,
- ctx_->initial_metadata_flags());
- if (ctx_->compression_level_set()) {
- ops->set_compression_level(ctx_->compression_level());
- }
- ctx_->sent_initial_metadata_ = true;
- }
- }
- grpc::internal::Call call_;
- grpc::ServerContext* ctx_;
- grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata>
- meta_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpRecvMessage<R>> read_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata,
- grpc::internal::CallOpSendMessage,
- grpc::internal::CallOpServerSendStatus>
- write_ops_;
- grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata,
- grpc::internal::CallOpServerSendStatus>
- finish_ops_;
- };
- } // namespace grpc
- #endif // GRPCPP_SUPPORT_ASYNC_STREAM_H
|