1 //
2 // Copyright 2019 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 //
17
18 #include "absl/status/status.h"
19
20 #include <grpcpp/support/server_callback.h>
21
22 #include "src/core/lib/iomgr/closure.h"
23 #include "src/core/lib/iomgr/error.h"
24 #include "src/core/lib/iomgr/exec_ctx.h"
25 #include "src/core/lib/iomgr/executor.h"
26
27 namespace grpc {
28 namespace internal {
29
ScheduleOnDone(bool inline_ondone)30 void ServerCallbackCall::ScheduleOnDone(bool inline_ondone) {
31 if (inline_ondone) {
32 CallOnDone();
33 } else {
34 // Unlike other uses of closure, do not Ref or Unref here since at this
35 // point, all the Ref'fing and Unref'fing is done for this call.
36 grpc_core::ExecCtx exec_ctx;
37 struct ClosureWithArg {
38 grpc_closure closure;
39 ServerCallbackCall* call;
40 explicit ClosureWithArg(ServerCallbackCall* call_arg) : call(call_arg) {
41 GRPC_CLOSURE_INIT(
42 &closure,
43 [](void* void_arg, grpc_error_handle) {
44 ClosureWithArg* arg = static_cast<ClosureWithArg*>(void_arg);
45 arg->call->CallOnDone();
46 delete arg;
47 },
48 this, grpc_schedule_on_exec_ctx);
49 }
50 };
51 ClosureWithArg* arg = new ClosureWithArg(this);
52 grpc_core::Executor::Run(&arg->closure, absl::OkStatus());
53 }
54 }
55
CallOnCancel(ServerReactor * reactor)56 void ServerCallbackCall::CallOnCancel(ServerReactor* reactor) {
57 if (reactor->InternalInlineable()) {
58 reactor->OnCancel();
59 } else {
60 // Ref to make sure that the closure executes before the whole call gets
61 // destructed, and Unref within the closure.
62 Ref();
63 grpc_core::ExecCtx exec_ctx;
64 struct ClosureWithArg {
65 grpc_closure closure;
66 ServerCallbackCall* call;
67 ServerReactor* reactor;
68 ClosureWithArg(ServerCallbackCall* call_arg, ServerReactor* reactor_arg)
69 : call(call_arg), reactor(reactor_arg) {
70 GRPC_CLOSURE_INIT(
71 &closure,
72 [](void* void_arg, grpc_error_handle) {
73 ClosureWithArg* arg = static_cast<ClosureWithArg*>(void_arg);
74 arg->reactor->OnCancel();
75 arg->call->MaybeDone();
76 delete arg;
77 },
78 this, grpc_schedule_on_exec_ctx);
79 }
80 };
81 ClosureWithArg* arg = new ClosureWithArg(this, reactor);
82 grpc_core::Executor::Run(&arg->closure, absl::OkStatus());
83 }
84 }
85
86 } // namespace internal
87 } // namespace grpc
88