• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpcpp/impl/codegen/server_callback.h>
19 
20 #include "src/core/lib/iomgr/closure.h"
21 #include "src/core/lib/iomgr/exec_ctx.h"
22 #include "src/core/lib/iomgr/executor.h"
23 
24 namespace grpc {
25 namespace internal {
26 
ScheduleOnDone(bool inline_ondone)27 void ServerCallbackCall::ScheduleOnDone(bool inline_ondone) {
28   if (inline_ondone) {
29     CallOnDone();
30   } else {
31     // Unlike other uses of closure, do not Ref or Unref here since at this
32     // point, all the Ref'fing and Unref'fing is done for this call.
33     grpc_core::ExecCtx exec_ctx;
34     struct ClosureWithArg {
35       grpc_closure closure;
36       ServerCallbackCall* call;
37       explicit ClosureWithArg(ServerCallbackCall* call_arg) : call(call_arg) {
38         GRPC_CLOSURE_INIT(
39             &closure,
40             [](void* void_arg, grpc_error_handle) {
41               ClosureWithArg* arg = static_cast<ClosureWithArg*>(void_arg);
42               arg->call->CallOnDone();
43               delete arg;
44             },
45             this, grpc_schedule_on_exec_ctx);
46       }
47     };
48     ClosureWithArg* arg = new ClosureWithArg(this);
49     grpc_core::Executor::Run(&arg->closure, GRPC_ERROR_NONE);
50   }
51 }
52 
CallOnCancel(ServerReactor * reactor)53 void ServerCallbackCall::CallOnCancel(ServerReactor* reactor) {
54   if (reactor->InternalInlineable()) {
55     reactor->OnCancel();
56   } else {
57     // Ref to make sure that the closure executes before the whole call gets
58     // destructed, and Unref within the closure.
59     Ref();
60     grpc_core::ExecCtx exec_ctx;
61     struct ClosureWithArg {
62       grpc_closure closure;
63       ServerCallbackCall* call;
64       ServerReactor* reactor;
65       ClosureWithArg(ServerCallbackCall* call_arg, ServerReactor* reactor_arg)
66           : call(call_arg), reactor(reactor_arg) {
67         GRPC_CLOSURE_INIT(
68             &closure,
69             [](void* void_arg, grpc_error_handle) {
70               ClosureWithArg* arg = static_cast<ClosureWithArg*>(void_arg);
71               arg->reactor->OnCancel();
72               arg->call->MaybeDone();
73               delete arg;
74             },
75             this, grpc_schedule_on_exec_ctx);
76       }
77     };
78     ClosureWithArg* arg = new ClosureWithArg(this, reactor);
79     grpc_core::Executor::Run(&arg->closure, GRPC_ERROR_NONE);
80   }
81 }
82 
83 }  // namespace internal
84 }  // namespace grpc
85