Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 | */ |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 33 | |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 34 | #include <assert.h> |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 35 | #include <limits.h> |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include <grpc/compression.h> |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 41 | #include <grpc/grpc.h> |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 42 | #include <grpc/slice.h> |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 43 | #include <grpc/support/alloc.h> |
| 44 | #include <grpc/support/log.h> |
| 45 | #include <grpc/support/string_util.h> |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 46 | #include <grpc/support/useful.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 48 | #include "src/core/lib/channel/channel_stack.h" |
| 49 | #include "src/core/lib/compression/algorithm_metadata.h" |
| 50 | #include "src/core/lib/iomgr/timer.h" |
| 51 | #include "src/core/lib/profiling/timers.h" |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 52 | #include "src/core/lib/slice/slice_internal.h" |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 53 | #include "src/core/lib/slice/slice_string_helpers.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 54 | #include "src/core/lib/support/string.h" |
| 55 | #include "src/core/lib/surface/api_trace.h" |
| 56 | #include "src/core/lib/surface/call.h" |
| 57 | #include "src/core/lib/surface/channel.h" |
| 58 | #include "src/core/lib/surface/completion_queue.h" |
Craig Tiller | f2b5b7e | 2017-01-10 08:28:59 -0800 | [diff] [blame] | 59 | #include "src/core/lib/surface/validate_metadata.h" |
Craig Tiller | 732351f | 2016-12-13 16:40:38 -0800 | [diff] [blame] | 60 | #include "src/core/lib/transport/error_utils.h" |
David Garcia Quintas | 73dcbda | 2016-04-23 00:17:05 -0700 | [diff] [blame] | 61 | #include "src/core/lib/transport/metadata.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 62 | #include "src/core/lib/transport/static_metadata.h" |
David Garcia Quintas | 73dcbda | 2016-04-23 00:17:05 -0700 | [diff] [blame] | 63 | #include "src/core/lib/transport/transport.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 65 | /** The maximum number of concurrent batches possible. |
Craig Tiller | 1b01167 | 2015-07-10 10:41:44 -0700 | [diff] [blame] | 66 | Based upon the maximum number of individually queueable ops in the batch |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 67 | api: |
Craig Tiller | 1b01167 | 2015-07-10 10:41:44 -0700 | [diff] [blame] | 68 | - initial metadata send |
| 69 | - message send |
| 70 | - status/close send (depending on client/server) |
| 71 | - initial metadata recv |
| 72 | - message recv |
| 73 | - status/close recv (depending on client/server) */ |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 74 | #define MAX_CONCURRENT_BATCHES 6 |
Craig Tiller | 1b01167 | 2015-07-10 10:41:44 -0700 | [diff] [blame] | 75 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 76 | #define MAX_SEND_EXTRA_METADATA_COUNT 3 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 78 | /* Status data for a request can come from several sources; this |
| 79 | enumerates them all, and acts as a priority sorting for which |
| 80 | status to return to the application - earlier entries override |
| 81 | later ones */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | typedef enum { |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 83 | /* Status came from the application layer overriding whatever |
| 84 | the wire says */ |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 85 | STATUS_FROM_API_OVERRIDE = 0, |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 86 | /* Status came from 'the wire' - or somewhere below the surface |
| 87 | layer */ |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 88 | STATUS_FROM_WIRE, |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 89 | /* Status was created by some internal channel stack operation: must come via |
| 90 | add_batch_error */ |
Craig Tiller | 2aa03df | 2016-03-16 08:24:55 -0700 | [diff] [blame] | 91 | STATUS_FROM_CORE, |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 92 | /* Status was created by some surface error */ |
| 93 | STATUS_FROM_SURFACE, |
Craig Tiller | aea081f | 2015-06-11 14:19:33 -0700 | [diff] [blame] | 94 | /* Status came from the server sending status */ |
| 95 | STATUS_FROM_SERVER_STATUS, |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 96 | STATUS_SOURCE_COUNT |
| 97 | } status_source; |
| 98 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 99 | typedef struct { |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 100 | bool is_set; |
| 101 | grpc_error *error; |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 102 | } received_status; |
| 103 | |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 104 | static gpr_atm pack_received_status(received_status r) { |
| 105 | return r.is_set ? (1 | (gpr_atm)r.error) : 0; |
| 106 | } |
| 107 | |
| 108 | static received_status unpack_received_status(gpr_atm atm) { |
| 109 | return (atm & 1) == 0 |
| 110 | ? (received_status){.is_set = false, .error = GRPC_ERROR_NONE} |
| 111 | : (received_status){.is_set = true, |
| 112 | .error = (grpc_error *)(atm & ~(gpr_atm)1)}; |
| 113 | } |
| 114 | |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 115 | #define MAX_ERRORS_PER_BATCH 3 |
| 116 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 117 | typedef struct batch_control { |
| 118 | grpc_call *call; |
| 119 | grpc_cq_completion cq_completion; |
| 120 | grpc_closure finish_batch; |
| 121 | void *notify_tag; |
| 122 | gpr_refcount steps_to_complete; |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 123 | |
| 124 | grpc_error *errors[MAX_ERRORS_PER_BATCH]; |
| 125 | gpr_atm num_errors; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 126 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 127 | uint8_t send_initial_metadata; |
| 128 | uint8_t send_message; |
| 129 | uint8_t send_final_op; |
| 130 | uint8_t recv_initial_metadata; |
| 131 | uint8_t recv_message; |
| 132 | uint8_t recv_final_op; |
| 133 | uint8_t is_notify_tag_closure; |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 134 | |
| 135 | /* TODO(ctiller): now that this is inlined, figure out how much of the above |
| 136 | state can be eliminated */ |
| 137 | grpc_transport_stream_op op; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 138 | } batch_control; |
| 139 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 140 | struct grpc_call { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 141 | grpc_completion_queue *cq; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 142 | grpc_polling_entity pollent; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 143 | grpc_channel *channel; |
Craig Tiller | 3e7c6a7 | 2015-07-31 16:17:04 -0700 | [diff] [blame] | 144 | grpc_call *parent; |
Craig Tiller | c7df0df | 2015-08-03 08:06:50 -0700 | [diff] [blame] | 145 | grpc_call *first_child; |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 146 | gpr_timespec start_time; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 147 | /* TODO(ctiller): share with cq if possible? */ |
| 148 | gpr_mu mu; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 149 | |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 150 | /* client or server call */ |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 151 | bool is_client; |
Craig Tiller | f3fba74 | 2015-06-11 09:36:33 -0700 | [diff] [blame] | 152 | /** has grpc_call_destroy been called */ |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 153 | bool destroy_called; |
Craig Tiller | c7df0df | 2015-08-03 08:06:50 -0700 | [diff] [blame] | 154 | /** flag indicating that cancellation is inherited */ |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 155 | bool cancellation_is_inherited; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 156 | /** which ops are in-flight */ |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 157 | bool sent_initial_metadata; |
| 158 | bool sending_message; |
| 159 | bool sent_final_op; |
| 160 | bool received_initial_metadata; |
| 161 | bool receiving_message; |
| 162 | bool requested_final_op; |
| 163 | bool received_final_op; |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 164 | |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 165 | /* have we received initial metadata */ |
| 166 | bool has_initial_md_been_received; |
| 167 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 168 | batch_control active_batches[MAX_CONCURRENT_BATCHES]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 169 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 170 | /* first idx: is_receiving, second idx: is_trailing */ |
| 171 | grpc_metadata_batch metadata_batch[2][2]; |
Craig Tiller | ebf94bf | 2015-02-05 08:48:46 -0800 | [diff] [blame] | 172 | |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 173 | /* Buffered read metadata waiting to be returned to the application. |
| 174 | Element 0 is initial metadata, element 1 is trailing metadata. */ |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 175 | grpc_metadata_array *buffered_metadata[2]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 176 | |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 177 | /* Packed received call statuses from various sources */ |
| 178 | gpr_atm status[STATUS_SOURCE_COUNT]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 179 | |
David Garcia Quintas | 01c4d99 | 2016-07-07 20:11:27 -0700 | [diff] [blame] | 180 | /* Call data useful used for reporting. Only valid after the call has |
| 181 | * completed */ |
| 182 | grpc_call_final_info final_info; |
Craig Tiller | 466129e | 2016-03-09 14:43:18 -0800 | [diff] [blame] | 183 | |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 184 | /* Compression algorithm for *incoming* data */ |
| 185 | grpc_compression_algorithm incoming_compression_algorithm; |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 186 | /* Supported encodings (compression algorithms), a bitset */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 187 | uint32_t encodings_accepted_by_peer; |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 188 | |
Julien Boeuf | c6f8d0a | 2015-05-11 22:40:02 -0700 | [diff] [blame] | 189 | /* Contexts for various subsystems (security, tracing, ...). */ |
Julien Boeuf | 83b0297 | 2015-05-20 22:50:34 -0700 | [diff] [blame] | 190 | grpc_call_context_element context[GRPC_CONTEXT_COUNT]; |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 191 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 192 | /* for the client, extra metadata is initial metadata; for the |
| 193 | server, it's trailing metadata */ |
| 194 | grpc_linked_mdelem send_extra_metadata[MAX_SEND_EXTRA_METADATA_COUNT]; |
| 195 | int send_extra_metadata_count; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 196 | gpr_timespec send_deadline; |
| 197 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 198 | /** siblings: children of the same parent form a list, and this list is |
| 199 | protected under |
Craig Tiller | c7df0df | 2015-08-03 08:06:50 -0700 | [diff] [blame] | 200 | parent->mu */ |
| 201 | grpc_call *sibling_next; |
| 202 | grpc_call *sibling_prev; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 203 | |
| 204 | grpc_slice_buffer_stream sending_stream; |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 205 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 206 | grpc_byte_stream *receiving_stream; |
| 207 | grpc_byte_buffer **receiving_buffer; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 208 | grpc_slice receiving_slice; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 209 | grpc_closure receiving_slice_ready; |
| 210 | grpc_closure receiving_stream_ready; |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 211 | grpc_closure receiving_initial_metadata_ready; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 212 | uint32_t test_only_last_message_flags; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 213 | |
| 214 | union { |
| 215 | struct { |
| 216 | grpc_status_code *status; |
Craig Tiller | 68208fe | 2016-11-14 14:35:02 -0800 | [diff] [blame] | 217 | grpc_slice *status_details; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 218 | } client; |
| 219 | struct { |
| 220 | int *cancelled; |
| 221 | } server; |
| 222 | } final_op; |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 223 | |
Craig Tiller | 8a67780 | 2016-04-22 15:07:53 -0700 | [diff] [blame] | 224 | void *saved_receiving_stream_ready_bctlp; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 225 | }; |
| 226 | |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 227 | int grpc_call_error_trace = 0; |
| 228 | |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 229 | #define CALL_STACK_FROM_CALL(call) ((grpc_call_stack *)((call) + 1)) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 230 | #define CALL_FROM_CALL_STACK(call_stack) (((grpc_call *)(call_stack)) - 1) |
| 231 | #define CALL_ELEM_FROM_CALL(call, idx) \ |
| 232 | grpc_call_stack_element(CALL_STACK_FROM_CALL(call), idx) |
| 233 | #define CALL_FROM_TOP_ELEM(top_elem) \ |
| 234 | CALL_FROM_CALL_STACK(grpc_call_stack_from_top_element(top_elem)) |
| 235 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 236 | static void execute_op(grpc_exec_ctx *exec_ctx, grpc_call *call, |
| 237 | grpc_transport_stream_op *op); |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 238 | static void cancel_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c, |
| 239 | status_source source, grpc_status_code status, |
| 240 | const char *description); |
Craig Tiller | 255edaa | 2016-12-13 09:04:55 -0800 | [diff] [blame] | 241 | static void cancel_with_error(grpc_exec_ctx *exec_ctx, grpc_call *c, |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 242 | status_source source, grpc_error *error); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 243 | static void destroy_call(grpc_exec_ctx *exec_ctx, void *call_stack, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 244 | grpc_error *error); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 245 | static void receiving_slice_ready(grpc_exec_ctx *exec_ctx, void *bctlp, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 246 | grpc_error *error); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 247 | static void get_final_status(grpc_call *call, |
| 248 | void (*set_value)(grpc_status_code code, |
| 249 | void *user_data), |
| 250 | void *set_value_user_data, grpc_slice *details); |
| 251 | static void set_status_value_directly(grpc_status_code status, void *dest); |
| 252 | static void set_status_from_error(grpc_exec_ctx *exec_ctx, grpc_call *call, |
| 253 | status_source source, grpc_error *error); |
| 254 | static void process_data_after_md(grpc_exec_ctx *exec_ctx, batch_control *bctl); |
| 255 | static void post_batch_completion(grpc_exec_ctx *exec_ctx, batch_control *bctl); |
| 256 | static void add_batch_error(grpc_exec_ctx *exec_ctx, batch_control *bctl, |
| 257 | grpc_error *error); |
Craig Tiller | bac4142 | 2015-05-29 16:32:28 -0700 | [diff] [blame] | 258 | |
Craig Tiller | f4484cd | 2017-02-01 08:28:40 -0800 | [diff] [blame] | 259 | static void add_init_error(grpc_error **composite, grpc_error *new) { |
| 260 | if (new == GRPC_ERROR_NONE) return; |
| 261 | if (*composite == GRPC_ERROR_NONE) |
| 262 | *composite = GRPC_ERROR_CREATE("Call creation failed"); |
| 263 | *composite = grpc_error_add_child(*composite, new); |
| 264 | } |
| 265 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 266 | grpc_error *grpc_call_create(grpc_exec_ctx *exec_ctx, |
| 267 | const grpc_call_create_args *args, |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 268 | grpc_call **out_call) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 269 | size_t i, j; |
Craig Tiller | f4484cd | 2017-02-01 08:28:40 -0800 | [diff] [blame] | 270 | grpc_error *error = GRPC_ERROR_NONE; |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 271 | grpc_channel_stack *channel_stack = |
| 272 | grpc_channel_get_channel_stack(args->channel); |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 273 | grpc_call *call; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 274 | GPR_TIMER_BEGIN("grpc_call_create", 0); |
Craig Tiller | 0eaed72 | 2016-09-21 10:44:18 -0700 | [diff] [blame] | 275 | call = gpr_malloc(sizeof(grpc_call) + channel_stack->call_stack_size); |
| 276 | *out_call = call; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 277 | memset(call, 0, sizeof(grpc_call)); |
| 278 | gpr_mu_init(&call->mu); |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 279 | call->channel = args->channel; |
| 280 | call->cq = args->cq; |
| 281 | call->parent = args->parent_call; |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 282 | call->start_time = gpr_now(GPR_CLOCK_MONOTONIC); |
David Garcia Quintas | 4612337 | 2016-05-09 15:28:42 -0700 | [diff] [blame] | 283 | /* Always support no compression */ |
| 284 | GPR_BITSET(&call->encodings_accepted_by_peer, GRPC_COMPRESS_NONE); |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 285 | call->is_client = args->server_transport_data == NULL; |
Craig Tiller | 4eecdde | 2016-11-14 08:21:17 -0800 | [diff] [blame] | 286 | grpc_slice path = grpc_empty_slice(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 287 | if (call->is_client) { |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 288 | GPR_ASSERT(args->add_initial_metadata_count < |
| 289 | MAX_SEND_EXTRA_METADATA_COUNT); |
| 290 | for (i = 0; i < args->add_initial_metadata_count; i++) { |
| 291 | call->send_extra_metadata[i].md = args->add_initial_metadata[i]; |
Craig Tiller | 3b05e1d | 2016-11-21 13:46:31 -0800 | [diff] [blame] | 292 | if (grpc_slice_eq(GRPC_MDKEY(args->add_initial_metadata[i]), |
| 293 | GRPC_MDSTR_PATH)) { |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 294 | path = grpc_slice_ref_internal( |
| 295 | GRPC_MDVALUE(args->add_initial_metadata[i])); |
Mark D. Roth | aa850a7 | 2016-09-26 13:38:02 -0700 | [diff] [blame] | 296 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 297 | } |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 298 | call->send_extra_metadata_count = (int)args->add_initial_metadata_count; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 299 | } else { |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 300 | GPR_ASSERT(args->add_initial_metadata_count == 0); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 301 | call->send_extra_metadata_count = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 302 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 303 | for (i = 0; i < 2; i++) { |
| 304 | for (j = 0; j < 2; j++) { |
| 305 | call->metadata_batch[i][j].deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC); |
| 306 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 307 | } |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 308 | gpr_timespec send_deadline = |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 309 | gpr_convert_clock_type(args->send_deadline, GPR_CLOCK_MONOTONIC); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 310 | |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 311 | if (args->parent_call != NULL) { |
| 312 | GRPC_CALL_INTERNAL_REF(args->parent_call, "child"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 313 | GPR_ASSERT(call->is_client); |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 314 | GPR_ASSERT(!args->parent_call->is_client); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 315 | |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 316 | gpr_mu_lock(&args->parent_call->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 317 | |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 318 | if (args->propagation_mask & GRPC_PROPAGATE_DEADLINE) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 319 | send_deadline = gpr_time_min( |
| 320 | gpr_convert_clock_type(send_deadline, |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 321 | args->parent_call->send_deadline.clock_type), |
| 322 | args->parent_call->send_deadline); |
Craig Tiller | c7df0df | 2015-08-03 08:06:50 -0700 | [diff] [blame] | 323 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 324 | /* for now GRPC_PROPAGATE_TRACING_CONTEXT *MUST* be passed with |
| 325 | * GRPC_PROPAGATE_STATS_CONTEXT */ |
| 326 | /* TODO(ctiller): This should change to use the appropriate census start_op |
| 327 | * call. */ |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 328 | if (args->propagation_mask & GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT) { |
Craig Tiller | 239af8b | 2017-02-01 10:21:42 -0800 | [diff] [blame] | 329 | if (0 == (args->propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT)) { |
| 330 | add_init_error(&error, |
| 331 | GRPC_ERROR_CREATE("Census tracing propagation requested " |
| 332 | "without Census context propagation")); |
| 333 | } |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 334 | grpc_call_context_set( |
| 335 | call, GRPC_CONTEXT_TRACING, |
| 336 | args->parent_call->context[GRPC_CONTEXT_TRACING].value, NULL); |
Craig Tiller | f20d307 | 2017-02-01 10:39:26 -0800 | [diff] [blame] | 337 | } else if (args->propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT) { |
Craig Tiller | f4484cd | 2017-02-01 08:28:40 -0800 | [diff] [blame] | 338 | add_init_error(&error, |
Craig Tiller | 239af8b | 2017-02-01 10:21:42 -0800 | [diff] [blame] | 339 | GRPC_ERROR_CREATE("Census context propagation requested " |
| 340 | "without Census tracing propagation")); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 341 | } |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 342 | if (args->propagation_mask & GRPC_PROPAGATE_CANCELLATION) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 343 | call->cancellation_is_inherited = 1; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 344 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 345 | |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 346 | if (args->parent_call->first_child == NULL) { |
| 347 | args->parent_call->first_child = call; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 348 | call->sibling_next = call->sibling_prev = call; |
| 349 | } else { |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 350 | call->sibling_next = args->parent_call->first_child; |
| 351 | call->sibling_prev = args->parent_call->first_child->sibling_prev; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 352 | call->sibling_next->sibling_prev = call->sibling_prev->sibling_next = |
| 353 | call; |
| 354 | } |
| 355 | |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 356 | gpr_mu_unlock(&args->parent_call->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 357 | } |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 358 | |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 359 | call->send_deadline = send_deadline; |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 360 | |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 361 | GRPC_CHANNEL_INTERNAL_REF(args->channel, "call"); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 362 | /* initial refcount dropped by grpc_call_destroy */ |
Craig Tiller | f4484cd | 2017-02-01 08:28:40 -0800 | [diff] [blame] | 363 | add_init_error(&error, grpc_call_stack_init(exec_ctx, channel_stack, 1, |
| 364 | destroy_call, call, call->context, |
| 365 | args->server_transport_data, path, |
| 366 | call->start_time, send_deadline, |
| 367 | CALL_STACK_FROM_CALL(call))); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 368 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 369 | cancel_with_error(exec_ctx, call, STATUS_FROM_SURFACE, |
| 370 | GRPC_ERROR_REF(error)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 371 | } |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 372 | if (args->cq != NULL) { |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 373 | GPR_ASSERT( |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 374 | args->pollset_set_alternative == NULL && |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 375 | "Only one of 'cq' and 'pollset_set_alternative' should be non-NULL."); |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 376 | GRPC_CQ_INTERNAL_REF(args->cq, "bind"); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 377 | call->pollent = |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 378 | grpc_polling_entity_create_from_pollset(grpc_cq_pollset(args->cq)); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 379 | } |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 380 | if (args->pollset_set_alternative != NULL) { |
| 381 | call->pollent = grpc_polling_entity_create_from_pollset_set( |
| 382 | args->pollset_set_alternative); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 383 | } |
| 384 | if (!grpc_polling_entity_is_empty(&call->pollent)) { |
| 385 | grpc_call_stack_set_pollset_or_pollset_set( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 386 | exec_ctx, CALL_STACK_FROM_CALL(call), &call->pollent); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Craig Tiller | 4eecdde | 2016-11-14 08:21:17 -0800 | [diff] [blame] | 389 | grpc_slice_unref_internal(exec_ctx, path); |
Mark D. Roth | aa850a7 | 2016-09-26 13:38:02 -0700 | [diff] [blame] | 390 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 391 | GPR_TIMER_END("grpc_call_create", 0); |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 392 | return error; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 395 | void grpc_call_set_completion_queue(grpc_exec_ctx *exec_ctx, grpc_call *call, |
| 396 | grpc_completion_queue *cq) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 397 | GPR_ASSERT(cq); |
David Garcia Quintas | f72eb97 | 2016-05-03 18:28:09 -0700 | [diff] [blame] | 398 | |
David Garcia Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 399 | if (grpc_polling_entity_pollset_set(&call->pollent) != NULL) { |
David Garcia Quintas | f72eb97 | 2016-05-03 18:28:09 -0700 | [diff] [blame] | 400 | gpr_log(GPR_ERROR, "A pollset_set is already registered for this call."); |
| 401 | abort(); |
| 402 | } |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 403 | call->cq = cq; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 404 | GRPC_CQ_INTERNAL_REF(cq, "bind"); |
David Garcia Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 405 | call->pollent = grpc_polling_entity_create_from_pollset(grpc_cq_pollset(cq)); |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 406 | grpc_call_stack_set_pollset_or_pollset_set( |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 407 | exec_ctx, CALL_STACK_FROM_CALL(call), &call->pollent); |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 410 | #ifdef GRPC_STREAM_REFCOUNT_DEBUG |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 411 | #define REF_REASON reason |
| 412 | #define REF_ARG , const char *reason |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 413 | #else |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 414 | #define REF_REASON "" |
| 415 | #define REF_ARG |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 416 | #endif |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 417 | void grpc_call_internal_ref(grpc_call *c REF_ARG) { |
| 418 | GRPC_CALL_STACK_REF(CALL_STACK_FROM_CALL(c), REF_REASON); |
| 419 | } |
| 420 | void grpc_call_internal_unref(grpc_exec_ctx *exec_ctx, grpc_call *c REF_ARG) { |
| 421 | GRPC_CALL_STACK_UNREF(exec_ctx, CALL_STACK_FROM_CALL(c), REF_REASON); |
| 422 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 423 | |
David Garcia Quintas | 01c4d99 | 2016-07-07 20:11:27 -0700 | [diff] [blame] | 424 | static void set_status_value_directly(grpc_status_code status, void *dest); |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 425 | static void destroy_call(grpc_exec_ctx *exec_ctx, void *call, |
| 426 | grpc_error *error) { |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 427 | size_t i; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 428 | int ii; |
Craig Tiller | aef25da | 2015-01-29 17:19:45 -0800 | [diff] [blame] | 429 | grpc_call *c = call; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 430 | GPR_TIMER_BEGIN("destroy_call", 0); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 431 | for (i = 0; i < 2; i++) { |
| 432 | grpc_metadata_batch_destroy( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 433 | exec_ctx, &c->metadata_batch[1 /* is_receiving */][i /* is_initial */]); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 434 | } |
| 435 | if (c->receiving_stream != NULL) { |
Craig Tiller | 3b66ab9 | 2015-12-09 19:42:22 -0800 | [diff] [blame] | 436 | grpc_byte_stream_destroy(exec_ctx, c->receiving_stream); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 437 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 438 | gpr_mu_destroy(&c->mu); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 439 | for (ii = 0; ii < c->send_extra_metadata_count; ii++) { |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 440 | GRPC_MDELEM_UNREF(exec_ctx, c->send_extra_metadata[ii].md); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 441 | } |
| 442 | for (i = 0; i < GRPC_CONTEXT_COUNT; i++) { |
| 443 | if (c->context[i].destroy) { |
| 444 | c->context[i].destroy(c->context[i].value); |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 445 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 446 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 447 | if (c->cq) { |
| 448 | GRPC_CQ_INTERNAL_UNREF(c->cq, "bind"); |
| 449 | } |
Craig Tiller | 9859d8d | 2016-04-26 21:07:53 -0700 | [diff] [blame] | 450 | grpc_channel *channel = c->channel; |
David Garcia Quintas | 01c4d99 | 2016-07-07 20:11:27 -0700 | [diff] [blame] | 451 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 452 | get_final_status(call, set_status_value_directly, &c->final_info.final_status, |
| 453 | NULL); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 454 | c->final_info.stats.latency = |
| 455 | gpr_time_sub(gpr_now(GPR_CLOCK_MONOTONIC), c->start_time); |
David Garcia Quintas | 01c4d99 | 2016-07-07 20:11:27 -0700 | [diff] [blame] | 456 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 457 | for (i = 0; i < STATUS_SOURCE_COUNT; i++) { |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 458 | GRPC_ERROR_UNREF( |
| 459 | unpack_received_status(gpr_atm_no_barrier_load(&c->status[i])).error); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 460 | } |
| 461 | |
David Garcia Quintas | 01c4d99 | 2016-07-07 20:11:27 -0700 | [diff] [blame] | 462 | grpc_call_stack_destroy(exec_ctx, CALL_STACK_FROM_CALL(c), &c->final_info, c); |
Craig Tiller | 9859d8d | 2016-04-26 21:07:53 -0700 | [diff] [blame] | 463 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, channel, "call"); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 464 | GPR_TIMER_END("destroy_call", 0); |
Craig Tiller | a454110 | 2015-01-29 11:46:11 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 467 | void grpc_call_destroy(grpc_call *c) { |
| 468 | int cancel; |
| 469 | grpc_call *parent = c->parent; |
| 470 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | b8d3a31 | 2015-06-19 17:27:53 -0700 | [diff] [blame] | 471 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 472 | GPR_TIMER_BEGIN("grpc_call_destroy", 0); |
| 473 | GRPC_API_TRACE("grpc_call_destroy(c=%p)", 1, (c)); |
| 474 | |
| 475 | if (parent) { |
| 476 | gpr_mu_lock(&parent->mu); |
| 477 | if (c == parent->first_child) { |
| 478 | parent->first_child = c->sibling_next; |
| 479 | if (c == parent->first_child) { |
| 480 | parent->first_child = NULL; |
| 481 | } |
| 482 | c->sibling_prev->sibling_next = c->sibling_next; |
| 483 | c->sibling_next->sibling_prev = c->sibling_prev; |
| 484 | } |
| 485 | gpr_mu_unlock(&parent->mu); |
| 486 | GRPC_CALL_INTERNAL_UNREF(&exec_ctx, parent, "child"); |
| 487 | } |
| 488 | |
| 489 | gpr_mu_lock(&c->mu); |
| 490 | GPR_ASSERT(!c->destroy_called); |
| 491 | c->destroy_called = 1; |
| 492 | cancel = !c->received_final_op; |
| 493 | gpr_mu_unlock(&c->mu); |
Craig Tiller | 37cbc3f | 2017-02-16 14:54:55 -0800 | [diff] [blame] | 494 | if (cancel) { |
| 495 | cancel_with_error(&exec_ctx, c, STATUS_FROM_API_OVERRIDE, |
| 496 | GRPC_ERROR_CANCELLED); |
| 497 | } |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 498 | GRPC_CALL_INTERNAL_UNREF(&exec_ctx, c, "destroy"); |
| 499 | grpc_exec_ctx_finish(&exec_ctx); |
| 500 | GPR_TIMER_END("grpc_call_destroy", 0); |
Craig Tiller | f0f70a8 | 2016-06-23 13:55:06 -0700 | [diff] [blame] | 501 | } |
Craig Tiller | 3054756 | 2015-02-05 17:04:51 -0800 | [diff] [blame] | 502 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 503 | grpc_call_error grpc_call_cancel(grpc_call *call, void *reserved) { |
| 504 | GRPC_API_TRACE("grpc_call_cancel(call=%p, reserved=%p)", 2, (call, reserved)); |
| 505 | GPR_ASSERT(!reserved); |
Craig Tiller | 37cbc3f | 2017-02-16 14:54:55 -0800 | [diff] [blame] | 506 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 507 | cancel_with_error(&exec_ctx, call, STATUS_FROM_API_OVERRIDE, |
| 508 | GRPC_ERROR_CANCELLED); |
| 509 | grpc_exec_ctx_finish(&exec_ctx); |
| 510 | return GRPC_CALL_OK; |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static void execute_op(grpc_exec_ctx *exec_ctx, grpc_call *call, |
| 514 | grpc_transport_stream_op *op) { |
| 515 | grpc_call_element *elem; |
| 516 | |
| 517 | GPR_TIMER_BEGIN("execute_op", 0); |
| 518 | elem = CALL_ELEM_FROM_CALL(call, 0); |
| 519 | op->context = call->context; |
| 520 | elem->filter->start_transport_stream_op(exec_ctx, elem, op); |
| 521 | GPR_TIMER_END("execute_op", 0); |
| 522 | } |
| 523 | |
| 524 | char *grpc_call_get_peer(grpc_call *call) { |
| 525 | grpc_call_element *elem = CALL_ELEM_FROM_CALL(call, 0); |
| 526 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 527 | char *result; |
| 528 | GRPC_API_TRACE("grpc_call_get_peer(%p)", 1, (call)); |
| 529 | result = elem->filter->get_peer(&exec_ctx, elem); |
| 530 | if (result == NULL) { |
| 531 | result = grpc_channel_get_target(call->channel); |
| 532 | } |
| 533 | if (result == NULL) { |
| 534 | result = gpr_strdup("unknown"); |
| 535 | } |
| 536 | grpc_exec_ctx_finish(&exec_ctx); |
| 537 | return result; |
| 538 | } |
| 539 | |
| 540 | grpc_call *grpc_call_from_top_element(grpc_call_element *elem) { |
| 541 | return CALL_FROM_TOP_ELEM(elem); |
| 542 | } |
| 543 | |
| 544 | /******************************************************************************* |
| 545 | * CANCELLATION |
| 546 | */ |
| 547 | |
| 548 | grpc_call_error grpc_call_cancel_with_status(grpc_call *c, |
| 549 | grpc_status_code status, |
| 550 | const char *description, |
| 551 | void *reserved) { |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 552 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 553 | GRPC_API_TRACE( |
| 554 | "grpc_call_cancel_with_status(" |
| 555 | "c=%p, status=%d, description=%s, reserved=%p)", |
| 556 | 4, (c, (int)status, description, reserved)); |
| 557 | GPR_ASSERT(reserved == NULL); |
| 558 | gpr_mu_lock(&c->mu); |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 559 | cancel_with_status(&exec_ctx, c, STATUS_FROM_API_OVERRIDE, status, |
| 560 | description); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 561 | gpr_mu_unlock(&c->mu); |
| 562 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 563 | return GRPC_CALL_OK; |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 566 | typedef struct termination_closure { |
| 567 | grpc_closure closure; |
| 568 | grpc_call *call; |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 569 | grpc_transport_stream_op op; |
| 570 | } termination_closure; |
| 571 | |
| 572 | static void done_termination(grpc_exec_ctx *exec_ctx, void *tcp, |
| 573 | grpc_error *error) { |
| 574 | termination_closure *tc = tcp; |
| 575 | GRPC_CALL_INTERNAL_UNREF(exec_ctx, tc->call, "termination"); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 576 | gpr_free(tc); |
| 577 | } |
| 578 | |
| 579 | static void send_termination(grpc_exec_ctx *exec_ctx, void *tcp, |
| 580 | grpc_error *error) { |
| 581 | termination_closure *tc = tcp; |
| 582 | memset(&tc->op, 0, sizeof(tc->op)); |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 583 | tc->op.cancel_error = GRPC_ERROR_REF(error); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 584 | /* reuse closure to catch completion */ |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 585 | tc->op.on_complete = grpc_closure_init(&tc->closure, done_termination, tc, |
| 586 | grpc_schedule_on_exec_ctx); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 587 | execute_op(exec_ctx, tc->call, &tc->op); |
| 588 | } |
| 589 | |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 590 | static void terminate_with_error(grpc_exec_ctx *exec_ctx, grpc_call *c, |
| 591 | grpc_error *error) { |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 592 | termination_closure *tc = gpr_malloc(sizeof(*tc)); |
| 593 | memset(tc, 0, sizeof(*tc)); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 594 | tc->call = c; |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 595 | GRPC_CALL_INTERNAL_REF(tc->call, "termination"); |
| 596 | grpc_closure_sched(exec_ctx, grpc_closure_init(&tc->closure, send_termination, |
| 597 | tc, grpc_schedule_on_exec_ctx), |
| 598 | error); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 599 | } |
| 600 | |
Craig Tiller | 255edaa | 2016-12-13 09:04:55 -0800 | [diff] [blame] | 601 | static void cancel_with_error(grpc_exec_ctx *exec_ctx, grpc_call *c, |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 602 | status_source source, grpc_error *error) { |
| 603 | set_status_from_error(exec_ctx, c, source, GRPC_ERROR_REF(error)); |
Craig Tiller | 255edaa | 2016-12-13 09:04:55 -0800 | [diff] [blame] | 604 | terminate_with_error(exec_ctx, c, error); |
| 605 | } |
| 606 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 607 | static grpc_error *error_from_status(grpc_status_code status, |
| 608 | const char *description) { |
| 609 | return grpc_error_set_int( |
| 610 | grpc_error_set_str(GRPC_ERROR_CREATE(description), |
| 611 | GRPC_ERROR_STR_GRPC_MESSAGE, description), |
| 612 | GRPC_ERROR_INT_GRPC_STATUS, status); |
| 613 | } |
| 614 | |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 615 | static void cancel_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c, |
| 616 | status_source source, grpc_status_code status, |
| 617 | const char *description) { |
| 618 | cancel_with_error(exec_ctx, c, source, |
| 619 | error_from_status(status, description)); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /******************************************************************************* |
| 623 | * FINAL STATUS CODE MANIPULATION |
| 624 | */ |
| 625 | |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 626 | static bool get_final_status_from( |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 627 | grpc_call *call, grpc_error *error, bool allow_ok_status, |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 628 | void (*set_value)(grpc_status_code code, void *user_data), |
| 629 | void *set_value_user_data, grpc_slice *details) { |
Craig Tiller | 737b625 | 2017-01-09 15:25:15 -0800 | [diff] [blame] | 630 | grpc_status_code code; |
| 631 | const char *msg = NULL; |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 632 | grpc_error_get_status(error, call->send_deadline, &code, &msg, NULL); |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 633 | if (code == GRPC_STATUS_OK && !allow_ok_status) { |
| 634 | return false; |
| 635 | } |
Craig Tiller | 737b625 | 2017-01-09 15:25:15 -0800 | [diff] [blame] | 636 | |
| 637 | set_value(code, set_value_user_data); |
| 638 | if (details != NULL) { |
Craig Tiller | 8f24d6a | 2017-01-10 06:36:46 -0800 | [diff] [blame] | 639 | *details = |
| 640 | msg == NULL ? grpc_empty_slice() : grpc_slice_from_copied_string(msg); |
Craig Tiller | 737b625 | 2017-01-09 15:25:15 -0800 | [diff] [blame] | 641 | } |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 642 | return true; |
Craig Tiller | 737b625 | 2017-01-09 15:25:15 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 645 | static void get_final_status(grpc_call *call, |
| 646 | void (*set_value)(grpc_status_code code, |
| 647 | void *user_data), |
| 648 | void *set_value_user_data, grpc_slice *details) { |
| 649 | int i; |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 650 | received_status status[STATUS_SOURCE_COUNT]; |
| 651 | for (i = 0; i < STATUS_SOURCE_COUNT; i++) { |
| 652 | status[i] = unpack_received_status(gpr_atm_acq_load(&call->status[i])); |
| 653 | } |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 654 | if (grpc_call_error_trace) { |
| 655 | gpr_log(GPR_DEBUG, "get_final_status %s", call->is_client ? "CLI" : "SVR"); |
| 656 | for (i = 0; i < STATUS_SOURCE_COUNT; i++) { |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 657 | if (status[i].is_set) { |
| 658 | gpr_log(GPR_DEBUG, " %d: %s", i, grpc_error_string(status[i].error)); |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 659 | } |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 660 | } |
| 661 | } |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 662 | /* first search through ignoring "OK" statuses: if something went wrong, |
| 663 | * ensure we report it */ |
| 664 | for (int allow_ok_status = 0; allow_ok_status < 2; allow_ok_status++) { |
| 665 | /* search for the best status we can present: ideally the error we use has a |
| 666 | clearly defined grpc-status, and we'll prefer that. */ |
| 667 | for (i = 0; i < STATUS_SOURCE_COUNT; i++) { |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 668 | if (status[i].is_set && |
| 669 | grpc_error_has_clear_grpc_status(status[i].error)) { |
| 670 | if (get_final_status_from(call, status[i].error, allow_ok_status != 0, |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 671 | set_value, set_value_user_data, details)) { |
| 672 | return; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | /* If no clearly defined status exists, search for 'anything' */ |
| 677 | for (i = 0; i < STATUS_SOURCE_COUNT; i++) { |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 678 | if (status[i].is_set) { |
| 679 | if (get_final_status_from(call, status[i].error, allow_ok_status != 0, |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 680 | set_value, set_value_user_data, details)) { |
| 681 | return; |
| 682 | } |
| 683 | } |
Craig Tiller | 737b625 | 2017-01-09 15:25:15 -0800 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | /* If nothing exists, set some default */ |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 687 | if (call->is_client) { |
| 688 | set_value(GRPC_STATUS_UNKNOWN, set_value_user_data); |
Craig Tiller | be1b9a7 | 2016-06-24 13:22:11 -0700 | [diff] [blame] | 689 | } else { |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 690 | set_value(GRPC_STATUS_OK, set_value_user_data); |
Craig Tiller | f0f70a8 | 2016-06-23 13:55:06 -0700 | [diff] [blame] | 691 | } |
Craig Tiller | f0f70a8 | 2016-06-23 13:55:06 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 694 | static void set_status_from_error(grpc_exec_ctx *exec_ctx, grpc_call *call, |
| 695 | status_source source, grpc_error *error) { |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 696 | if (!gpr_atm_rel_cas(&call->status[source], |
| 697 | pack_received_status((received_status){ |
| 698 | .is_set = false, .error = GRPC_ERROR_NONE}), |
| 699 | pack_received_status((received_status){ |
| 700 | .is_set = true, .error = error}))) { |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 701 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 702 | } |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 703 | } |
| 704 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 705 | /******************************************************************************* |
| 706 | * COMPRESSION |
| 707 | */ |
| 708 | |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 709 | static void set_incoming_compression_algorithm( |
| 710 | grpc_call *call, grpc_compression_algorithm algo) { |
David Garcia Quintas | 303d308 | 2016-05-05 18:25:34 -0700 | [diff] [blame] | 711 | GPR_ASSERT(algo < GRPC_COMPRESS_ALGORITHMS_COUNT); |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 712 | call->incoming_compression_algorithm = algo; |
David Garcia Quintas | db94b27 | 2015-06-15 18:37:01 -0700 | [diff] [blame] | 713 | } |
| 714 | |
David Garcia Quintas | 0c33188 | 2015-10-08 14:51:54 -0700 | [diff] [blame] | 715 | grpc_compression_algorithm grpc_call_test_only_get_compression_algorithm( |
David Garcia Quintas | 64824be | 2015-10-06 19:45:36 -0700 | [diff] [blame] | 716 | grpc_call *call) { |
| 717 | grpc_compression_algorithm algorithm; |
| 718 | gpr_mu_lock(&call->mu); |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 719 | algorithm = call->incoming_compression_algorithm; |
David Garcia Quintas | 64824be | 2015-10-06 19:45:36 -0700 | [diff] [blame] | 720 | gpr_mu_unlock(&call->mu); |
| 721 | return algorithm; |
David Garcia Quintas | 7c0d914 | 2015-07-23 04:58:20 -0700 | [diff] [blame] | 722 | } |
| 723 | |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 724 | static grpc_compression_algorithm compression_algorithm_for_level_locked( |
| 725 | grpc_call *call, grpc_compression_level level) { |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 726 | return grpc_compression_algorithm_for_level(level, |
| 727 | call->encodings_accepted_by_peer); |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 730 | uint32_t grpc_call_test_only_get_message_flags(grpc_call *call) { |
| 731 | uint32_t flags; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 732 | gpr_mu_lock(&call->mu); |
| 733 | flags = call->test_only_last_message_flags; |
| 734 | gpr_mu_unlock(&call->mu); |
| 735 | return flags; |
| 736 | } |
| 737 | |
Craig Tiller | 3ff2754 | 2015-10-09 15:39:44 -0700 | [diff] [blame] | 738 | static void destroy_encodings_accepted_by_peer(void *p) { return; } |
| 739 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 740 | static void set_encodings_accepted_by_peer(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 741 | grpc_call *call, grpc_mdelem mdel) { |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 742 | size_t i; |
| 743 | grpc_compression_algorithm algorithm; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 744 | grpc_slice_buffer accept_encoding_parts; |
| 745 | grpc_slice accept_encoding_slice; |
Craig Tiller | 3ff2754 | 2015-10-09 15:39:44 -0700 | [diff] [blame] | 746 | void *accepted_user_data; |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 747 | |
Craig Tiller | 3ff2754 | 2015-10-09 15:39:44 -0700 | [diff] [blame] | 748 | accepted_user_data = |
| 749 | grpc_mdelem_get_user_data(mdel, destroy_encodings_accepted_by_peer); |
| 750 | if (accepted_user_data != NULL) { |
| 751 | call->encodings_accepted_by_peer = |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 752 | (uint32_t)(((uintptr_t)accepted_user_data) - 1); |
Craig Tiller | 3ff2754 | 2015-10-09 15:39:44 -0700 | [diff] [blame] | 753 | return; |
| 754 | } |
| 755 | |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 756 | accept_encoding_slice = GRPC_MDVALUE(mdel); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 757 | grpc_slice_buffer_init(&accept_encoding_parts); |
| 758 | grpc_slice_split(accept_encoding_slice, ",", &accept_encoding_parts); |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 759 | |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 760 | /* No need to zero call->encodings_accepted_by_peer: grpc_call_create already |
| 761 | * zeroes the whole grpc_call */ |
David Garcia Quintas | b1866bd | 2015-07-08 22:37:01 -0700 | [diff] [blame] | 762 | /* Always support no compression */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 763 | GPR_BITSET(&call->encodings_accepted_by_peer, GRPC_COMPRESS_NONE); |
| 764 | for (i = 0; i < accept_encoding_parts.count; i++) { |
Craig Tiller | 68208fe | 2016-11-14 14:35:02 -0800 | [diff] [blame] | 765 | grpc_slice accept_encoding_entry_slice = accept_encoding_parts.slices[i]; |
| 766 | if (grpc_compression_algorithm_parse(accept_encoding_entry_slice, |
| 767 | &algorithm)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 768 | GPR_BITSET(&call->encodings_accepted_by_peer, algorithm); |
| 769 | } else { |
| 770 | char *accept_encoding_entry_str = |
Craig Tiller | b4aa70e | 2016-12-09 09:40:11 -0800 | [diff] [blame] | 771 | grpc_slice_to_c_string(accept_encoding_entry_slice); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 772 | gpr_log(GPR_ERROR, |
| 773 | "Invalid entry in accept encoding metadata: '%s'. Ignoring.", |
| 774 | accept_encoding_entry_str); |
| 775 | gpr_free(accept_encoding_entry_str); |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 776 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 777 | } |
Craig Tiller | 3ff2754 | 2015-10-09 15:39:44 -0700 | [diff] [blame] | 778 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 779 | grpc_slice_buffer_destroy_internal(exec_ctx, &accept_encoding_parts); |
Craig Tiller | 3ff2754 | 2015-10-09 15:39:44 -0700 | [diff] [blame] | 780 | |
| 781 | grpc_mdelem_set_user_data( |
| 782 | mdel, destroy_encodings_accepted_by_peer, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 783 | (void *)(((uintptr_t)call->encodings_accepted_by_peer) + 1)); |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 786 | uint32_t grpc_call_test_only_get_encodings_accepted_by_peer(grpc_call *call) { |
| 787 | uint32_t encodings_accepted_by_peer; |
David Garcia Quintas | 0c33188 | 2015-10-08 14:51:54 -0700 | [diff] [blame] | 788 | gpr_mu_lock(&call->mu); |
| 789 | encodings_accepted_by_peer = call->encodings_accepted_by_peer; |
| 790 | gpr_mu_unlock(&call->mu); |
| 791 | return encodings_accepted_by_peer; |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 792 | } |
| 793 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 794 | static grpc_linked_mdelem *linked_from_md(grpc_metadata *md) { |
| 795 | return (grpc_linked_mdelem *)&md->internal_data; |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 796 | } |
| 797 | |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 798 | static grpc_metadata *get_md_elem(grpc_metadata *metadata, |
| 799 | grpc_metadata *additional_metadata, int i, |
| 800 | int count) { |
| 801 | grpc_metadata *res = |
| 802 | i < count ? &metadata[i] : &additional_metadata[i - count]; |
| 803 | GPR_ASSERT(res); |
| 804 | return res; |
| 805 | } |
| 806 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 807 | static int prepare_application_metadata( |
| 808 | grpc_exec_ctx *exec_ctx, grpc_call *call, int count, |
| 809 | grpc_metadata *metadata, int is_trailing, int prepend_extra_metadata, |
| 810 | grpc_metadata *additional_metadata, int additional_metadata_count) { |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 811 | int total_count = count + additional_metadata_count; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 812 | int i; |
| 813 | grpc_metadata_batch *batch = |
| 814 | &call->metadata_batch[0 /* is_receiving */][is_trailing]; |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 815 | for (i = 0; i < total_count; i++) { |
| 816 | const grpc_metadata *md = |
| 817 | get_md_elem(metadata, additional_metadata, i, count); |
Craig Tiller | b42445c | 2016-04-22 13:11:44 -0700 | [diff] [blame] | 818 | grpc_linked_mdelem *l = (grpc_linked_mdelem *)&md->internal_data; |
| 819 | GPR_ASSERT(sizeof(grpc_linked_mdelem) == sizeof(md->internal_data)); |
Craig Tiller | f2b5b7e | 2017-01-10 08:28:59 -0800 | [diff] [blame] | 820 | if (!GRPC_LOG_IF_ERROR("validate_metadata", |
| 821 | grpc_validate_header_key_is_legal(md->key))) { |
Craig Tiller | b42445c | 2016-04-22 13:11:44 -0700 | [diff] [blame] | 822 | break; |
Craig Tiller | df2d922 | 2016-11-18 16:38:57 -0800 | [diff] [blame] | 823 | } else if (!grpc_is_binary_header(md->key) && |
Craig Tiller | f2b5b7e | 2017-01-10 08:28:59 -0800 | [diff] [blame] | 824 | !GRPC_LOG_IF_ERROR( |
| 825 | "validate_metadata", |
| 826 | grpc_validate_header_nonbin_value_is_legal(md->value))) { |
Craig Tiller | b42445c | 2016-04-22 13:11:44 -0700 | [diff] [blame] | 827 | break; |
| 828 | } |
Craig Tiller | 1282a67 | 2016-11-18 14:57:53 -0800 | [diff] [blame] | 829 | l->md = grpc_mdelem_from_grpc_metadata(exec_ctx, (grpc_metadata *)md); |
Craig Tiller | b42445c | 2016-04-22 13:11:44 -0700 | [diff] [blame] | 830 | } |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 831 | if (i != total_count) { |
Craig Tiller | 5ae3ffb | 2016-11-18 14:58:32 -0800 | [diff] [blame] | 832 | for (int j = 0; j < i; j++) { |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 833 | const grpc_metadata *md = |
| 834 | get_md_elem(metadata, additional_metadata, j, count); |
Craig Tiller | b42445c | 2016-04-22 13:11:44 -0700 | [diff] [blame] | 835 | grpc_linked_mdelem *l = (grpc_linked_mdelem *)&md->internal_data; |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 836 | GRPC_MDELEM_UNREF(exec_ctx, l->md); |
Craig Tiller | b42445c | 2016-04-22 13:11:44 -0700 | [diff] [blame] | 837 | } |
| 838 | return 0; |
| 839 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 840 | if (prepend_extra_metadata) { |
| 841 | if (call->send_extra_metadata_count == 0) { |
| 842 | prepend_extra_metadata = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 843 | } else { |
Craig Tiller | 0960818 | 2016-11-22 15:43:56 -0800 | [diff] [blame] | 844 | for (i = 0; i < call->send_extra_metadata_count; i++) { |
| 845 | GRPC_LOG_IF_ERROR("prepare_application_metadata", |
| 846 | grpc_metadata_batch_link_tail( |
Craig Tiller | 9277aa7 | 2017-01-11 14:15:38 -0800 | [diff] [blame] | 847 | exec_ctx, batch, &call->send_extra_metadata[i])); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 848 | } |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 849 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 850 | } |
Craig Tiller | 0960818 | 2016-11-22 15:43:56 -0800 | [diff] [blame] | 851 | for (i = 0; i < total_count; i++) { |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 852 | grpc_metadata *md = get_md_elem(metadata, additional_metadata, i, count); |
Craig Tiller | 9277aa7 | 2017-01-11 14:15:38 -0800 | [diff] [blame] | 853 | GRPC_LOG_IF_ERROR( |
| 854 | "prepare_application_metadata", |
| 855 | grpc_metadata_batch_link_tail(exec_ctx, batch, linked_from_md(md))); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 856 | } |
Craig Tiller | 0960818 | 2016-11-22 15:43:56 -0800 | [diff] [blame] | 857 | call->send_extra_metadata_count = 0; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 858 | |
Craig Tiller | b96d001 | 2015-05-06 15:33:23 -0700 | [diff] [blame] | 859 | return 1; |
| 860 | } |
| 861 | |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 862 | /* we offset status by a small amount when storing it into transport metadata |
| 863 | as metadata cannot store a 0 value (which is used as OK for grpc_status_codes |
| 864 | */ |
| 865 | #define STATUS_OFFSET 1 |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 866 | static void destroy_status(void *ignored) {} |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 867 | |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 868 | static uint32_t decode_status(grpc_mdelem md) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 869 | uint32_t status; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 870 | void *user_data; |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 871 | if (grpc_mdelem_eq(md, GRPC_MDELEM_GRPC_STATUS_0)) return 0; |
| 872 | if (grpc_mdelem_eq(md, GRPC_MDELEM_GRPC_STATUS_1)) return 1; |
| 873 | if (grpc_mdelem_eq(md, GRPC_MDELEM_GRPC_STATUS_2)) return 2; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 874 | user_data = grpc_mdelem_get_user_data(md, destroy_status); |
| 875 | if (user_data != NULL) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 876 | status = ((uint32_t)(intptr_t)user_data) - STATUS_OFFSET; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 877 | } else { |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 878 | if (!grpc_parse_slice_to_uint32(GRPC_MDVALUE(md), &status)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 879 | status = GRPC_STATUS_UNKNOWN; /* could not parse status code */ |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 880 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 881 | grpc_mdelem_set_user_data(md, destroy_status, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 882 | (void *)(intptr_t)(status + STATUS_OFFSET)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 883 | } |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 884 | return status; |
| 885 | } |
| 886 | |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 887 | static grpc_compression_algorithm decode_compression(grpc_mdelem md) { |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 888 | grpc_compression_algorithm algorithm = |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 889 | grpc_compression_algorithm_from_slice(GRPC_MDVALUE(md)); |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 890 | if (algorithm == GRPC_COMPRESS_ALGORITHMS_COUNT) { |
Craig Tiller | b4aa70e | 2016-12-09 09:40:11 -0800 | [diff] [blame] | 891 | char *md_c_str = grpc_slice_to_c_string(GRPC_MDVALUE(md)); |
David Garcia Quintas | 303d308 | 2016-05-05 18:25:34 -0700 | [diff] [blame] | 892 | gpr_log(GPR_ERROR, |
| 893 | "Invalid incoming compression algorithm: '%s'. Interpreting " |
| 894 | "incoming data as uncompressed.", |
| 895 | md_c_str); |
Craig Tiller | 68208fe | 2016-11-14 14:35:02 -0800 | [diff] [blame] | 896 | gpr_free(md_c_str); |
David Garcia Quintas | 303d308 | 2016-05-05 18:25:34 -0700 | [diff] [blame] | 897 | return GRPC_COMPRESS_NONE; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 898 | } |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 899 | return algorithm; |
David Garcia Quintas | db94b27 | 2015-06-15 18:37:01 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 902 | static void recv_common_filter(grpc_exec_ctx *exec_ctx, grpc_call *call, |
| 903 | grpc_metadata_batch *b) { |
Craig Tiller | de2508b | 2017-01-06 15:23:23 -0800 | [diff] [blame] | 904 | if (b->idx.named.grpc_status != NULL) { |
| 905 | uint32_t status_code = decode_status(b->idx.named.grpc_status->md); |
| 906 | grpc_error *error = |
| 907 | status_code == GRPC_STATUS_OK |
| 908 | ? GRPC_ERROR_NONE |
| 909 | : grpc_error_set_int(GRPC_ERROR_CREATE("Error received from peer"), |
Craig Tiller | cae37f3 | 2017-01-09 15:50:03 -0800 | [diff] [blame] | 910 | GRPC_ERROR_INT_GRPC_STATUS, |
| 911 | (intptr_t)status_code); |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 912 | |
Craig Tiller | de2508b | 2017-01-06 15:23:23 -0800 | [diff] [blame] | 913 | if (b->idx.named.grpc_message != NULL) { |
| 914 | char *msg = |
| 915 | grpc_slice_to_c_string(GRPC_MDVALUE(b->idx.named.grpc_message->md)); |
| 916 | error = grpc_error_set_str(error, GRPC_ERROR_STR_GRPC_MESSAGE, msg); |
| 917 | gpr_free(msg); |
| 918 | grpc_metadata_batch_remove(exec_ctx, b, b->idx.named.grpc_message); |
Craig Tiller | 8c58a48 | 2017-02-07 14:52:59 -0800 | [diff] [blame] | 919 | } else if (error != GRPC_ERROR_NONE) { |
Craig Tiller | 8f24d6a | 2017-01-10 06:36:46 -0800 | [diff] [blame] | 920 | error = grpc_error_set_str(error, GRPC_ERROR_STR_GRPC_MESSAGE, ""); |
Craig Tiller | de2508b | 2017-01-06 15:23:23 -0800 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | set_status_from_error(exec_ctx, call, STATUS_FROM_WIRE, error); |
| 924 | grpc_metadata_batch_remove(exec_ctx, b, b->idx.named.grpc_status); |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 925 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 926 | } |
| 927 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 928 | static void publish_app_metadata(grpc_call *call, grpc_metadata_batch *b, |
| 929 | int is_trailing) { |
Craig Tiller | 0960818 | 2016-11-22 15:43:56 -0800 | [diff] [blame] | 930 | if (b->list.count == 0) return; |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 931 | GPR_TIMER_BEGIN("publish_app_metadata", 0); |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 932 | grpc_metadata_array *dest; |
| 933 | grpc_metadata *mdusr; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 934 | dest = call->buffered_metadata[is_trailing]; |
Craig Tiller | b0f3bca | 2016-11-22 14:54:10 -0800 | [diff] [blame] | 935 | if (dest->count + b->list.count > dest->capacity) { |
| 936 | dest->capacity = |
| 937 | GPR_MAX(dest->capacity + b->list.count, dest->capacity * 3 / 2); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 938 | dest->metadata = |
| 939 | gpr_realloc(dest->metadata, sizeof(grpc_metadata) * dest->capacity); |
| 940 | } |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 941 | for (grpc_linked_mdelem *l = b->list.head; l != NULL; l = l->next) { |
| 942 | mdusr = &dest->metadata[dest->count++]; |
Craig Tiller | cf0a202 | 2016-11-23 11:36:21 -0800 | [diff] [blame] | 943 | /* we pass back borrowed slices that are valid whilst the call is valid */ |
| 944 | mdusr->key = GRPC_MDKEY(l->md); |
| 945 | mdusr->value = GRPC_MDVALUE(l->md); |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 946 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 947 | GPR_TIMER_END("publish_app_metadata", 0); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 948 | } |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 949 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 950 | static void recv_initial_filter(grpc_exec_ctx *exec_ctx, grpc_call *call, |
| 951 | grpc_metadata_batch *b) { |
| 952 | recv_common_filter(exec_ctx, call, b); |
| 953 | |
| 954 | if (b->idx.named.grpc_encoding != NULL) { |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 955 | GPR_TIMER_BEGIN("incoming_compression_algorithm", 0); |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 956 | set_incoming_compression_algorithm( |
| 957 | call, decode_compression(b->idx.named.grpc_encoding->md)); |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 958 | GPR_TIMER_END("incoming_compression_algorithm", 0); |
Craig Tiller | de7b467 | 2016-11-23 11:13:46 -0800 | [diff] [blame] | 959 | grpc_metadata_batch_remove(exec_ctx, b, b->idx.named.grpc_encoding); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 960 | } |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 961 | |
| 962 | if (b->idx.named.grpc_accept_encoding != NULL) { |
| 963 | GPR_TIMER_BEGIN("encodings_accepted_by_peer", 0); |
| 964 | set_encodings_accepted_by_peer(exec_ctx, call, |
| 965 | b->idx.named.grpc_accept_encoding->md); |
Craig Tiller | de7b467 | 2016-11-23 11:13:46 -0800 | [diff] [blame] | 966 | grpc_metadata_batch_remove(exec_ctx, b, b->idx.named.grpc_accept_encoding); |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 967 | GPR_TIMER_END("encodings_accepted_by_peer", 0); |
| 968 | } |
| 969 | |
| 970 | publish_app_metadata(call, b, false); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 971 | } |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 972 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 973 | static void recv_trailing_filter(grpc_exec_ctx *exec_ctx, void *args, |
| 974 | grpc_metadata_batch *b) { |
Craig Tiller | c586666 | 2016-11-16 15:25:00 -0800 | [diff] [blame] | 975 | grpc_call *call = args; |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 976 | recv_common_filter(exec_ctx, call, b); |
| 977 | publish_app_metadata(call, b, true); |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 978 | } |
Craig Tiller | 8b282cb | 2015-04-17 14:57:44 -0700 | [diff] [blame] | 979 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 980 | grpc_call_stack *grpc_call_get_call_stack(grpc_call *call) { |
| 981 | return CALL_STACK_FROM_CALL(call); |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 982 | } |
| 983 | |
Craig Tiller | 255edaa | 2016-12-13 09:04:55 -0800 | [diff] [blame] | 984 | /******************************************************************************* |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 985 | * BATCH API IMPLEMENTATION |
| 986 | */ |
| 987 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 988 | static void set_status_value_directly(grpc_status_code status, void *dest) { |
| 989 | *(grpc_status_code *)dest = status; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 990 | } |
| 991 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 992 | static void set_cancelled_value(grpc_status_code status, void *dest) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 993 | *(int *)dest = (status != GRPC_STATUS_OK); |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 994 | } |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 995 | |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 996 | static bool are_write_flags_valid(uint32_t flags) { |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 997 | /* check that only bits in GRPC_WRITE_(INTERNAL?)_USED_MASK are set */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 998 | const uint32_t allowed_write_positions = |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 999 | (GRPC_WRITE_USED_MASK | GRPC_WRITE_INTERNAL_USED_MASK); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1000 | const uint32_t invalid_positions = ~allowed_write_positions; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1001 | return !(flags & invalid_positions); |
| 1002 | } |
| 1003 | |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 1004 | static bool are_initial_metadata_flags_valid(uint32_t flags, bool is_client) { |
| 1005 | /* check that only bits in GRPC_WRITE_(INTERNAL?)_USED_MASK are set */ |
| 1006 | uint32_t invalid_positions = ~GRPC_INITIAL_METADATA_USED_MASK; |
| 1007 | if (!is_client) { |
| 1008 | invalid_positions |= GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST; |
| 1009 | } |
| 1010 | return !(flags & invalid_positions); |
| 1011 | } |
| 1012 | |
Craig Tiller | 2a11ad1 | 2017-02-08 17:09:02 -0800 | [diff] [blame] | 1013 | static int batch_slot_for_op(grpc_op_type type) { |
| 1014 | switch (type) { |
| 1015 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 1016 | return 0; |
| 1017 | case GRPC_OP_SEND_MESSAGE: |
| 1018 | return 1; |
| 1019 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 1020 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 1021 | return 2; |
| 1022 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 1023 | return 3; |
| 1024 | case GRPC_OP_RECV_MESSAGE: |
| 1025 | return 4; |
| 1026 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 1027 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 1028 | return 5; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1029 | } |
Craig Tiller | c869da0 | 2017-02-08 17:11:17 -0800 | [diff] [blame] | 1030 | GPR_UNREACHABLE_CODE(return 123456789); |
Craig Tiller | 2a11ad1 | 2017-02-08 17:09:02 -0800 | [diff] [blame] | 1031 | } |
Craig Tiller | 89d3379 | 2017-02-08 16:39:16 -0800 | [diff] [blame] | 1032 | |
| 1033 | static batch_control *allocate_batch_control(grpc_call *call, |
| 1034 | const grpc_op *ops, |
| 1035 | size_t num_ops) { |
| 1036 | int slot = batch_slot_for_op(ops[0].op); |
| 1037 | for (size_t i = 1; i < num_ops; i++) { |
| 1038 | int op_slot = batch_slot_for_op(ops[i].op); |
| 1039 | slot = GPR_MIN(slot, op_slot); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1040 | } |
Craig Tiller | 5e5ef30 | 2017-02-09 08:46:49 -0800 | [diff] [blame] | 1041 | batch_control *bctl = &call->active_batches[slot]; |
| 1042 | if (bctl->call != NULL) { |
| 1043 | return NULL; |
| 1044 | } |
| 1045 | memset(bctl, 0, sizeof(*bctl)); |
| 1046 | bctl->call = call; |
| 1047 | return bctl; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | static void finish_batch_completion(grpc_exec_ctx *exec_ctx, void *user_data, |
| 1051 | grpc_cq_completion *storage) { |
| 1052 | batch_control *bctl = user_data; |
| 1053 | grpc_call *call = bctl->call; |
Craig Tiller | 5e5ef30 | 2017-02-09 08:46:49 -0800 | [diff] [blame] | 1054 | bctl->call = NULL; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1055 | GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "completion"); |
| 1056 | } |
| 1057 | |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1058 | static grpc_error *consolidate_batch_errors(batch_control *bctl) { |
| 1059 | size_t n = (size_t)gpr_atm_no_barrier_load(&bctl->num_errors); |
| 1060 | if (n == 0) { |
| 1061 | return GRPC_ERROR_NONE; |
| 1062 | } else if (n == 1) { |
Craig Tiller | a78da60 | 2017-01-27 08:16:23 -0800 | [diff] [blame] | 1063 | /* Skip creating a composite error in the case that only one error was |
| 1064 | logged */ |
Craig Tiller | ad980e3 | 2017-01-23 07:46:25 -0800 | [diff] [blame] | 1065 | grpc_error *e = bctl->errors[0]; |
| 1066 | bctl->errors[0] = NULL; |
| 1067 | return e; |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1068 | } else { |
Craig Tiller | 1c4775c | 2017-01-06 16:07:45 -0800 | [diff] [blame] | 1069 | grpc_error *error = |
| 1070 | GRPC_ERROR_CREATE_REFERENCING("Call batch failed", bctl->errors, n); |
| 1071 | for (size_t i = 0; i < n; i++) { |
| 1072 | GRPC_ERROR_UNREF(bctl->errors[i]); |
Craig Tiller | ad980e3 | 2017-01-23 07:46:25 -0800 | [diff] [blame] | 1073 | bctl->errors[i] = NULL; |
Craig Tiller | 1c4775c | 2017-01-06 16:07:45 -0800 | [diff] [blame] | 1074 | } |
| 1075 | return error; |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1079 | static void post_batch_completion(grpc_exec_ctx *exec_ctx, |
| 1080 | batch_control *bctl) { |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1081 | grpc_call *child_call; |
| 1082 | grpc_call *next_child_call; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1083 | grpc_call *call = bctl->call; |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1084 | grpc_error *error = consolidate_batch_errors(bctl); |
| 1085 | |
| 1086 | gpr_mu_lock(&call->mu); |
| 1087 | |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1088 | if (bctl->send_initial_metadata) { |
| 1089 | grpc_metadata_batch_destroy( |
| 1090 | exec_ctx, |
| 1091 | &call->metadata_batch[0 /* is_receiving */][0 /* is_trailing */]); |
| 1092 | } |
Craig Tiller | f927ad1 | 2017-01-06 15:27:31 -0800 | [diff] [blame] | 1093 | if (bctl->send_message) { |
| 1094 | call->sending_message = false; |
| 1095 | } |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1096 | if (bctl->send_final_op) { |
| 1097 | grpc_metadata_batch_destroy( |
| 1098 | exec_ctx, |
| 1099 | &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */]); |
| 1100 | } |
Craig Tiller | 02b87cd | 2016-09-02 09:50:08 -0700 | [diff] [blame] | 1101 | if (bctl->recv_final_op) { |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1102 | grpc_metadata_batch *md = |
| 1103 | &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */]; |
| 1104 | recv_trailing_filter(exec_ctx, call, md); |
| 1105 | |
| 1106 | call->received_final_op = true; |
| 1107 | /* propagate cancellation to any interested children */ |
| 1108 | child_call = call->first_child; |
| 1109 | if (child_call != NULL) { |
| 1110 | do { |
| 1111 | next_child_call = child_call->sibling_next; |
| 1112 | if (child_call->cancellation_is_inherited) { |
| 1113 | GRPC_CALL_INTERNAL_REF(child_call, "propagate_cancel"); |
| 1114 | grpc_call_cancel(child_call, NULL); |
| 1115 | GRPC_CALL_INTERNAL_UNREF(exec_ctx, child_call, "propagate_cancel"); |
| 1116 | } |
| 1117 | child_call = next_child_call; |
| 1118 | } while (child_call != call->first_child); |
| 1119 | } |
| 1120 | |
| 1121 | if (call->is_client) { |
| 1122 | get_final_status(call, set_status_value_directly, |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 1123 | call->final_op.client.status, |
| 1124 | call->final_op.client.status_details); |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1125 | } else { |
| 1126 | get_final_status(call, set_cancelled_value, |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 1127 | call->final_op.server.cancelled, NULL); |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Craig Tiller | 02b87cd | 2016-09-02 09:50:08 -0700 | [diff] [blame] | 1130 | GRPC_ERROR_UNREF(error); |
| 1131 | error = GRPC_ERROR_NONE; |
| 1132 | } |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1133 | gpr_mu_unlock(&call->mu); |
| 1134 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1135 | if (bctl->is_notify_tag_closure) { |
Craig Tiller | b08fa49 | 2016-05-10 14:56:05 -0700 | [diff] [blame] | 1136 | /* unrefs bctl->error */ |
Craig Tiller | 2db5bda | 2017-02-09 10:30:55 -0800 | [diff] [blame] | 1137 | bctl->call = NULL; |
Craig Tiller | 02b87cd | 2016-09-02 09:50:08 -0700 | [diff] [blame] | 1138 | grpc_closure_run(exec_ctx, bctl->notify_tag, error); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1139 | GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "completion"); |
| 1140 | } else { |
Craig Tiller | b08fa49 | 2016-05-10 14:56:05 -0700 | [diff] [blame] | 1141 | /* unrefs bctl->error */ |
Craig Tiller | 02b87cd | 2016-09-02 09:50:08 -0700 | [diff] [blame] | 1142 | grpc_cq_end_op(exec_ctx, bctl->call->cq, bctl->notify_tag, error, |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1143 | finish_batch_completion, bctl, &bctl->cq_completion); |
| 1144 | } |
| 1145 | } |
| 1146 | |
Craig Tiller | d04dc4d | 2017-01-09 14:40:28 -0800 | [diff] [blame] | 1147 | static void finish_batch_step(grpc_exec_ctx *exec_ctx, batch_control *bctl) { |
Craig Tiller | 065b139 | 2017-01-09 14:05:07 -0800 | [diff] [blame] | 1148 | if (gpr_unref(&bctl->steps_to_complete)) { |
| 1149 | post_batch_completion(exec_ctx, bctl); |
| 1150 | } |
| 1151 | } |
| 1152 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1153 | static void continue_receiving_slices(grpc_exec_ctx *exec_ctx, |
| 1154 | batch_control *bctl) { |
| 1155 | grpc_call *call = bctl->call; |
| 1156 | for (;;) { |
| 1157 | size_t remaining = call->receiving_stream->length - |
| 1158 | (*call->receiving_buffer)->data.raw.slice_buffer.length; |
| 1159 | if (remaining == 0) { |
| 1160 | call->receiving_message = 0; |
Craig Tiller | 3b66ab9 | 2015-12-09 19:42:22 -0800 | [diff] [blame] | 1161 | grpc_byte_stream_destroy(exec_ctx, call->receiving_stream); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1162 | call->receiving_stream = NULL; |
Craig Tiller | d04dc4d | 2017-01-09 14:40:28 -0800 | [diff] [blame] | 1163 | finish_batch_step(exec_ctx, bctl); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1164 | return; |
| 1165 | } |
| 1166 | if (grpc_byte_stream_next(exec_ctx, call->receiving_stream, |
| 1167 | &call->receiving_slice, remaining, |
| 1168 | &call->receiving_slice_ready)) { |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 1169 | grpc_slice_buffer_add(&(*call->receiving_buffer)->data.raw.slice_buffer, |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 1170 | call->receiving_slice); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1171 | } else { |
| 1172 | return; |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | static void receiving_slice_ready(grpc_exec_ctx *exec_ctx, void *bctlp, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 1178 | grpc_error *error) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1179 | batch_control *bctl = bctlp; |
| 1180 | grpc_call *call = bctl->call; |
| 1181 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 1182 | if (error == GRPC_ERROR_NONE) { |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 1183 | grpc_slice_buffer_add(&(*call->receiving_buffer)->data.raw.slice_buffer, |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 1184 | call->receiving_slice); |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 1185 | continue_receiving_slices(exec_ctx, bctl); |
| 1186 | } else { |
Craig Tiller | a286b04 | 2016-06-13 15:20:39 +0000 | [diff] [blame] | 1187 | if (grpc_trace_operation_failures) { |
| 1188 | GRPC_LOG_IF_ERROR("receiving_slice_ready", GRPC_ERROR_REF(error)); |
| 1189 | } |
Craig Tiller | e1b8c2b | 2015-12-16 19:27:52 -0800 | [diff] [blame] | 1190 | grpc_byte_stream_destroy(exec_ctx, call->receiving_stream); |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 1191 | call->receiving_stream = NULL; |
| 1192 | grpc_byte_buffer_destroy(*call->receiving_buffer); |
| 1193 | *call->receiving_buffer = NULL; |
Craig Tiller | d04dc4d | 2017-01-09 14:40:28 -0800 | [diff] [blame] | 1194 | finish_batch_step(exec_ctx, bctl); |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 1195 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Mark D. Roth | 274c8ed | 2016-10-04 09:21:42 -0700 | [diff] [blame] | 1198 | static void process_data_after_md(grpc_exec_ctx *exec_ctx, |
| 1199 | batch_control *bctl) { |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1200 | grpc_call *call = bctl->call; |
| 1201 | if (call->receiving_stream == NULL) { |
| 1202 | *call->receiving_buffer = NULL; |
| 1203 | call->receiving_message = 0; |
Craig Tiller | d04dc4d | 2017-01-09 14:40:28 -0800 | [diff] [blame] | 1204 | finish_batch_step(exec_ctx, bctl); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1205 | } else { |
| 1206 | call->test_only_last_message_flags = call->receiving_stream->flags; |
| 1207 | if ((call->receiving_stream->flags & GRPC_WRITE_INTERNAL_COMPRESS) && |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1208 | (call->incoming_compression_algorithm > GRPC_COMPRESS_NONE)) { |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1209 | *call->receiving_buffer = grpc_raw_compressed_byte_buffer_create( |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1210 | NULL, 0, call->incoming_compression_algorithm); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1211 | } else { |
| 1212 | *call->receiving_buffer = grpc_raw_byte_buffer_create(NULL, 0); |
| 1213 | } |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 1214 | grpc_closure_init(&call->receiving_slice_ready, receiving_slice_ready, bctl, |
| 1215 | grpc_schedule_on_exec_ctx); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1216 | continue_receiving_slices(exec_ctx, bctl); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | static void receiving_stream_ready(grpc_exec_ctx *exec_ctx, void *bctlp, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 1221 | grpc_error *error) { |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1222 | batch_control *bctl = bctlp; |
| 1223 | grpc_call *call = bctl->call; |
Craig Tiller | 065b139 | 2017-01-09 14:05:07 -0800 | [diff] [blame] | 1224 | gpr_mu_lock(&bctl->call->mu); |
Mark D. Roth | 274c8ed | 2016-10-04 09:21:42 -0700 | [diff] [blame] | 1225 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 1226 | cancel_with_error(exec_ctx, call, STATUS_FROM_SURFACE, |
| 1227 | GRPC_ERROR_REF(error)); |
Mark D. Roth | 274c8ed | 2016-10-04 09:21:42 -0700 | [diff] [blame] | 1228 | } |
Craig Tiller | 065b139 | 2017-01-09 14:05:07 -0800 | [diff] [blame] | 1229 | if (call->has_initial_md_been_received || error != GRPC_ERROR_NONE || |
Craig Tiller | 52cf871 | 2016-04-23 22:54:21 -0700 | [diff] [blame] | 1230 | call->receiving_stream == NULL) { |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1231 | gpr_mu_unlock(&bctl->call->mu); |
Mark D. Roth | 274c8ed | 2016-10-04 09:21:42 -0700 | [diff] [blame] | 1232 | process_data_after_md(exec_ctx, bctlp); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1233 | } else { |
Craig Tiller | 8a67780 | 2016-04-22 15:07:53 -0700 | [diff] [blame] | 1234 | call->saved_receiving_stream_ready_bctlp = bctlp; |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1235 | gpr_mu_unlock(&bctl->call->mu); |
| 1236 | } |
| 1237 | } |
| 1238 | |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1239 | static void validate_filtered_metadata(grpc_exec_ctx *exec_ctx, |
| 1240 | batch_control *bctl) { |
| 1241 | grpc_call *call = bctl->call; |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 1242 | /* validate call->incoming_compression_algorithm */ |
| 1243 | if (call->incoming_compression_algorithm != GRPC_COMPRESS_NONE) { |
| 1244 | const grpc_compression_algorithm algo = |
| 1245 | call->incoming_compression_algorithm; |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1246 | char *error_msg = NULL; |
| 1247 | const grpc_compression_options compression_options = |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 1248 | grpc_channel_compression_options(call->channel); |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1249 | /* check if algorithm is known */ |
| 1250 | if (algo >= GRPC_COMPRESS_ALGORITHMS_COUNT) { |
| 1251 | gpr_asprintf(&error_msg, "Invalid compression algorithm value '%d'.", |
| 1252 | algo); |
Yuchen Zeng | 64c0e8d | 2016-06-10 11:19:51 -0700 | [diff] [blame] | 1253 | gpr_log(GPR_ERROR, "%s", error_msg); |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 1254 | cancel_with_status(exec_ctx, call, STATUS_FROM_SURFACE, |
| 1255 | GRPC_STATUS_UNIMPLEMENTED, error_msg); |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1256 | } else if (grpc_compression_options_is_algorithm_enabled( |
| 1257 | &compression_options, algo) == 0) { |
| 1258 | /* check if algorithm is supported by current channel config */ |
David Garcia Quintas | 1ff168a | 2016-06-30 10:25:04 -0700 | [diff] [blame] | 1259 | char *algo_name = NULL; |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1260 | grpc_compression_algorithm_name(algo, &algo_name); |
| 1261 | gpr_asprintf(&error_msg, "Compression algorithm '%s' is disabled.", |
| 1262 | algo_name); |
Yuchen Zeng | 64c0e8d | 2016-06-10 11:19:51 -0700 | [diff] [blame] | 1263 | gpr_log(GPR_ERROR, "%s", error_msg); |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 1264 | cancel_with_status(exec_ctx, call, STATUS_FROM_SURFACE, |
| 1265 | GRPC_STATUS_UNIMPLEMENTED, error_msg); |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1266 | } else { |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 1267 | call->incoming_compression_algorithm = algo; |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1268 | } |
| 1269 | gpr_free(error_msg); |
| 1270 | } |
David Garcia Quintas | f1945f2 | 2016-05-18 10:53:14 -0700 | [diff] [blame] | 1271 | |
| 1272 | /* make sure the received grpc-encoding is amongst the ones listed in |
| 1273 | * grpc-accept-encoding */ |
| 1274 | GPR_ASSERT(call->encodings_accepted_by_peer != 0); |
| 1275 | if (!GPR_BITGET(call->encodings_accepted_by_peer, |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 1276 | call->incoming_compression_algorithm)) { |
David Garcia Quintas | f1945f2 | 2016-05-18 10:53:14 -0700 | [diff] [blame] | 1277 | extern int grpc_compression_trace; |
| 1278 | if (grpc_compression_trace) { |
David Garcia Quintas | 1ff168a | 2016-06-30 10:25:04 -0700 | [diff] [blame] | 1279 | char *algo_name = NULL; |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 1280 | grpc_compression_algorithm_name(call->incoming_compression_algorithm, |
| 1281 | &algo_name); |
David Garcia Quintas | f1945f2 | 2016-05-18 10:53:14 -0700 | [diff] [blame] | 1282 | gpr_log(GPR_ERROR, |
| 1283 | "Compression algorithm (grpc-encoding = '%s') not present in " |
| 1284 | "the bitset of accepted encodings (grpc-accept-encodings: " |
| 1285 | "'0x%x')", |
| 1286 | algo_name, call->encodings_accepted_by_peer); |
| 1287 | } |
| 1288 | } |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Craig Tiller | 3ba16e4 | 2016-12-08 16:46:18 -0800 | [diff] [blame] | 1291 | static void add_batch_error(grpc_exec_ctx *exec_ctx, batch_control *bctl, |
| 1292 | grpc_error *error) { |
Craig Tiller | 452422e | 2016-09-01 15:54:56 -0700 | [diff] [blame] | 1293 | if (error == GRPC_ERROR_NONE) return; |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1294 | int idx = (int)gpr_atm_no_barrier_fetch_add(&bctl->num_errors, 1); |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 1295 | if (idx == 0) { |
| 1296 | cancel_with_error(exec_ctx, bctl->call, STATUS_FROM_CORE, |
| 1297 | GRPC_ERROR_REF(error)); |
| 1298 | } |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1299 | bctl->errors[idx] = error; |
Craig Tiller | 452422e | 2016-09-01 15:54:56 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1302 | static void receiving_initial_metadata_ready(grpc_exec_ctx *exec_ctx, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 1303 | void *bctlp, grpc_error *error) { |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1304 | batch_control *bctl = bctlp; |
| 1305 | grpc_call *call = bctl->call; |
| 1306 | |
| 1307 | gpr_mu_lock(&call->mu); |
| 1308 | |
Craig Tiller | 3ba16e4 | 2016-12-08 16:46:18 -0800 | [diff] [blame] | 1309 | add_batch_error(exec_ctx, bctl, GRPC_ERROR_REF(error)); |
Craig Tiller | 452422e | 2016-09-01 15:54:56 -0700 | [diff] [blame] | 1310 | if (error == GRPC_ERROR_NONE) { |
Craig Tiller | c48ca71 | 2016-04-04 13:42:04 -0700 | [diff] [blame] | 1311 | grpc_metadata_batch *md = |
| 1312 | &call->metadata_batch[1 /* is_receiving */][0 /* is_trailing */]; |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 1313 | recv_initial_filter(exec_ctx, call, md); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1314 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 1315 | /* TODO(ctiller): this could be moved into recv_initial_filter now */ |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1316 | GPR_TIMER_BEGIN("validate_filtered_metadata", 0); |
| 1317 | validate_filtered_metadata(exec_ctx, bctl); |
| 1318 | GPR_TIMER_END("validate_filtered_metadata", 0); |
David Garcia Quintas | 4612337 | 2016-05-09 15:28:42 -0700 | [diff] [blame] | 1319 | |
Craig Tiller | c48ca71 | 2016-04-04 13:42:04 -0700 | [diff] [blame] | 1320 | if (gpr_time_cmp(md->deadline, gpr_inf_future(md->deadline.clock_type)) != |
| 1321 | 0 && |
| 1322 | !call->is_client) { |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 1323 | call->send_deadline = |
| 1324 | gpr_convert_clock_type(md->deadline, GPR_CLOCK_MONOTONIC); |
Craig Tiller | c48ca71 | 2016-04-04 13:42:04 -0700 | [diff] [blame] | 1325 | } |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
Craig Tiller | c48ca71 | 2016-04-04 13:42:04 -0700 | [diff] [blame] | 1328 | call->has_initial_md_been_received = true; |
Craig Tiller | 8a67780 | 2016-04-22 15:07:53 -0700 | [diff] [blame] | 1329 | if (call->saved_receiving_stream_ready_bctlp != NULL) { |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1330 | grpc_closure *saved_rsr_closure = grpc_closure_create( |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 1331 | receiving_stream_ready, call->saved_receiving_stream_ready_bctlp, |
| 1332 | grpc_schedule_on_exec_ctx); |
Craig Tiller | 8a67780 | 2016-04-22 15:07:53 -0700 | [diff] [blame] | 1333 | call->saved_receiving_stream_ready_bctlp = NULL; |
Craig Tiller | ad980e3 | 2017-01-23 07:46:25 -0800 | [diff] [blame] | 1334 | grpc_closure_sched(exec_ctx, saved_rsr_closure, GRPC_ERROR_REF(error)); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | gpr_mu_unlock(&call->mu); |
| 1338 | |
Craig Tiller | d04dc4d | 2017-01-09 14:40:28 -0800 | [diff] [blame] | 1339 | finish_batch_step(exec_ctx, bctl); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 1342 | static void finish_batch(grpc_exec_ctx *exec_ctx, void *bctlp, |
| 1343 | grpc_error *error) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1344 | batch_control *bctl = bctlp; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1345 | |
Craig Tiller | 3ba16e4 | 2016-12-08 16:46:18 -0800 | [diff] [blame] | 1346 | add_batch_error(exec_ctx, bctl, GRPC_ERROR_REF(error)); |
Craig Tiller | d04dc4d | 2017-01-09 14:40:28 -0800 | [diff] [blame] | 1347 | finish_batch_step(exec_ctx, bctl); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
Craig Tiller | 89d3379 | 2017-02-08 16:39:16 -0800 | [diff] [blame] | 1350 | static void free_no_op_completion(grpc_exec_ctx *exec_ctx, void *p, |
| 1351 | grpc_cq_completion *completion) { |
| 1352 | gpr_free(completion); |
| 1353 | } |
| 1354 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1355 | static grpc_call_error call_start_batch(grpc_exec_ctx *exec_ctx, |
| 1356 | grpc_call *call, const grpc_op *ops, |
| 1357 | size_t nops, void *notify_tag, |
| 1358 | int is_notify_tag_closure) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1359 | size_t i; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1360 | const grpc_op *op; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1361 | batch_control *bctl; |
| 1362 | int num_completion_callbacks_needed = 1; |
| 1363 | grpc_call_error error = GRPC_CALL_OK; |
Craig Tiller | 9928d39 | 2015-08-18 09:40:24 -0700 | [diff] [blame] | 1364 | |
Vitaly Buka | e60003d | 2016-08-01 19:34:51 -0700 | [diff] [blame] | 1365 | // sent_initial_metadata guards against variable reuse. |
| 1366 | grpc_metadata compression_md; |
| 1367 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 1368 | GPR_TIMER_BEGIN("grpc_call_start_batch", 0); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1369 | GRPC_CALL_LOG_BATCH(GPR_INFO, call, ops, nops, notify_tag); |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 1370 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1371 | if (nops == 0) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1372 | if (!is_notify_tag_closure) { |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 1373 | grpc_cq_begin_op(call->cq, notify_tag); |
Craig Tiller | 89d3379 | 2017-02-08 16:39:16 -0800 | [diff] [blame] | 1374 | grpc_cq_end_op(exec_ctx, call->cq, notify_tag, GRPC_ERROR_NONE, |
| 1375 | free_no_op_completion, NULL, |
| 1376 | gpr_malloc(sizeof(grpc_cq_completion))); |
Craig Tiller | 2db5bda | 2017-02-09 10:30:55 -0800 | [diff] [blame] | 1377 | } else { |
| 1378 | grpc_closure_sched(exec_ctx, notify_tag, GRPC_ERROR_NONE); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1379 | } |
Craig Tiller | ea50b90 | 2015-12-15 07:05:25 -0800 | [diff] [blame] | 1380 | error = GRPC_CALL_OK; |
| 1381 | goto done; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1384 | /* TODO(ctiller): this feels like it could be made lock-free */ |
Craig Tiller | 89d3379 | 2017-02-08 16:39:16 -0800 | [diff] [blame] | 1385 | bctl = allocate_batch_control(call, ops, nops); |
Craig Tiller | 5e5ef30 | 2017-02-09 08:46:49 -0800 | [diff] [blame] | 1386 | if (bctl == NULL) { |
| 1387 | return GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1388 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1389 | bctl->notify_tag = notify_tag; |
| 1390 | bctl->is_notify_tag_closure = (uint8_t)(is_notify_tag_closure != 0); |
| 1391 | |
Craig Tiller | 89d3379 | 2017-02-08 16:39:16 -0800 | [diff] [blame] | 1392 | gpr_mu_lock(&call->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1393 | grpc_transport_stream_op *stream_op = &bctl->op; |
| 1394 | memset(stream_op, 0, sizeof(*stream_op)); |
| 1395 | stream_op->covered_by_poller = true; |
| 1396 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1397 | /* rewrite batch ops into a transport op */ |
| 1398 | for (i = 0; i < nops; i++) { |
| 1399 | op = &ops[i]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1400 | if (op->reserved != NULL) { |
Craig Tiller | 3ffd822 | 2015-09-21 08:21:57 -0700 | [diff] [blame] | 1401 | error = GRPC_CALL_ERROR; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1402 | goto done_with_error; |
Craig Tiller | 3ffd822 | 2015-09-21 08:21:57 -0700 | [diff] [blame] | 1403 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1404 | switch (op->op) { |
| 1405 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 1406 | /* Flag validation: currently allow no flags */ |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 1407 | if (!are_initial_metadata_flags_valid(op->flags, call->is_client)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1408 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1409 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1410 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1411 | if (call->sent_initial_metadata) { |
| 1412 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1413 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1414 | } |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1415 | /* process compression level */ |
Vitaly Buka | e60003d | 2016-08-01 19:34:51 -0700 | [diff] [blame] | 1416 | memset(&compression_md, 0, sizeof(compression_md)); |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1417 | size_t additional_metadata_count = 0; |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1418 | 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 Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1421 | effective_compression_level = |
David Garcia Quintas | 8ba42be | 2016-06-07 17:30:20 -0700 | [diff] [blame] | 1422 | op->data.send_initial_metadata.maybe_compression_level.level; |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1423 | level_set = true; |
| 1424 | } else { |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 1425 | 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 Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1431 | } |
David Garcia Quintas | 3e4f49f | 2016-05-18 23:59:02 -0700 | [diff] [blame] | 1432 | if (level_set && !call->is_client) { |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1433 | const grpc_compression_algorithm calgo = |
| 1434 | compression_algorithm_for_level_locked( |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1435 | call, effective_compression_level); |
David Garcia Quintas | 3e4f49f | 2016-05-18 23:59:02 -0700 | [diff] [blame] | 1436 | // the following will be picked up by the compress filter and used as |
| 1437 | // the call's compression algorithm. |
Craig Tiller | 68208fe | 2016-11-14 14:35:02 -0800 | [diff] [blame] | 1438 | compression_md.key = GRPC_MDSTR_GRPC_INTERNAL_ENCODING_REQUEST; |
| 1439 | compression_md.value = grpc_compression_algorithm_slice(calgo); |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1440 | additional_metadata_count++; |
| 1441 | } |
| 1442 | |
| 1443 | if (op->data.send_initial_metadata.count + additional_metadata_count > |
| 1444 | INT_MAX) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1445 | error = GRPC_CALL_ERROR_INVALID_METADATA; |
| 1446 | goto done_with_error; |
| 1447 | } |
| 1448 | bctl->send_initial_metadata = 1; |
| 1449 | call->sent_initial_metadata = 1; |
| 1450 | if (!prepare_application_metadata( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 1451 | exec_ctx, call, (int)op->data.send_initial_metadata.count, |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1452 | op->data.send_initial_metadata.metadata, 0, call->is_client, |
| 1453 | &compression_md, (int)additional_metadata_count)) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1454 | error = GRPC_CALL_ERROR_INVALID_METADATA; |
| 1455 | goto done_with_error; |
| 1456 | } |
| 1457 | /* TODO(ctiller): just make these the same variable? */ |
| 1458 | call->metadata_batch[0][0].deadline = call->send_deadline; |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1459 | stream_op->send_initial_metadata = |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1460 | &call->metadata_batch[0 /* is_receiving */][0 /* is_trailing */]; |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1461 | stream_op->send_initial_metadata_flags = op->flags; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1462 | break; |
| 1463 | case GRPC_OP_SEND_MESSAGE: |
| 1464 | if (!are_write_flags_valid(op->flags)) { |
| 1465 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1466 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1467 | } |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 1468 | if (op->data.send_message.send_message == NULL) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1469 | error = GRPC_CALL_ERROR_INVALID_MESSAGE; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1470 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1471 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1472 | if (call->sending_message) { |
| 1473 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1474 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1475 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1476 | bctl->send_message = 1; |
| 1477 | call->sending_message = 1; |
| 1478 | grpc_slice_buffer_stream_init( |
| 1479 | &call->sending_stream, |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 1480 | &op->data.send_message.send_message->data.raw.slice_buffer, |
| 1481 | op->flags); |
Lizan Zhou | 61f0973 | 2016-10-26 14:09:52 -0700 | [diff] [blame] | 1482 | /* If the outgoing buffer is already compressed, mark it as so in the |
| 1483 | flags. These will be picked up by the compression filter and further |
| 1484 | (wasteful) attempts at compression skipped. */ |
Mark D. Roth | 9d76dbe | 2017-01-25 15:02:56 -0800 | [diff] [blame] | 1485 | if (op->data.send_message.send_message->data.raw.compression > |
| 1486 | GRPC_COMPRESS_NONE) { |
Lizan Zhou | 61f0973 | 2016-10-26 14:09:52 -0700 | [diff] [blame] | 1487 | call->sending_stream.base.flags |= GRPC_WRITE_INTERNAL_COMPRESS; |
| 1488 | } |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1489 | stream_op->send_message = &call->sending_stream.base; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1490 | break; |
| 1491 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 1492 | /* Flag validation: currently allow no flags */ |
| 1493 | if (op->flags != 0) { |
| 1494 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1495 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1496 | } |
| 1497 | if (!call->is_client) { |
| 1498 | error = GRPC_CALL_ERROR_NOT_ON_SERVER; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1499 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1500 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1501 | if (call->sent_final_op) { |
| 1502 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1503 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1504 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1505 | bctl->send_final_op = 1; |
| 1506 | call->sent_final_op = 1; |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1507 | stream_op->send_trailing_metadata = |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1508 | &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1509 | break; |
| 1510 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 1511 | /* Flag validation: currently allow no flags */ |
| 1512 | if (op->flags != 0) { |
| 1513 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1514 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1515 | } |
| 1516 | if (call->is_client) { |
| 1517 | error = GRPC_CALL_ERROR_NOT_ON_CLIENT; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1518 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1519 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1520 | if (call->sent_final_op) { |
| 1521 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1522 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1523 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1524 | if (op->data.send_status_from_server.trailing_metadata_count > |
| 1525 | INT_MAX) { |
| 1526 | error = GRPC_CALL_ERROR_INVALID_METADATA; |
| 1527 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1528 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1529 | bctl->send_final_op = 1; |
| 1530 | call->sent_final_op = 1; |
Craig Tiller | 93727aa | 2017-02-06 13:05:39 -0800 | [diff] [blame] | 1531 | GPR_ASSERT(call->send_extra_metadata_count == 0); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1532 | call->send_extra_metadata_count = 1; |
| 1533 | call->send_extra_metadata[0].md = grpc_channel_get_reffed_status_elem( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 1534 | exec_ctx, call->channel, op->data.send_status_from_server.status); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 1535 | { |
| 1536 | grpc_error *override_error = GRPC_ERROR_NONE; |
| 1537 | if (op->data.send_status_from_server.status != GRPC_STATUS_OK) { |
| 1538 | override_error = GRPC_ERROR_CREATE("Error from server send status"); |
| 1539 | } |
| 1540 | if (op->data.send_status_from_server.status_details != NULL) { |
| 1541 | call->send_extra_metadata[1].md = grpc_mdelem_from_slices( |
| 1542 | exec_ctx, GRPC_MDSTR_GRPC_MESSAGE, |
| 1543 | grpc_slice_ref_internal( |
| 1544 | *op->data.send_status_from_server.status_details)); |
| 1545 | call->send_extra_metadata_count++; |
| 1546 | char *msg = grpc_slice_to_c_string( |
| 1547 | GRPC_MDVALUE(call->send_extra_metadata[1].md)); |
| 1548 | override_error = grpc_error_set_str( |
| 1549 | override_error, GRPC_ERROR_STR_GRPC_MESSAGE, msg); |
| 1550 | gpr_free(msg); |
| 1551 | } |
| 1552 | set_status_from_error(exec_ctx, call, STATUS_FROM_API_OVERRIDE, |
| 1553 | override_error); |
Craig Tiller | 69a1f66 | 2016-09-28 10:24:21 -0700 | [diff] [blame] | 1554 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1555 | if (!prepare_application_metadata( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 1556 | exec_ctx, call, |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1557 | (int)op->data.send_status_from_server.trailing_metadata_count, |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1558 | op->data.send_status_from_server.trailing_metadata, 1, 1, NULL, |
| 1559 | 0)) { |
Craig Tiller | 93727aa | 2017-02-06 13:05:39 -0800 | [diff] [blame] | 1560 | for (int n = 0; n < call->send_extra_metadata_count; n++) { |
| 1561 | GRPC_MDELEM_UNREF(exec_ctx, call->send_extra_metadata[n].md); |
| 1562 | } |
| 1563 | call->send_extra_metadata_count = 0; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1564 | error = GRPC_CALL_ERROR_INVALID_METADATA; |
| 1565 | goto done_with_error; |
| 1566 | } |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1567 | stream_op->send_trailing_metadata = |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1568 | &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1569 | break; |
| 1570 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 1571 | /* Flag validation: currently allow no flags */ |
| 1572 | if (op->flags != 0) { |
| 1573 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1574 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1575 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1576 | if (call->received_initial_metadata) { |
| 1577 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1578 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1579 | } |
Craig Tiller | 9c5318a | 2016-12-05 15:07:04 -0800 | [diff] [blame] | 1580 | /* IF this is a server, then GRPC_OP_RECV_INITIAL_METADATA *must* come |
| 1581 | from server.c. In that case, it's coming from accept_stream, and in |
| 1582 | that case we're not necessarily covered by a poller. */ |
| 1583 | stream_op->covered_by_poller = call->is_client; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1584 | call->received_initial_metadata = 1; |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 1585 | call->buffered_metadata[0] = |
| 1586 | op->data.recv_initial_metadata.recv_initial_metadata; |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1587 | grpc_closure_init(&call->receiving_initial_metadata_ready, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 1588 | receiving_initial_metadata_ready, bctl, |
| 1589 | grpc_schedule_on_exec_ctx); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1590 | bctl->recv_initial_metadata = 1; |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1591 | stream_op->recv_initial_metadata = |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1592 | &call->metadata_batch[1 /* is_receiving */][0 /* is_trailing */]; |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1593 | stream_op->recv_initial_metadata_ready = |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1594 | &call->receiving_initial_metadata_ready; |
| 1595 | num_completion_callbacks_needed++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1596 | break; |
| 1597 | case GRPC_OP_RECV_MESSAGE: |
| 1598 | /* Flag validation: currently allow no flags */ |
| 1599 | if (op->flags != 0) { |
| 1600 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1601 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1602 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1603 | if (call->receiving_message) { |
| 1604 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
yang-g | 48f3a71 | 2015-12-07 11:23:50 -0800 | [diff] [blame] | 1605 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1606 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1607 | call->receiving_message = 1; |
| 1608 | bctl->recv_message = 1; |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 1609 | call->receiving_buffer = op->data.recv_message.recv_message; |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1610 | stream_op->recv_message = &call->receiving_stream; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1611 | grpc_closure_init(&call->receiving_stream_ready, receiving_stream_ready, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 1612 | bctl, grpc_schedule_on_exec_ctx); |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1613 | stream_op->recv_message_ready = &call->receiving_stream_ready; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1614 | num_completion_callbacks_needed++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1615 | break; |
| 1616 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 1617 | /* Flag validation: currently allow no flags */ |
| 1618 | if (op->flags != 0) { |
| 1619 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1620 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1621 | } |
| 1622 | if (!call->is_client) { |
| 1623 | error = GRPC_CALL_ERROR_NOT_ON_SERVER; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1624 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1625 | } |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 1626 | if (call->requested_final_op) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1627 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1628 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1629 | } |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 1630 | call->requested_final_op = 1; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1631 | call->buffered_metadata[1] = |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1632 | op->data.recv_status_on_client.trailing_metadata; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1633 | call->final_op.client.status = op->data.recv_status_on_client.status; |
| 1634 | call->final_op.client.status_details = |
| 1635 | op->data.recv_status_on_client.status_details; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1636 | bctl->recv_final_op = 1; |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1637 | stream_op->recv_trailing_metadata = |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1638 | &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */]; |
Craig Tiller | 106df11 | 2016-08-04 09:22:34 -0700 | [diff] [blame] | 1639 | stream_op->collect_stats = |
David Garcia Quintas | 5dde14c | 2016-07-28 17:29:27 -0700 | [diff] [blame] | 1640 | &call->final_info.stats.transport_stream_stats; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1641 | break; |
| 1642 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 1643 | /* Flag validation: currently allow no flags */ |
| 1644 | if (op->flags != 0) { |
| 1645 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1646 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1647 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1648 | if (call->is_client) { |
| 1649 | error = GRPC_CALL_ERROR_NOT_ON_CLIENT; |
| 1650 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1651 | } |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 1652 | if (call->requested_final_op) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1653 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1654 | goto done_with_error; |
| 1655 | } |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 1656 | call->requested_final_op = 1; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1657 | call->final_op.server.cancelled = |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1658 | op->data.recv_close_on_server.cancelled; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1659 | bctl->recv_final_op = 1; |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1660 | stream_op->recv_trailing_metadata = |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1661 | &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */]; |
Craig Tiller | 106df11 | 2016-08-04 09:22:34 -0700 | [diff] [blame] | 1662 | stream_op->collect_stats = |
David Garcia Quintas | 5dde14c | 2016-07-28 17:29:27 -0700 | [diff] [blame] | 1663 | &call->final_info.stats.transport_stream_stats; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1664 | break; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1665 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1666 | } |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1667 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1668 | GRPC_CALL_INTERNAL_REF(call, "completion"); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1669 | if (!is_notify_tag_closure) { |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 1670 | grpc_cq_begin_op(call->cq, notify_tag); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1671 | } |
| 1672 | gpr_ref_init(&bctl->steps_to_complete, num_completion_callbacks_needed); |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1673 | |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1674 | stream_op->context = call->context; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 1675 | grpc_closure_init(&bctl->finish_batch, finish_batch, bctl, |
| 1676 | grpc_schedule_on_exec_ctx); |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1677 | stream_op->on_complete = &bctl->finish_batch; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1678 | gpr_mu_unlock(&call->mu); |
| 1679 | |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1680 | execute_op(exec_ctx, call, stream_op); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1681 | |
Craig Tiller | 3ffd822 | 2015-09-21 08:21:57 -0700 | [diff] [blame] | 1682 | done: |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 1683 | GPR_TIMER_END("grpc_call_start_batch", 0); |
Craig Tiller | 3ffd822 | 2015-09-21 08:21:57 -0700 | [diff] [blame] | 1684 | return error; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1685 | |
| 1686 | done_with_error: |
| 1687 | /* reverse any mutations that occured */ |
| 1688 | if (bctl->send_initial_metadata) { |
| 1689 | call->sent_initial_metadata = 0; |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 1690 | grpc_metadata_batch_clear(exec_ctx, &call->metadata_batch[0][0]); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1691 | } |
| 1692 | if (bctl->send_message) { |
| 1693 | call->sending_message = 0; |
Craig Tiller | 3b66ab9 | 2015-12-09 19:42:22 -0800 | [diff] [blame] | 1694 | grpc_byte_stream_destroy(exec_ctx, &call->sending_stream.base); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1695 | } |
| 1696 | if (bctl->send_final_op) { |
| 1697 | call->sent_final_op = 0; |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 1698 | grpc_metadata_batch_clear(exec_ctx, &call->metadata_batch[0][1]); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1699 | } |
| 1700 | if (bctl->recv_initial_metadata) { |
| 1701 | call->received_initial_metadata = 0; |
| 1702 | } |
| 1703 | if (bctl->recv_message) { |
| 1704 | call->receiving_message = 0; |
| 1705 | } |
| 1706 | if (bctl->recv_final_op) { |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 1707 | call->requested_final_op = 0; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1708 | } |
| 1709 | gpr_mu_unlock(&call->mu); |
| 1710 | goto done; |
| 1711 | } |
| 1712 | |
| 1713 | grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, |
| 1714 | size_t nops, void *tag, void *reserved) { |
| 1715 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 1716 | grpc_call_error err; |
| 1717 | |
| 1718 | GRPC_API_TRACE( |
David Garcia Quintas | 4612337 | 2016-05-09 15:28:42 -0700 | [diff] [blame] | 1719 | "grpc_call_start_batch(call=%p, ops=%p, nops=%lu, tag=%p, " |
| 1720 | "reserved=%p)", |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1721 | 5, (call, ops, (unsigned long)nops, tag, reserved)); |
| 1722 | |
| 1723 | if (reserved != NULL) { |
| 1724 | err = GRPC_CALL_ERROR; |
| 1725 | } else { |
| 1726 | err = call_start_batch(&exec_ctx, call, ops, nops, tag, 0); |
| 1727 | } |
| 1728 | |
| 1729 | grpc_exec_ctx_finish(&exec_ctx); |
| 1730 | return err; |
| 1731 | } |
| 1732 | |
| 1733 | grpc_call_error grpc_call_start_batch_and_execute(grpc_exec_ctx *exec_ctx, |
| 1734 | grpc_call *call, |
| 1735 | const grpc_op *ops, |
| 1736 | size_t nops, |
| 1737 | grpc_closure *closure) { |
| 1738 | return call_start_batch(exec_ctx, call, ops, nops, closure, 1); |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1739 | } |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 1740 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1741 | void grpc_call_context_set(grpc_call *call, grpc_context_index elem, |
| 1742 | void *value, void (*destroy)(void *value)) { |
| 1743 | if (call->context[elem].destroy) { |
| 1744 | call->context[elem].destroy(call->context[elem].value); |
| 1745 | } |
Julien Boeuf | 83b0297 | 2015-05-20 22:50:34 -0700 | [diff] [blame] | 1746 | call->context[elem].value = value; |
| 1747 | call->context[elem].destroy = destroy; |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 1748 | } |
| 1749 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1750 | void *grpc_call_context_get(grpc_call *call, grpc_context_index elem) { |
Julien Boeuf | 83b0297 | 2015-05-20 22:50:34 -0700 | [diff] [blame] | 1751 | return call->context[elem].value; |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 1752 | } |
Julien Boeuf | 9f218dd | 2015-04-23 10:24:02 -0700 | [diff] [blame] | 1753 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1754 | uint8_t grpc_call_is_client(grpc_call *call) { return call->is_client; } |
David Garcia Quintas | 13c2f6e | 2016-03-17 22:51:52 -0700 | [diff] [blame] | 1755 | |
| 1756 | grpc_compression_algorithm grpc_call_compression_for_level( |
| 1757 | grpc_call *call, grpc_compression_level level) { |
David Garcia Quintas | 7d2c733 | 2016-03-18 11:37:14 -0700 | [diff] [blame] | 1758 | gpr_mu_lock(&call->mu); |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1759 | grpc_compression_algorithm algo = |
| 1760 | compression_algorithm_for_level_locked(call, level); |
David Garcia Quintas | 7d2c733 | 2016-03-18 11:37:14 -0700 | [diff] [blame] | 1761 | gpr_mu_unlock(&call->mu); |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1762 | return algo; |
David Garcia Quintas | 13c2f6e | 2016-03-17 22:51:52 -0700 | [diff] [blame] | 1763 | } |
Yuchen Zeng | 2e7d957 | 2016-04-15 17:29:57 -0700 | [diff] [blame] | 1764 | |
| 1765 | const char *grpc_call_error_to_string(grpc_call_error error) { |
| 1766 | switch (error) { |
| 1767 | case GRPC_CALL_ERROR: |
| 1768 | return "GRPC_CALL_ERROR"; |
| 1769 | case GRPC_CALL_ERROR_ALREADY_ACCEPTED: |
| 1770 | return "GRPC_CALL_ERROR_ALREADY_ACCEPTED"; |
| 1771 | case GRPC_CALL_ERROR_ALREADY_FINISHED: |
| 1772 | return "GRPC_CALL_ERROR_ALREADY_FINISHED"; |
| 1773 | case GRPC_CALL_ERROR_ALREADY_INVOKED: |
| 1774 | return "GRPC_CALL_ERROR_ALREADY_INVOKED"; |
| 1775 | case GRPC_CALL_ERROR_BATCH_TOO_BIG: |
| 1776 | return "GRPC_CALL_ERROR_BATCH_TOO_BIG"; |
| 1777 | case GRPC_CALL_ERROR_INVALID_FLAGS: |
| 1778 | return "GRPC_CALL_ERROR_INVALID_FLAGS"; |
| 1779 | case GRPC_CALL_ERROR_INVALID_MESSAGE: |
| 1780 | return "GRPC_CALL_ERROR_INVALID_MESSAGE"; |
| 1781 | case GRPC_CALL_ERROR_INVALID_METADATA: |
| 1782 | return "GRPC_CALL_ERROR_INVALID_METADATA"; |
| 1783 | case GRPC_CALL_ERROR_NOT_INVOKED: |
| 1784 | return "GRPC_CALL_ERROR_NOT_INVOKED"; |
| 1785 | case GRPC_CALL_ERROR_NOT_ON_CLIENT: |
| 1786 | return "GRPC_CALL_ERROR_NOT_ON_CLIENT"; |
| 1787 | case GRPC_CALL_ERROR_NOT_ON_SERVER: |
| 1788 | return "GRPC_CALL_ERROR_NOT_ON_SERVER"; |
| 1789 | case GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE: |
| 1790 | return "GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE"; |
| 1791 | case GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH: |
| 1792 | return "GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH"; |
| 1793 | case GRPC_CALL_ERROR_TOO_MANY_OPERATIONS: |
| 1794 | return "GRPC_CALL_ERROR_TOO_MANY_OPERATIONS"; |
| 1795 | case GRPC_CALL_OK: |
| 1796 | return "GRPC_CALL_OK"; |
Yuchen Zeng | 2e7d957 | 2016-04-15 17:29:57 -0700 | [diff] [blame] | 1797 | } |
Yuchen Zeng | f02bada | 2016-04-19 14:12:27 -0700 | [diff] [blame] | 1798 | GPR_UNREACHABLE_CODE(return "GRPC_CALL_ERROR_UNKNOW"); |
Yuchen Zeng | 2e7d957 | 2016-04-15 17:29:57 -0700 | [diff] [blame] | 1799 | } |