GRPC C++  1.85.0
client_callback.h
Go to the documentation of this file.
1 //
2 //
3 // Copyright 2018 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPCPP_SUPPORT_CLIENT_CALLBACK_H
20 #define GRPCPP_SUPPORT_CLIENT_CALLBACK_H
21 
22 #include <grpc/grpc.h>
23 #include <grpc/impl/call.h>
24 #include <grpcpp/impl/call.h>
27 #include <grpcpp/impl/sync.h>
30 #include <grpcpp/support/config.h>
31 #include <grpcpp/support/status.h>
32 
33 #include <atomic>
34 #include <cstddef>
35 #include <functional>
36 #include <memory>
37 #include <type_traits>
38 #include <utility>
39 
40 #include "absl/functional/any_invocable.h"
41 #include "absl/log/absl_check.h"
42 
43 namespace grpc {
44 class Channel;
45 class ClientContext;
46 
47 namespace internal {
48 class RpcMethod;
49 
56 template <class InputMessage, class OutputMessage,
57  class BaseInputMessage = InputMessage,
58  class BaseOutputMessage = OutputMessage>
60  const grpc::internal::RpcMethod& method,
61  grpc::ClientContext* context,
62  const InputMessage* request, OutputMessage* result,
63  std::function<void(grpc::Status)>&& on_completion) {
64  static_assert(std::is_base_of<BaseInputMessage, InputMessage>::value,
65  "Invalid input message specification");
66  static_assert(std::is_base_of<BaseOutputMessage, OutputMessage>::value,
67  "Invalid output message specification");
69  channel, method, context, request, result, std::move(on_completion));
70 }
71 
72 template <class InputMessage, class OutputMessage>
73 class CallbackUnaryCallImpl {
74  public:
76  const grpc::internal::RpcMethod& method,
77  grpc::ClientContext* context,
78  const InputMessage* request, OutputMessage* result,
79  std::function<void(grpc::Status)>&& on_completion) {
80  grpc::CompletionQueue* cq = channel->CallbackCQ();
81  ABSL_CHECK_NE(cq, nullptr);
82  grpc::internal::Call call(channel->CreateCall(method, context, cq));
83 
84  using FullCallOpSet = grpc::internal::CallOpSet<
91 
92  struct OpSetAndTag {
93  FullCallOpSet opset;
95  };
96  const size_t alloc_sz = sizeof(OpSetAndTag);
97  auto* const alloced =
98  static_cast<OpSetAndTag*>(grpc_call_arena_alloc(call.call(), alloc_sz));
99  auto* ops = new (&alloced->opset) FullCallOpSet;
100  auto* tag = new (&alloced->tag) grpc::internal::CallbackWithStatusTag(
101  call.call(), std::move(on_completion), ops);
102 
103  // TODO(vjpai): Unify code with sync API as much as possible
104  grpc::Status s = ops->SendMessagePtr(request, channel->memory_allocator());
105  if (!s.ok()) {
106  tag->force_run(s);
107  return;
108  }
109  ops->SendInitialMetadata(&context->send_initial_metadata_,
110  context->initial_metadata_flags());
111  ops->RecvInitialMetadata(context);
112  ops->RecvMessage(result);
113  ops->AllowNoMessage();
114  ops->ClientSendClose();
115  ops->ClientRecvStatus(context, tag->status_ptr());
116  ops->set_core_cq_tag(tag);
117  ops->FillOps(&call);
118  }
119 };
120 
121 // Base class for public API classes.
123  public:
124  virtual ~ClientReactor() = default;
125 
133  virtual void OnDone(const grpc::Status& /*s*/) = 0;
134 
142  virtual bool InternalTrailersOnly(const grpc_call* call) const;
143 };
144 
145 } // namespace internal
146 
147 namespace experimental {
148 template <class RequestType, class ResponseType>
150 
153 } // namespace experimental
154 
155 // Forward declarations
156 template <class Request, class Response>
158 template <class Response>
160 template <class Request>
162 class ClientUnaryReactor;
163 
164 // NOTE: The streaming objects are not actually implemented in the public API.
165 // These interfaces are provided for mocking only. Typical applications
166 // will interact exclusively with the reactors that they define.
167 template <class Request, class Response>
169  public:
171  virtual void StartCall() = 0;
172  virtual void Write(const Request* req, grpc::WriteOptions options) = 0;
173  virtual void WritesDone() = 0;
174  virtual void Read(Response* resp) = 0;
175  virtual void AddHold(int holds) = 0;
176  virtual void RemoveHold() = 0;
177 
178  protected:
180  reactor->BindStream(this);
181  }
182 };
183 
184 template <class Response>
186  public:
188  virtual void StartCall() = 0;
189  virtual void Read(Response* resp) = 0;
190  virtual void AddHold(int holds) = 0;
191  virtual void RemoveHold() = 0;
192 
193  protected:
195  reactor->BindReader(this);
196  }
197 };
198 
199 template <class Request>
201  public:
203  virtual void StartCall() = 0;
204  void Write(const Request* req) { Write(req, grpc::WriteOptions()); }
205  virtual void Write(const Request* req, grpc::WriteOptions options) = 0;
206  void WriteLast(const Request* req, grpc::WriteOptions options) {
207  Write(req, options.set_last_message());
208  }
209  virtual void WritesDone() = 0;
210 
211  virtual void AddHold(int holds) = 0;
212  virtual void RemoveHold() = 0;
213 
214  protected:
216  reactor->BindWriter(this);
217  }
218 };
219 
221  public:
222  virtual ~ClientCallbackUnary() {}
223  virtual void StartCall() = 0;
224 
225  protected:
226  void BindReactor(ClientUnaryReactor* reactor);
227 };
228 
229 namespace experimental {
231  public:
233  virtual void StartCall() = 0;
234 
235  protected:
236  void BindReactor(ClientSessionReactor* reactor);
237 };
238 } // namespace experimental
239 
240 // The following classes are the reactor interfaces that are to be implemented
241 // by the user. They are passed in to the library as an argument to a call on a
242 // stub (either a codegen-ed call or a generic call). The streaming RPC is
243 // activated by calling StartCall, possibly after initiating StartRead,
244 // StartWrite, or AddHold operations on the streaming object. Note that none of
245 // the classes are pure; all reactions have a default empty reaction so that the
246 // user class only needs to override those reactions that it cares about.
247 // The reactor must be passed to the stub invocation before any of the below
248 // operations can be called and its reactions will be invoked by the library in
249 // response to the completion of various operations. Reactions must not include
250 // blocking operations (such as blocking I/O, starting synchronous RPCs, or
251 // waiting on condition variables). Reactions may be invoked concurrently,
252 // except that OnDone is called after all others (assuming proper API usage).
253 // The reactor may not be deleted until OnDone is called.
254 
256 template <class Request, class Response>
257 class ClientBidiReactor : public internal::ClientReactor {
258  public:
263  void StartCall() { stream_->StartCall(); }
264 
270  void StartRead(Response* resp) { stream_->Read(resp); }
271 
278  void StartWrite(const Request* req) { StartWrite(req, grpc::WriteOptions()); }
279 
286  void StartWrite(const Request* req, grpc::WriteOptions options) {
287  stream_->Write(req, options);
288  }
289 
299  void StartWriteLast(const Request* req, grpc::WriteOptions options) {
300  StartWrite(req, options.set_last_message());
301  }
302 
308  void StartWritesDone() { stream_->WritesDone(); }
309 
332  void AddHold() { AddMultipleHolds(1); }
333  void AddMultipleHolds(int holds) {
334  ABSL_DCHECK_GT(holds, 0);
335  stream_->AddHold(holds);
336  }
337  void RemoveHold() { stream_->RemoveHold(); }
338 
350  void OnDone(const grpc::Status& /*s*/) override {}
351 
359  virtual void OnReadInitialMetadataDone(bool /*ok*/) {}
360 
365  virtual void OnReadDone(bool /*ok*/) {}
366 
372  virtual void OnWriteDone(bool /*ok*/) {}
373 
381  virtual void OnWritesDoneDone(bool /*ok*/) {}
382 
383  private:
384  friend class ClientCallbackReaderWriter<Request, Response>;
385  void BindStream(ClientCallbackReaderWriter<Request, Response>* stream) {
386  stream_ = stream;
387  }
389 };
390 
393 template <class Response>
394 class ClientReadReactor : public internal::ClientReactor {
395  public:
396  void StartCall() { reader_->StartCall(); }
397  void StartRead(Response* resp) { reader_->Read(resp); }
398 
399  void AddHold() { AddMultipleHolds(1); }
400  void AddMultipleHolds(int holds) {
401  ABSL_DCHECK_GT(holds, 0);
402  reader_->AddHold(holds);
403  }
404  void RemoveHold() { reader_->RemoveHold(); }
405 
406  void OnDone(const grpc::Status& /*s*/) override {}
407  virtual void OnReadInitialMetadataDone(bool /*ok*/) {}
408  virtual void OnReadDone(bool /*ok*/) {}
409 
410  private:
411  friend class ClientCallbackReader<Response>;
412  void BindReader(ClientCallbackReader<Response>* reader) { reader_ = reader; }
414 };
415 
418 template <class Request>
419 class ClientWriteReactor : public internal::ClientReactor {
420  public:
421  void StartCall() { writer_->StartCall(); }
422  void StartWrite(const Request* req) { StartWrite(req, grpc::WriteOptions()); }
423  void StartWrite(const Request* req, grpc::WriteOptions options) {
424  writer_->Write(req, options);
425  }
426  void StartWriteLast(const Request* req, grpc::WriteOptions options) {
427  StartWrite(req, options.set_last_message());
428  }
429  void StartWritesDone() { writer_->WritesDone(); }
430 
431  void AddHold() { AddMultipleHolds(1); }
432  void AddMultipleHolds(int holds) {
433  ABSL_DCHECK_GT(holds, 0);
434  writer_->AddHold(holds);
435  }
436  void RemoveHold() { writer_->RemoveHold(); }
437 
438  void OnDone(const grpc::Status& /*s*/) override {}
439  virtual void OnReadInitialMetadataDone(bool /*ok*/) {}
440  virtual void OnWriteDone(bool /*ok*/) {}
441  virtual void OnWritesDoneDone(bool /*ok*/) {}
442 
443  private:
444  friend class ClientCallbackWriter<Request>;
445  void BindWriter(ClientCallbackWriter<Request>* writer) { writer_ = writer; }
446 
448 };
449 
462  public:
463  void StartCall() { call_->StartCall(); }
464  void OnDone(const grpc::Status& /*s*/) override {}
465  virtual void OnReadInitialMetadataDone(bool /*ok*/) {}
466 
467  private:
468  friend class ClientCallbackUnary;
469  void BindCall(ClientCallbackUnary* call) { call_ = call; }
470  ClientCallbackUnary* call_;
471 };
472 
473 // Define function out-of-line from class to avoid forward declaration issue
475  reactor->BindCall(this);
476 }
477 
478 namespace internal {
479 std::shared_ptr<Channel> CreateVirtualChannel(
480  Call call, const ChannelArguments& args,
481  absl::AnyInvocable<void()> goaway_callback);
482 } // namespace internal
483 
484 namespace experimental {
489  public:
490  void StartCall() { call_->StartCall(); }
491  void OnDone(const grpc::Status& /*s*/) override {}
492  virtual void OnSessionReady(std::shared_ptr<Channel> virtual_channel) = 0;
493  virtual void OnSessionAcknowledged(bool /*ok*/) {}
494  virtual void OnGracefulShutdown() {}
495 
496  private:
497  friend class ClientCallbackSession;
498  void BindCall(ClientCallbackSession* call) { call_ = call; }
499  ClientCallbackSession* call_;
500 };
501 
502 // Define function out-of-line from class to avoid forward declaration issue
504  reactor->BindCall(this);
505 }
506 } // namespace experimental
507 
508 namespace internal {
509 
510 // Forward declare factory classes for friendship
511 template <class Request, class Response>
512 class ClientCallbackReaderWriterFactory;
513 template <class Response>
514 class ClientCallbackReaderFactory;
515 template <class Request>
516 class ClientCallbackWriterFactory;
517 
518 template <class Request, class Response>
519 class ClientCallbackReaderWriterImpl
520  : public ClientCallbackReaderWriter<Request, Response> {
521  public:
522  // always allocated against a call arena, no memory free required
523  static void operator delete(void* /*ptr*/, std::size_t size) {
524  ABSL_CHECK_EQ(size, sizeof(ClientCallbackReaderWriterImpl));
525  }
526 
527  // This operator should never be called as the memory should be freed as part
528  // of the arena destruction. It only exists to provide a matching operator
529  // delete to the operator new so that some compilers will not complain (see
530  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
531  // there are no tests catching the compiler warning.
532  static void operator delete(void*, void*) { ABSL_CHECK(false); }
533 
534  void StartCall() ABSL_LOCKS_EXCLUDED(start_mu_) override {
535  // This call initiates two batches, plus any backlog, each with a callback
536  // 1. Send initial metadata (unless corked) + recv initial metadata
537  // 2. Any read backlog
538  // 3. Any write backlog
539  // 4. Recv trailing metadata (unless corked)
540  if (!start_corked_) {
541  start_ops_.SendInitialMetadata(&context_->send_initial_metadata_,
542  context_->initial_metadata_flags());
543  }
544 
545  start_ops_.FillOps(&call_);
546 
547  {
548  grpc::internal::MutexLock lock(start_mu_);
549 
550  if (backlog_.read_ops) {
551  read_ops_.FillOps(&call_);
552  }
553  if (backlog_.write_ops) {
554  write_ops_.FillOps(&call_);
555  }
556  if (backlog_.writes_done_ops) {
557  writes_done_ops_.FillOps(&call_);
558  }
559  finish_ops_.FillOps(&call_);
560  // The last thing in this critical section is to set started_ so that it
561  // can be used lock-free as well.
562  started_.store(true, std::memory_order_release);
563  }
564  // MaybeFinish outside the lock to make sure that destruction of this object
565  // doesn't take place while holding the lock (which would cause the lock to
566  // be released after destruction)
567  this->MaybeFinish(/*from_reaction=*/false);
568  }
569 
570  void Read(Response* msg) override {
571  read_ops_.RecvMessage(msg);
572  callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed);
573  if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) {
574  grpc::internal::MutexLock lock(start_mu_);
575  if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) {
576  backlog_.read_ops = true;
577  return;
578  }
579  }
580  read_ops_.FillOps(&call_);
581  }
582 
583  void Write(const Request* msg, grpc::WriteOptions options)
584  ABSL_LOCKS_EXCLUDED(start_mu_) override {
585  if (options.is_last_message()) {
586  options.set_buffer_hint();
587  write_ops_.ClientSendClose();
588  }
589 
590  // TODO(vjpai): don't assert
591  ABSL_CHECK(
592  write_ops_.SendMessagePtr(msg, options, channel_->memory_allocator())
593  .ok());
594  callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed);
595  if (GPR_UNLIKELY(corked_write_needed_)) {
596  write_ops_.SendInitialMetadata(&context_->send_initial_metadata_,
597  context_->initial_metadata_flags());
598  corked_write_needed_ = false;
599  }
600 
601  if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) {
602  grpc::internal::MutexLock lock(start_mu_);
603  if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) {
604  backlog_.write_ops = true;
605  return;
606  }
607  }
608  write_ops_.FillOps(&call_);
609  }
610  void WritesDone() ABSL_LOCKS_EXCLUDED(start_mu_) override {
611  writes_done_ops_.ClientSendClose();
612  writes_done_tag_.Set(
613  call_.call(),
614  [this](bool ok) {
615  reactor_->OnWritesDoneDone(ok);
616  MaybeFinish(/*from_reaction=*/true);
617  },
618  &writes_done_ops_, /*can_inline=*/false);
619  writes_done_ops_.set_core_cq_tag(&writes_done_tag_);
620  callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed);
621  if (GPR_UNLIKELY(corked_write_needed_)) {
622  writes_done_ops_.SendInitialMetadata(&context_->send_initial_metadata_,
623  context_->initial_metadata_flags());
624  corked_write_needed_ = false;
625  }
626  if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) {
627  grpc::internal::MutexLock lock(start_mu_);
628  if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) {
629  backlog_.writes_done_ops = true;
630  return;
631  }
632  }
633  writes_done_ops_.FillOps(&call_);
634  }
635 
636  void AddHold(int holds) override {
637  callbacks_outstanding_.fetch_add(holds, std::memory_order_relaxed);
638  }
639  void RemoveHold() override { MaybeFinish(/*from_reaction=*/false); }
640 
641  private:
642  friend class ClientCallbackReaderWriterFactory<Request, Response>;
643 
646  grpc::ClientContext* context,
648  : channel_(channel),
649  context_(context),
650  call_(call),
651  reactor_(reactor),
652  start_corked_(context_->initial_metadata_corked_),
653  corked_write_needed_(start_corked_) {
654  this->BindReactor(reactor);
655 
656  // Set up the unchanging parts of the start, read, and write tags and ops.
657  start_tag_.Set(
658  call_.call(),
659  [this](bool ok) {
660  reactor_->OnReadInitialMetadataDone(
661  ok && !reactor_->InternalTrailersOnly(call_.call()));
662  MaybeFinish(/*from_reaction=*/true);
663  },
664  &start_ops_, /*can_inline=*/false);
665  start_ops_.RecvInitialMetadata(context_);
666  start_ops_.set_core_cq_tag(&start_tag_);
667 
668  write_tag_.Set(
669  call_.call(),
670  [this](bool ok) {
671  reactor_->OnWriteDone(ok);
672  MaybeFinish(/*from_reaction=*/true);
673  },
674  &write_ops_, /*can_inline=*/false);
675  write_ops_.set_core_cq_tag(&write_tag_);
676 
677  read_tag_.Set(
678  call_.call(),
679  [this](bool ok) {
680  reactor_->OnReadDone(ok);
681  MaybeFinish(/*from_reaction=*/true);
682  },
683  &read_ops_, /*can_inline=*/false);
684  read_ops_.set_core_cq_tag(&read_tag_);
685 
686  // Also set up the Finish tag and op set.
687  finish_tag_.Set(
688  call_.call(),
689  [this](bool /*ok*/) { MaybeFinish(/*from_reaction=*/true); },
690  &finish_ops_,
691  /*can_inline=*/false);
692  finish_ops_.ClientRecvStatus(context_, &finish_status_);
693  finish_ops_.set_core_cq_tag(&finish_tag_);
694  }
695 
696  // MaybeFinish can be called from reactions or from user-initiated operations
697  // like StartCall or RemoveHold. If this is the last operation or hold on this
698  // object, it will invoke the OnDone reaction. If MaybeFinish was called from
699  // a reaction, it can call OnDone directly. If not, it would need to schedule
700  // OnDone onto an EventEngine thread to avoid the possibility of deadlocking
701  // with any locks in the user code that invoked it.
702  void MaybeFinish(bool from_reaction) {
703  if (GPR_UNLIKELY(callbacks_outstanding_.fetch_sub(
704  1, std::memory_order_acq_rel) == 1)) {
705  grpc::Status s = std::move(finish_status_);
706  auto* reactor = reactor_;
707  auto* call = call_.call();
708  this->~ClientCallbackReaderWriterImpl();
709  if (GPR_LIKELY(from_reaction)) {
710  grpc_call_unref(call);
711  reactor->OnDone(s);
712  } else {
714  call, [reactor, s = std::move(s)]() { reactor->OnDone(s); });
715  grpc_call_unref(call);
716  }
717  }
718  }
719 
720  grpc::ChannelInterface* const channel_;
721  grpc::ClientContext* const context_;
722  grpc::internal::Call call_;
723  ClientBidiReactor<Request, Response>* const reactor_;
724 
727  start_ops_;
729  const bool start_corked_;
730  bool corked_write_needed_; // no lock needed since only accessed in
731  // Write/WritesDone which cannot be concurrent
732 
735  grpc::Status finish_status_;
736 
740  write_ops_;
742 
745  writes_done_ops_;
747 
749  read_ops_;
751 
752  struct StartCallBacklog {
753  bool write_ops = false;
754  bool writes_done_ops = false;
755  bool read_ops = false;
756  };
757  StartCallBacklog backlog_ ABSL_GUARDED_BY(start_mu_);
758 
759  // Minimum of 3 callbacks to pre-register for start ops, StartCall, and finish
760  std::atomic<intptr_t> callbacks_outstanding_{3};
761  std::atomic_bool started_{false};
762  grpc::internal::Mutex start_mu_;
763 };
764 
765 template <class Request, class Response>
766 class ClientCallbackReaderWriterFactory {
767  public:
768  static void Create(grpc::ChannelInterface* channel,
769  const grpc::internal::RpcMethod& method,
770  grpc::ClientContext* context,
772  grpc::internal::Call call =
773  channel->CreateCall(method, context, channel->CallbackCQ());
774 
775  grpc_call_ref(call.call());
779  context, reactor);
780  }
781 };
782 
783 template <class Response>
784 class ClientCallbackReaderImpl : public ClientCallbackReader<Response> {
785  public:
786  // always allocated against a call arena, no memory free required
787  static void operator delete(void* /*ptr*/, std::size_t size) {
788  ABSL_CHECK_EQ(size, sizeof(ClientCallbackReaderImpl));
789  }
790 
791  // This operator should never be called as the memory should be freed as part
792  // of the arena destruction. It only exists to provide a matching operator
793  // delete to the operator new so that some compilers will not complain (see
794  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
795  // there are no tests catching the compiler warning.
796  static void operator delete(void*, void*) { ABSL_CHECK(false); }
797 
798  void StartCall() override {
799  // This call initiates two batches, plus any backlog, each with a callback
800  // 1. Send initial metadata (unless corked) + recv initial metadata
801  // 2. Any backlog
802  // 3. Recv trailing metadata
803 
804  start_tag_.Set(
805  call_.call(),
806  [this](bool ok) {
807  reactor_->OnReadInitialMetadataDone(
808  ok && !reactor_->InternalTrailersOnly(call_.call()));
809  MaybeFinish(/*from_reaction=*/true);
810  },
811  &start_ops_, /*can_inline=*/false);
812  start_ops_.SendInitialMetadata(&context_->send_initial_metadata_,
813  context_->initial_metadata_flags());
814  start_ops_.RecvInitialMetadata(context_);
815  start_ops_.set_core_cq_tag(&start_tag_);
816  start_ops_.FillOps(&call_);
817 
818  // Also set up the read tag so it doesn't have to be set up each time
819  read_tag_.Set(
820  call_.call(),
821  [this](bool ok) {
822  reactor_->OnReadDone(ok);
823  MaybeFinish(/*from_reaction=*/true);
824  },
825  &read_ops_, /*can_inline=*/false);
826  read_ops_.set_core_cq_tag(&read_tag_);
827 
828  {
829  grpc::internal::MutexLock lock(start_mu_);
830  if (backlog_.read_ops) {
831  read_ops_.FillOps(&call_);
832  }
833  started_.store(true, std::memory_order_release);
834  }
835 
836  finish_tag_.Set(
837  call_.call(),
838  [this](bool /*ok*/) { MaybeFinish(/*from_reaction=*/true); },
839  &finish_ops_, /*can_inline=*/false);
840  finish_ops_.ClientRecvStatus(context_, &finish_status_);
841  finish_ops_.set_core_cq_tag(&finish_tag_);
842  finish_ops_.FillOps(&call_);
843  }
844 
845  void Read(Response* msg) override {
846  read_ops_.RecvMessage(msg);
847  callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed);
848  if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) {
849  grpc::internal::MutexLock lock(start_mu_);
850  if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) {
851  backlog_.read_ops = true;
852  return;
853  }
854  }
855  read_ops_.FillOps(&call_);
856  }
857 
858  void AddHold(int holds) override {
859  callbacks_outstanding_.fetch_add(holds, std::memory_order_relaxed);
860  }
861  void RemoveHold() override { MaybeFinish(/*from_reaction=*/false); }
862 
863  private:
864  friend class ClientCallbackReaderFactory<Response>;
865 
866  template <class Request>
869  grpc::ClientContext* context, Request* request,
871  : context_(context), call_(call), reactor_(reactor) {
872  this->BindReactor(reactor);
873  // TODO(vjpai): don't assert
874  ABSL_CHECK(
875  start_ops_.SendMessagePtr(request, channel->memory_allocator()).ok());
876  start_ops_.ClientSendClose();
877  }
878 
879  // MaybeFinish behaves as in ClientCallbackReaderWriterImpl.
880  void MaybeFinish(bool from_reaction) {
881  if (GPR_UNLIKELY(callbacks_outstanding_.fetch_sub(
882  1, std::memory_order_acq_rel) == 1)) {
883  grpc::Status s = std::move(finish_status_);
884  auto* reactor = reactor_;
885  auto* call = call_.call();
886  this->~ClientCallbackReaderImpl();
887  if (GPR_LIKELY(from_reaction)) {
888  grpc_call_unref(call);
889  reactor->OnDone(s);
890  } else {
892  call, [reactor, s = std::move(s)]() { reactor->OnDone(s); });
893  grpc_call_unref(call);
894  }
895  }
896  }
897 
898  grpc::ClientContext* const context_;
899  grpc::internal::Call call_;
900  ClientReadReactor<Response>* const reactor_;
901 
906  start_ops_;
908 
911  grpc::Status finish_status_;
912 
914  read_ops_;
916 
917  struct StartCallBacklog {
918  bool read_ops = false;
919  };
920  StartCallBacklog backlog_ ABSL_GUARDED_BY(start_mu_);
921 
922  // Minimum of 2 callbacks to pre-register for start and finish
923  std::atomic<intptr_t> callbacks_outstanding_{2};
924  std::atomic_bool started_{false};
925  grpc::internal::Mutex start_mu_;
926 };
927 
928 template <class Response>
929 class ClientCallbackReaderFactory {
930  public:
931  template <class Request>
932  static void Create(grpc::ChannelInterface* channel,
933  const grpc::internal::RpcMethod& method,
934  grpc::ClientContext* context, const Request* request,
935  ClientReadReactor<Response>* reactor) {
936  grpc::internal::Call call =
937  channel->CreateCall(method, context, channel->CallbackCQ());
938 
939  grpc_call_ref(call.call());
940  new (grpc_call_arena_alloc(call.call(),
942  ClientCallbackReaderImpl<Response>(channel, call, context, request,
943  reactor);
944  }
945 };
946 
947 template <class Request>
948 class ClientCallbackWriterImpl : public ClientCallbackWriter<Request> {
949  public:
950  // always allocated against a call arena, no memory free required
951  static void operator delete(void* /*ptr*/, std::size_t size) {
952  ABSL_CHECK_EQ(size, sizeof(ClientCallbackWriterImpl));
953  }
954 
955  // This operator should never be called as the memory should be freed as part
956  // of the arena destruction. It only exists to provide a matching operator
957  // delete to the operator new so that some compilers will not complain (see
958  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
959  // there are no tests catching the compiler warning.
960  static void operator delete(void*, void*) { ABSL_CHECK(false); }
961 
962  void StartCall() ABSL_LOCKS_EXCLUDED(start_mu_) override {
963  // This call initiates two batches, plus any backlog, each with a callback
964  // 1. Send initial metadata (unless corked) + recv initial metadata
965  // 2. Any backlog
966  // 3. Recv trailing metadata
967 
968  if (!start_corked_) {
969  start_ops_.SendInitialMetadata(&context_->send_initial_metadata_,
970  context_->initial_metadata_flags());
971  }
972  start_ops_.FillOps(&call_);
973 
974  {
975  grpc::internal::MutexLock lock(start_mu_);
976 
977  if (backlog_.write_ops) {
978  write_ops_.FillOps(&call_);
979  }
980  if (backlog_.writes_done_ops) {
981  writes_done_ops_.FillOps(&call_);
982  }
983  finish_ops_.FillOps(&call_);
984  // The last thing in this critical section is to set started_ so that it
985  // can be used lock-free as well.
986  started_.store(true, std::memory_order_release);
987  }
988  // MaybeFinish outside the lock to make sure that destruction of this object
989  // doesn't take place while holding the lock (which would cause the lock to
990  // be released after destruction)
991  this->MaybeFinish(/*from_reaction=*/false);
992  }
993 
994  void Write(const Request* msg, grpc::WriteOptions options)
995  ABSL_LOCKS_EXCLUDED(start_mu_) override {
996  if (GPR_UNLIKELY(options.is_last_message())) {
997  options.set_buffer_hint();
998  write_ops_.ClientSendClose();
999  }
1000 
1001  // TODO(vjpai): don't assert
1002  ABSL_CHECK(
1003  write_ops_.SendMessagePtr(msg, options, channel_->memory_allocator())
1004  .ok());
1005  callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed);
1006 
1007  if (GPR_UNLIKELY(corked_write_needed_)) {
1008  write_ops_.SendInitialMetadata(&context_->send_initial_metadata_,
1009  context_->initial_metadata_flags());
1010  corked_write_needed_ = false;
1011  }
1012 
1013  if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) {
1014  grpc::internal::MutexLock lock(start_mu_);
1015  if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) {
1016  backlog_.write_ops = true;
1017  return;
1018  }
1019  }
1020  write_ops_.FillOps(&call_);
1021  }
1022 
1023  void WritesDone() ABSL_LOCKS_EXCLUDED(start_mu_) override {
1024  writes_done_ops_.ClientSendClose();
1025  writes_done_tag_.Set(
1026  call_.call(),
1027  [this](bool ok) {
1028  reactor_->OnWritesDoneDone(ok);
1029  MaybeFinish(/*from_reaction=*/true);
1030  },
1031  &writes_done_ops_, /*can_inline=*/false);
1032  writes_done_ops_.set_core_cq_tag(&writes_done_tag_);
1033  callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed);
1034 
1035  if (GPR_UNLIKELY(corked_write_needed_)) {
1036  writes_done_ops_.SendInitialMetadata(&context_->send_initial_metadata_,
1037  context_->initial_metadata_flags());
1038  corked_write_needed_ = false;
1039  }
1040 
1041  if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) {
1042  grpc::internal::MutexLock lock(start_mu_);
1043  if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) {
1044  backlog_.writes_done_ops = true;
1045  return;
1046  }
1047  }
1048  writes_done_ops_.FillOps(&call_);
1049  }
1050 
1051  void AddHold(int holds) override {
1052  callbacks_outstanding_.fetch_add(holds, std::memory_order_relaxed);
1053  }
1054  void RemoveHold() override { MaybeFinish(/*from_reaction=*/false); }
1055 
1056  private:
1057  friend class ClientCallbackWriterFactory<Request>;
1058 
1059  template <class Response>
1061  grpc::internal::Call call,
1062  grpc::ClientContext* context, Response* response,
1063  ClientWriteReactor<Request>* reactor)
1064  : channel_(channel),
1065  context_(context),
1066  call_(call),
1067  reactor_(reactor),
1068  start_corked_(context_->initial_metadata_corked_),
1069  corked_write_needed_(start_corked_) {
1070  this->BindReactor(reactor);
1071 
1072  // Set up the unchanging parts of the start and write tags and ops.
1073  start_tag_.Set(
1074  call_.call(),
1075  [this](bool ok) {
1076  reactor_->OnReadInitialMetadataDone(
1077  ok && !reactor_->InternalTrailersOnly(call_.call()));
1078  MaybeFinish(/*from_reaction=*/true);
1079  },
1080  &start_ops_, /*can_inline=*/false);
1081  start_ops_.RecvInitialMetadata(context_);
1082  start_ops_.set_core_cq_tag(&start_tag_);
1083 
1084  write_tag_.Set(
1085  call_.call(),
1086  [this](bool ok) {
1087  reactor_->OnWriteDone(ok);
1088  MaybeFinish(/*from_reaction=*/true);
1089  },
1090  &write_ops_, /*can_inline=*/false);
1091  write_ops_.set_core_cq_tag(&write_tag_);
1092 
1093  // Also set up the Finish tag and op set.
1094  finish_ops_.RecvMessage(response);
1095  finish_ops_.AllowNoMessage();
1096  finish_tag_.Set(
1097  call_.call(),
1098  [this](bool /*ok*/) { MaybeFinish(/*from_reaction=*/true); },
1099  &finish_ops_,
1100  /*can_inline=*/false);
1101  finish_ops_.ClientRecvStatus(context_, &finish_status_);
1102  finish_ops_.set_core_cq_tag(&finish_tag_);
1103  }
1104 
1105  // MaybeFinish behaves as in ClientCallbackReaderWriterImpl.
1106  void MaybeFinish(bool from_reaction) {
1107  if (GPR_UNLIKELY(callbacks_outstanding_.fetch_sub(
1108  1, std::memory_order_acq_rel) == 1)) {
1109  grpc::Status s = std::move(finish_status_);
1110  auto* reactor = reactor_;
1111  auto* call = call_.call();
1112  this->~ClientCallbackWriterImpl();
1113  if (GPR_LIKELY(from_reaction)) {
1114  grpc_call_unref(call);
1115  reactor->OnDone(s);
1116  } else {
1118  call, [reactor, s = std::move(s)]() { reactor->OnDone(s); });
1119  grpc_call_unref(call);
1120  }
1121  }
1122  }
1123 
1124  grpc::ChannelInterface* const channel_;
1125  grpc::ClientContext* const context_;
1126  grpc::internal::Call call_;
1127  ClientWriteReactor<Request>* const reactor_;
1128 
1131  start_ops_;
1133  const bool start_corked_;
1134  bool corked_write_needed_; // no lock needed since only accessed in
1135  // Write/WritesDone which cannot be concurrent
1136 
1139  finish_ops_;
1141  grpc::Status finish_status_;
1142 
1146  write_ops_;
1148 
1151  writes_done_ops_;
1152  grpc::internal::CallbackWithSuccessTag writes_done_tag_;
1153 
1154  struct StartCallBacklog {
1155  bool write_ops = false;
1156  bool writes_done_ops = false;
1157  };
1158  StartCallBacklog backlog_ ABSL_GUARDED_BY(start_mu_);
1159 
1160  // Minimum of 3 callbacks to pre-register for start ops, StartCall, and finish
1161  std::atomic<intptr_t> callbacks_outstanding_{3};
1162  std::atomic_bool started_{false};
1163  grpc::internal::Mutex start_mu_;
1164 };
1165 
1166 template <class Request>
1167 class ClientCallbackWriterFactory {
1168  public:
1169  template <class Response>
1170  static void Create(grpc::ChannelInterface* channel,
1171  const grpc::internal::RpcMethod& method,
1172  grpc::ClientContext* context, Response* response,
1173  ClientWriteReactor<Request>* reactor) {
1174  grpc::internal::Call call =
1175  channel->CreateCall(method, context, channel->CallbackCQ());
1176 
1177  grpc_call_ref(call.call());
1178  new (grpc_call_arena_alloc(call.call(),
1180  ClientCallbackWriterImpl<Request>(channel, call, context, response,
1181  reactor);
1182  }
1183 };
1184 
1186  public:
1187  // always allocated against a call arena, no memory free required
1188  static void operator delete(void* /*ptr*/, std::size_t size) {
1189  ABSL_CHECK_EQ(size, sizeof(ClientCallbackUnaryImpl));
1190  }
1191 
1192  // This operator should never be called as the memory should be freed as part
1193  // of the arena destruction. It only exists to provide a matching operator
1194  // delete to the operator new so that some compilers will not complain (see
1195  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
1196  // there are no tests catching the compiler warning.
1197  static void operator delete(void*, void*) { ABSL_CHECK(false); }
1198 
1199  void StartCall() override {
1200  // This call initiates two batches, each with a callback
1201  // 1. Send initial metadata + write + writes done + recv initial metadata
1202  // 2. Read message, recv trailing metadata
1203 
1204  start_tag_.Set(
1205  call_.call(),
1206  [this](bool ok) {
1207  reactor_->OnReadInitialMetadataDone(
1208  ok && !reactor_->InternalTrailersOnly(call_.call()));
1209  MaybeFinish();
1210  },
1211  &start_ops_, /*can_inline=*/false);
1212  start_ops_.SendInitialMetadata(&context_->send_initial_metadata_,
1213  context_->initial_metadata_flags());
1214  start_ops_.RecvInitialMetadata(context_);
1215  start_ops_.set_core_cq_tag(&start_tag_);
1216  start_ops_.FillOps(&call_);
1217 
1218  finish_tag_.Set(
1219  call_.call(), [this](bool /*ok*/) { MaybeFinish(); }, &finish_ops_,
1220  /*can_inline=*/false);
1221  finish_ops_.ClientRecvStatus(context_, &finish_status_);
1222  finish_ops_.set_core_cq_tag(&finish_tag_);
1223  finish_ops_.FillOps(&call_);
1224  }
1225 
1226  private:
1228 
1229  template <class Request, class Response>
1231  grpc::internal::Call call,
1232  grpc::ClientContext* context, Request* request,
1233  Response* response, ClientUnaryReactor* reactor)
1234  : context_(context), call_(call), reactor_(reactor) {
1235  this->BindReactor(reactor);
1236 
1237  // TODO(vjpai): don't assert
1238  ABSL_CHECK(
1239  start_ops_.SendMessagePtr(request, channel->memory_allocator()).ok());
1240  start_ops_.ClientSendClose();
1241  finish_ops_.RecvMessage(response);
1242  finish_ops_.AllowNoMessage();
1243  }
1244 
1245  // In the unary case, MaybeFinish is only ever invoked from a
1246  // library-initiated reaction, so it will just directly call OnDone if this is
1247  // the last reaction for this RPC.
1248  void MaybeFinish() {
1249  if (GPR_UNLIKELY(callbacks_outstanding_.fetch_sub(
1250  1, std::memory_order_acq_rel) == 1)) {
1251  grpc::Status s = std::move(finish_status_);
1252  auto* reactor = reactor_;
1253  auto* call = call_.call();
1254  this->~ClientCallbackUnaryImpl();
1255  grpc_call_unref(call);
1256  reactor->OnDone(s);
1257  }
1258  }
1259 
1260  grpc::ClientContext* const context_;
1261  grpc::internal::Call call_;
1262  ClientUnaryReactor* const reactor_;
1263 
1268  start_ops_;
1270 
1273  finish_ops_;
1275  grpc::Status finish_status_;
1276 
1277  // This call will have 2 callbacks: start and finish
1278  std::atomic<intptr_t> callbacks_outstanding_{2};
1279 };
1280 
1282  public:
1283  template <class Request, class Response, class BaseRequest = Request,
1284  class BaseResponse = Response>
1285  static void Create(grpc::ChannelInterface* channel,
1286  const grpc::internal::RpcMethod& method,
1287  grpc::ClientContext* context, const Request* request,
1288  Response* response, ClientUnaryReactor* reactor) {
1289  grpc::internal::Call call =
1290  channel->CreateCall(method, context, channel->CallbackCQ());
1291 
1292  grpc_call_ref(call.call());
1293 
1294  new (grpc_call_arena_alloc(call.call(), sizeof(ClientCallbackUnaryImpl)))
1295  ClientCallbackUnaryImpl(channel, call, context,
1296  static_cast<const BaseRequest*>(request),
1297  static_cast<BaseResponse*>(response), reactor);
1298  }
1299 };
1300 
1301 } // namespace internal
1302 
1303 namespace experimental {
1304 namespace internal {
1305 
1307  public:
1309  : reactor_(reactor) {}
1310  void Invalidate() {
1311  grpc::internal::MutexLock lock(mu_);
1312  reactor_ = nullptr;
1313  }
1315  grpc::internal::MutexLock lock(mu_);
1316  if (reactor_ != nullptr) {
1317  reactor_->OnGracefulShutdown();
1318  }
1319  }
1320 
1321  private:
1323  ClientSessionReactor* reactor_ ABSL_GUARDED_BY(mu_);
1324 };
1325 
1327  public:
1328  // always allocated against a call arena, no memory free required
1329  static void operator delete(void* /*ptr*/, std::size_t size) {
1330  ABSL_CHECK_EQ(size, sizeof(ClientCallbackSessionImpl));
1331  }
1332 
1333  // This operator should never be called as the memory should be freed as part
1334  // of the arena destruction. It only exists to provide a matching operator
1335  // delete to the operator new so that some compilers will not complain (see
1336  // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
1337  // there are no tests catching the compiler warning.
1338  static void operator delete(void*, void*) { ABSL_CHECK(false); }
1339 
1341  if (tracker_ != nullptr) {
1342  tracker_->Invalidate();
1343  }
1344  }
1345 
1346  void StartCall() override {
1347  send_tag_.Set(
1348  call_.call(), [this](bool ok) { OnSendDone(ok); }, &send_ops_,
1349  /*can_inline=*/false);
1350 
1351  send_ops_.SendInitialMetadata(&context_->send_initial_metadata_,
1352  context_->initial_metadata_flags());
1353  send_ops_.set_core_cq_tag(&send_tag_);
1354  send_ops_.FillOps(&call_);
1355 
1356  // Also start the receive status op
1357  meta_tag_.Set(
1358  call_.call(), [this](bool ok) { OnRecvInitialMetadataDone(ok); },
1359  &meta_ops_,
1360  /*can_inline=*/false);
1361  meta_ops_.RecvInitialMetadata(context_);
1362  meta_ops_.set_core_cq_tag(&meta_tag_);
1363  meta_ops_.FillOps(&call_);
1364  finish_tag_.Set(
1365  call_.call(), [this](bool ok) { OnFinishDone(ok); }, &finish_ops_,
1366  /*can_inline=*/false);
1367  finish_ops_.ClientRecvStatus(context_, &status_);
1368  finish_ops_.set_core_cq_tag(&finish_tag_);
1369  finish_ops_.FillOps(&call_);
1370  }
1371 
1372  private:
1374 
1375  template <class Request>
1377  grpc::internal::Call call,
1378  grpc::ClientContext* context,
1379  const Request* request,
1381  grpc::ChannelArguments virtual_args)
1382  : context_(context),
1383  call_(call),
1384  reactor_(reactor),
1385  virtual_args_(virtual_args),
1386  tracker_(std::make_shared<SessionReactorTracker>(reactor)) {
1387  this->BindReactor(reactor);
1388  ABSL_CHECK(
1389  send_ops_.SendMessagePtr(request, channel->memory_allocator()).ok());
1390  }
1391 
1392  void OnSendDone(bool ok) {
1393  if (ok) {
1394  auto virtual_channel = grpc::internal::CreateVirtualChannel(
1395  call_, virtual_args_,
1396  [tracker = tracker_]() { tracker->OnGracefulShutdown(); });
1397  reactor_->OnSessionReady(std::move(virtual_channel));
1398  }
1399  MaybeFinish();
1400  }
1401 
1402  void OnRecvInitialMetadataDone(bool ok) {
1403  reactor_->OnSessionAcknowledged(
1404  ok && !reactor_->InternalTrailersOnly(call_.call()));
1405  MaybeFinish();
1406  }
1407 
1408  void OnFinishDone(bool /*ok*/) { MaybeFinish(); }
1409 
1410  void MaybeFinish() {
1411  if (--ops_outstanding_ == 0) {
1412  grpc::Status s = std::move(status_);
1413  auto* reactor = reactor_;
1414  auto* call = call_.call();
1417  [reactor, s]() { reactor->OnDone(s); });
1418  grpc_call_unref(call);
1419  }
1420  }
1421 
1422  grpc::ClientContext* context_;
1423  grpc::internal::Call call_;
1425  grpc::ChannelArguments virtual_args_;
1426  std::shared_ptr<SessionReactorTracker> tracker_;
1429  send_ops_;
1432  meta_ops_;
1436  grpc::Status status_;
1437  std::atomic<int> ops_outstanding_{3};
1438 };
1439 
1440 // ClientCallbackSessionFactory is an experimental API for creating a
1441 // ClientCallbackSession. It is not part of the public API and may be removed or
1442 // changed without notice.
1444  private:
1445  template <class Request, class BaseRequest = Request>
1446  static void Create(grpc::ChannelInterface* channel,
1447  const grpc::internal::RpcMethod& method,
1448  grpc::ClientContext* context, const Request* request,
1450  grpc::ChannelArguments virtual_args) {
1451  grpc::internal::Call call =
1452  channel->CreateCall(method, context, channel->CallbackCQ());
1453 
1454  grpc_call_ref(call.call());
1455 
1457  ClientCallbackSessionImpl(channel, call, context,
1458  static_cast<const BaseRequest*>(request),
1459  reactor, std::move(virtual_args));
1460  }
1461 
1462  template <class RequestType, class ResponseType>
1464 };
1465 } // namespace internal
1466 } // namespace experimental
1467 } // namespace grpc
1468 
1469 #endif // GRPCPP_SUPPORT_CLIENT_CALLBACK_H
grpc::internal::ClientCallbackUnaryImpl
Definition: client_callback.h:1185
grpc::experimental::ClientSessionReactor::OnSessionReady
virtual void OnSessionReady(std::shared_ptr< Channel > virtual_channel)=0
grpc::internal::CallbackWithSuccessTag
CallbackWithSuccessTag can be reused multiple times, and will be used in this fashion for streaming o...
Definition: callback_common.h:153
grpc::ClientReadReactor::OnReadInitialMetadataDone
virtual void OnReadInitialMetadataDone(bool)
Definition: client_callback.h:407
grpc::ClientWriteReactor::OnReadInitialMetadataDone
virtual void OnReadInitialMetadataDone(bool)
Definition: client_callback.h:439
grpc::ClientWriteReactor::StartWritesDone
void StartWritesDone()
Definition: client_callback.h:429
grpc::internal::CallOpRecvInitialMetadata
Definition: call_op_set.h:723
grpc::ClientCallbackWriter::~ClientCallbackWriter
virtual ~ClientCallbackWriter()
Definition: client_callback.h:202
grpc::internal::ClientCallbackReaderWriterImpl::Read
void Read(Response *msg) override
Definition: client_callback.h:570
grpc::internal::ClientCallbackWriterImpl::AddHold
void AddHold(int holds) override
Definition: client_callback.h:1051
grpc::internal::ClientCallbackReaderWriterImpl::StartCall
void StartCall() ABSL_LOCKS_EXCLUDED(start_mu_) override
Definition: client_callback.h:534
grpc::internal::CallbackUnaryCallImpl::CallbackUnaryCallImpl
CallbackUnaryCallImpl(grpc::ChannelInterface *channel, const grpc::internal::RpcMethod &method, grpc::ClientContext *context, const InputMessage *request, OutputMessage *result, std::function< void(grpc::Status)> &&on_completion)
Definition: client_callback.h:75
grpc::internal::CallOpClientSendClose
Definition: call_op_set.h:621
grpc::ClientBidiReactor::StartWrite
void StartWrite(const Request *req)
Initiate a write operation (or post it for later initiation if StartCall has not yet been invoked).
Definition: client_callback.h:278
grpc_call_arena_alloc
GRPCAPI void * grpc_call_arena_alloc(grpc_call *call, size_t size)
Allocate memory in the grpc_call arena: this memory is automatically discarded at call completion.
grpc::internal::CallOpGenericRecvMessage
Definition: call_op_set.h:528
grpc::ClientBidiReactor::AddHold
void AddHold()
Holds are needed if (and only if) this stream has operations that take place on it after StartCall bu...
Definition: client_callback.h:332
grpc::internal::ClientCallbackReaderWriterImpl
Definition: client_context.h:72
grpc::ClientReadReactor::OnReadDone
virtual void OnReadDone(bool)
Definition: client_callback.h:408
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::internal::CallOpSet
Primary implementation of CallOpSetInterface.
Definition: completion_queue.h:97
grpc::internal::ClientCallbackReaderWriterImpl::Write
void Write(const Request *msg, grpc::WriteOptions options) ABSL_LOCKS_EXCLUDED(start_mu_) override
Definition: client_callback.h:583
grpc::internal::ClientCallbackReaderImpl::RemoveHold
void RemoveHold() override
Definition: client_callback.h:861
grpc::internal::CallOpSendMessage
Definition: call_op_set.h:287
grpc::WriteOptions::set_last_message
WriteOptions & set_last_message()
last-message bit: indicates this is the last message in a stream client-side: makes Write the equival...
Definition: call_op_set.h:156
grpc::ClientUnaryReactor
ClientUnaryReactor is a reactor-style interface for a unary RPC.
Definition: client_callback.h:461
grpc::ClientCallbackReaderWriter::Read
virtual void Read(Response *resp)=0
grpc::internal::ClientCallbackReaderWriterFactory::Create
static void Create(grpc::ChannelInterface *channel, const grpc::internal::RpcMethod &method, grpc::ClientContext *context, ClientBidiReactor< Request, Response > *reactor)
Definition: client_callback.h:768
grpc::ClientWriteReactor::StartWrite
void StartWrite(const Request *req, grpc::WriteOptions options)
Definition: client_callback.h:423
grpc::internal::CallOpSet::FillOps
void FillOps(Call *call) override
Definition: call_op_set.h:895
grpc::ClientWriteReactor::StartCall
void StartCall()
Definition: client_callback.h:421
grpc::ClientWriteReactor::OnDone
void OnDone(const grpc::Status &) override
Definition: client_callback.h:438
grpc::internal::ClientReactor::OnDone
virtual void OnDone(const grpc::Status &)=0
Called by the library when all operations associated with this RPC have completed and all Holds have ...
grpc::internal::ClientCallbackReaderWriterImpl::RemoveHold
void RemoveHold() override
Definition: client_callback.h:639
grpc::ClientReadReactor::OnDone
void OnDone(const grpc::Status &) override
Definition: client_callback.h:406
grpc::experimental::ClientCallbackSession
Definition: client_callback.h:230
grpc::internal::Call
Straightforward wrapping of the C call object.
Definition: call.h:34
grpc::ClientCallbackReaderWriter::WritesDone
virtual void WritesDone()=0
grpc::internal::ClientCallbackWriterImpl::RemoveHold
void RemoveHold() override
Definition: client_callback.h:1054
grpc::experimental::internal::SessionReactorTracker::SessionReactorTracker
SessionReactorTracker(ClientSessionReactor *reactor)
Definition: client_callback.h:1308
status.h
grpc::internal::CallOpSendInitialMetadata
Definition: call_op_set.h:217
grpc::experimental::ClientCallbackSession::StartCall
virtual void StartCall()=0
grpc::internal::ClientCallbackWriterFactory::Create
static void Create(grpc::ChannelInterface *channel, const grpc::internal::RpcMethod &method, grpc::ClientContext *context, Response *response, ClientWriteReactor< Request > *reactor)
Definition: client_callback.h:1170
grpc::ClientBidiReactor::OnReadInitialMetadataDone
virtual void OnReadInitialMetadataDone(bool)
Notifies the application that a read of initial metadata from the server is done.
Definition: client_callback.h:359
grpc::internal::CallbackWithStatusTag
Definition: callback_common.h:74
grpc::ClientCallbackReaderWriter::BindReactor
void BindReactor(ClientBidiReactor< Request, Response > *reactor)
Definition: client_callback.h:179
grpc::internal::CallbackUnaryCall
void CallbackUnaryCall(grpc::ChannelInterface *channel, const grpc::internal::RpcMethod &method, grpc::ClientContext *context, const InputMessage *request, OutputMessage *result, std::function< void(grpc::Status)> &&on_completion)
Perform a callback-based unary call.
Definition: client_callback.h:59
grpc::Status::ok
bool ok() const
Is the status OK?
Definition: status.h:124
grpc::ClientCallbackUnary::BindReactor
void BindReactor(ClientUnaryReactor *reactor)
Definition: client_callback.h:474
grpc_call_ref
GRPCAPI void grpc_call_ref(grpc_call *call)
Ref a call.
grpc::ClientBidiReactor::StartRead
void StartRead(Response *resp)
Initiate a read operation (or post it for later initiation if StartCall has not yet been invoked).
Definition: client_callback.h:270
grpc::ClientReadReactor::AddMultipleHolds
void AddMultipleHolds(int holds)
Definition: client_callback.h:400
grpc::internal::ClientCallbackWriterImpl::StartCall
void StartCall() ABSL_LOCKS_EXCLUDED(start_mu_) override
Definition: client_callback.h:962
grpc::internal::ClientCallbackWriterImpl::Write
void Write(const Request *msg, grpc::WriteOptions options) ABSL_LOCKS_EXCLUDED(start_mu_) override
Definition: client_callback.h:994
grpc::ClientBidiReactor::StartWrite
void StartWrite(const Request *req, grpc::WriteOptions options)
Initiate/post a write operation with specified options.
Definition: client_callback.h:286
grpc::ClientBidiReactor::OnDone
void OnDone(const grpc::Status &) override
Notifies the application that all operations associated with this RPC have completed and all Holds ha...
Definition: client_callback.h:350
grpc::ChannelArguments
Options for channel creation.
Definition: channel_arguments.h:39
grpc::experimental::ClientSessionReactor::StartCall
void StartCall()
Definition: client_callback.h:490
grpc::internal::ClientReactor::~ClientReactor
virtual ~ClientReactor()=default
grpc::ClientCallbackUnary::StartCall
virtual void StartCall()=0
grpc::ClientCallbackReaderWriter::AddHold
virtual void AddHold(int holds)=0
grpc::ClientBidiReactor::OnReadDone
virtual void OnReadDone(bool)
Notifies the application that a StartRead operation completed.
Definition: client_callback.h:365
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:34
GPR_UNLIKELY
#define GPR_UNLIKELY(x)
Definition: port_platform.h:861
grpc::ClientCallbackWriter::WritesDone
virtual void WritesDone()=0
channel_arguments.h
grpc_call_unref
GRPCAPI void grpc_call_unref(grpc_call *call)
Unref a call.
grpc::internal::ClientCallbackWriterFactory
Definition: channel_interface.h:52
grpc::ClientReadReactor::StartCall
void StartCall()
Definition: client_callback.h:396
callback_common.h
grpc::ClientBidiReactor::OnWriteDone
virtual void OnWriteDone(bool)
Notifies the application that a StartWrite or StartWriteLast operation completed.
Definition: client_callback.h:372
grpc::ClientContext
A ClientContext allows the person implementing a service client to:
Definition: client_context.h:203
grpc::internal::ClientCallbackReaderImpl::StartCall
void StartCall() override
Definition: client_callback.h:798
grpc::internal::ClientCallbackReaderWriterFactory
Definition: channel_interface.h:48
grpc.h
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:68
grpc::internal::CallbackUnaryCallImpl
Definition: client_context.h:69
grpc::ClientUnaryReactor::OnReadInitialMetadataDone
virtual void OnReadInitialMetadataDone(bool)
Definition: client_callback.h:465
grpc::ClientCallbackWriter::BindReactor
void BindReactor(ClientWriteReactor< Request > *reactor)
Definition: client_callback.h:215
grpc::experimental::internal::SessionReactorTracker::Invalidate
void Invalidate()
Definition: client_callback.h:1310
grpc::ClientCallbackReader::RemoveHold
virtual void RemoveHold()=0
grpc::ClientCallbackReaderWriter
Definition: client_callback.h:168
grpc::ClientBidiReactor::StartWritesDone
void StartWritesDone()
Indicate that the RPC will have no more write operations.
Definition: client_callback.h:308
grpc::ClientCallbackReader::Read
virtual void Read(Response *resp)=0
grpc::ClientReadReactor::StartRead
void StartRead(Response *resp)
Definition: client_callback.h:397
channel_interface.h
grpc::ChannelInterface
Codegen interface for grpc::Channel.
Definition: channel_interface.h:79
grpc::internal::ClientReactor
Definition: client_callback.h:122
grpc::experimental::internal::ClientCallbackSessionImpl::~ClientCallbackSessionImpl
~ClientCallbackSessionImpl() override
Definition: client_callback.h:1340
grpc::experimental::internal::ClientCallbackSessionImpl::StartCall
void StartCall() override
Definition: client_callback.h:1346
grpc::internal::ClientCallbackUnaryFactory
Definition: client_callback.h:1281
grpc::internal::ClientCallbackReaderImpl::Read
void Read(Response *msg) override
Definition: client_callback.h:845
grpc::experimental::internal::SessionReactorTracker::OnGracefulShutdown
void OnGracefulShutdown()
Definition: client_callback.h:1314
grpc::ClientWriteReactor::StartWriteLast
void StartWriteLast(const Request *req, grpc::WriteOptions options)
Definition: client_callback.h:426
grpc::internal::ClientCallbackWriterImpl
Definition: client_context.h:76
grpc::ClientCallbackReaderWriter::Write
virtual void Write(const Request *req, grpc::WriteOptions options)=0
grpc::experimental::internal::SessionReactorTracker
Definition: client_callback.h:1306
grpc::ClientWriteReactor
ClientWriteReactor is the interface for a client-streaming RPC.
Definition: client_callback.h:161
grpc_call_run_in_event_engine
void grpc_call_run_in_event_engine(const grpc_call *call, absl::AnyInvocable< void()> cb)
grpc::experimental::ClientSessionReactor
ClientSessionReactor is a reactor-style interface for a session RPC.
Definition: client_callback.h:488
grpc::ClientCallbackUnary
Definition: client_callback.h:220
grpc::ClientBidiReactor::AddMultipleHolds
void AddMultipleHolds(int holds)
Definition: client_callback.h:333
grpc::experimental::ClientCallbackSession::~ClientCallbackSession
virtual ~ClientCallbackSession()
Definition: client_callback.h:232
grpc::ClientReadReactor::RemoveHold
void RemoveHold()
Definition: client_callback.h:404
grpc::internal::ClientCallbackUnaryFactory::Create
static void Create(grpc::ChannelInterface *channel, const grpc::internal::RpcMethod &method, grpc::ClientContext *context, const Request *request, Response *response, ClientUnaryReactor *reactor)
Definition: client_callback.h:1285
grpc::ClientBidiReactor::StartWriteLast
void StartWriteLast(const Request *req, grpc::WriteOptions options)
Initiate/post a write operation with specified options and an indication that this is the last write ...
Definition: client_callback.h:299
grpc::WriteOptions
Per-message write options.
Definition: call_op_set.h:79
grpc::ClientCallbackWriter::RemoveHold
virtual void RemoveHold()=0
grpc::ClientBidiReactor::OnWritesDoneDone
virtual void OnWritesDoneDone(bool)
Notifies the application that a StartWritesDone operation completed.
Definition: client_callback.h:381
grpc::internal::ClientCallbackWriterImpl::WritesDone
void WritesDone() ABSL_LOCKS_EXCLUDED(start_mu_) override
Definition: client_callback.h:1023
grpc::ClientCallbackReader::~ClientCallbackReader
virtual ~ClientCallbackReader()
Definition: client_callback.h:187
grpc::experimental::GenericStubSession
Definition: client_callback.h:149
grpc::internal::CallbackWithSuccessTag::Set
void Set(grpc_call *call, std::function< void(bool)> f, CompletionQueueTag *ops, bool can_inline)
Definition: callback_common.h:180
grpc::internal::ClientCallbackReaderImpl::AddHold
void AddHold(int holds) override
Definition: client_callback.h:858
grpc::ClientCallbackReader::BindReactor
void BindReactor(ClientReadReactor< Response > *reactor)
Definition: client_callback.h:194
grpc::internal::MutexLock
Definition: sync.h:80
grpc::experimental::ClientSessionReactor::OnGracefulShutdown
virtual void OnGracefulShutdown()
Definition: client_callback.h:494
grpc::ClientCallbackWriter
Definition: client_callback.h:200
grpc::ClientCallbackWriter::AddHold
virtual void AddHold(int holds)=0
grpc::ClientBidiReactor::RemoveHold
void RemoveHold()
Definition: client_callback.h:337
grpc::ClientCallbackReaderWriter::~ClientCallbackReaderWriter
virtual ~ClientCallbackReaderWriter()
Definition: client_callback.h:170
grpc::ClientCallbackReaderWriter::RemoveHold
virtual void RemoveHold()=0
grpc::ClientCallbackWriter::Write
void Write(const Request *req)
Definition: client_callback.h:204
config.h
grpc::ClientCallbackReader::StartCall
virtual void StartCall()=0
call.h
grpc::ClientWriteReactor::AddHold
void AddHold()
Definition: client_callback.h:431
grpc::experimental::ClientCallbackSession::BindReactor
void BindReactor(ClientSessionReactor *reactor)
Definition: client_callback.h:503
grpc::internal::Call::call
grpc_call * call() const
Definition: call.h:55
grpc::ClientUnaryReactor::StartCall
void StartCall()
Definition: client_callback.h:463
grpc::ClientCallbackReaderWriter::StartCall
virtual void StartCall()=0
call_op_set.h
std
Definition: async_unary_call.h:410
call.h
grpc::internal::CallOpClientRecvStatus
Definition: call_op_set.h:771
grpc::internal::CreateVirtualChannel
std::shared_ptr< Channel > CreateVirtualChannel(Call call, const ChannelArguments &args, absl::AnyInvocable< void()> goaway_callback)
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:104
grpc::ClientBidiReactor
ClientBidiReactor is the interface for a bidirectional streaming RPC.
Definition: client_callback.h:157
grpc::internal::ClientCallbackReaderFactory
Definition: channel_interface.h:50
grpc::experimental::internal::ClientCallbackSessionImpl
Definition: client_callback.h:1326
grpc::experimental::internal::ClientCallbackSessionFactory
Definition: client_callback.h:1443
grpc::ClientCallbackWriter::WriteLast
void WriteLast(const Request *req, grpc::WriteOptions options)
Definition: client_callback.h:206
grpc::internal::ClientCallbackReaderImpl
Definition: client_context.h:74
grpc::internal::ClientReactor::InternalTrailersOnly
virtual bool InternalTrailersOnly(const grpc_call *call) const
InternalTrailersOnly is not part of the API and is not meant to be overridden.
grpc::ClientCallbackWriter::StartCall
virtual void StartCall()=0
grpc::internal::ClientCallbackUnaryImpl::StartCall
void StartCall() override
Definition: client_callback.h:1199
grpc::ClientWriteReactor::StartWrite
void StartWrite(const Request *req)
Definition: client_callback.h:422
grpc::experimental::ClientSessionReactor::OnSessionAcknowledged
virtual void OnSessionAcknowledged(bool)
Definition: client_callback.h:493
grpc::ClientBidiReactor::StartCall
void StartCall()
Activate the RPC and initiate any reads or writes that have been Start'ed before this call.
Definition: client_callback.h:263
grpc::ClientWriteReactor::OnWriteDone
virtual void OnWriteDone(bool)
Definition: client_callback.h:440
grpc::internal::CallOpRecvMessage
Definition: call_op_set.h:427
grpc::internal::Mutex
Definition: sync.h:57
grpc::internal::RpcMethod
Descriptor of an RPC method.
Definition: rpc_method.h:29
grpc::ClientCallbackReader::AddHold
virtual void AddHold(int holds)=0
grpc::ClientWriteReactor::AddMultipleHolds
void AddMultipleHolds(int holds)
Definition: client_callback.h:432
grpc::ClientCallbackReader
Definition: client_callback.h:185
GPR_LIKELY
#define GPR_LIKELY(x)
Definition: port_platform.h:860
grpc::ClientCallbackUnary::~ClientCallbackUnary
virtual ~ClientCallbackUnary()
Definition: client_callback.h:222
grpc::internal::CallOpSet::set_core_cq_tag
void set_core_cq_tag(void *core_cq_tag)
set_core_cq_tag is used to provide a different core CQ tag than "this".
Definition: call_op_set.h:947
sync.h
grpc::internal::ClientCallbackReaderWriterImpl::AddHold
void AddHold(int holds) override
Definition: client_callback.h:636
grpc::experimental::ClientSessionReactor::OnDone
void OnDone(const grpc::Status &) override
Called by the library when all operations associated with this RPC have completed and all Holds have ...
Definition: client_callback.h:491
grpc::ClientWriteReactor::RemoveHold
void RemoveHold()
Definition: client_callback.h:436
grpc::ClientReadReactor
ClientReadReactor is the interface for a server-streaming RPC.
Definition: client_callback.h:159
grpc::internal::ClientCallbackReaderWriterImpl::WritesDone
void WritesDone() ABSL_LOCKS_EXCLUDED(start_mu_) override
Definition: client_callback.h:610
grpc::ClientReadReactor::AddHold
void AddHold()
Definition: client_callback.h:399
grpc::ClientUnaryReactor::OnDone
void OnDone(const grpc::Status &) override
Called by the library when all operations associated with this RPC have completed and all Holds have ...
Definition: client_callback.h:464
grpc::ChannelInterface::memory_allocator
virtual grpc_event_engine::experimental::MemoryAllocator * memory_allocator() const
Definition: channel_interface.h:113
grpc::internal::ClientCallbackReaderFactory::Create
static void Create(grpc::ChannelInterface *channel, const grpc::internal::RpcMethod &method, grpc::ClientContext *context, const Request *request, ClientReadReactor< Response > *reactor)
Definition: client_callback.h:932
grpc::ClientWriteReactor::OnWritesDoneDone
virtual void OnWritesDoneDone(bool)
Definition: client_callback.h:441