Debian Codegraph @

src/core/lib/surface/call.h

raw
1 /*
2 *
3 * Copyright 2015 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 GRPC_CORE_LIB_SURFACE_CALL_H
20 #define GRPC_CORE_LIB_SURFACE_CALL_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <stddef.h>
25 #include <stdint.h>
26
27 #include "absl/functional/any_invocable.h"
28 #include "absl/strings/string_view.h"
29 #include "absl/types/optional.h"
30
31 #include <grpc/impl/codegen/compression_types.h>
32 #include <grpc/impl/codegen/grpc_types.h>
33 #include <grpc/support/atm.h>
34 #include <grpc/support/log.h>
35
36 #include "src/core/lib/channel/channel_fwd.h"
37 #include "src/core/lib/channel/channel_stack.h"
38 #include "src/core/lib/channel/context.h"
39 #include "src/core/lib/debug/trace.h"
40 #include "src/core/lib/gprpp/ref_counted_ptr.h"
41 #include "src/core/lib/gprpp/time.h"
42 #include "src/core/lib/iomgr/closure.h"
43 #include "src/core/lib/iomgr/error.h"
44 #include "src/core/lib/iomgr/iomgr_fwd.h"
45 #include "src/core/lib/iomgr/polling_entity.h"
46 #include "src/core/lib/promise/context.h"
47 #include "src/core/lib/resource_quota/arena.h"
48 #include "src/core/lib/slice/slice.h"
49 #include "src/core/lib/surface/api_trace.h"
50 #include "src/core/lib/surface/channel.h"
51 #include "src/core/lib/surface/server.h"
52
53 typedef void (*grpc_ioreq_completion_func)(grpc_call* call, int success,
54 void* user_data);
55
56 typedef struct grpc_call_create_args {
57 grpc_core::RefCountedPtr<grpc_core::Channel> channel;
58 grpc_core::Server* server;
59
60 grpc_call* parent;
61 uint32_t propagation_mask;
62
63 grpc_completion_queue* cq;
64 /* if not NULL, it'll be used in lieu of cq */
65 grpc_pollset_set* pollset_set_alternative;
66
67 const void* server_transport_data;
68
69 absl::optional<grpc_core::Slice> path;
70 absl::optional<grpc_core::Slice> authority;
71
72 grpc_core::Timestamp send_deadline;
73 } grpc_call_create_args;
74
75 namespace grpc_core {
76 class PromiseBasedCall;
77
78 // TODO(ctiller): move more call things into this type
79 class CallContext {
80 public:
81 explicit CallContext(PromiseBasedCall* call) : call_(call) {}
82
83 // Run some action in the call activity context. This is needed to adapt some
84 // legacy systems to promises, and will likely disappear once that conversion
85 // is complete.
86 void RunInContext(absl::AnyInvocable<void()> fn);
87
88 // TODO(ctiller): remove this once transport APIs are promise based
89 void IncrementRefCount(const char* reason = "call_context");
90
91 // TODO(ctiller): remove this once transport APIs are promise based
92 void Unref(const char* reason = "call_context");
93
94 grpc_call_stats* call_stats() { return &call_stats_; }
95 gpr_atm* peer_string_atm_ptr();
96 grpc_polling_entity* polling_entity() { return &pollent_; }
97
98 private:
99 friend class PromiseBasedCall;
100 // Call final info.
101 grpc_call_stats call_stats_;
102 // Pollset stuff, can't wait to remove.
103 // TODO(ctiller): bring forth EventEngine.
104 grpc_polling_entity pollent_;
105 // TODO(ctiller): remove this once transport APIs are promise based and we
106 // don't need refcounting here.
107 PromiseBasedCall* const call_;
108 };
109
110 template <>
111 struct ContextType<CallContext> {};
112 } // namespace grpc_core
113
114 /* Create a new call based on \a args.
115 Regardless of success or failure, always returns a valid new call into *call
116 */
117 grpc_error_handle grpc_call_create(grpc_call_create_args* args,
118 grpc_call** call);
119
120 void grpc_call_set_completion_queue(grpc_call* call, grpc_completion_queue* cq);
121
122 grpc_core::Arena* grpc_call_get_arena(grpc_call* call);
123
124 grpc_call_stack* grpc_call_get_call_stack(grpc_call* call);
125
126 grpc_call_error grpc_call_start_batch_and_execute(grpc_call* call,
127 const grpc_op* ops,
128 size_t nops,
129 grpc_closure* closure);
130
131 /* gRPC core internal version of grpc_call_cancel that does not create
132 * exec_ctx. */
133 void grpc_call_cancel_internal(grpc_call* call);
134
135 /* Given the top call_element, get the call object. */
136 grpc_call* grpc_call_from_top_element(grpc_call_element* surface_element);
137
138 void grpc_call_log_batch(const char* file, int line, gpr_log_severity severity,
139 const grpc_op* ops, size_t nops);
140
141 /* Set a context pointer.
142 No thread safety guarantees are made wrt this value. */
143 /* TODO(#9731): add exec_ctx to destroy */
144 void grpc_call_context_set(grpc_call* call, grpc_context_index elem,
145 void* value, void (*destroy)(void* value));
146 /* Get a context pointer. */
147 void* grpc_call_context_get(grpc_call* call, grpc_context_index elem);
148
149 #define GRPC_CALL_LOG_BATCH(sev, ops, nops) \
150 do { \
151 if (GRPC_TRACE_FLAG_ENABLED(grpc_api_trace)) { \
152 grpc_call_log_batch(sev, ops, nops); \
153 } \
154 } while (0)
155
156 uint8_t grpc_call_is_client(grpc_call* call);
157
158 /* Get the estimated memory size for a call BESIDES the call stack. Combined
159 * with the size of the call stack, it helps estimate the arena size for the
160 * initial call. */
161 size_t grpc_call_get_initial_size_estimate();
162
163 /* Return an appropriate compression algorithm for the requested compression \a
164 * level in the context of \a call. */
165 grpc_compression_algorithm grpc_call_compression_for_level(
166 grpc_call* call, grpc_compression_level level);
167
168 /* Did this client call receive a trailers-only response */
169 /* TODO(markdroth): This is currently available only to the C++ API.
170 Move to surface API if requested by other languages. */
171 bool grpc_call_is_trailers_only(const grpc_call* call);
172
173 // Returns the authority for the call, as seen on the server side.
174 absl::string_view grpc_call_server_authority(const grpc_call* call);
175
176 extern grpc_core::TraceFlag grpc_call_error_trace;
177 extern grpc_core::TraceFlag grpc_compression_trace;
178
179 #endif /* GRPC_CORE_LIB_SURFACE_CALL_H */