Go to the documentation of this file.
18 #ifndef GRPCPP_IMPL_CODEGEN_SERVER_CALLBACK_IMPL_H
19 #define GRPCPP_IMPL_CODEGEN_SERVER_CALLBACK_IMPL_H
23 #include <type_traits>
39 template <
class Request,
class Response>
40 class CallbackUnaryHandler;
41 template <
class Request,
class Response>
42 class CallbackClientStreamingHandler;
43 template <
class Request,
class Response>
44 class CallbackServerStreamingHandler;
45 template <
class Request,
class Response>
46 class CallbackBidiHandler;
61 template <
class Request,
class Response>
63 template <
class Request,
class Response>
65 template <
class Request,
class Response>
67 template <
class Request,
class Response>
94 ScheduleOnDone(reactor()->InternalInlineable());
100 ScheduleOnDone(inline_ondone);
108 CallOnCancel(reactor);
118 CallOnCancel(reactor());
124 void Ref() { callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); }
132 virtual void CallOnDone() = 0;
136 void ScheduleOnDone(
bool inline_ondone);
144 bool UnblockCancellation() {
145 return on_cancel_conditions_remaining_.fetch_sub(
146 1, std::memory_order_acq_rel) == 1;
151 return callbacks_outstanding_.fetch_sub(1, std::memory_order_acq_rel);
154 std::atomic_int on_cancel_conditions_remaining_{2};
155 std::atomic_int callbacks_outstanding_{
159 template <
class Request,
class Response>
173 Request request_obj_;
174 Response response_obj_;
181 template <
class Request>
183 template <
class Response>
185 template <
class Request,
class Response>
200 template <
class Reactor>
202 reactor->InternalBindCall(
this);
206 template <
class Request>
212 virtual void Read(Request* msg) = 0;
216 reactor->InternalBindReader(
this);
220 template <
class Response>
233 reactor->InternalBindWriter(
this);
237 template <
class Request,
class Response>
244 virtual void Read(Request* msg) = 0;
251 reactor->InternalBindStream(
this);
262 template <
class Request,
class Response>
278 stream_.load(std::memory_order_acquire);
279 if (stream ==
nullptr) {
281 stream = stream_.load(std::memory_order_relaxed);
282 if (stream ==
nullptr) {
283 backlog_.send_initial_metadata_wanted =
true;
296 stream_.load(std::memory_order_acquire);
297 if (stream ==
nullptr) {
299 stream = stream_.load(std::memory_order_relaxed);
300 if (stream ==
nullptr) {
301 backlog_.read_wanted = req;
325 stream_.load(std::memory_order_acquire);
326 if (stream ==
nullptr) {
328 stream = stream_.load(std::memory_order_relaxed);
329 if (stream ==
nullptr) {
330 backlog_.write_wanted = resp;
331 backlog_.write_options_wanted = std::move(options);
335 stream->
Write(resp, std::move(options));
354 stream_.load(std::memory_order_acquire);
355 if (stream ==
nullptr) {
357 stream = stream_.load(std::memory_order_relaxed);
358 if (stream ==
nullptr) {
359 backlog_.write_and_finish_wanted =
true;
360 backlog_.write_wanted = resp;
361 backlog_.write_options_wanted = std::move(options);
362 backlog_.status_wanted = std::move(s);
389 stream_.load(std::memory_order_acquire);
390 if (stream ==
nullptr) {
392 stream = stream_.load(std::memory_order_relaxed);
393 if (stream ==
nullptr) {
394 backlog_.finish_wanted =
true;
395 backlog_.status_wanted = std::move(s);
399 stream->
Finish(std::move(s));
426 void OnDone()
override = 0;
437 virtual void InternalBindStream(
444 PreBindBacklog ops(std::move(backlog_));
445 stream_.store(stream, std::memory_order_release);
448 if (ops.send_initial_metadata_wanted) {
451 if (ops.read_wanted !=
nullptr) {
452 stream->
Read(ops.read_wanted);
454 if (ops.write_and_finish_wanted) {
456 std::move(ops.write_options_wanted),
457 std::move(ops.status_wanted));
459 if (ops.write_wanted !=
nullptr) {
460 stream->
Write(ops.write_wanted, std::move(ops.write_options_wanted));
462 if (ops.finish_wanted) {
463 stream->
Finish(std::move(ops.status_wanted));
473 std::atomic<ServerCallbackReaderWriter<Request, Response>*> stream_{
nullptr};
474 struct PreBindBacklog {
475 bool send_initial_metadata_wanted =
false;
476 bool write_and_finish_wanted =
false;
477 bool finish_wanted =
false;
478 Request* read_wanted =
nullptr;
479 const Response* write_wanted =
nullptr;
483 PreBindBacklog backlog_ ;
487 template <
class Request>
496 reader_.load(std::memory_order_acquire);
497 if (reader ==
nullptr) {
499 reader = reader_.load(std::memory_order_relaxed);
500 if (reader ==
nullptr) {
501 backlog_.send_initial_metadata_wanted =
true;
509 reader_.load(std::memory_order_acquire);
510 if (reader ==
nullptr) {
512 reader = reader_.load(std::memory_order_relaxed);
513 if (reader ==
nullptr) {
514 backlog_.read_wanted = req;
522 reader_.load(std::memory_order_acquire);
523 if (reader ==
nullptr) {
525 reader = reader_.load(std::memory_order_relaxed);
526 if (reader ==
nullptr) {
527 backlog_.finish_wanted =
true;
528 backlog_.status_wanted = std::move(s);
532 reader->
Finish(std::move(s));
538 void OnDone()
override = 0;
548 PreBindBacklog ops(std::move(backlog_));
549 reader_.store(reader, std::memory_order_release);
552 if (ops.send_initial_metadata_wanted) {
555 if (ops.read_wanted !=
nullptr) {
556 reader->
Read(ops.read_wanted);
558 if (ops.finish_wanted) {
559 reader->
Finish(std::move(ops.status_wanted));
564 std::atomic<ServerCallbackReader<Request>*> reader_{
nullptr};
565 struct PreBindBacklog {
566 bool send_initial_metadata_wanted =
false;
567 bool finish_wanted =
false;
568 Request* read_wanted =
nullptr;
571 PreBindBacklog backlog_ ;
575 template <
class Response>
584 writer_.load(std::memory_order_acquire);
585 if (writer ==
nullptr) {
587 writer = writer_.load(std::memory_order_relaxed);
588 if (writer ==
nullptr) {
589 backlog_.send_initial_metadata_wanted =
true;
600 writer_.load(std::memory_order_acquire);
601 if (writer ==
nullptr) {
603 writer = writer_.load(std::memory_order_relaxed);
604 if (writer ==
nullptr) {
605 backlog_.write_wanted = resp;
606 backlog_.write_options_wanted = std::move(options);
610 writer->
Write(resp, std::move(options));
615 writer_.load(std::memory_order_acquire);
616 if (writer ==
nullptr) {
618 writer = writer_.load(std::memory_order_relaxed);
619 if (writer ==
nullptr) {
620 backlog_.write_and_finish_wanted =
true;
621 backlog_.write_wanted = resp;
622 backlog_.write_options_wanted = std::move(options);
623 backlog_.status_wanted = std::move(s);
634 writer_.load(std::memory_order_acquire);
635 if (writer ==
nullptr) {
637 writer = writer_.load(std::memory_order_relaxed);
638 if (writer ==
nullptr) {
639 backlog_.finish_wanted =
true;
640 backlog_.status_wanted = std::move(s);
644 writer->
Finish(std::move(s));
650 void OnDone()
override = 0;
659 PreBindBacklog ops(std::move(backlog_));
660 writer_.store(writer, std::memory_order_release);
663 if (ops.send_initial_metadata_wanted) {
666 if (ops.write_and_finish_wanted) {
668 std::move(ops.write_options_wanted),
669 std::move(ops.status_wanted));
671 if (ops.write_wanted !=
nullptr) {
672 writer->
Write(ops.write_wanted, std::move(ops.write_options_wanted));
674 if (ops.finish_wanted) {
675 writer->
Finish(std::move(ops.status_wanted));
681 std::atomic<ServerCallbackWriter<Response>*> writer_{
nullptr};
682 struct PreBindBacklog {
683 bool send_initial_metadata_wanted =
false;
684 bool write_and_finish_wanted =
false;
685 bool finish_wanted =
false;
686 const Response* write_wanted =
nullptr;
690 PreBindBacklog backlog_ ;
701 if (call ==
nullptr) {
703 call = call_.load(std::memory_order_relaxed);
704 if (call ==
nullptr) {
705 backlog_.send_initial_metadata_wanted =
true;
716 if (call ==
nullptr) {
718 call = call_.load(std::memory_order_relaxed);
719 if (call ==
nullptr) {
720 backlog_.finish_wanted =
true;
721 backlog_.status_wanted = std::move(s);
725 call->
Finish(std::move(s));
730 void OnDone()
override = 0;
739 PreBindBacklog ops(std::move(backlog_));
740 call_.store(call, std::memory_order_release);
743 if (ops.send_initial_metadata_wanted) {
746 if (ops.finish_wanted) {
747 call->
Finish(std::move(ops.status_wanted));
752 std::atomic<ServerCallbackUnary*> call_{
nullptr};
753 struct PreBindBacklog {
754 bool send_initial_metadata_wanted =
false;
755 bool finish_wanted =
false;
758 PreBindBacklog backlog_ ;
763 template <
class Base>
771 template <
class Request>
773 template <
class Response>
776 template <
class Request,
class Response>
783 #endif // GRPCPP_IMPL_CODEGEN_SERVER_CALLBACK_IMPL_H
virtual void OnWriteDone(bool)
Notifies the application that a StartWrite (or StartWriteLast) operation completed.
Definition: server_callback_impl.h:421
virtual void Read(Request *msg)=0
void set_request(Request *request)
Definition: message_allocator.h:52
ServerUnaryReactor()
Definition: server_callback_impl.h:695
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:161
void OnCancel() override
Definition: server_callback_impl.h:539
void BindReactor(Reactor *reactor)
Definition: server_callback_impl.h:201
virtual void Read(Request *msg)=0
void StartWriteAndFinish(const Response *resp, ::grpc::WriteOptions options, ::grpc::Status s)
Initiate a write operation with specified options and final RPC Status, which also causes any trailin...
Definition: server_callback_impl.h:351
Definition: server_callback_impl.h:693
virtual ~ServerCallbackReaderWriter()
Definition: server_callback_impl.h:240
void StartSendInitialMetadata()
Send any initial metadata stored in the RPC context.
Definition: server_callback_impl.h:276
ServerBidiReactor is the interface for a bidirectional streaming RPC.
Definition: server_callback_impl.h:186
virtual void OnReadDone(bool)
Definition: server_callback_impl.h:537
virtual void Write(const Response *msg, ::grpc::WriteOptions options)=0
void OnCancel() override
Notifies the application that this RPC has been cancelled.
Definition: server_callback_impl.h:431
Definition: byte_buffer.h:38
virtual bool InternalInlineable()
Definition: server_callback_impl.h:58
void OnDone() override=0
Notifies the application that all operations associated with this RPC have completed.
~ServerReadReactor()=default
virtual void Finish(::grpc::Status s)=0
ServerBidiReactor()
Definition: server_callback_impl.h:270
virtual ~ServerCallbackWriter()
Definition: server_callback_impl.h:223
void MaybeDone()
Definition: server_callback_impl.h:92
DefaultMessageHolder()
Definition: server_callback_impl.h:163
Definition: server_callback_impl.h:221
ServerReadReactor is the interface for a client-streaming RPC.
Definition: server_callback_impl.h:182
void StartWrite(const Response *resp, ::grpc::WriteOptions options)
Definition: server_callback_impl.h:598
void Finish(::grpc::Status s)
Definition: server_callback_impl.h:632
virtual void SendInitialMetadata()=0
ServerWriteReactor()
Definition: server_callback_impl.h:578
void OnCancel() override
Definition: server_callback_impl.h:651
virtual void Finish(::grpc::Status s)=0
Did it work? If it didn't, why?
Definition: status.h:31
Definition: server_callback_impl.h:160
virtual void WriteAndFinish(const Response *msg, ::grpc::WriteOptions options, ::grpc::Status s)=0
virtual void Finish(::grpc::Status s)=0
void Release() override
Definition: server_callback_impl.h:167
void StartSendInitialMetadata()
The following operation initiations are exactly like ServerBidiReactor.
Definition: server_callback_impl.h:582
void MaybeCallOnCancel(ServerReactor *reactor)
Definition: server_callback_impl.h:106
virtual void WriteAndFinish(const Response *msg, ::grpc::WriteOptions options, ::grpc::Status s)=0
void BindReactor(ServerReadReactor< Request > *reactor)
Definition: server_callback_impl.h:215
void StartRead(Request *req)
Definition: server_callback_impl.h:507
Definition: server_callback_handlers.h:664
void StartWrite(const Response *resp)
Definition: server_callback_impl.h:595
void BindReactor(ServerWriteReactor< Response > *reactor)
Definition: server_callback_impl.h:232
virtual void OnSendInitialMetadataDone(bool)
The following notifications are exactly like ServerBidiReactor.
Definition: server_callback_impl.h:648
ServerReadReactor()
Definition: server_callback_impl.h:490
void set_response(Response *response)
Definition: message_allocator.h:53
virtual void OnWriteDone(bool)
Definition: server_callback_impl.h:649
~ServerWriteReactor()=default
::grpc_impl::ServerReadReactor< Request > ServerReadReactor
Definition: server_callback.h:43
Definition: server_callback_handlers.h:255
virtual void SendInitialMetadata()=0
void StartWriteAndFinish(const Response *resp, ::grpc::WriteOptions options, ::grpc::Status s)
Definition: server_callback_impl.h:612
virtual ~ServerCallbackUnary()
Definition: server_callback_impl.h:193
void MaybeDone(bool inline_ondone)
Definition: server_callback_impl.h:98
void StartSendInitialMetadata()
StartSendInitialMetadata is exactly like ServerBidiReactor.
Definition: server_callback_impl.h:699
::grpc_impl::ServerUnaryReactor ServerUnaryReactor
Definition: server_callback.h:51
::grpc_impl::ServerBidiReactor< Request, Response > ServerBidiReactor
Definition: server_callback.h:49
Definition: server_callback_impl.h:238
The base class of ServerCallbackUnary etc.
Definition: server_callback_impl.h:72
Definition: server_callback_impl.h:48
ServerWriteReactor is the interface for a server-streaming RPC.
Definition: server_callback_impl.h:184
Per-message write options.
Definition: call_op_set.h:79
virtual ~ServerCallbackReader()
Definition: server_callback_impl.h:209
void StartWrite(const Response *resp, ::grpc::WriteOptions options)
Initiate a write operation with specified options.
Definition: server_callback_impl.h:323
Definition: server_callback_impl.h:191
void Finish(::grpc::Status s)
Definition: server_callback_impl.h:520
::grpc_impl::ServerWriteReactor< Response > ServerWriteReactor
Definition: server_callback.h:46
void StartSendInitialMetadata()
The following operation initiations are exactly like ServerBidiReactor.
Definition: server_callback_impl.h:494
void MaybeCallOnCancel()
Definition: server_callback_impl.h:116
virtual void OnSendInitialMetadataDone(bool)
Notifies the application that an explicit StartSendInitialMetadata operation completed.
Definition: server_callback_impl.h:408
~ServerBidiReactor()=default
~ServerUnaryReactor()=default
void OnDone() override
Definition: server_callback_impl.h:767
void Ref()
Increases the reference count.
Definition: server_callback_impl.h:124
void OnCancel() override
Definition: server_callback_impl.h:731
virtual void Write(const Response *msg, ::grpc::WriteOptions options)=0
virtual void SendInitialMetadata()=0
void StartWriteLast(const Response *resp, ::grpc::WriteOptions options)
Inform system of a planned write operation with specified options, but allow the library to schedule ...
Definition: server_callback_impl.h:377
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
void StartWriteLast(const Response *resp, ::grpc::WriteOptions options)
Definition: server_callback_impl.h:629
virtual void OnCancel()=0
Definition: server_callback_impl.h:207
virtual void Finish(::grpc::Status s)=0
void StartWrite(const Response *resp)
Initiate a write operation.
Definition: server_callback_impl.h:313
Definition: byte_buffer.h:36
virtual void OnSendInitialMetadataDone(bool)
The following notifications are exactly like ServerBidiReactor.
Definition: server_callback_impl.h:729
void BindReactor(ServerBidiReactor< Request, Response > *reactor)
Definition: server_callback_impl.h:250
virtual void SendInitialMetadata()=0
virtual void OnSendInitialMetadataDone(bool)
The following notifications are exactly like ServerBidiReactor.
Definition: server_callback_impl.h:536
void StartRead(Request *req)
Initiate a read operation.
Definition: server_callback_impl.h:294
virtual ~ServerReactor()=default
Definition: message_allocator.h:42
virtual void OnReadDone(bool)
Notifies the application that a StartRead operation completed.
Definition: server_callback_impl.h:414
virtual ~ServerCallbackCall()
Definition: server_callback_impl.h:74
FinishOnlyReactor(::grpc::Status s)
Definition: server_callback_impl.h:766
Definition: server_callback_impl.h:764
void Finish(::grpc::Status s)
Indicate that the stream is to be finished and the trailing metadata and RPC status are to be sent.
Definition: server_callback_impl.h:387
void Finish(::grpc::Status s)
Finish is similar to ServerBidiReactor except for one detail.
Definition: server_callback_impl.h:714