blob: 70c94791f8cc69d2eff3e44cc68c49ce0e7d2a51 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070033#include <assert.h>
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080034#include <limits.h>
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070035#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38
39#include <grpc/compression.h>
murgatroid99c3910ca2016-01-06 13:14:23 -080040#include <grpc/grpc.h>
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070041#include <grpc/support/alloc.h>
42#include <grpc/support/log.h>
David Garcia Quintas73dcbda2016-04-23 00:17:05 -070043#include <grpc/support/slice.h>
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070044#include <grpc/support/string_util.h>
David Garcia Quintase091af82015-07-15 21:37:02 -070045#include <grpc/support/useful.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046
Craig Tiller9533d042016-03-25 17:11:06 -070047#include "src/core/lib/channel/channel_stack.h"
48#include "src/core/lib/compression/algorithm_metadata.h"
49#include "src/core/lib/iomgr/timer.h"
50#include "src/core/lib/profiling/timers.h"
51#include "src/core/lib/support/string.h"
52#include "src/core/lib/surface/api_trace.h"
53#include "src/core/lib/surface/call.h"
54#include "src/core/lib/surface/channel.h"
55#include "src/core/lib/surface/completion_queue.h"
David Garcia Quintas73dcbda2016-04-23 00:17:05 -070056#include "src/core/lib/transport/metadata.h"
Craig Tiller9533d042016-03-25 17:11:06 -070057#include "src/core/lib/transport/static_metadata.h"
David Garcia Quintas73dcbda2016-04-23 00:17:05 -070058#include "src/core/lib/transport/transport.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080060/** The maximum number of concurrent batches possible.
Craig Tiller1b011672015-07-10 10:41:44 -070061 Based upon the maximum number of individually queueable ops in the batch
62 api:
63 - initial metadata send
64 - message send
65 - status/close send (depending on client/server)
66 - initial metadata recv
67 - message recv
68 - status/close recv (depending on client/server) */
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080069#define MAX_CONCURRENT_BATCHES 6
Craig Tiller1b011672015-07-10 10:41:44 -070070
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080071#define MAX_SEND_EXTRA_METADATA_COUNT 3
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072
Craig Tillerdaceea82015-02-02 16:15:53 -080073/* Status data for a request can come from several sources; this
74 enumerates them all, and acts as a priority sorting for which
75 status to return to the application - earlier entries override
76 later ones */
Craig Tillera82950e2015-09-22 12:33:20 -070077typedef enum {
Craig Tillerdaceea82015-02-02 16:15:53 -080078 /* Status came from the application layer overriding whatever
79 the wire says */
Craig Tiller68752722015-01-29 14:59:54 -080080 STATUS_FROM_API_OVERRIDE = 0,
Craig Tillerdaceea82015-02-02 16:15:53 -080081 /* Status came from 'the wire' - or somewhere below the surface
82 layer */
Craig Tiller68752722015-01-29 14:59:54 -080083 STATUS_FROM_WIRE,
Craig Tiller2aa03df2016-03-16 08:24:55 -070084 /* Status was created by some internal channel stack operation */
85 STATUS_FROM_CORE,
Craig Tilleraea081f2015-06-11 14:19:33 -070086 /* Status came from the server sending status */
87 STATUS_FROM_SERVER_STATUS,
Craig Tiller68752722015-01-29 14:59:54 -080088 STATUS_SOURCE_COUNT
89} status_source;
90
Craig Tillera82950e2015-09-22 12:33:20 -070091typedef struct {
Craig Tiller7536af02015-12-22 13:49:30 -080092 uint8_t is_set;
Craig Tiller68752722015-01-29 14:59:54 -080093 grpc_status_code code;
94 grpc_mdstr *details;
95} received_status;
96
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080097typedef struct batch_control {
98 grpc_call *call;
99 grpc_cq_completion cq_completion;
100 grpc_closure finish_batch;
101 void *notify_tag;
102 gpr_refcount steps_to_complete;
Craig Tillerc027e772016-05-03 16:27:00 -0700103 grpc_error *error;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800104
Craig Tiller7536af02015-12-22 13:49:30 -0800105 uint8_t send_initial_metadata;
106 uint8_t send_message;
107 uint8_t send_final_op;
108 uint8_t recv_initial_metadata;
109 uint8_t recv_message;
110 uint8_t recv_final_op;
111 uint8_t is_notify_tag_closure;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800112} batch_control;
113
Craig Tillera82950e2015-09-22 12:33:20 -0700114struct grpc_call {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800115 grpc_completion_queue *cq;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700116 grpc_polling_entity pollent;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117 grpc_channel *channel;
Craig Tiller3e7c6a72015-07-31 16:17:04 -0700118 grpc_call *parent;
Craig Tillerc7df0df2015-08-03 08:06:50 -0700119 grpc_call *first_child;
Craig Tillercce17ac2015-01-20 09:29:28 -0800120 /* TODO(ctiller): share with cq if possible? */
121 gpr_mu mu;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800122
Craig Tillere5d683c2015-02-03 16:37:36 -0800123 /* client or server call */
Craig Tiller1cbf5762016-04-22 16:02:55 -0700124 bool is_client;
Craig Tillere5d683c2015-02-03 16:37:36 -0800125 /* is the alarm set */
Craig Tiller1cbf5762016-04-22 16:02:55 -0700126 bool have_alarm;
Craig Tillerf3fba742015-06-11 09:36:33 -0700127 /** has grpc_call_destroy been called */
Craig Tiller1cbf5762016-04-22 16:02:55 -0700128 bool destroy_called;
Craig Tillerc7df0df2015-08-03 08:06:50 -0700129 /** flag indicating that cancellation is inherited */
Craig Tiller1cbf5762016-04-22 16:02:55 -0700130 bool cancellation_is_inherited;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800131 /** bitmask of live batches */
Craig Tiller7536af02015-12-22 13:49:30 -0800132 uint8_t used_batches;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800133 /** which ops are in-flight */
Craig Tiller1cbf5762016-04-22 16:02:55 -0700134 bool sent_initial_metadata;
135 bool sending_message;
136 bool sent_final_op;
137 bool received_initial_metadata;
138 bool receiving_message;
139 bool requested_final_op;
140 bool received_final_op;
yang-g0b6ad7d2015-06-25 14:39:01 -0700141
Craig Tillera44cbfc2016-02-03 16:02:49 -0800142 /* have we received initial metadata */
143 bool has_initial_md_been_received;
144
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800145 batch_control active_batches[MAX_CONCURRENT_BATCHES];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800147 /* first idx: is_receiving, second idx: is_trailing */
148 grpc_metadata_batch metadata_batch[2][2];
Craig Tillerebf94bf2015-02-05 08:48:46 -0800149
Craig Tillere5d683c2015-02-03 16:37:36 -0800150 /* Buffered read metadata waiting to be returned to the application.
151 Element 0 is initial metadata, element 1 is trailing metadata. */
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800152 grpc_metadata_array *buffered_metadata[2];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800153
Craig Tillere5d683c2015-02-03 16:37:36 -0800154 /* Received call statuses from various sources */
Craig Tiller68752722015-01-29 14:59:54 -0800155 received_status status[STATUS_SOURCE_COUNT];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800156
David Garcia Quintas01c4d992016-07-07 20:11:27 -0700157 /* Call data useful used for reporting. Only valid after the call has
158 * completed */
159 grpc_call_final_info final_info;
Craig Tiller466129e2016-03-09 14:43:18 -0800160
David Garcia Quintas749367f2016-05-17 19:15:24 -0700161 /* Compression algorithm for *incoming* data */
162 grpc_compression_algorithm incoming_compression_algorithm;
David Garcia Quintase091af82015-07-15 21:37:02 -0700163 /* Supported encodings (compression algorithms), a bitset */
Craig Tiller7536af02015-12-22 13:49:30 -0800164 uint32_t encodings_accepted_by_peer;
David Garcia Quintasb8edf7e2015-07-08 20:18:57 -0700165
Julien Boeufc6f8d0a2015-05-11 22:40:02 -0700166 /* Contexts for various subsystems (security, tracing, ...). */
Julien Boeuf83b02972015-05-20 22:50:34 -0700167 grpc_call_context_element context[GRPC_CONTEXT_COUNT];
Craig Tiller935cf422015-05-01 14:10:46 -0700168
Craig Tillere5d683c2015-02-03 16:37:36 -0800169 /* Deadline alarm - if have_alarm is non-zero */
David Garcia Quintasf747bbc2015-10-04 23:09:47 -0700170 grpc_timer alarm;
Craig Tillercce17ac2015-01-20 09:29:28 -0800171
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800172 /* for the client, extra metadata is initial metadata; for the
173 server, it's trailing metadata */
174 grpc_linked_mdelem send_extra_metadata[MAX_SEND_EXTRA_METADATA_COUNT];
175 int send_extra_metadata_count;
Craig Tiller6902ad22015-04-16 08:01:49 -0700176 gpr_timespec send_deadline;
177
Craig Tillerd6c98df2015-08-18 09:33:44 -0700178 /** siblings: children of the same parent form a list, and this list is
179 protected under
Craig Tillerc7df0df2015-08-03 08:06:50 -0700180 parent->mu */
181 grpc_call *sibling_next;
182 grpc_call *sibling_prev;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800183
184 grpc_slice_buffer_stream sending_stream;
185 grpc_byte_stream *receiving_stream;
186 grpc_byte_buffer **receiving_buffer;
187 gpr_slice receiving_slice;
188 grpc_closure receiving_slice_ready;
189 grpc_closure receiving_stream_ready;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800190 grpc_closure receiving_initial_metadata_ready;
Craig Tiller7536af02015-12-22 13:49:30 -0800191 uint32_t test_only_last_message_flags;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800192
193 union {
194 struct {
195 grpc_status_code *status;
196 char **status_details;
197 size_t *status_details_capacity;
198 } client;
199 struct {
200 int *cancelled;
201 } server;
202 } final_op;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800203
Craig Tiller8a677802016-04-22 15:07:53 -0700204 void *saved_receiving_stream_ready_bctlp;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800205};
206
Craig Tiller87d5b192015-04-16 14:37:57 -0700207#define CALL_STACK_FROM_CALL(call) ((grpc_call_stack *)((call) + 1))
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800208#define CALL_FROM_CALL_STACK(call_stack) (((grpc_call *)(call_stack)) - 1)
209#define CALL_ELEM_FROM_CALL(call, idx) \
210 grpc_call_stack_element(CALL_STACK_FROM_CALL(call), idx)
211#define CALL_FROM_TOP_ELEM(top_elem) \
212 CALL_FROM_CALL_STACK(grpc_call_stack_from_top_element(top_elem))
213
Craig Tillera82950e2015-09-22 12:33:20 -0700214static void set_deadline_alarm(grpc_exec_ctx *exec_ctx, grpc_call *call,
215 gpr_timespec deadline);
Craig Tillera82950e2015-09-22 12:33:20 -0700216static void execute_op(grpc_exec_ctx *exec_ctx, grpc_call *call,
217 grpc_transport_stream_op *op);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800218static grpc_call_error cancel_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c,
219 grpc_status_code status,
Craig Tillera82950e2015-09-22 12:33:20 -0700220 const char *description);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700221static grpc_call_error close_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c,
222 grpc_status_code status,
223 const char *description);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800224static void destroy_call(grpc_exec_ctx *exec_ctx, void *call_stack,
Craig Tillerc027e772016-05-03 16:27:00 -0700225 grpc_error *error);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800226static void receiving_slice_ready(grpc_exec_ctx *exec_ctx, void *bctlp,
Craig Tillerc027e772016-05-03 16:27:00 -0700227 grpc_error *error);
Craig Tillerbac41422015-05-29 16:32:28 -0700228
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700229grpc_call *grpc_call_create(
230 grpc_channel *channel, grpc_call *parent_call, uint32_t propagation_mask,
David Garcia Quintas879b3b92016-04-25 11:23:38 -0700231 grpc_completion_queue *cq, grpc_pollset_set *pollset_set_alternative,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700232 const void *server_transport_data, grpc_mdelem **add_initial_metadata,
233 size_t add_initial_metadata_count, gpr_timespec send_deadline) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800234 size_t i, j;
Craig Tillera82950e2015-09-22 12:33:20 -0700235 grpc_channel_stack *channel_stack = grpc_channel_get_channel_stack(channel);
Craig Tillerf5768a62015-09-22 10:54:34 -0700236 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller1f41b6b2015-10-09 15:07:02 -0700237 grpc_call *call;
Craig Tiller0ba432d2015-10-09 16:57:11 -0700238 GPR_TIMER_BEGIN("grpc_call_create", 0);
Craig Tiller1f41b6b2015-10-09 15:07:02 -0700239 call = gpr_malloc(sizeof(grpc_call) + channel_stack->call_stack_size);
Craig Tillera82950e2015-09-22 12:33:20 -0700240 memset(call, 0, sizeof(grpc_call));
241 gpr_mu_init(&call->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800242 call->channel = channel;
Craig Tillerfb189f82015-02-03 12:07:07 -0800243 call->cq = cq;
Craig Tiller3e7c6a72015-07-31 16:17:04 -0700244 call->parent = parent_call;
David Garcia Quintas46123372016-05-09 15:28:42 -0700245 /* Always support no compression */
246 GPR_BITSET(&call->encodings_accepted_by_peer, GRPC_COMPRESS_NONE);
Craig Tillercce17ac2015-01-20 09:29:28 -0800247 call->is_client = server_transport_data == NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700248 if (call->is_client) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800249 GPR_ASSERT(add_initial_metadata_count < MAX_SEND_EXTRA_METADATA_COUNT);
250 for (i = 0; i < add_initial_metadata_count; i++) {
251 call->send_extra_metadata[i].md = add_initial_metadata[i];
252 }
253 call->send_extra_metadata_count = (int)add_initial_metadata_count;
254 } else {
255 GPR_ASSERT(add_initial_metadata_count == 0);
256 call->send_extra_metadata_count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700257 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800258 for (i = 0; i < 2; i++) {
259 for (j = 0; j < 2; j++) {
260 call->metadata_batch[i][j].deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
261 }
Craig Tillera82950e2015-09-22 12:33:20 -0700262 }
Craig Tiller33ab1822016-07-08 15:19:06 -0700263 call->send_deadline =
264 gpr_convert_clock_type(send_deadline, GPR_CLOCK_MONOTONIC);
Craig Tillera82950e2015-09-22 12:33:20 -0700265 GRPC_CHANNEL_INTERNAL_REF(channel, "call");
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800266 /* initial refcount dropped by grpc_call_destroy */
267 grpc_call_stack_init(&exec_ctx, channel_stack, 1, destroy_call, call,
268 call->context, server_transport_data,
269 CALL_STACK_FROM_CALL(call));
270 if (cq != NULL) {
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700271 GPR_ASSERT(
272 pollset_set_alternative == NULL &&
273 "Only one of 'cq' and 'pollset_set_alternative' should be non-NULL.");
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800274 GRPC_CQ_INTERNAL_REF(cq, "bind");
David Garcia Quintas69ff63d2016-06-06 16:39:47 -0700275 call->pollent =
276 grpc_polling_entity_create_from_pollset(grpc_cq_pollset(cq));
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700277 }
David Garcia Quintas879b3b92016-04-25 11:23:38 -0700278 if (pollset_set_alternative != NULL) {
David Garcia Quintas69ff63d2016-06-06 16:39:47 -0700279 call->pollent =
280 grpc_polling_entity_create_from_pollset_set(pollset_set_alternative);
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700281 }
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700282 if (!grpc_polling_entity_is_empty(&call->pollent)) {
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700283 grpc_call_stack_set_pollset_or_pollset_set(
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700284 &exec_ctx, CALL_STACK_FROM_CALL(call), &call->pollent);
Craig Tillera82950e2015-09-22 12:33:20 -0700285 }
Craig Tillera82950e2015-09-22 12:33:20 -0700286 if (parent_call != NULL) {
287 GRPC_CALL_INTERNAL_REF(parent_call, "child");
288 GPR_ASSERT(call->is_client);
289 GPR_ASSERT(!parent_call->is_client);
290
291 gpr_mu_lock(&parent_call->mu);
292
293 if (propagation_mask & GRPC_PROPAGATE_DEADLINE) {
294 send_deadline = gpr_time_min(
295 gpr_convert_clock_type(send_deadline,
296 parent_call->send_deadline.clock_type),
297 parent_call->send_deadline);
Craig Tillerc7df0df2015-08-03 08:06:50 -0700298 }
Craig Tillera82950e2015-09-22 12:33:20 -0700299 /* for now GRPC_PROPAGATE_TRACING_CONTEXT *MUST* be passed with
300 * GRPC_PROPAGATE_STATS_CONTEXT */
301 /* TODO(ctiller): This should change to use the appropriate census start_op
302 * call. */
303 if (propagation_mask & GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT) {
304 GPR_ASSERT(propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT);
305 grpc_call_context_set(call, GRPC_CONTEXT_TRACING,
306 parent_call->context[GRPC_CONTEXT_TRACING].value,
307 NULL);
308 } else {
309 GPR_ASSERT(propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT);
Craig Tiller45724b32015-09-22 10:42:19 -0700310 }
Craig Tillera82950e2015-09-22 12:33:20 -0700311 if (propagation_mask & GRPC_PROPAGATE_CANCELLATION) {
312 call->cancellation_is_inherited = 1;
Craig Tiller45724b32015-09-22 10:42:19 -0700313 }
Craig Tillera82950e2015-09-22 12:33:20 -0700314
315 if (parent_call->first_child == NULL) {
316 parent_call->first_child = call;
317 call->sibling_next = call->sibling_prev = call;
318 } else {
319 call->sibling_next = parent_call->first_child;
320 call->sibling_prev = parent_call->first_child->sibling_prev;
321 call->sibling_next->sibling_prev = call->sibling_prev->sibling_next =
322 call;
323 }
324
325 gpr_mu_unlock(&parent_call->mu);
326 }
327 if (gpr_time_cmp(send_deadline, gpr_inf_future(send_deadline.clock_type)) !=
328 0) {
329 set_deadline_alarm(&exec_ctx, call, send_deadline);
330 }
331 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller0ba432d2015-10-09 16:57:11 -0700332 GPR_TIMER_END("grpc_call_create", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800333 return call;
334}
335
Craig Tillera82950e2015-09-22 12:33:20 -0700336void grpc_call_set_completion_queue(grpc_exec_ctx *exec_ctx, grpc_call *call,
337 grpc_completion_queue *cq) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800338 GPR_ASSERT(cq);
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700339
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700340 if (grpc_polling_entity_pollset_set(&call->pollent) != NULL) {
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700341 gpr_log(GPR_ERROR, "A pollset_set is already registered for this call.");
342 abort();
343 }
Craig Tiller166e2502015-02-03 20:14:41 -0800344 call->cq = cq;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800345 GRPC_CQ_INTERNAL_REF(cq, "bind");
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700346 call->pollent = grpc_polling_entity_create_from_pollset(grpc_cq_pollset(cq));
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700347 grpc_call_stack_set_pollset_or_pollset_set(
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700348 exec_ctx, CALL_STACK_FROM_CALL(call), &call->pollent);
Craig Tiller166e2502015-02-03 20:14:41 -0800349}
350
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800351#ifdef GRPC_STREAM_REFCOUNT_DEBUG
Craig Tiller7b435612015-11-24 08:15:05 -0800352#define REF_REASON reason
353#define REF_ARG , const char *reason
Craig Tiller4df412b2015-04-28 07:57:54 -0700354#else
Craig Tiller7b435612015-11-24 08:15:05 -0800355#define REF_REASON ""
356#define REF_ARG
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800357#endif
Craig Tiller7b435612015-11-24 08:15:05 -0800358void grpc_call_internal_ref(grpc_call *c REF_ARG) {
359 GRPC_CALL_STACK_REF(CALL_STACK_FROM_CALL(c), REF_REASON);
360}
361void grpc_call_internal_unref(grpc_exec_ctx *exec_ctx, grpc_call *c REF_ARG) {
362 GRPC_CALL_STACK_UNREF(exec_ctx, CALL_STACK_FROM_CALL(c), REF_REASON);
363}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800364
David Garcia Quintas01c4d992016-07-07 20:11:27 -0700365static void get_final_status(grpc_call *call,
366 void (*set_value)(grpc_status_code code,
367 void *user_data),
368 void *set_value_user_data) {
369 int i;
370 for (i = 0; i < STATUS_SOURCE_COUNT; i++) {
371 if (call->status[i].is_set) {
372 set_value(call->status[i].code, set_value_user_data);
373 return;
374 }
375 }
376 if (call->is_client) {
377 set_value(GRPC_STATUS_UNKNOWN, set_value_user_data);
378 } else {
379 set_value(GRPC_STATUS_OK, set_value_user_data);
380 }
381}
382
383static void set_status_value_directly(grpc_status_code status, void *dest);
Craig Tillerc027e772016-05-03 16:27:00 -0700384static void destroy_call(grpc_exec_ctx *exec_ctx, void *call,
385 grpc_error *error) {
Craig Tiller566316f2015-02-02 15:25:32 -0800386 size_t i;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800387 int ii;
Craig Tilleraef25da2015-01-29 17:19:45 -0800388 grpc_call *c = call;
Craig Tiller0ba432d2015-10-09 16:57:11 -0700389 GPR_TIMER_BEGIN("destroy_call", 0);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800390 for (i = 0; i < 2; i++) {
391 grpc_metadata_batch_destroy(
392 &c->metadata_batch[1 /* is_receiving */][i /* is_initial */]);
393 }
394 if (c->receiving_stream != NULL) {
Craig Tiller3b66ab92015-12-09 19:42:22 -0800395 grpc_byte_stream_destroy(exec_ctx, c->receiving_stream);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800396 }
Craig Tillera82950e2015-09-22 12:33:20 -0700397 gpr_mu_destroy(&c->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700398 for (i = 0; i < STATUS_SOURCE_COUNT; i++) {
399 if (c->status[i].details) {
400 GRPC_MDSTR_UNREF(c->status[i].details);
Craig Tiller68752722015-01-29 14:59:54 -0800401 }
Craig Tillera82950e2015-09-22 12:33:20 -0700402 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800403 for (ii = 0; ii < c->send_extra_metadata_count; ii++) {
404 GRPC_MDELEM_UNREF(c->send_extra_metadata[ii].md);
Craig Tillera82950e2015-09-22 12:33:20 -0700405 }
406 for (i = 0; i < GRPC_CONTEXT_COUNT; i++) {
407 if (c->context[i].destroy) {
408 c->context[i].destroy(c->context[i].value);
Craig Tiller935cf422015-05-01 14:10:46 -0700409 }
Craig Tillera82950e2015-09-22 12:33:20 -0700410 }
Craig Tillera82950e2015-09-22 12:33:20 -0700411 if (c->cq) {
412 GRPC_CQ_INTERNAL_UNREF(c->cq, "bind");
413 }
Craig Tiller9859d8d2016-04-26 21:07:53 -0700414 grpc_channel *channel = c->channel;
David Garcia Quintas01c4d992016-07-07 20:11:27 -0700415
416 get_final_status(call, set_status_value_directly,
417 &c->final_info.final_status);
418
419 grpc_call_stack_destroy(exec_ctx, CALL_STACK_FROM_CALL(c), &c->final_info, c);
Craig Tiller9859d8d2016-04-26 21:07:53 -0700420 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, channel, "call");
Craig Tiller0ba432d2015-10-09 16:57:11 -0700421 GPR_TIMER_END("destroy_call", 0);
Craig Tillera4541102015-01-29 11:46:11 -0800422}
423
Craig Tillera82950e2015-09-22 12:33:20 -0700424static void set_status_code(grpc_call *call, status_source source,
Craig Tiller7536af02015-12-22 13:49:30 -0800425 uint32_t status) {
Craig Tillera82950e2015-09-22 12:33:20 -0700426 if (call->status[source].is_set) return;
Craig Tillerb8d3a312015-06-19 17:27:53 -0700427
Craig Tillerdaceea82015-02-02 16:15:53 -0800428 call->status[source].is_set = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700429 call->status[source].code = (grpc_status_code)status;
Craig Tillerf0f70a82016-06-23 13:55:06 -0700430}
Craig Tiller30547562015-02-05 17:04:51 -0800431
Craig Tillerf0f70a82016-06-23 13:55:06 -0700432static void set_status_details(grpc_call *call, status_source source,
433 grpc_mdstr *status) {
434 if (call->status[source].details != NULL) {
Craig Tillerbe1b9a72016-06-24 13:22:11 -0700435 GRPC_MDSTR_UNREF(status);
436 } else {
437 call->status[source].details = status;
Craig Tillerf0f70a82016-06-23 13:55:06 -0700438 }
Craig Tillerf0f70a82016-06-23 13:55:06 -0700439}
440
Craig Tillerf0f70a82016-06-23 13:55:06 -0700441static void set_status_from_error(grpc_call *call, status_source source,
442 grpc_error *error) {
443 intptr_t status;
444 if (grpc_error_get_int(error, GRPC_ERROR_INT_GRPC_STATUS, &status)) {
445 set_status_code(call, source, (uint32_t)status);
446 } else {
447 set_status_code(call, source, GRPC_STATUS_INTERNAL);
448 }
449 const char *msg = grpc_error_get_str(error, GRPC_ERROR_STR_GRPC_MESSAGE);
450 bool free_msg = false;
451 if (msg == NULL) {
452 free_msg = true;
453 msg = grpc_error_string(error);
454 }
455 set_status_details(call, source, grpc_mdstr_from_string(msg));
456 if (free_msg) grpc_error_free_string(msg);
Craig Tiller68752722015-01-29 14:59:54 -0800457}
458
David Garcia Quintasac094472016-05-18 20:25:57 -0700459static void set_incoming_compression_algorithm(
460 grpc_call *call, grpc_compression_algorithm algo) {
David Garcia Quintas303d3082016-05-05 18:25:34 -0700461 GPR_ASSERT(algo < GRPC_COMPRESS_ALGORITHMS_COUNT);
David Garcia Quintas749367f2016-05-17 19:15:24 -0700462 call->incoming_compression_algorithm = algo;
David Garcia Quintasdb94b272015-06-15 18:37:01 -0700463}
464
David Garcia Quintas0c331882015-10-08 14:51:54 -0700465grpc_compression_algorithm grpc_call_test_only_get_compression_algorithm(
David Garcia Quintas64824be2015-10-06 19:45:36 -0700466 grpc_call *call) {
467 grpc_compression_algorithm algorithm;
468 gpr_mu_lock(&call->mu);
David Garcia Quintas749367f2016-05-17 19:15:24 -0700469 algorithm = call->incoming_compression_algorithm;
David Garcia Quintas64824be2015-10-06 19:45:36 -0700470 gpr_mu_unlock(&call->mu);
471 return algorithm;
David Garcia Quintas7c0d9142015-07-23 04:58:20 -0700472}
473
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700474static grpc_compression_algorithm compression_algorithm_for_level_locked(
475 grpc_call *call, grpc_compression_level level) {
David Garcia Quintasac094472016-05-18 20:25:57 -0700476 return grpc_compression_algorithm_for_level(level,
477 call->encodings_accepted_by_peer);
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700478}
479
Craig Tiller7536af02015-12-22 13:49:30 -0800480uint32_t grpc_call_test_only_get_message_flags(grpc_call *call) {
481 uint32_t flags;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800482 gpr_mu_lock(&call->mu);
483 flags = call->test_only_last_message_flags;
484 gpr_mu_unlock(&call->mu);
485 return flags;
486}
487
Craig Tiller3ff27542015-10-09 15:39:44 -0700488static void destroy_encodings_accepted_by_peer(void *p) { return; }
489
490static void set_encodings_accepted_by_peer(grpc_call *call, grpc_mdelem *mdel) {
David Garcia Quintasb8edf7e2015-07-08 20:18:57 -0700491 size_t i;
492 grpc_compression_algorithm algorithm;
493 gpr_slice_buffer accept_encoding_parts;
Craig Tiller3ff27542015-10-09 15:39:44 -0700494 gpr_slice accept_encoding_slice;
495 void *accepted_user_data;
David Garcia Quintasb8edf7e2015-07-08 20:18:57 -0700496
Craig Tiller3ff27542015-10-09 15:39:44 -0700497 accepted_user_data =
498 grpc_mdelem_get_user_data(mdel, destroy_encodings_accepted_by_peer);
499 if (accepted_user_data != NULL) {
500 call->encodings_accepted_by_peer =
Craig Tiller7536af02015-12-22 13:49:30 -0800501 (uint32_t)(((uintptr_t)accepted_user_data) - 1);
Craig Tiller3ff27542015-10-09 15:39:44 -0700502 return;
503 }
504
505 accept_encoding_slice = mdel->value->slice;
Craig Tillera82950e2015-09-22 12:33:20 -0700506 gpr_slice_buffer_init(&accept_encoding_parts);
507 gpr_slice_split(accept_encoding_slice, ",", &accept_encoding_parts);
David Garcia Quintasb8edf7e2015-07-08 20:18:57 -0700508
David Garcia Quintase091af82015-07-15 21:37:02 -0700509 /* No need to zero call->encodings_accepted_by_peer: grpc_call_create already
510 * zeroes the whole grpc_call */
David Garcia Quintasb1866bd2015-07-08 22:37:01 -0700511 /* Always support no compression */
Craig Tillera82950e2015-09-22 12:33:20 -0700512 GPR_BITSET(&call->encodings_accepted_by_peer, GRPC_COMPRESS_NONE);
513 for (i = 0; i < accept_encoding_parts.count; i++) {
514 const gpr_slice *accept_encoding_entry_slice =
515 &accept_encoding_parts.slices[i];
516 if (grpc_compression_algorithm_parse(
517 (const char *)GPR_SLICE_START_PTR(*accept_encoding_entry_slice),
518 GPR_SLICE_LENGTH(*accept_encoding_entry_slice), &algorithm)) {
519 GPR_BITSET(&call->encodings_accepted_by_peer, algorithm);
520 } else {
521 char *accept_encoding_entry_str =
522 gpr_dump_slice(*accept_encoding_entry_slice, GPR_DUMP_ASCII);
523 gpr_log(GPR_ERROR,
524 "Invalid entry in accept encoding metadata: '%s'. Ignoring.",
525 accept_encoding_entry_str);
526 gpr_free(accept_encoding_entry_str);
David Garcia Quintasb8edf7e2015-07-08 20:18:57 -0700527 }
Craig Tillera82950e2015-09-22 12:33:20 -0700528 }
Craig Tiller3ff27542015-10-09 15:39:44 -0700529
530 gpr_slice_buffer_destroy(&accept_encoding_parts);
531
532 grpc_mdelem_set_user_data(
533 mdel, destroy_encodings_accepted_by_peer,
Craig Tiller7536af02015-12-22 13:49:30 -0800534 (void *)(((uintptr_t)call->encodings_accepted_by_peer) + 1));
David Garcia Quintasb8edf7e2015-07-08 20:18:57 -0700535}
536
Craig Tiller7536af02015-12-22 13:49:30 -0800537uint32_t grpc_call_test_only_get_encodings_accepted_by_peer(grpc_call *call) {
538 uint32_t encodings_accepted_by_peer;
David Garcia Quintas0c331882015-10-08 14:51:54 -0700539 gpr_mu_lock(&call->mu);
540 encodings_accepted_by_peer = call->encodings_accepted_by_peer;
541 gpr_mu_unlock(&call->mu);
542 return encodings_accepted_by_peer;
Craig Tiller68752722015-01-29 14:59:54 -0800543}
544
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800545static void get_final_details(grpc_call *call, char **out_details,
546 size_t *out_details_capacity) {
Craig Tillerfb189f82015-02-03 12:07:07 -0800547 int i;
Craig Tillera82950e2015-09-22 12:33:20 -0700548 for (i = 0; i < STATUS_SOURCE_COUNT; i++) {
549 if (call->status[i].is_set) {
550 if (call->status[i].details) {
551 gpr_slice details = call->status[i].details->slice;
552 size_t len = GPR_SLICE_LENGTH(details);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800553 if (len + 1 > *out_details_capacity) {
554 *out_details_capacity =
555 GPR_MAX(len + 1, *out_details_capacity * 3 / 2);
556 *out_details = gpr_realloc(*out_details, *out_details_capacity);
Craig Tillera82950e2015-09-22 12:33:20 -0700557 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800558 memcpy(*out_details, GPR_SLICE_START_PTR(details), len);
559 (*out_details)[len] = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700560 } else {
561 goto no_details;
562 }
563 return;
Craig Tiller68752722015-01-29 14:59:54 -0800564 }
Craig Tillera82950e2015-09-22 12:33:20 -0700565 }
Craig Tiller1e0d4c42015-01-30 16:17:29 -0800566
567no_details:
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800568 if (0 == *out_details_capacity) {
569 *out_details_capacity = 8;
570 *out_details = gpr_malloc(*out_details_capacity);
Craig Tillera82950e2015-09-22 12:33:20 -0700571 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800572 **out_details = 0;
Craig Tiller68752722015-01-29 14:59:54 -0800573}
574
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800575static grpc_linked_mdelem *linked_from_md(grpc_metadata *md) {
576 return (grpc_linked_mdelem *)&md->internal_data;
Craig Tillerc12fee62015-02-03 11:55:50 -0800577}
578
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700579static grpc_metadata *get_md_elem(grpc_metadata *metadata,
580 grpc_metadata *additional_metadata, int i,
581 int count) {
582 grpc_metadata *res =
583 i < count ? &metadata[i] : &additional_metadata[i - count];
584 GPR_ASSERT(res);
585 return res;
586}
587
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800588static int prepare_application_metadata(grpc_call *call, int count,
589 grpc_metadata *metadata,
590 int is_trailing,
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700591 int prepend_extra_metadata,
592 grpc_metadata *additional_metadata,
593 int additional_metadata_count) {
594 int total_count = count + additional_metadata_count;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800595 int i;
596 grpc_metadata_batch *batch =
597 &call->metadata_batch[0 /* is_receiving */][is_trailing];
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700598 for (i = 0; i < total_count; i++) {
599 const grpc_metadata *md =
600 get_md_elem(metadata, additional_metadata, i, count);
Craig Tillerb42445c2016-04-22 13:11:44 -0700601 grpc_linked_mdelem *l = (grpc_linked_mdelem *)&md->internal_data;
602 GPR_ASSERT(sizeof(grpc_linked_mdelem) == sizeof(md->internal_data));
603 l->md = grpc_mdelem_from_string_and_buffer(
604 md->key, (const uint8_t *)md->value, md->value_length);
605 if (!grpc_header_key_is_legal(grpc_mdstr_as_c_string(l->md->key),
606 GRPC_MDSTR_LENGTH(l->md->key))) {
607 gpr_log(GPR_ERROR, "attempt to send invalid metadata key: %s",
608 grpc_mdstr_as_c_string(l->md->key));
609 break;
610 } else if (!grpc_is_binary_header(grpc_mdstr_as_c_string(l->md->key),
611 GRPC_MDSTR_LENGTH(l->md->key)) &&
612 !grpc_header_nonbin_value_is_legal(
613 grpc_mdstr_as_c_string(l->md->value),
614 GRPC_MDSTR_LENGTH(l->md->value))) {
615 gpr_log(GPR_ERROR, "attempt to send invalid metadata value");
616 break;
617 }
618 }
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700619 if (i != total_count) {
Craig Tillerb42445c2016-04-22 13:11:44 -0700620 for (int j = 0; j <= i; j++) {
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700621 const grpc_metadata *md =
622 get_md_elem(metadata, additional_metadata, j, count);
Craig Tillerb42445c2016-04-22 13:11:44 -0700623 grpc_linked_mdelem *l = (grpc_linked_mdelem *)&md->internal_data;
624 GRPC_MDELEM_UNREF(l->md);
625 }
626 return 0;
627 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800628 if (prepend_extra_metadata) {
629 if (call->send_extra_metadata_count == 0) {
630 prepend_extra_metadata = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700631 } else {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800632 for (i = 0; i < call->send_extra_metadata_count; i++) {
633 GRPC_MDELEM_REF(call->send_extra_metadata[i].md);
634 }
635 for (i = 1; i < call->send_extra_metadata_count; i++) {
636 call->send_extra_metadata[i].prev = &call->send_extra_metadata[i - 1];
637 }
638 for (i = 0; i < call->send_extra_metadata_count - 1; i++) {
639 call->send_extra_metadata[i].next = &call->send_extra_metadata[i + 1];
Craig Tillera82950e2015-09-22 12:33:20 -0700640 }
Craig Tiller629b0ed2015-04-22 11:14:26 -0700641 }
Craig Tillera82950e2015-09-22 12:33:20 -0700642 }
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700643 for (i = 1; i < total_count; i++) {
644 grpc_metadata *md = get_md_elem(metadata, additional_metadata, i, count);
645 grpc_metadata *prev_md =
646 get_md_elem(metadata, additional_metadata, i - 1, count);
647 linked_from_md(md)->prev = linked_from_md(prev_md);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800648 }
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700649 for (i = 0; i < total_count - 1; i++) {
650 grpc_metadata *md = get_md_elem(metadata, additional_metadata, i, count);
651 grpc_metadata *next_md =
652 get_md_elem(metadata, additional_metadata, i + 1, count);
653 linked_from_md(md)->next = linked_from_md(next_md);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800654 }
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700655
656 switch (prepend_extra_metadata * 2 + (total_count != 0)) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800657 case 0:
658 /* no prepend, no metadata => nothing to do */
659 batch->list.head = batch->list.tail = NULL;
660 break;
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700661 case 1: {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800662 /* metadata, but no prepend */
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700663 grpc_metadata *first_md =
664 get_md_elem(metadata, additional_metadata, 0, count);
665 grpc_metadata *last_md =
666 get_md_elem(metadata, additional_metadata, total_count - 1, count);
667 batch->list.head = linked_from_md(first_md);
668 batch->list.tail = linked_from_md(last_md);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800669 batch->list.head->prev = NULL;
670 batch->list.tail->next = NULL;
671 break;
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700672 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800673 case 2:
674 /* prepend, but no md */
675 batch->list.head = &call->send_extra_metadata[0];
676 batch->list.tail =
677 &call->send_extra_metadata[call->send_extra_metadata_count - 1];
678 batch->list.head->prev = NULL;
679 batch->list.tail->next = NULL;
680 break;
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700681 case 3: {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800682 /* prepend AND md */
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700683 grpc_metadata *first_md =
684 get_md_elem(metadata, additional_metadata, 0, count);
685 grpc_metadata *last_md =
686 get_md_elem(metadata, additional_metadata, total_count - 1, count);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800687 batch->list.head = &call->send_extra_metadata[0];
688 call->send_extra_metadata[call->send_extra_metadata_count - 1].next =
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700689 linked_from_md(first_md);
690 linked_from_md(first_md)->prev =
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800691 &call->send_extra_metadata[call->send_extra_metadata_count - 1];
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700692 batch->list.tail = linked_from_md(last_md);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800693 batch->list.head->prev = NULL;
694 batch->list.tail->next = NULL;
695 break;
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700696 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800697 default:
698 GPR_UNREACHABLE_CODE(return 0);
699 }
700
Craig Tillerb96d0012015-05-06 15:33:23 -0700701 return 1;
702}
703
Craig Tillera82950e2015-09-22 12:33:20 -0700704void grpc_call_destroy(grpc_call *c) {
ctillerc6d61c42014-12-15 14:52:08 -0800705 int cancel;
Craig Tiller2e95e4a2015-08-07 10:40:33 -0700706 grpc_call *parent = c->parent;
Craig Tillerf5768a62015-09-22 10:54:34 -0700707 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller2e95e4a2015-08-07 10:40:33 -0700708
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800709 GPR_TIMER_BEGIN("grpc_call_destroy", 0);
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700710 GRPC_API_TRACE("grpc_call_destroy(c=%p)", 1, (c));
711
Craig Tillera82950e2015-09-22 12:33:20 -0700712 if (parent) {
713 gpr_mu_lock(&parent->mu);
714 if (c == parent->first_child) {
715 parent->first_child = c->sibling_next;
716 if (c == parent->first_child) {
717 parent->first_child = NULL;
718 }
719 c->sibling_prev->sibling_next = c->sibling_next;
720 c->sibling_next->sibling_prev = c->sibling_prev;
Craig Tiller2e95e4a2015-08-07 10:40:33 -0700721 }
Craig Tillera82950e2015-09-22 12:33:20 -0700722 gpr_mu_unlock(&parent->mu);
723 GRPC_CALL_INTERNAL_UNREF(&exec_ctx, parent, "child");
724 }
Craig Tiller2e95e4a2015-08-07 10:40:33 -0700725
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800726 gpr_mu_lock(&c->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700727 GPR_ASSERT(!c->destroy_called);
Craig Tillerf3fba742015-06-11 09:36:33 -0700728 c->destroy_called = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700729 if (c->have_alarm) {
David Garcia Quintasf747bbc2015-10-04 23:09:47 -0700730 grpc_timer_cancel(&exec_ctx, &c->alarm);
Craig Tillera82950e2015-09-22 12:33:20 -0700731 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800732 cancel = !c->received_final_op;
733 gpr_mu_unlock(&c->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700734 if (cancel) grpc_call_cancel(c, NULL);
735 GRPC_CALL_INTERNAL_UNREF(&exec_ctx, c, "destroy");
736 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800737 GPR_TIMER_END("grpc_call_destroy", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800738}
739
Craig Tillera82950e2015-09-22 12:33:20 -0700740grpc_call_error grpc_call_cancel(grpc_call *call, void *reserved) {
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700741 GRPC_API_TRACE("grpc_call_cancel(call=%p, reserved=%p)", 2, (call, reserved));
Craig Tillera82950e2015-09-22 12:33:20 -0700742 GPR_ASSERT(!reserved);
743 return grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED, "Cancelled",
744 NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800745}
746
Craig Tillera82950e2015-09-22 12:33:20 -0700747grpc_call_error grpc_call_cancel_with_status(grpc_call *c,
748 grpc_status_code status,
749 const char *description,
750 void *reserved) {
Craig Tiller5dde66e2015-06-02 09:05:23 -0700751 grpc_call_error r;
Craig Tillerf5768a62015-09-22 10:54:34 -0700752 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700753 GRPC_API_TRACE(
754 "grpc_call_cancel_with_status("
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700755 "c=%p, status=%d, description=%s, reserved=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700756 4, (c, (int)status, description, reserved));
Craig Tillera82950e2015-09-22 12:33:20 -0700757 GPR_ASSERT(reserved == NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800758 gpr_mu_lock(&c->mu);
759 r = cancel_with_status(&exec_ctx, c, status, description);
760 gpr_mu_unlock(&c->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700761 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller5dde66e2015-06-02 09:05:23 -0700762 return r;
Yang Gaoff30f8e2015-05-04 00:13:39 -0700763}
764
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700765typedef struct termination_closure {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800766 grpc_closure closure;
767 grpc_call *call;
Craig Tillerf0f70a82016-06-23 13:55:06 -0700768 grpc_error *error;
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700769 grpc_closure *op_closure;
770 enum { TC_CANCEL, TC_CLOSE } type;
771} termination_closure;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800772
Craig Tiller0ca68b72016-06-09 07:50:50 -0700773static void done_termination(grpc_exec_ctx *exec_ctx, void *tcp,
774 grpc_error *error) {
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700775 termination_closure *tc = tcp;
Craig Tiller0ca68b72016-06-09 07:50:50 -0700776 switch (tc->type) {
777 case TC_CANCEL:
778 GRPC_CALL_INTERNAL_UNREF(exec_ctx, tc->call, "cancel");
779 break;
780 case TC_CLOSE:
781 GRPC_CALL_INTERNAL_UNREF(exec_ctx, tc->call, "close");
782 break;
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700783 }
Craig Tillerf0f70a82016-06-23 13:55:06 -0700784 GRPC_ERROR_UNREF(tc->error);
Craig Tiller0ca68b72016-06-09 07:50:50 -0700785 grpc_exec_ctx_sched(exec_ctx, tc->op_closure, GRPC_ERROR_NONE, NULL);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700786 gpr_free(tc);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800787}
788
Craig Tiller0ca68b72016-06-09 07:50:50 -0700789static void send_cancel(grpc_exec_ctx *exec_ctx, void *tcp, grpc_error *error) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800790 grpc_transport_stream_op op;
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700791 termination_closure *tc = tcp;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800792 memset(&op, 0, sizeof(op));
Craig Tillerf0f70a82016-06-23 13:55:06 -0700793 op.cancel_error = tc->error;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800794 /* reuse closure to catch completion */
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700795 grpc_closure_init(&tc->closure, done_termination, tc);
796 op.on_complete = &tc->closure;
797 execute_op(exec_ctx, tc->call, &op);
798}
799
Craig Tiller0ca68b72016-06-09 07:50:50 -0700800static void send_close(grpc_exec_ctx *exec_ctx, void *tcp, grpc_error *error) {
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700801 grpc_transport_stream_op op;
802 termination_closure *tc = tcp;
803 memset(&op, 0, sizeof(op));
Craig Tillerf0f70a82016-06-23 13:55:06 -0700804 op.close_error = tc->error;
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700805 /* reuse closure to catch completion */
806 grpc_closure_init(&tc->closure, done_termination, tc);
807 tc->op_closure = op.on_complete;
808 op.on_complete = &tc->closure;
809 execute_op(exec_ctx, tc->call, &op);
810}
811
812static grpc_call_error terminate_with_status(grpc_exec_ctx *exec_ctx,
813 termination_closure *tc) {
Craig Tillerf0f70a82016-06-23 13:55:06 -0700814 set_status_from_error(tc->call, STATUS_FROM_API_OVERRIDE, tc->error);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700815
816 if (tc->type == TC_CANCEL) {
817 grpc_closure_init(&tc->closure, send_cancel, tc);
818 GRPC_CALL_INTERNAL_REF(tc->call, "cancel");
819 } else if (tc->type == TC_CLOSE) {
820 grpc_closure_init(&tc->closure, send_close, tc);
821 GRPC_CALL_INTERNAL_REF(tc->call, "close");
822 }
Craig Tiller0ca68b72016-06-09 07:50:50 -0700823 grpc_exec_ctx_sched(exec_ctx, &tc->closure, GRPC_ERROR_NONE, NULL);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700824 return GRPC_CALL_OK;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800825}
826
827static grpc_call_error cancel_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c,
828 grpc_status_code status,
Craig Tillera82950e2015-09-22 12:33:20 -0700829 const char *description) {
Craig Tillerf0f70a82016-06-23 13:55:06 -0700830 GPR_ASSERT(status != GRPC_STATUS_OK);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700831 termination_closure *tc = gpr_malloc(sizeof(*tc));
832 memset(tc, 0, sizeof(termination_closure));
833 tc->type = TC_CANCEL;
834 tc->call = c;
Craig Tillerf0f70a82016-06-23 13:55:06 -0700835 tc->error = grpc_error_set_int(
836 grpc_error_set_str(GRPC_ERROR_CREATE(description),
837 GRPC_ERROR_STR_GRPC_MESSAGE, description),
838 GRPC_ERROR_INT_GRPC_STATUS, status);
Craig Tiller5dde66e2015-06-02 09:05:23 -0700839
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700840 return terminate_with_status(exec_ctx, tc);
841}
Craig Tiller48b9fde2015-04-24 08:04:59 -0700842
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700843static grpc_call_error close_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c,
844 grpc_status_code status,
845 const char *description) {
Craig Tillerf0f70a82016-06-23 13:55:06 -0700846 GPR_ASSERT(status != GRPC_STATUS_OK);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700847 termination_closure *tc = gpr_malloc(sizeof(*tc));
848 memset(tc, 0, sizeof(termination_closure));
849 tc->type = TC_CLOSE;
850 tc->call = c;
Craig Tillerf0f70a82016-06-23 13:55:06 -0700851 tc->error = grpc_error_set_int(
852 grpc_error_set_str(GRPC_ERROR_CREATE(description),
853 GRPC_ERROR_STR_GRPC_MESSAGE, description),
854 GRPC_ERROR_INT_GRPC_STATUS, status);
Craig Tiller48b9fde2015-04-24 08:04:59 -0700855
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700856 return terminate_with_status(exec_ctx, tc);
Craig Tillerd248c242015-01-14 11:49:12 -0800857}
858
Craig Tillera82950e2015-09-22 12:33:20 -0700859static void execute_op(grpc_exec_ctx *exec_ctx, grpc_call *call,
860 grpc_transport_stream_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800861 grpc_call_element *elem;
Craig Tiller5dde66e2015-06-02 09:05:23 -0700862
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800863 GPR_TIMER_BEGIN("execute_op", 0);
Craig Tillera82950e2015-09-22 12:33:20 -0700864 elem = CALL_ELEM_FROM_CALL(call, 0);
Craig Tiller935cf422015-05-01 14:10:46 -0700865 op->context = call->context;
Craig Tillera82950e2015-09-22 12:33:20 -0700866 elem->filter->start_transport_stream_op(exec_ctx, elem, op);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800867 GPR_TIMER_END("execute_op", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800868}
869
Craig Tillera82950e2015-09-22 12:33:20 -0700870char *grpc_call_get_peer(grpc_call *call) {
871 grpc_call_element *elem = CALL_ELEM_FROM_CALL(call, 0);
Craig Tillerf5768a62015-09-22 10:54:34 -0700872 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller27e5aa42015-11-24 16:28:54 -0800873 char *result;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700874 GRPC_API_TRACE("grpc_call_get_peer(%p)", 1, (call));
Craig Tiller27e5aa42015-11-24 16:28:54 -0800875 result = elem->filter->get_peer(&exec_ctx, elem);
876 if (result == NULL) {
877 result = grpc_channel_get_target(call->channel);
878 }
879 if (result == NULL) {
880 result = gpr_strdup("unknown");
881 }
Craig Tillera82950e2015-09-22 12:33:20 -0700882 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller3ffd8222015-09-21 08:21:57 -0700883 return result;
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700884}
885
Craig Tillera82950e2015-09-22 12:33:20 -0700886grpc_call *grpc_call_from_top_element(grpc_call_element *elem) {
887 return CALL_FROM_TOP_ELEM(elem);
Craig Tiller566316f2015-02-02 15:25:32 -0800888}
889
Craig Tillerc027e772016-05-03 16:27:00 -0700890static void call_alarm(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
Craig Tiller566316f2015-02-02 15:25:32 -0800891 grpc_call *call = arg;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800892 gpr_mu_lock(&call->mu);
Craig Tiller77f04612015-07-01 13:39:45 -0700893 call->have_alarm = 0;
Craig Tiller1c51edc2016-05-07 16:18:43 -0700894 if (error != GRPC_ERROR_CANCELLED) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800895 cancel_with_status(exec_ctx, call, GRPC_STATUS_DEADLINE_EXCEEDED,
Craig Tillera82950e2015-09-22 12:33:20 -0700896 "Deadline Exceeded");
897 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800898 gpr_mu_unlock(&call->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700899 GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "alarm");
Craig Tiller566316f2015-02-02 15:25:32 -0800900}
901
Craig Tillera82950e2015-09-22 12:33:20 -0700902static void set_deadline_alarm(grpc_exec_ctx *exec_ctx, grpc_call *call,
903 gpr_timespec deadline) {
904 if (call->have_alarm) {
905 gpr_log(GPR_ERROR, "Attempt to set deadline alarm twice");
906 assert(0);
907 return;
908 }
909 GRPC_CALL_INTERNAL_REF(call, "alarm");
Craig Tiller566316f2015-02-02 15:25:32 -0800910 call->have_alarm = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700911 call->send_deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
David Garcia Quintasf747bbc2015-10-04 23:09:47 -0700912 grpc_timer_init(exec_ctx, &call->alarm, call->send_deadline, call_alarm, call,
Craig Tillera82950e2015-09-22 12:33:20 -0700913 gpr_now(GPR_CLOCK_MONOTONIC));
Craig Tiller566316f2015-02-02 15:25:32 -0800914}
915
Craig Tiller566316f2015-02-02 15:25:32 -0800916/* we offset status by a small amount when storing it into transport metadata
917 as metadata cannot store a 0 value (which is used as OK for grpc_status_codes
918 */
919#define STATUS_OFFSET 1
Craig Tillera82950e2015-09-22 12:33:20 -0700920static void destroy_status(void *ignored) {}
Craig Tiller566316f2015-02-02 15:25:32 -0800921
Craig Tiller7536af02015-12-22 13:49:30 -0800922static uint32_t decode_status(grpc_mdelem *md) {
923 uint32_t status;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800924 void *user_data;
925 if (md == GRPC_MDELEM_GRPC_STATUS_0) return 0;
926 if (md == GRPC_MDELEM_GRPC_STATUS_1) return 1;
927 if (md == GRPC_MDELEM_GRPC_STATUS_2) return 2;
928 user_data = grpc_mdelem_get_user_data(md, destroy_status);
929 if (user_data != NULL) {
Craig Tiller7536af02015-12-22 13:49:30 -0800930 status = ((uint32_t)(intptr_t)user_data) - STATUS_OFFSET;
Craig Tillera82950e2015-09-22 12:33:20 -0700931 } else {
932 if (!gpr_parse_bytes_to_uint32(grpc_mdstr_as_c_string(md->value),
933 GPR_SLICE_LENGTH(md->value->slice),
934 &status)) {
935 status = GRPC_STATUS_UNKNOWN; /* could not parse status code */
Craig Tiller566316f2015-02-02 15:25:32 -0800936 }
Craig Tillera82950e2015-09-22 12:33:20 -0700937 grpc_mdelem_set_user_data(md, destroy_status,
Craig Tiller7536af02015-12-22 13:49:30 -0800938 (void *)(intptr_t)(status + STATUS_OFFSET));
Craig Tillera82950e2015-09-22 12:33:20 -0700939 }
Craig Tiller566316f2015-02-02 15:25:32 -0800940 return status;
941}
942
David Garcia Quintas303d3082016-05-05 18:25:34 -0700943static grpc_compression_algorithm decode_compression(grpc_mdelem *md) {
Craig Tillerebdef9d2015-11-19 17:09:49 -0800944 grpc_compression_algorithm algorithm =
945 grpc_compression_algorithm_from_mdstr(md->value);
946 if (algorithm == GRPC_COMPRESS_ALGORITHMS_COUNT) {
Craig Tillera82950e2015-09-22 12:33:20 -0700947 const char *md_c_str = grpc_mdstr_as_c_string(md->value);
David Garcia Quintas303d3082016-05-05 18:25:34 -0700948 gpr_log(GPR_ERROR,
949 "Invalid incoming compression algorithm: '%s'. Interpreting "
950 "incoming data as uncompressed.",
951 md_c_str);
952 return GRPC_COMPRESS_NONE;
Craig Tillera82950e2015-09-22 12:33:20 -0700953 }
David Garcia Quintasfc0fa332015-06-25 18:11:07 -0700954 return algorithm;
David Garcia Quintasdb94b272015-06-15 18:37:01 -0700955}
956
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800957static grpc_mdelem *recv_common_filter(grpc_call *call, grpc_mdelem *elem) {
Craig Tillerebdef9d2015-11-19 17:09:49 -0800958 if (elem->key == GRPC_MDSTR_GRPC_STATUS) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800959 GPR_TIMER_BEGIN("status", 0);
960 set_status_code(call, STATUS_FROM_WIRE, decode_status(elem));
961 GPR_TIMER_END("status", 0);
962 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800963 } else if (elem->key == GRPC_MDSTR_GRPC_MESSAGE) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800964 GPR_TIMER_BEGIN("status-details", 0);
965 set_status_details(call, STATUS_FROM_WIRE, GRPC_MDSTR_REF(elem->value));
966 GPR_TIMER_END("status-details", 0);
967 return NULL;
968 }
969 return elem;
970}
971
972static grpc_mdelem *publish_app_metadata(grpc_call *call, grpc_mdelem *elem,
973 int is_trailing) {
Craig Tiller566316f2015-02-02 15:25:32 -0800974 grpc_metadata_array *dest;
975 grpc_metadata *mdusr;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800976 GPR_TIMER_BEGIN("publish_app_metadata", 0);
977 dest = call->buffered_metadata[is_trailing];
978 if (dest->count == dest->capacity) {
979 dest->capacity = GPR_MAX(dest->capacity + 8, dest->capacity * 2);
980 dest->metadata =
981 gpr_realloc(dest->metadata, sizeof(grpc_metadata) * dest->capacity);
982 }
983 mdusr = &dest->metadata[dest->count++];
984 mdusr->key = grpc_mdstr_as_c_string(elem->key);
985 mdusr->value = grpc_mdstr_as_c_string(elem->value);
986 mdusr->value_length = GPR_SLICE_LENGTH(elem->value->slice);
987 GPR_TIMER_END("publish_app_metadata", 0);
988 return elem;
989}
Craig Tiller566316f2015-02-02 15:25:32 -0800990
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800991static grpc_mdelem *recv_initial_filter(void *callp, grpc_mdelem *elem) {
992 grpc_call *call = callp;
993 elem = recv_common_filter(call, elem);
994 if (elem == NULL) {
995 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800996 } else if (elem->key == GRPC_MDSTR_GRPC_ENCODING) {
David Garcia Quintas749367f2016-05-17 19:15:24 -0700997 GPR_TIMER_BEGIN("incoming_compression_algorithm", 0);
David Garcia Quintasac094472016-05-18 20:25:57 -0700998 set_incoming_compression_algorithm(call, decode_compression(elem));
David Garcia Quintas749367f2016-05-17 19:15:24 -0700999 GPR_TIMER_END("incoming_compression_algorithm", 0);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001000 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -08001001 } else if (elem->key == GRPC_MDSTR_GRPC_ACCEPT_ENCODING) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001002 GPR_TIMER_BEGIN("encodings_accepted_by_peer", 0);
1003 set_encodings_accepted_by_peer(call, elem);
1004 GPR_TIMER_END("encodings_accepted_by_peer", 0);
1005 return NULL;
1006 } else {
1007 return publish_app_metadata(call, elem, 0);
Craig Tillera82950e2015-09-22 12:33:20 -07001008 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001009}
Craig Tiller6902ad22015-04-16 08:01:49 -07001010
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001011static grpc_mdelem *recv_trailing_filter(void *callp, grpc_mdelem *elem) {
1012 grpc_call *call = callp;
1013 elem = recv_common_filter(call, elem);
1014 if (elem == NULL) {
1015 return NULL;
1016 } else {
1017 return publish_app_metadata(call, elem, 1);
Craig Tillera82950e2015-09-22 12:33:20 -07001018 }
Craig Tiller629b0ed2015-04-22 11:14:26 -07001019}
Craig Tiller8b282cb2015-04-17 14:57:44 -07001020
Craig Tillera82950e2015-09-22 12:33:20 -07001021grpc_call_stack *grpc_call_get_call_stack(grpc_call *call) {
1022 return CALL_STACK_FROM_CALL(call);
Craig Tiller566316f2015-02-02 15:25:32 -08001023}
1024
1025/*
Craig Tillerfb189f82015-02-03 12:07:07 -08001026 * BATCH API IMPLEMENTATION
1027 */
1028
Craig Tillera82950e2015-09-22 12:33:20 -07001029static void set_status_value_directly(grpc_status_code status, void *dest) {
1030 *(grpc_status_code *)dest = status;
Craig Tillerfb189f82015-02-03 12:07:07 -08001031}
1032
Craig Tillera82950e2015-09-22 12:33:20 -07001033static void set_cancelled_value(grpc_status_code status, void *dest) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001034 *(int *)dest = (status != GRPC_STATUS_OK);
Craig Tiller166e2502015-02-03 20:14:41 -08001035}
Craig Tillerfb189f82015-02-03 12:07:07 -08001036
Craig Tillerc6549762016-03-09 17:10:43 -08001037static bool are_write_flags_valid(uint32_t flags) {
David Garcia Quintas1d5aca52015-06-14 14:42:04 -07001038 /* check that only bits in GRPC_WRITE_(INTERNAL?)_USED_MASK are set */
Craig Tiller7536af02015-12-22 13:49:30 -08001039 const uint32_t allowed_write_positions =
Craig Tillera82950e2015-09-22 12:33:20 -07001040 (GRPC_WRITE_USED_MASK | GRPC_WRITE_INTERNAL_USED_MASK);
Craig Tiller7536af02015-12-22 13:49:30 -08001041 const uint32_t invalid_positions = ~allowed_write_positions;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -07001042 return !(flags & invalid_positions);
1043}
1044
Craig Tillerc6549762016-03-09 17:10:43 -08001045static bool are_initial_metadata_flags_valid(uint32_t flags, bool is_client) {
1046 /* check that only bits in GRPC_WRITE_(INTERNAL?)_USED_MASK are set */
1047 uint32_t invalid_positions = ~GRPC_INITIAL_METADATA_USED_MASK;
1048 if (!is_client) {
1049 invalid_positions |= GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST;
1050 }
1051 return !(flags & invalid_positions);
1052}
1053
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001054static batch_control *allocate_batch_control(grpc_call *call) {
1055 size_t i;
1056 for (i = 0; i < MAX_CONCURRENT_BATCHES; i++) {
1057 if ((call->used_batches & (1 << i)) == 0) {
Craig Tiller7536af02015-12-22 13:49:30 -08001058 call->used_batches = (uint8_t)(call->used_batches | (uint8_t)(1 << i));
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001059 return &call->active_batches[i];
1060 }
1061 }
yang-g49209762015-12-02 11:28:19 -08001062 return NULL;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001063}
1064
1065static void finish_batch_completion(grpc_exec_ctx *exec_ctx, void *user_data,
1066 grpc_cq_completion *storage) {
1067 batch_control *bctl = user_data;
1068 grpc_call *call = bctl->call;
1069 gpr_mu_lock(&call->mu);
Craig Tiller7536af02015-12-22 13:49:30 -08001070 call->used_batches = (uint8_t)(
1071 call->used_batches & ~(uint8_t)(1 << (bctl - call->active_batches)));
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001072 gpr_mu_unlock(&call->mu);
1073 GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "completion");
1074}
1075
1076static void post_batch_completion(grpc_exec_ctx *exec_ctx,
1077 batch_control *bctl) {
1078 grpc_call *call = bctl->call;
1079 if (bctl->is_notify_tag_closure) {
Craig Tillerb08fa492016-05-10 14:56:05 -07001080 /* unrefs bctl->error */
Craig Tiller332f1b32016-05-24 13:21:21 -07001081 grpc_exec_ctx_sched(exec_ctx, bctl->notify_tag, bctl->error, NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001082 gpr_mu_lock(&call->mu);
1083 bctl->call->used_batches =
Craig Tiller7536af02015-12-22 13:49:30 -08001084 (uint8_t)(bctl->call->used_batches &
1085 ~(uint8_t)(1 << (bctl - bctl->call->active_batches)));
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001086 gpr_mu_unlock(&call->mu);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001087 GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "completion");
1088 } else {
Craig Tillerb08fa492016-05-10 14:56:05 -07001089 /* unrefs bctl->error */
Craig Tillerc027e772016-05-03 16:27:00 -07001090 grpc_cq_end_op(exec_ctx, bctl->call->cq, bctl->notify_tag, bctl->error,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001091 finish_batch_completion, bctl, &bctl->cq_completion);
1092 }
1093}
1094
1095static void continue_receiving_slices(grpc_exec_ctx *exec_ctx,
1096 batch_control *bctl) {
1097 grpc_call *call = bctl->call;
1098 for (;;) {
1099 size_t remaining = call->receiving_stream->length -
1100 (*call->receiving_buffer)->data.raw.slice_buffer.length;
1101 if (remaining == 0) {
1102 call->receiving_message = 0;
Craig Tiller3b66ab92015-12-09 19:42:22 -08001103 grpc_byte_stream_destroy(exec_ctx, call->receiving_stream);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001104 call->receiving_stream = NULL;
1105 if (gpr_unref(&bctl->steps_to_complete)) {
1106 post_batch_completion(exec_ctx, bctl);
1107 }
1108 return;
1109 }
1110 if (grpc_byte_stream_next(exec_ctx, call->receiving_stream,
1111 &call->receiving_slice, remaining,
1112 &call->receiving_slice_ready)) {
1113 gpr_slice_buffer_add(&(*call->receiving_buffer)->data.raw.slice_buffer,
1114 call->receiving_slice);
1115 } else {
1116 return;
1117 }
1118 }
1119}
1120
1121static void receiving_slice_ready(grpc_exec_ctx *exec_ctx, void *bctlp,
Craig Tillerc027e772016-05-03 16:27:00 -07001122 grpc_error *error) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001123 batch_control *bctl = bctlp;
1124 grpc_call *call = bctl->call;
1125
Craig Tillerc027e772016-05-03 16:27:00 -07001126 if (error == GRPC_ERROR_NONE) {
Craig Tiller38edec62015-12-14 15:01:29 -08001127 gpr_slice_buffer_add(&(*call->receiving_buffer)->data.raw.slice_buffer,
1128 call->receiving_slice);
1129 continue_receiving_slices(exec_ctx, bctl);
1130 } else {
Craig Tillera286b042016-06-13 15:20:39 +00001131 if (grpc_trace_operation_failures) {
1132 GRPC_LOG_IF_ERROR("receiving_slice_ready", GRPC_ERROR_REF(error));
1133 }
Craig Tillere1b8c2b2015-12-16 19:27:52 -08001134 grpc_byte_stream_destroy(exec_ctx, call->receiving_stream);
Craig Tiller38edec62015-12-14 15:01:29 -08001135 call->receiving_stream = NULL;
1136 grpc_byte_buffer_destroy(*call->receiving_buffer);
1137 *call->receiving_buffer = NULL;
1138 if (gpr_unref(&bctl->steps_to_complete)) {
1139 post_batch_completion(exec_ctx, bctl);
1140 }
1141 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001142}
1143
Craig Tillera44cbfc2016-02-03 16:02:49 -08001144static void process_data_after_md(grpc_exec_ctx *exec_ctx, batch_control *bctl,
1145 bool success) {
1146 grpc_call *call = bctl->call;
1147 if (call->receiving_stream == NULL) {
1148 *call->receiving_buffer = NULL;
1149 call->receiving_message = 0;
1150 if (gpr_unref(&bctl->steps_to_complete)) {
1151 post_batch_completion(exec_ctx, bctl);
1152 }
1153 } else if (call->receiving_stream->length >
1154 grpc_channel_get_max_message_length(call->channel)) {
1155 cancel_with_status(exec_ctx, call, GRPC_STATUS_INTERNAL,
1156 "Max message size exceeded");
1157 grpc_byte_stream_destroy(exec_ctx, call->receiving_stream);
1158 call->receiving_stream = NULL;
1159 *call->receiving_buffer = NULL;
1160 call->receiving_message = 0;
1161 if (gpr_unref(&bctl->steps_to_complete)) {
1162 post_batch_completion(exec_ctx, bctl);
1163 }
1164 } else {
1165 call->test_only_last_message_flags = call->receiving_stream->flags;
1166 if ((call->receiving_stream->flags & GRPC_WRITE_INTERNAL_COMPRESS) &&
David Garcia Quintas749367f2016-05-17 19:15:24 -07001167 (call->incoming_compression_algorithm > GRPC_COMPRESS_NONE)) {
Craig Tillera44cbfc2016-02-03 16:02:49 -08001168 *call->receiving_buffer = grpc_raw_compressed_byte_buffer_create(
David Garcia Quintas749367f2016-05-17 19:15:24 -07001169 NULL, 0, call->incoming_compression_algorithm);
Craig Tillera44cbfc2016-02-03 16:02:49 -08001170 } else {
1171 *call->receiving_buffer = grpc_raw_byte_buffer_create(NULL, 0);
1172 }
1173 grpc_closure_init(&call->receiving_slice_ready, receiving_slice_ready,
1174 bctl);
1175 continue_receiving_slices(exec_ctx, bctl);
1176 /* early out */
1177 return;
1178 }
1179}
1180
1181static void receiving_stream_ready(grpc_exec_ctx *exec_ctx, void *bctlp,
Craig Tillerc027e772016-05-03 16:27:00 -07001182 grpc_error *error) {
Craig Tillera44cbfc2016-02-03 16:02:49 -08001183 batch_control *bctl = bctlp;
1184 grpc_call *call = bctl->call;
1185
1186 gpr_mu_lock(&bctl->call->mu);
Craig Tillerc027e772016-05-03 16:27:00 -07001187 if (bctl->call->has_initial_md_been_received || error != GRPC_ERROR_NONE ||
Craig Tiller52cf8712016-04-23 22:54:21 -07001188 call->receiving_stream == NULL) {
Craig Tillera44cbfc2016-02-03 16:02:49 -08001189 gpr_mu_unlock(&bctl->call->mu);
Craig Tillerc027e772016-05-03 16:27:00 -07001190 process_data_after_md(exec_ctx, bctlp, error);
Craig Tillera44cbfc2016-02-03 16:02:49 -08001191 } else {
Craig Tiller8a677802016-04-22 15:07:53 -07001192 call->saved_receiving_stream_ready_bctlp = bctlp;
Craig Tillera44cbfc2016-02-03 16:02:49 -08001193 gpr_mu_unlock(&bctl->call->mu);
1194 }
1195}
1196
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001197static void validate_filtered_metadata(grpc_exec_ctx *exec_ctx,
1198 batch_control *bctl) {
1199 grpc_call *call = bctl->call;
David Garcia Quintasac094472016-05-18 20:25:57 -07001200 /* validate call->incoming_compression_algorithm */
1201 if (call->incoming_compression_algorithm != GRPC_COMPRESS_NONE) {
1202 const grpc_compression_algorithm algo =
1203 call->incoming_compression_algorithm;
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001204 char *error_msg = NULL;
1205 const grpc_compression_options compression_options =
David Garcia Quintasac094472016-05-18 20:25:57 -07001206 grpc_channel_compression_options(call->channel);
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001207 /* check if algorithm is known */
1208 if (algo >= GRPC_COMPRESS_ALGORITHMS_COUNT) {
1209 gpr_asprintf(&error_msg, "Invalid compression algorithm value '%d'.",
1210 algo);
Yuchen Zeng64c0e8d2016-06-10 11:19:51 -07001211 gpr_log(GPR_ERROR, "%s", error_msg);
David Garcia Quintas824f8372016-05-18 17:52:46 -07001212 close_with_status(exec_ctx, call, GRPC_STATUS_UNIMPLEMENTED, error_msg);
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001213 } else if (grpc_compression_options_is_algorithm_enabled(
1214 &compression_options, algo) == 0) {
1215 /* check if algorithm is supported by current channel config */
David Garcia Quintas1ff168a2016-06-30 10:25:04 -07001216 char *algo_name = NULL;
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001217 grpc_compression_algorithm_name(algo, &algo_name);
1218 gpr_asprintf(&error_msg, "Compression algorithm '%s' is disabled.",
1219 algo_name);
Yuchen Zeng64c0e8d2016-06-10 11:19:51 -07001220 gpr_log(GPR_ERROR, "%s", error_msg);
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001221 close_with_status(exec_ctx, call, GRPC_STATUS_UNIMPLEMENTED, error_msg);
1222 } else {
David Garcia Quintasac094472016-05-18 20:25:57 -07001223 call->incoming_compression_algorithm = algo;
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001224 }
1225 gpr_free(error_msg);
1226 }
David Garcia Quintasf1945f22016-05-18 10:53:14 -07001227
1228 /* make sure the received grpc-encoding is amongst the ones listed in
1229 * grpc-accept-encoding */
1230 GPR_ASSERT(call->encodings_accepted_by_peer != 0);
1231 if (!GPR_BITGET(call->encodings_accepted_by_peer,
David Garcia Quintasac094472016-05-18 20:25:57 -07001232 call->incoming_compression_algorithm)) {
David Garcia Quintasf1945f22016-05-18 10:53:14 -07001233 extern int grpc_compression_trace;
1234 if (grpc_compression_trace) {
David Garcia Quintas1ff168a2016-06-30 10:25:04 -07001235 char *algo_name = NULL;
David Garcia Quintasac094472016-05-18 20:25:57 -07001236 grpc_compression_algorithm_name(call->incoming_compression_algorithm,
1237 &algo_name);
David Garcia Quintasf1945f22016-05-18 10:53:14 -07001238 gpr_log(GPR_ERROR,
1239 "Compression algorithm (grpc-encoding = '%s') not present in "
1240 "the bitset of accepted encodings (grpc-accept-encodings: "
1241 "'0x%x')",
1242 algo_name, call->encodings_accepted_by_peer);
1243 }
1244 }
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001245}
1246
Craig Tillera44cbfc2016-02-03 16:02:49 -08001247static void receiving_initial_metadata_ready(grpc_exec_ctx *exec_ctx,
Craig Tillerc027e772016-05-03 16:27:00 -07001248 void *bctlp, grpc_error *error) {
Craig Tillera44cbfc2016-02-03 16:02:49 -08001249 batch_control *bctl = bctlp;
1250 grpc_call *call = bctl->call;
1251
1252 gpr_mu_lock(&call->mu);
1253
Craig Tillerc027e772016-05-03 16:27:00 -07001254 if (error != GRPC_ERROR_NONE) {
Craig Tillerf707d622016-05-06 14:26:12 -07001255 bctl->error = GRPC_ERROR_REF(error);
Craig Tillerc48ca712016-04-04 13:42:04 -07001256 } else {
1257 grpc_metadata_batch *md =
1258 &call->metadata_batch[1 /* is_receiving */][0 /* is_trailing */];
1259 grpc_metadata_batch_filter(md, recv_initial_filter, call);
Craig Tillera44cbfc2016-02-03 16:02:49 -08001260
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001261 GPR_TIMER_BEGIN("validate_filtered_metadata", 0);
1262 validate_filtered_metadata(exec_ctx, bctl);
1263 GPR_TIMER_END("validate_filtered_metadata", 0);
David Garcia Quintas46123372016-05-09 15:28:42 -07001264
Craig Tillerc48ca712016-04-04 13:42:04 -07001265 if (gpr_time_cmp(md->deadline, gpr_inf_future(md->deadline.clock_type)) !=
1266 0 &&
1267 !call->is_client) {
1268 GPR_TIMER_BEGIN("set_deadline_alarm", 0);
1269 set_deadline_alarm(exec_ctx, call, md->deadline);
1270 GPR_TIMER_END("set_deadline_alarm", 0);
1271 }
Craig Tillera44cbfc2016-02-03 16:02:49 -08001272 }
1273
Craig Tillerc48ca712016-04-04 13:42:04 -07001274 call->has_initial_md_been_received = true;
Craig Tiller8a677802016-04-22 15:07:53 -07001275 if (call->saved_receiving_stream_ready_bctlp != NULL) {
Craig Tillera44cbfc2016-02-03 16:02:49 -08001276 grpc_closure *saved_rsr_closure = grpc_closure_create(
Craig Tiller8a677802016-04-22 15:07:53 -07001277 receiving_stream_ready, call->saved_receiving_stream_ready_bctlp);
1278 call->saved_receiving_stream_ready_bctlp = NULL;
Craig Tiller332f1b32016-05-24 13:21:21 -07001279 grpc_exec_ctx_sched(exec_ctx, saved_rsr_closure, error, NULL);
Craig Tillera44cbfc2016-02-03 16:02:49 -08001280 }
1281
1282 gpr_mu_unlock(&call->mu);
1283
1284 if (gpr_unref(&bctl->steps_to_complete)) {
1285 post_batch_completion(exec_ctx, bctl);
1286 }
1287}
1288
Craig Tillerc027e772016-05-03 16:27:00 -07001289static void finish_batch(grpc_exec_ctx *exec_ctx, void *bctlp,
1290 grpc_error *error) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001291 batch_control *bctl = bctlp;
1292 grpc_call *call = bctl->call;
1293 grpc_call *child_call;
1294 grpc_call *next_child_call;
1295
Craig Tiller9ccf5f12016-05-07 21:41:01 -07001296 GRPC_ERROR_REF(error);
1297
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001298 gpr_mu_lock(&call->mu);
1299 if (bctl->send_initial_metadata) {
Craig Tillerc027e772016-05-03 16:27:00 -07001300 if (error != GRPC_ERROR_NONE) {
Craig Tiller5b1e6592016-03-11 15:05:30 -08001301 set_status_code(call, STATUS_FROM_CORE, GRPC_STATUS_UNAVAILABLE);
1302 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001303 grpc_metadata_batch_destroy(
1304 &call->metadata_batch[0 /* is_receiving */][0 /* is_trailing */]);
1305 }
1306 if (bctl->send_message) {
1307 call->sending_message = 0;
1308 }
1309 if (bctl->send_final_op) {
1310 grpc_metadata_batch_destroy(
1311 &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */]);
1312 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001313 if (bctl->recv_final_op) {
1314 grpc_metadata_batch *md =
1315 &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */];
1316 grpc_metadata_batch_filter(md, recv_trailing_filter, call);
1317
Craig Tiller1cbf5762016-04-22 16:02:55 -07001318 call->received_final_op = true;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001319 if (call->have_alarm) {
1320 grpc_timer_cancel(exec_ctx, &call->alarm);
1321 }
1322 /* propagate cancellation to any interested children */
1323 child_call = call->first_child;
1324 if (child_call != NULL) {
1325 do {
1326 next_child_call = child_call->sibling_next;
1327 if (child_call->cancellation_is_inherited) {
1328 GRPC_CALL_INTERNAL_REF(child_call, "propagate_cancel");
1329 grpc_call_cancel(child_call, NULL);
1330 GRPC_CALL_INTERNAL_UNREF(exec_ctx, child_call, "propagate_cancel");
1331 }
1332 child_call = next_child_call;
1333 } while (child_call != call->first_child);
1334 }
1335
1336 if (call->is_client) {
1337 get_final_status(call, set_status_value_directly,
1338 call->final_op.client.status);
1339 get_final_details(call, call->final_op.client.status_details,
1340 call->final_op.client.status_details_capacity);
1341 } else {
1342 get_final_status(call, set_cancelled_value,
1343 call->final_op.server.cancelled);
1344 }
1345
Craig Tillerf707d622016-05-06 14:26:12 -07001346 GRPC_ERROR_UNREF(error);
Craig Tillerc027e772016-05-03 16:27:00 -07001347 error = GRPC_ERROR_NONE;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001348 }
Craig Tiller10dd6f22016-05-10 13:06:15 -07001349 GRPC_ERROR_UNREF(bctl->error);
Craig Tillerb08fa492016-05-10 14:56:05 -07001350 bctl->error = GRPC_ERROR_REF(error);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001351 gpr_mu_unlock(&call->mu);
1352 if (gpr_unref(&bctl->steps_to_complete)) {
1353 post_batch_completion(exec_ctx, bctl);
1354 }
Craig Tiller10dd6f22016-05-10 13:06:15 -07001355
1356 GRPC_ERROR_UNREF(error);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001357}
1358
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001359static grpc_call_error call_start_batch(grpc_exec_ctx *exec_ctx,
1360 grpc_call *call, const grpc_op *ops,
1361 size_t nops, void *notify_tag,
1362 int is_notify_tag_closure) {
1363 grpc_transport_stream_op stream_op;
1364 size_t i;
Craig Tillerfb189f82015-02-03 12:07:07 -08001365 const grpc_op *op;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001366 batch_control *bctl;
1367 int num_completion_callbacks_needed = 1;
1368 grpc_call_error error = GRPC_CALL_OK;
Craig Tiller9928d392015-08-18 09:40:24 -07001369
Craig Tiller0ba432d2015-10-09 16:57:11 -07001370 GPR_TIMER_BEGIN("grpc_call_start_batch", 0);
Craig Tiller86253ca2015-10-08 13:31:02 -07001371
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001372 GRPC_CALL_LOG_BATCH(GPR_INFO, call, ops, nops, notify_tag);
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001373
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001374 memset(&stream_op, 0, sizeof(stream_op));
Craig Tillera82950e2015-09-22 12:33:20 -07001375
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001376 /* TODO(ctiller): this feels like it could be made lock-free */
1377 gpr_mu_lock(&call->mu);
1378 bctl = allocate_batch_control(call);
1379 memset(bctl, 0, sizeof(*bctl));
1380 bctl->call = call;
1381 bctl->notify_tag = notify_tag;
Craig Tiller7536af02015-12-22 13:49:30 -08001382 bctl->is_notify_tag_closure = (uint8_t)(is_notify_tag_closure != 0);
Craig Tillera82950e2015-09-22 12:33:20 -07001383
1384 if (nops == 0) {
Craig Tillera82950e2015-09-22 12:33:20 -07001385 GRPC_CALL_INTERNAL_REF(call, "completion");
Craig Tillerc027e772016-05-03 16:27:00 -07001386 bctl->error = GRPC_ERROR_NONE;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001387 if (!is_notify_tag_closure) {
Craig Tiller4bf29282015-12-14 11:25:48 -08001388 grpc_cq_begin_op(call->cq, notify_tag);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001389 }
1390 gpr_mu_unlock(&call->mu);
1391 post_batch_completion(exec_ctx, bctl);
Craig Tillerea50b902015-12-15 07:05:25 -08001392 error = GRPC_CALL_OK;
1393 goto done;
Craig Tillera82950e2015-09-22 12:33:20 -07001394 }
1395
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001396 /* rewrite batch ops into a transport op */
1397 for (i = 0; i < nops; i++) {
1398 op = &ops[i];
Craig Tillera82950e2015-09-22 12:33:20 -07001399 if (op->reserved != NULL) {
Craig Tiller3ffd8222015-09-21 08:21:57 -07001400 error = GRPC_CALL_ERROR;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001401 goto done_with_error;
Craig Tiller3ffd8222015-09-21 08:21:57 -07001402 }
Craig Tillera82950e2015-09-22 12:33:20 -07001403 switch (op->op) {
1404 case GRPC_OP_SEND_INITIAL_METADATA:
1405 /* Flag validation: currently allow no flags */
Craig Tillerc6549762016-03-09 17:10:43 -08001406 if (!are_initial_metadata_flags_valid(op->flags, call->is_client)) {
Craig Tillera82950e2015-09-22 12:33:20 -07001407 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001408 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001409 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001410 if (call->sent_initial_metadata) {
1411 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1412 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001413 }
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001414 /* process compression level */
1415 grpc_metadata compression_md;
1416 memset(&compression_md, 0, sizeof(grpc_metadata));
1417 size_t additional_metadata_count = 0;
David Garcia Quintas749367f2016-05-17 19:15:24 -07001418 grpc_compression_level effective_compression_level;
1419 bool level_set = false;
1420 if (op->data.send_initial_metadata.maybe_compression_level.is_set) {
David Garcia Quintas749367f2016-05-17 19:15:24 -07001421 effective_compression_level =
David Garcia Quintas8ba42be2016-06-07 17:30:20 -07001422 op->data.send_initial_metadata.maybe_compression_level.level;
David Garcia Quintas749367f2016-05-17 19:15:24 -07001423 level_set = true;
1424 } else {
David Garcia Quintasac094472016-05-18 20:25:57 -07001425 const grpc_compression_options copts =
1426 grpc_channel_compression_options(call->channel);
1427 level_set = copts.default_level.is_set;
1428 if (level_set) {
1429 effective_compression_level = copts.default_level.level;
1430 }
David Garcia Quintas749367f2016-05-17 19:15:24 -07001431 }
David Garcia Quintas3e4f49f2016-05-18 23:59:02 -07001432 if (level_set && !call->is_client) {
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001433 const grpc_compression_algorithm calgo =
1434 compression_algorithm_for_level_locked(
David Garcia Quintas749367f2016-05-17 19:15:24 -07001435 call, effective_compression_level);
David Garcia Quintas1ff168a2016-06-30 10:25:04 -07001436 char *calgo_name = NULL;
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001437 grpc_compression_algorithm_name(calgo, &calgo_name);
David Garcia Quintas3e4f49f2016-05-18 23:59:02 -07001438 // the following will be picked up by the compress filter and used as
1439 // the call's compression algorithm.
David Garcia Quintasf54171c2016-05-17 17:19:33 -07001440 compression_md.key = GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY;
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001441 compression_md.value = calgo_name;
1442 compression_md.value_length = strlen(calgo_name);
1443 additional_metadata_count++;
1444 }
1445
1446 if (op->data.send_initial_metadata.count + additional_metadata_count >
1447 INT_MAX) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001448 error = GRPC_CALL_ERROR_INVALID_METADATA;
1449 goto done_with_error;
1450 }
1451 bctl->send_initial_metadata = 1;
1452 call->sent_initial_metadata = 1;
1453 if (!prepare_application_metadata(
1454 call, (int)op->data.send_initial_metadata.count,
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001455 op->data.send_initial_metadata.metadata, 0, call->is_client,
1456 &compression_md, (int)additional_metadata_count)) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001457 error = GRPC_CALL_ERROR_INVALID_METADATA;
1458 goto done_with_error;
1459 }
1460 /* TODO(ctiller): just make these the same variable? */
1461 call->metadata_batch[0][0].deadline = call->send_deadline;
1462 stream_op.send_initial_metadata =
1463 &call->metadata_batch[0 /* is_receiving */][0 /* is_trailing */];
Craig Tiller8c0d96f2016-03-11 14:27:52 -08001464 stream_op.send_initial_metadata_flags = op->flags;
Craig Tillera82950e2015-09-22 12:33:20 -07001465 break;
1466 case GRPC_OP_SEND_MESSAGE:
1467 if (!are_write_flags_valid(op->flags)) {
1468 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001469 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001470 }
1471 if (op->data.send_message == NULL) {
1472 error = GRPC_CALL_ERROR_INVALID_MESSAGE;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001473 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001474 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001475 if (call->sending_message) {
1476 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1477 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001478 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001479 bctl->send_message = 1;
1480 call->sending_message = 1;
1481 grpc_slice_buffer_stream_init(
1482 &call->sending_stream,
1483 &op->data.send_message->data.raw.slice_buffer, op->flags);
1484 stream_op.send_message = &call->sending_stream.base;
Craig Tillera82950e2015-09-22 12:33:20 -07001485 break;
1486 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
1487 /* Flag validation: currently allow no flags */
1488 if (op->flags != 0) {
1489 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001490 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001491 }
1492 if (!call->is_client) {
1493 error = GRPC_CALL_ERROR_NOT_ON_SERVER;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001494 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001495 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001496 if (call->sent_final_op) {
1497 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1498 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001499 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001500 bctl->send_final_op = 1;
1501 call->sent_final_op = 1;
1502 stream_op.send_trailing_metadata =
1503 &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */];
Craig Tillera82950e2015-09-22 12:33:20 -07001504 break;
1505 case GRPC_OP_SEND_STATUS_FROM_SERVER:
1506 /* Flag validation: currently allow no flags */
1507 if (op->flags != 0) {
1508 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001509 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001510 }
1511 if (call->is_client) {
1512 error = GRPC_CALL_ERROR_NOT_ON_CLIENT;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001513 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001514 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001515 if (call->sent_final_op) {
1516 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1517 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001518 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001519 if (op->data.send_status_from_server.trailing_metadata_count >
1520 INT_MAX) {
1521 error = GRPC_CALL_ERROR_INVALID_METADATA;
1522 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001523 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001524 bctl->send_final_op = 1;
1525 call->sent_final_op = 1;
1526 call->send_extra_metadata_count = 1;
1527 call->send_extra_metadata[0].md = grpc_channel_get_reffed_status_elem(
1528 call->channel, op->data.send_status_from_server.status);
1529 if (op->data.send_status_from_server.status_details != NULL) {
1530 call->send_extra_metadata[1].md = grpc_mdelem_from_metadata_strings(
Craig Tillerb2b42612015-11-20 12:02:17 -08001531 GRPC_MDSTR_GRPC_MESSAGE,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001532 grpc_mdstr_from_string(
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001533 op->data.send_status_from_server.status_details));
1534 call->send_extra_metadata_count++;
1535 set_status_details(
1536 call, STATUS_FROM_API_OVERRIDE,
1537 GRPC_MDSTR_REF(call->send_extra_metadata[1].md->value));
Craig Tillera82950e2015-09-22 12:33:20 -07001538 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001539 set_status_code(call, STATUS_FROM_API_OVERRIDE,
Craig Tiller7536af02015-12-22 13:49:30 -08001540 (uint32_t)op->data.send_status_from_server.status);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001541 if (!prepare_application_metadata(
1542 call,
1543 (int)op->data.send_status_from_server.trailing_metadata_count,
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001544 op->data.send_status_from_server.trailing_metadata, 1, 1, NULL,
1545 0)) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001546 error = GRPC_CALL_ERROR_INVALID_METADATA;
1547 goto done_with_error;
1548 }
1549 stream_op.send_trailing_metadata =
1550 &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */];
Craig Tillera82950e2015-09-22 12:33:20 -07001551 break;
1552 case GRPC_OP_RECV_INITIAL_METADATA:
1553 /* Flag validation: currently allow no flags */
1554 if (op->flags != 0) {
1555 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001556 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001557 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001558 if (call->received_initial_metadata) {
1559 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1560 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001561 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001562 call->received_initial_metadata = 1;
1563 call->buffered_metadata[0] = op->data.recv_initial_metadata;
Craig Tillera44cbfc2016-02-03 16:02:49 -08001564 grpc_closure_init(&call->receiving_initial_metadata_ready,
1565 receiving_initial_metadata_ready, bctl);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001566 bctl->recv_initial_metadata = 1;
1567 stream_op.recv_initial_metadata =
1568 &call->metadata_batch[1 /* is_receiving */][0 /* is_trailing */];
Craig Tillera44cbfc2016-02-03 16:02:49 -08001569 stream_op.recv_initial_metadata_ready =
1570 &call->receiving_initial_metadata_ready;
1571 num_completion_callbacks_needed++;
Craig Tillera82950e2015-09-22 12:33:20 -07001572 break;
1573 case GRPC_OP_RECV_MESSAGE:
1574 /* Flag validation: currently allow no flags */
1575 if (op->flags != 0) {
1576 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001577 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001578 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001579 if (call->receiving_message) {
1580 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
yang-g48f3a712015-12-07 11:23:50 -08001581 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001582 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001583 call->receiving_message = 1;
1584 bctl->recv_message = 1;
1585 call->receiving_buffer = op->data.recv_message;
1586 stream_op.recv_message = &call->receiving_stream;
1587 grpc_closure_init(&call->receiving_stream_ready, receiving_stream_ready,
1588 bctl);
1589 stream_op.recv_message_ready = &call->receiving_stream_ready;
1590 num_completion_callbacks_needed++;
Craig Tillera82950e2015-09-22 12:33:20 -07001591 break;
1592 case GRPC_OP_RECV_STATUS_ON_CLIENT:
1593 /* Flag validation: currently allow no flags */
1594 if (op->flags != 0) {
1595 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001596 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001597 }
1598 if (!call->is_client) {
1599 error = GRPC_CALL_ERROR_NOT_ON_SERVER;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001600 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001601 }
Craig Tiller1cbf5762016-04-22 16:02:55 -07001602 if (call->requested_final_op) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001603 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1604 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001605 }
Craig Tiller1cbf5762016-04-22 16:02:55 -07001606 call->requested_final_op = 1;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001607 call->buffered_metadata[1] =
Craig Tillera82950e2015-09-22 12:33:20 -07001608 op->data.recv_status_on_client.trailing_metadata;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001609 call->final_op.client.status = op->data.recv_status_on_client.status;
1610 call->final_op.client.status_details =
1611 op->data.recv_status_on_client.status_details;
1612 call->final_op.client.status_details_capacity =
1613 op->data.recv_status_on_client.status_details_capacity;
1614 bctl->recv_final_op = 1;
1615 stream_op.recv_trailing_metadata =
1616 &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */];
David Garcia Quintas5dde14c2016-07-28 17:29:27 -07001617 stream_op.collect_stats =
1618 &call->final_info.stats.transport_stream_stats;
Craig Tillera82950e2015-09-22 12:33:20 -07001619 break;
1620 case GRPC_OP_RECV_CLOSE_ON_SERVER:
1621 /* Flag validation: currently allow no flags */
1622 if (op->flags != 0) {
1623 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001624 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001625 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001626 if (call->is_client) {
1627 error = GRPC_CALL_ERROR_NOT_ON_CLIENT;
1628 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001629 }
Craig Tiller1cbf5762016-04-22 16:02:55 -07001630 if (call->requested_final_op) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001631 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1632 goto done_with_error;
1633 }
Craig Tiller1cbf5762016-04-22 16:02:55 -07001634 call->requested_final_op = 1;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001635 call->final_op.server.cancelled =
Craig Tillera82950e2015-09-22 12:33:20 -07001636 op->data.recv_close_on_server.cancelled;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001637 bctl->recv_final_op = 1;
1638 stream_op.recv_trailing_metadata =
1639 &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */];
David Garcia Quintas5dde14c2016-07-28 17:29:27 -07001640 stream_op.collect_stats =
1641 &call->final_info.stats.transport_stream_stats;
Craig Tillera82950e2015-09-22 12:33:20 -07001642 break;
Craig Tillerfb189f82015-02-03 12:07:07 -08001643 }
Craig Tillera82950e2015-09-22 12:33:20 -07001644 }
Craig Tillerfb189f82015-02-03 12:07:07 -08001645
Craig Tillera82950e2015-09-22 12:33:20 -07001646 GRPC_CALL_INTERNAL_REF(call, "completion");
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001647 if (!is_notify_tag_closure) {
Craig Tiller4bf29282015-12-14 11:25:48 -08001648 grpc_cq_begin_op(call->cq, notify_tag);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001649 }
1650 gpr_ref_init(&bctl->steps_to_complete, num_completion_callbacks_needed);
Craig Tillerfb189f82015-02-03 12:07:07 -08001651
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001652 stream_op.context = call->context;
1653 grpc_closure_init(&bctl->finish_batch, finish_batch, bctl);
1654 stream_op.on_complete = &bctl->finish_batch;
1655 gpr_mu_unlock(&call->mu);
1656
1657 execute_op(exec_ctx, call, &stream_op);
1658
Craig Tiller3ffd8222015-09-21 08:21:57 -07001659done:
Craig Tiller0ba432d2015-10-09 16:57:11 -07001660 GPR_TIMER_END("grpc_call_start_batch", 0);
Craig Tiller3ffd8222015-09-21 08:21:57 -07001661 return error;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001662
1663done_with_error:
1664 /* reverse any mutations that occured */
1665 if (bctl->send_initial_metadata) {
1666 call->sent_initial_metadata = 0;
1667 grpc_metadata_batch_clear(&call->metadata_batch[0][0]);
1668 }
1669 if (bctl->send_message) {
1670 call->sending_message = 0;
Craig Tiller3b66ab92015-12-09 19:42:22 -08001671 grpc_byte_stream_destroy(exec_ctx, &call->sending_stream.base);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001672 }
1673 if (bctl->send_final_op) {
1674 call->sent_final_op = 0;
1675 grpc_metadata_batch_clear(&call->metadata_batch[0][1]);
1676 }
1677 if (bctl->recv_initial_metadata) {
1678 call->received_initial_metadata = 0;
1679 }
1680 if (bctl->recv_message) {
1681 call->receiving_message = 0;
1682 }
1683 if (bctl->recv_final_op) {
Craig Tiller1cbf5762016-04-22 16:02:55 -07001684 call->requested_final_op = 0;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001685 }
1686 gpr_mu_unlock(&call->mu);
1687 goto done;
1688}
1689
1690grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops,
1691 size_t nops, void *tag, void *reserved) {
1692 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
1693 grpc_call_error err;
1694
1695 GRPC_API_TRACE(
David Garcia Quintas46123372016-05-09 15:28:42 -07001696 "grpc_call_start_batch(call=%p, ops=%p, nops=%lu, tag=%p, "
1697 "reserved=%p)",
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001698 5, (call, ops, (unsigned long)nops, tag, reserved));
1699
1700 if (reserved != NULL) {
1701 err = GRPC_CALL_ERROR;
1702 } else {
1703 err = call_start_batch(&exec_ctx, call, ops, nops, tag, 0);
1704 }
1705
1706 grpc_exec_ctx_finish(&exec_ctx);
1707 return err;
1708}
1709
1710grpc_call_error grpc_call_start_batch_and_execute(grpc_exec_ctx *exec_ctx,
1711 grpc_call *call,
1712 const grpc_op *ops,
1713 size_t nops,
1714 grpc_closure *closure) {
1715 return call_start_batch(exec_ctx, call, ops, nops, closure, 1);
Craig Tillerfb189f82015-02-03 12:07:07 -08001716}
Craig Tiller935cf422015-05-01 14:10:46 -07001717
Craig Tillera82950e2015-09-22 12:33:20 -07001718void grpc_call_context_set(grpc_call *call, grpc_context_index elem,
1719 void *value, void (*destroy)(void *value)) {
1720 if (call->context[elem].destroy) {
1721 call->context[elem].destroy(call->context[elem].value);
1722 }
Julien Boeuf83b02972015-05-20 22:50:34 -07001723 call->context[elem].value = value;
1724 call->context[elem].destroy = destroy;
Craig Tiller935cf422015-05-01 14:10:46 -07001725}
1726
Craig Tillera82950e2015-09-22 12:33:20 -07001727void *grpc_call_context_get(grpc_call *call, grpc_context_index elem) {
Julien Boeuf83b02972015-05-20 22:50:34 -07001728 return call->context[elem].value;
Craig Tiller935cf422015-05-01 14:10:46 -07001729}
Julien Boeuf9f218dd2015-04-23 10:24:02 -07001730
Craig Tiller7536af02015-12-22 13:49:30 -08001731uint8_t grpc_call_is_client(grpc_call *call) { return call->is_client; }
David Garcia Quintas13c2f6e2016-03-17 22:51:52 -07001732
1733grpc_compression_algorithm grpc_call_compression_for_level(
1734 grpc_call *call, grpc_compression_level level) {
David Garcia Quintas7d2c7332016-03-18 11:37:14 -07001735 gpr_mu_lock(&call->mu);
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001736 grpc_compression_algorithm algo =
1737 compression_algorithm_for_level_locked(call, level);
David Garcia Quintas7d2c7332016-03-18 11:37:14 -07001738 gpr_mu_unlock(&call->mu);
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001739 return algo;
David Garcia Quintas13c2f6e2016-03-17 22:51:52 -07001740}
Yuchen Zeng2e7d9572016-04-15 17:29:57 -07001741
1742const char *grpc_call_error_to_string(grpc_call_error error) {
1743 switch (error) {
1744 case GRPC_CALL_ERROR:
1745 return "GRPC_CALL_ERROR";
1746 case GRPC_CALL_ERROR_ALREADY_ACCEPTED:
1747 return "GRPC_CALL_ERROR_ALREADY_ACCEPTED";
1748 case GRPC_CALL_ERROR_ALREADY_FINISHED:
1749 return "GRPC_CALL_ERROR_ALREADY_FINISHED";
1750 case GRPC_CALL_ERROR_ALREADY_INVOKED:
1751 return "GRPC_CALL_ERROR_ALREADY_INVOKED";
1752 case GRPC_CALL_ERROR_BATCH_TOO_BIG:
1753 return "GRPC_CALL_ERROR_BATCH_TOO_BIG";
1754 case GRPC_CALL_ERROR_INVALID_FLAGS:
1755 return "GRPC_CALL_ERROR_INVALID_FLAGS";
1756 case GRPC_CALL_ERROR_INVALID_MESSAGE:
1757 return "GRPC_CALL_ERROR_INVALID_MESSAGE";
1758 case GRPC_CALL_ERROR_INVALID_METADATA:
1759 return "GRPC_CALL_ERROR_INVALID_METADATA";
1760 case GRPC_CALL_ERROR_NOT_INVOKED:
1761 return "GRPC_CALL_ERROR_NOT_INVOKED";
1762 case GRPC_CALL_ERROR_NOT_ON_CLIENT:
1763 return "GRPC_CALL_ERROR_NOT_ON_CLIENT";
1764 case GRPC_CALL_ERROR_NOT_ON_SERVER:
1765 return "GRPC_CALL_ERROR_NOT_ON_SERVER";
1766 case GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE:
1767 return "GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE";
1768 case GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH:
1769 return "GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH";
1770 case GRPC_CALL_ERROR_TOO_MANY_OPERATIONS:
1771 return "GRPC_CALL_ERROR_TOO_MANY_OPERATIONS";
1772 case GRPC_CALL_OK:
1773 return "GRPC_CALL_OK";
Yuchen Zeng2e7d9572016-04-15 17:29:57 -07001774 }
Yuchen Zengf02bada2016-04-19 14:12:27 -07001775 GPR_UNREACHABLE_CODE(return "GRPC_CALL_ERROR_UNKNOW");
Yuchen Zeng2e7d9572016-04-15 17:29:57 -07001776}