Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [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 | */ |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 33 | #include <assert.h> |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include <grpc/compression.h> |
| 39 | #include <grpc/support/alloc.h> |
| 40 | #include <grpc/support/log.h> |
| 41 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 43 | #include "src/core/census/grpc_context.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | #include "src/core/channel/channel_stack.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 45 | #include "src/core/iomgr/alarm.h" |
Craig Tiller | 5096849 | 2015-04-28 17:05:49 -0700 | [diff] [blame] | 46 | #include "src/core/profiling/timers.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 47 | #include "src/core/support/string.h" |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 48 | #include "src/core/surface/byte_buffer_queue.h" |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 49 | #include "src/core/surface/call.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 50 | #include "src/core/surface/channel.h" |
| 51 | #include "src/core/surface/completion_queue.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | |
Craig Tiller | 1b01167 | 2015-07-10 10:41:44 -0700 | [diff] [blame] | 53 | /** The maximum number of completions possible. |
| 54 | Based upon the maximum number of individually queueable ops in the batch |
| 55 | api: |
| 56 | - initial metadata send |
| 57 | - message send |
| 58 | - status/close send (depending on client/server) |
| 59 | - initial metadata recv |
| 60 | - message recv |
| 61 | - status/close recv (depending on client/server) */ |
| 62 | #define MAX_CONCURRENT_COMPLETIONS 6 |
| 63 | |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 64 | typedef enum { REQ_INITIAL = 0, REQ_READY, REQ_DONE } req_state; |
| 65 | |
| 66 | typedef enum { |
| 67 | SEND_NOTHING, |
| 68 | SEND_INITIAL_METADATA, |
Craig Tiller | 7bd9b99 | 2015-02-04 08:38:02 -0800 | [diff] [blame] | 69 | SEND_BUFFERED_INITIAL_METADATA, |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 70 | SEND_MESSAGE, |
Craig Tiller | 7bd9b99 | 2015-02-04 08:38:02 -0800 | [diff] [blame] | 71 | SEND_BUFFERED_MESSAGE, |
Craig Tiller | 1c14190 | 2015-01-31 08:51:54 -0800 | [diff] [blame] | 72 | SEND_TRAILING_METADATA_AND_FINISH, |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 73 | SEND_FINISH |
| 74 | } send_action; |
| 75 | |
| 76 | typedef struct { |
| 77 | grpc_ioreq_completion_func on_complete; |
| 78 | void *user_data; |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 79 | int success; |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 80 | } completed_request; |
| 81 | |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 82 | /* See request_set in grpc_call below for a description */ |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 83 | #define REQSET_EMPTY 'X' |
| 84 | #define REQSET_DONE 'Y' |
| 85 | |
| 86 | #define MAX_SEND_INITIAL_METADATA_COUNT 3 |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 87 | |
Craig Tiller | c18c56e | 2015-02-02 15:59:13 -0800 | [diff] [blame] | 88 | typedef struct { |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 89 | /* Overall status of the operation: starts OK, may degrade to |
| 90 | non-OK */ |
Craig Tiller | cae5bf5 | 2015-07-02 08:46:59 -0700 | [diff] [blame] | 91 | gpr_uint8 success; |
Craig Tiller | d642dcf | 2015-02-03 20:39:09 -0800 | [diff] [blame] | 92 | /* a bit mask of which request ops are needed (1u << opid) */ |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 93 | gpr_uint16 need_mask; |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 94 | /* a bit mask of which request ops are now completed */ |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 95 | gpr_uint16 complete_mask; |
Craig Tiller | cae5bf5 | 2015-07-02 08:46:59 -0700 | [diff] [blame] | 96 | /* Completion function to call at the end of the operation */ |
| 97 | grpc_ioreq_completion_func on_complete; |
| 98 | void *user_data; |
Craig Tiller | c18c56e | 2015-02-02 15:59:13 -0800 | [diff] [blame] | 99 | } reqinfo_master; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 100 | |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 101 | /* Status data for a request can come from several sources; this |
| 102 | enumerates them all, and acts as a priority sorting for which |
| 103 | status to return to the application - earlier entries override |
| 104 | later ones */ |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 105 | typedef enum { |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 106 | /* Status came from the application layer overriding whatever |
| 107 | the wire says */ |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 108 | STATUS_FROM_API_OVERRIDE = 0, |
Craig Tiller | 8b282cb | 2015-04-17 14:57:44 -0700 | [diff] [blame] | 109 | /* Status was created by some internal channel stack operation */ |
| 110 | STATUS_FROM_CORE, |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 111 | /* Status came from 'the wire' - or somewhere below the surface |
| 112 | layer */ |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 113 | STATUS_FROM_WIRE, |
Craig Tiller | aea081f | 2015-06-11 14:19:33 -0700 | [diff] [blame] | 114 | /* Status came from the server sending status */ |
| 115 | STATUS_FROM_SERVER_STATUS, |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 116 | STATUS_SOURCE_COUNT |
| 117 | } status_source; |
| 118 | |
| 119 | typedef struct { |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 120 | gpr_uint8 is_set; |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 121 | grpc_status_code code; |
| 122 | grpc_mdstr *details; |
| 123 | } received_status; |
| 124 | |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 125 | /* How far through the GRPC stream have we read? */ |
| 126 | typedef enum { |
| 127 | /* We are still waiting for initial metadata to complete */ |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 128 | READ_STATE_INITIAL = 0, |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 129 | /* We have gotten initial metadata, and are reading either |
| 130 | messages or trailing metadata */ |
| 131 | READ_STATE_GOT_INITIAL_METADATA, |
| 132 | /* The stream is closed for reading */ |
| 133 | READ_STATE_READ_CLOSED, |
| 134 | /* The stream is closed for reading & writing */ |
| 135 | READ_STATE_STREAM_CLOSED |
| 136 | } read_state; |
| 137 | |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 138 | typedef enum { |
| 139 | WRITE_STATE_INITIAL = 0, |
| 140 | WRITE_STATE_STARTED, |
| 141 | WRITE_STATE_WRITE_CLOSED |
| 142 | } write_state; |
| 143 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 144 | struct grpc_call { |
| 145 | grpc_completion_queue *cq; |
| 146 | grpc_channel *channel; |
| 147 | grpc_mdctx *metadata_context; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 148 | /* TODO(ctiller): share with cq if possible? */ |
| 149 | gpr_mu mu; |
Craig Tiller | 396fab2 | 2015-07-09 14:14:09 -0700 | [diff] [blame] | 150 | gpr_mu completion_mu; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 151 | |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 152 | /* how far through the stream have we read? */ |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 153 | read_state read_state; |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 154 | /* how far through the stream have we written? */ |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 155 | write_state write_state; |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 156 | /* client or server call */ |
| 157 | gpr_uint8 is_client; |
| 158 | /* is the alarm set */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 159 | gpr_uint8 have_alarm; |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 160 | /* are we currently performing a send operation */ |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 161 | gpr_uint8 sending; |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 162 | /* are we currently performing a recv operation */ |
| 163 | gpr_uint8 receiving; |
Craig Tiller | 991ca9f | 2015-03-03 09:59:22 -0800 | [diff] [blame] | 164 | /* are we currently completing requests */ |
| 165 | gpr_uint8 completing; |
Craig Tiller | f3fba74 | 2015-06-11 09:36:33 -0700 | [diff] [blame] | 166 | /** has grpc_call_destroy been called */ |
| 167 | gpr_uint8 destroy_called; |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 168 | /* pairs with completed_requests */ |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 169 | gpr_uint8 num_completed_requests; |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 170 | /* are we currently reading a message? */ |
| 171 | gpr_uint8 reading_message; |
Craig Tiller | bac4142 | 2015-05-29 16:32:28 -0700 | [diff] [blame] | 172 | /* have we bound a pollset yet? */ |
| 173 | gpr_uint8 bound_pollset; |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 174 | /* is an error status set */ |
| 175 | gpr_uint8 error_status_set; |
Craig Tiller | 77f0461 | 2015-07-01 13:39:45 -0700 | [diff] [blame] | 176 | /** should the alarm be cancelled */ |
| 177 | gpr_uint8 cancel_alarm; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 178 | /** bitmask of allocated completion events in completions */ |
| 179 | gpr_uint8 allocated_completions; |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 180 | |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 181 | /* flags with bits corresponding to write states allowing us to determine |
| 182 | what was sent */ |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 183 | gpr_uint16 last_send_contains; |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 184 | /* cancel with this status on the next outgoing transport op */ |
| 185 | grpc_status_code cancel_with_status; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 186 | |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 187 | /* Active ioreqs. |
| 188 | request_set and request_data contain one element per active ioreq |
| 189 | operation. |
Craig Tiller | ebf94bf | 2015-02-05 08:48:46 -0800 | [diff] [blame] | 190 | |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 191 | request_set[op] is an integer specifying a set of operations to which |
| 192 | the request belongs: |
Craig Tiller | ebf94bf | 2015-02-05 08:48:46 -0800 | [diff] [blame] | 193 | - if it is < GRPC_IOREQ_OP_COUNT, then this operation is pending |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 194 | completion, and the integer represents to which group of operations |
| 195 | the ioreq belongs. Each group is represented by one master, and the |
| 196 | integer in request_set is an index into masters to find the master |
| 197 | data. |
| 198 | - if it is REQSET_EMPTY, the ioreq op is inactive and available to be |
| 199 | started |
| 200 | - finally, if request_set[op] is REQSET_DONE, then the operation is |
| 201 | complete and unavailable to be started again |
Craig Tiller | ebf94bf | 2015-02-05 08:48:46 -0800 | [diff] [blame] | 202 | |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 203 | request_data[op] is the request data as supplied by the initiator of |
| 204 | a request, and is valid iff request_set[op] <= GRPC_IOREQ_OP_COUNT. |
| 205 | The set fields are as per the request type specified by op. |
| 206 | |
Craig Tiller | d673162 | 2015-02-03 22:44:13 -0800 | [diff] [blame] | 207 | Finally, one element of masters is set per active _set_ of ioreq |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 208 | operations. It describes work left outstanding, result status, and |
| 209 | what work to perform upon operation completion. As one ioreq of each |
| 210 | op type can be active at once, by convention we choose the first element |
Craig Tiller | d673162 | 2015-02-03 22:44:13 -0800 | [diff] [blame] | 211 | of the group to be the master -- ie the master of in-progress operation |
| 212 | op is masters[request_set[op]]. This allows constant time allocation |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 213 | and a strong upper bound of a count of masters to be calculated. */ |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 214 | gpr_uint8 request_set[GRPC_IOREQ_OP_COUNT]; |
| 215 | grpc_ioreq_data request_data[GRPC_IOREQ_OP_COUNT]; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 216 | gpr_uint32 request_flags[GRPC_IOREQ_OP_COUNT]; |
Craig Tiller | c18c56e | 2015-02-02 15:59:13 -0800 | [diff] [blame] | 217 | reqinfo_master masters[GRPC_IOREQ_OP_COUNT]; |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 218 | |
| 219 | /* Dynamic array of ioreq's that have completed: the count of |
| 220 | elements is queued in num_completed_requests. |
| 221 | This list is built up under lock(), and flushed entirely during |
| 222 | unlock(). |
| 223 | We know the upper bound of the number of elements as we can only |
| 224 | have one ioreq of each type active at once. */ |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 225 | completed_request completed_requests[GRPC_IOREQ_OP_COUNT]; |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 226 | /* Incoming buffer of messages */ |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 227 | grpc_byte_buffer_queue incoming_queue; |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 228 | /* Buffered read metadata waiting to be returned to the application. |
| 229 | Element 0 is initial metadata, element 1 is trailing metadata. */ |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 230 | grpc_metadata_array buffered_metadata[2]; |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 231 | /* All metadata received - unreffed at once at the end of the call */ |
Craig Tiller | 3a4749f | 2015-01-30 07:51:45 -0800 | [diff] [blame] | 232 | grpc_mdelem **owned_metadata; |
| 233 | size_t owned_metadata_count; |
| 234 | size_t owned_metadata_capacity; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 235 | |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 236 | /* Received call statuses from various sources */ |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 237 | received_status status[STATUS_SOURCE_COUNT]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 238 | |
David Garcia Quintas | 8a18709 | 2015-07-01 14:52:44 -0700 | [diff] [blame] | 239 | /* Compression algorithm for the call */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 240 | grpc_compression_algorithm compression_algorithm; |
David Garcia Quintas | db94b27 | 2015-06-15 18:37:01 -0700 | [diff] [blame] | 241 | |
Julien Boeuf | c6f8d0a | 2015-05-11 22:40:02 -0700 | [diff] [blame] | 242 | /* Contexts for various subsystems (security, tracing, ...). */ |
Julien Boeuf | 83b0297 | 2015-05-20 22:50:34 -0700 | [diff] [blame] | 243 | grpc_call_context_element context[GRPC_CONTEXT_COUNT]; |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 244 | |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 245 | /* Deadline alarm - if have_alarm is non-zero */ |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 246 | grpc_alarm alarm; |
| 247 | |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 248 | /* Call refcount - to keep the call alive during asynchronous operations */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 249 | gpr_refcount internal_refcount; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 250 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 251 | grpc_linked_mdelem send_initial_metadata[MAX_SEND_INITIAL_METADATA_COUNT]; |
| 252 | grpc_linked_mdelem status_link; |
| 253 | grpc_linked_mdelem details_link; |
| 254 | size_t send_initial_metadata_count; |
| 255 | gpr_timespec send_deadline; |
| 256 | |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 257 | grpc_stream_op_buffer send_ops; |
| 258 | grpc_stream_op_buffer recv_ops; |
| 259 | grpc_stream_state recv_state; |
| 260 | |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 261 | gpr_slice_buffer incoming_message; |
| 262 | gpr_uint32 incoming_message_length; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 263 | gpr_uint32 incoming_message_flags; |
David Garcia Quintas | 284488b | 2015-05-28 16:27:39 -0700 | [diff] [blame] | 264 | grpc_iomgr_closure destroy_closure; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 265 | grpc_iomgr_closure on_done_recv; |
| 266 | grpc_iomgr_closure on_done_send; |
| 267 | grpc_iomgr_closure on_done_bind; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 268 | |
| 269 | /** completion events - for completion queue use */ |
Craig Tiller | 1b01167 | 2015-07-10 10:41:44 -0700 | [diff] [blame] | 270 | grpc_cq_completion completions[MAX_CONCURRENT_COMPLETIONS]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 271 | }; |
| 272 | |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 273 | #define CALL_STACK_FROM_CALL(call) ((grpc_call_stack *)((call) + 1)) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 274 | #define CALL_FROM_CALL_STACK(call_stack) (((grpc_call *)(call_stack)) - 1) |
| 275 | #define CALL_ELEM_FROM_CALL(call, idx) \ |
| 276 | grpc_call_stack_element(CALL_STACK_FROM_CALL(call), idx) |
| 277 | #define CALL_FROM_TOP_ELEM(top_elem) \ |
| 278 | CALL_FROM_CALL_STACK(grpc_call_stack_from_top_element(top_elem)) |
| 279 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 280 | static void set_deadline_alarm(grpc_call *call, gpr_timespec deadline); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 281 | static void call_on_done_recv(void *call, int success); |
| 282 | static void call_on_done_send(void *call, int success); |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 283 | static int fill_send_ops(grpc_call *call, grpc_transport_stream_op *op); |
| 284 | static void execute_op(grpc_call *call, grpc_transport_stream_op *op); |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 285 | static void recv_metadata(grpc_call *call, grpc_metadata_batch *metadata); |
| 286 | static void finish_read_ops(grpc_call *call); |
Craig Tiller | f9fae98 | 2015-05-29 23:02:18 -0700 | [diff] [blame] | 287 | static grpc_call_error cancel_with_status(grpc_call *c, grpc_status_code status, |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 288 | const char *description); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 289 | static void finished_loose_op(void *call, int success); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 290 | |
Craig Tiller | bac4142 | 2015-05-29 16:32:28 -0700 | [diff] [blame] | 291 | static void lock(grpc_call *call); |
| 292 | static void unlock(grpc_call *call); |
| 293 | |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 294 | grpc_call *grpc_call_create(grpc_channel *channel, grpc_completion_queue *cq, |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 295 | const void *server_transport_data, |
| 296 | grpc_mdelem **add_initial_metadata, |
| 297 | size_t add_initial_metadata_count, |
| 298 | gpr_timespec send_deadline) { |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 299 | size_t i; |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 300 | grpc_transport_stream_op initial_op; |
| 301 | grpc_transport_stream_op *initial_op_ptr = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 302 | grpc_channel_stack *channel_stack = grpc_channel_get_channel_stack(channel); |
| 303 | grpc_call *call = |
| 304 | gpr_malloc(sizeof(grpc_call) + channel_stack->call_stack_size); |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 305 | memset(call, 0, sizeof(grpc_call)); |
| 306 | gpr_mu_init(&call->mu); |
Craig Tiller | 396fab2 | 2015-07-09 14:14:09 -0700 | [diff] [blame] | 307 | gpr_mu_init(&call->completion_mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 308 | call->channel = channel; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 309 | call->cq = cq; |
Craig Tiller | 09f2bdc | 2015-06-03 00:50:56 -0700 | [diff] [blame] | 310 | if (cq) { |
Craig Tiller | 7ae5a38 | 2015-06-03 07:58:58 -0700 | [diff] [blame] | 311 | GRPC_CQ_INTERNAL_REF(cq, "bind"); |
Craig Tiller | 09f2bdc | 2015-06-03 00:50:56 -0700 | [diff] [blame] | 312 | } |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 313 | call->is_client = server_transport_data == NULL; |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 314 | for (i = 0; i < GRPC_IOREQ_OP_COUNT; i++) { |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 315 | call->request_set[i] = REQSET_EMPTY; |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 316 | } |
Craig Tiller | 23aa6c4 | 2015-01-27 17:16:12 -0800 | [diff] [blame] | 317 | if (call->is_client) { |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 318 | call->request_set[GRPC_IOREQ_SEND_TRAILING_METADATA] = REQSET_DONE; |
| 319 | call->request_set[GRPC_IOREQ_SEND_STATUS] = REQSET_DONE; |
Craig Tiller | 23aa6c4 | 2015-01-27 17:16:12 -0800 | [diff] [blame] | 320 | } |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 321 | GPR_ASSERT(add_initial_metadata_count < MAX_SEND_INITIAL_METADATA_COUNT); |
| 322 | for (i = 0; i < add_initial_metadata_count; i++) { |
| 323 | call->send_initial_metadata[i].md = add_initial_metadata[i]; |
| 324 | } |
| 325 | call->send_initial_metadata_count = add_initial_metadata_count; |
| 326 | call->send_deadline = send_deadline; |
Craig Tiller | 9ec2a52 | 2015-05-29 22:46:54 -0700 | [diff] [blame] | 327 | GRPC_CHANNEL_INTERNAL_REF(channel, "call"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 328 | call->metadata_context = grpc_channel_get_metadata_context(channel); |
Craig Tiller | fbf5be2 | 2015-04-22 16:17:09 -0700 | [diff] [blame] | 329 | grpc_sopb_init(&call->send_ops); |
| 330 | grpc_sopb_init(&call->recv_ops); |
Craig Tiller | fa4f994 | 2015-04-23 15:22:09 -0700 | [diff] [blame] | 331 | gpr_slice_buffer_init(&call->incoming_message); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 332 | grpc_iomgr_closure_init(&call->on_done_recv, call_on_done_recv, call); |
| 333 | grpc_iomgr_closure_init(&call->on_done_send, call_on_done_send, call); |
| 334 | grpc_iomgr_closure_init(&call->on_done_bind, finished_loose_op, call); |
Craig Tiller | f3fba74 | 2015-06-11 09:36:33 -0700 | [diff] [blame] | 335 | /* dropped in destroy and when READ_STATE_STREAM_CLOSED received */ |
| 336 | gpr_ref_init(&call->internal_refcount, 2); |
Craig Tiller | 7e8489a | 2015-04-23 12:41:16 -0700 | [diff] [blame] | 337 | /* server hack: start reads immediately so we can get initial metadata. |
| 338 | TODO(ctiller): figure out a cleaner solution */ |
| 339 | if (!call->is_client) { |
| 340 | memset(&initial_op, 0, sizeof(initial_op)); |
| 341 | initial_op.recv_ops = &call->recv_ops; |
| 342 | initial_op.recv_state = &call->recv_state; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 343 | initial_op.on_done_recv = &call->on_done_recv; |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 344 | initial_op.context = call->context; |
Craig Tiller | 7e8489a | 2015-04-23 12:41:16 -0700 | [diff] [blame] | 345 | call->receiving = 1; |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 346 | GRPC_CALL_INTERNAL_REF(call, "receiving"); |
Craig Tiller | 7e8489a | 2015-04-23 12:41:16 -0700 | [diff] [blame] | 347 | initial_op_ptr = &initial_op; |
| 348 | } |
| 349 | grpc_call_stack_init(channel_stack, server_transport_data, initial_op_ptr, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 350 | CALL_STACK_FROM_CALL(call)); |
Craig Tiller | 6a7626c | 2015-07-19 22:21:41 -0700 | [diff] [blame] | 351 | if (gpr_time_cmp(send_deadline, gpr_inf_future(send_deadline.clock_type)) != 0) { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 352 | set_deadline_alarm(call, send_deadline); |
| 353 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 354 | return call; |
| 355 | } |
| 356 | |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 357 | void grpc_call_set_completion_queue(grpc_call *call, |
| 358 | grpc_completion_queue *cq) { |
Craig Tiller | bac4142 | 2015-05-29 16:32:28 -0700 | [diff] [blame] | 359 | lock(call); |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 360 | call->cq = cq; |
Craig Tiller | 09f2bdc | 2015-06-03 00:50:56 -0700 | [diff] [blame] | 361 | if (cq) { |
Craig Tiller | 7ae5a38 | 2015-06-03 07:58:58 -0700 | [diff] [blame] | 362 | GRPC_CQ_INTERNAL_REF(cq, "bind"); |
Craig Tiller | 09f2bdc | 2015-06-03 00:50:56 -0700 | [diff] [blame] | 363 | } |
Craig Tiller | bac4142 | 2015-05-29 16:32:28 -0700 | [diff] [blame] | 364 | unlock(call); |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 367 | grpc_completion_queue *grpc_call_get_completion_queue(grpc_call *call) { |
| 368 | return call->cq; |
| 369 | } |
| 370 | |
Craig Tiller | b4580d4 | 2015-07-10 10:42:34 -0700 | [diff] [blame] | 371 | static grpc_cq_completion *allocate_completion(grpc_call *call) { |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 372 | gpr_uint8 i; |
Craig Tiller | 396fab2 | 2015-07-09 14:14:09 -0700 | [diff] [blame] | 373 | gpr_mu_lock(&call->completion_mu); |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 374 | for (i = 0; i < GPR_ARRAY_SIZE(call->completions); i++) { |
| 375 | if (call->allocated_completions & (1u << i)) { |
| 376 | continue; |
| 377 | } |
| 378 | call->allocated_completions |= 1u << i; |
Craig Tiller | 396fab2 | 2015-07-09 14:14:09 -0700 | [diff] [blame] | 379 | gpr_mu_unlock(&call->completion_mu); |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 380 | return &call->completions[i]; |
| 381 | } |
| 382 | gpr_log(GPR_ERROR, "should never reach here"); |
| 383 | abort(); |
| 384 | } |
| 385 | |
Craig Tiller | b4580d4 | 2015-07-10 10:42:34 -0700 | [diff] [blame] | 386 | static void done_completion(void *call, grpc_cq_completion *completion) { |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 387 | grpc_call *c = call; |
Craig Tiller | 396fab2 | 2015-07-09 14:14:09 -0700 | [diff] [blame] | 388 | gpr_mu_lock(&c->completion_mu); |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 389 | c->allocated_completions &= ~(1u << (completion - c->completions)); |
Craig Tiller | 396fab2 | 2015-07-09 14:14:09 -0700 | [diff] [blame] | 390 | gpr_mu_unlock(&c->completion_mu); |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 391 | GRPC_CALL_INTERNAL_UNREF(c, "completion", 1); |
| 392 | } |
| 393 | |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 394 | #ifdef GRPC_CALL_REF_COUNT_DEBUG |
| 395 | void grpc_call_internal_ref(grpc_call *c, const char *reason) { |
| 396 | gpr_log(GPR_DEBUG, "CALL: ref %p %d -> %d [%s]", c, |
| 397 | c->internal_refcount.count, c->internal_refcount.count + 1, reason); |
| 398 | #else |
| 399 | void grpc_call_internal_ref(grpc_call *c) { |
| 400 | #endif |
| 401 | gpr_ref(&c->internal_refcount); |
| 402 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 403 | |
Craig Tiller | aef25da | 2015-01-29 17:19:45 -0800 | [diff] [blame] | 404 | static void destroy_call(void *call, int ignored_success) { |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 405 | size_t i; |
Craig Tiller | aef25da | 2015-01-29 17:19:45 -0800 | [diff] [blame] | 406 | grpc_call *c = call; |
Craig Tiller | a454110 | 2015-01-29 11:46:11 -0800 | [diff] [blame] | 407 | grpc_call_stack_destroy(CALL_STACK_FROM_CALL(c)); |
Craig Tiller | 9ec2a52 | 2015-05-29 22:46:54 -0700 | [diff] [blame] | 408 | GRPC_CHANNEL_INTERNAL_UNREF(c->channel, "call"); |
Craig Tiller | a454110 | 2015-01-29 11:46:11 -0800 | [diff] [blame] | 409 | gpr_mu_destroy(&c->mu); |
Craig Tiller | 396fab2 | 2015-07-09 14:14:09 -0700 | [diff] [blame] | 410 | gpr_mu_destroy(&c->completion_mu); |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 411 | for (i = 0; i < STATUS_SOURCE_COUNT; i++) { |
| 412 | if (c->status[i].details) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 413 | GRPC_MDSTR_UNREF(c->status[i].details); |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 414 | } |
Craig Tiller | a454110 | 2015-01-29 11:46:11 -0800 | [diff] [blame] | 415 | } |
Craig Tiller | 3a4749f | 2015-01-30 07:51:45 -0800 | [diff] [blame] | 416 | for (i = 0; i < c->owned_metadata_count; i++) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 417 | GRPC_MDELEM_UNREF(c->owned_metadata[i]); |
Craig Tiller | 3a4749f | 2015-01-30 07:51:45 -0800 | [diff] [blame] | 418 | } |
| 419 | gpr_free(c->owned_metadata); |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 420 | for (i = 0; i < GPR_ARRAY_SIZE(c->buffered_metadata); i++) { |
| 421 | gpr_free(c->buffered_metadata[i].metadata); |
| 422 | } |
Craig Tiller | eb40a53 | 2015-04-17 16:46:20 -0700 | [diff] [blame] | 423 | for (i = 0; i < c->send_initial_metadata_count; i++) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 424 | GRPC_MDELEM_UNREF(c->send_initial_metadata[i].md); |
Craig Tiller | eb40a53 | 2015-04-17 16:46:20 -0700 | [diff] [blame] | 425 | } |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 426 | for (i = 0; i < GRPC_CONTEXT_COUNT; i++) { |
Julien Boeuf | 83b0297 | 2015-05-20 22:50:34 -0700 | [diff] [blame] | 427 | if (c->context[i].destroy) { |
| 428 | c->context[i].destroy(c->context[i].value); |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 429 | } |
| 430 | } |
Craig Tiller | b56ca8d | 2015-04-24 17:16:22 -0700 | [diff] [blame] | 431 | grpc_sopb_destroy(&c->send_ops); |
| 432 | grpc_sopb_destroy(&c->recv_ops); |
Craig Tiller | 37bbead | 2015-02-05 08:43:49 -0800 | [diff] [blame] | 433 | grpc_bbq_destroy(&c->incoming_queue); |
Craig Tiller | fa4f994 | 2015-04-23 15:22:09 -0700 | [diff] [blame] | 434 | gpr_slice_buffer_destroy(&c->incoming_message); |
Craig Tiller | 09f2bdc | 2015-06-03 00:50:56 -0700 | [diff] [blame] | 435 | if (c->cq) { |
Craig Tiller | 7ae5a38 | 2015-06-03 07:58:58 -0700 | [diff] [blame] | 436 | GRPC_CQ_INTERNAL_UNREF(c->cq, "bind"); |
Craig Tiller | 09f2bdc | 2015-06-03 00:50:56 -0700 | [diff] [blame] | 437 | } |
Craig Tiller | a454110 | 2015-01-29 11:46:11 -0800 | [diff] [blame] | 438 | gpr_free(c); |
| 439 | } |
| 440 | |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 441 | #ifdef GRPC_CALL_REF_COUNT_DEBUG |
| 442 | void grpc_call_internal_unref(grpc_call *c, const char *reason, |
| 443 | int allow_immediate_deletion) { |
| 444 | gpr_log(GPR_DEBUG, "CALL: unref %p %d -> %d [%s]", c, |
| 445 | c->internal_refcount.count, c->internal_refcount.count - 1, reason); |
| 446 | #else |
Craig Tiller | 64bc3fd | 2015-04-24 17:07:12 -0700 | [diff] [blame] | 447 | void grpc_call_internal_unref(grpc_call *c, int allow_immediate_deletion) { |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 448 | #endif |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 449 | if (gpr_unref(&c->internal_refcount)) { |
Craig Tiller | aef25da | 2015-01-29 17:19:45 -0800 | [diff] [blame] | 450 | if (allow_immediate_deletion) { |
| 451 | destroy_call(c, 1); |
| 452 | } else { |
David Garcia Quintas | 284488b | 2015-05-28 16:27:39 -0700 | [diff] [blame] | 453 | c->destroy_closure.cb = destroy_call; |
| 454 | c->destroy_closure.cb_arg = c; |
| 455 | grpc_iomgr_add_callback(&c->destroy_closure); |
Craig Tiller | aef25da | 2015-01-29 17:19:45 -0800 | [diff] [blame] | 456 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
Craig Tiller | 928fbc8 | 2015-01-29 15:06:42 -0800 | [diff] [blame] | 460 | static void set_status_code(grpc_call *call, status_source source, |
| 461 | gpr_uint32 status) { |
Craig Tiller | b8d3a31 | 2015-06-19 17:27:53 -0700 | [diff] [blame] | 462 | if (call->status[source].is_set) return; |
| 463 | |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 464 | call->status[source].is_set = 1; |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 465 | call->status[source].code = status; |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 466 | call->error_status_set = status != GRPC_STATUS_OK; |
Craig Tiller | 3054756 | 2015-02-05 17:04:51 -0800 | [diff] [blame] | 467 | |
Craig Tiller | d1abc81 | 2015-05-06 14:35:19 -0700 | [diff] [blame] | 468 | if (status != GRPC_STATUS_OK && !grpc_bbq_empty(&call->incoming_queue)) { |
Craig Tiller | 3054756 | 2015-02-05 17:04:51 -0800 | [diff] [blame] | 469 | grpc_bbq_flush(&call->incoming_queue); |
| 470 | } |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 471 | } |
| 472 | |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 473 | static void set_compression_algorithm(grpc_call *call, |
| 474 | grpc_compression_algorithm algo) { |
| 475 | call->compression_algorithm = algo; |
David Garcia Quintas | db94b27 | 2015-06-15 18:37:01 -0700 | [diff] [blame] | 476 | } |
| 477 | |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 478 | grpc_compression_algorithm grpc_call_get_compression_algorithm( |
| 479 | const grpc_call *call) { |
| 480 | return call->compression_algorithm; |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Craig Tiller | 928fbc8 | 2015-01-29 15:06:42 -0800 | [diff] [blame] | 483 | static void set_status_details(grpc_call *call, status_source source, |
| 484 | grpc_mdstr *status) { |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 485 | if (call->status[source].details != NULL) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 486 | GRPC_MDSTR_UNREF(call->status[source].details); |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 487 | } |
| 488 | call->status[source].details = status; |
| 489 | } |
| 490 | |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 491 | static int is_op_live(grpc_call *call, grpc_ioreq_op op) { |
| 492 | gpr_uint8 set = call->request_set[op]; |
| 493 | reqinfo_master *master; |
| 494 | if (set >= GRPC_IOREQ_OP_COUNT) return 0; |
| 495 | master = &call->masters[set]; |
Craig Tiller | d642dcf | 2015-02-03 20:39:09 -0800 | [diff] [blame] | 496 | return (master->complete_mask & (1u << op)) == 0; |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 499 | static void lock(grpc_call *call) { gpr_mu_lock(&call->mu); } |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 500 | |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 501 | static int need_more_data(grpc_call *call) { |
Craig Tiller | d4919d0 | 2015-05-20 16:08:18 -0700 | [diff] [blame] | 502 | if (call->read_state == READ_STATE_STREAM_CLOSED) return 0; |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 503 | /* TODO(ctiller): this needs some serious cleanup */ |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 504 | return is_op_live(call, GRPC_IOREQ_RECV_INITIAL_METADATA) || |
Craig Tiller | f9fae98 | 2015-05-29 23:02:18 -0700 | [diff] [blame] | 505 | (is_op_live(call, GRPC_IOREQ_RECV_MESSAGE) && |
| 506 | grpc_bbq_empty(&call->incoming_queue)) || |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 507 | is_op_live(call, GRPC_IOREQ_RECV_TRAILING_METADATA) || |
| 508 | is_op_live(call, GRPC_IOREQ_RECV_STATUS) || |
| 509 | is_op_live(call, GRPC_IOREQ_RECV_STATUS_DETAILS) || |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 510 | (is_op_live(call, GRPC_IOREQ_RECV_CLOSE) && |
| 511 | grpc_bbq_empty(&call->incoming_queue)) || |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 512 | (call->write_state == WRITE_STATE_INITIAL && !call->is_client) || |
Craig Tiller | f1bff01 | 2015-07-06 11:20:50 -0700 | [diff] [blame] | 513 | (call->cancel_with_status != GRPC_STATUS_OK) || call->destroy_called; |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 516 | static void unlock(grpc_call *call) { |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 517 | grpc_transport_stream_op op; |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 518 | completed_request completed_requests[GRPC_IOREQ_OP_COUNT]; |
Craig Tiller | 991ca9f | 2015-03-03 09:59:22 -0800 | [diff] [blame] | 519 | int completing_requests = 0; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 520 | int start_op = 0; |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 521 | int i; |
Craig Tiller | 4efb696 | 2015-06-03 09:32:41 -0700 | [diff] [blame] | 522 | const gpr_uint32 MAX_RECV_PEEK_AHEAD = 65536; |
| 523 | size_t buffered_bytes; |
Craig Tiller | 77f0461 | 2015-07-01 13:39:45 -0700 | [diff] [blame] | 524 | int cancel_alarm = 0; |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 525 | |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 526 | memset(&op, 0, sizeof(op)); |
| 527 | |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 528 | op.cancel_with_status = call->cancel_with_status; |
| 529 | start_op = op.cancel_with_status != GRPC_STATUS_OK; |
| 530 | call->cancel_with_status = GRPC_STATUS_OK; /* reset */ |
Craig Tiller | bac4142 | 2015-05-29 16:32:28 -0700 | [diff] [blame] | 531 | |
Craig Tiller | 77f0461 | 2015-07-01 13:39:45 -0700 | [diff] [blame] | 532 | cancel_alarm = call->cancel_alarm; |
| 533 | call->cancel_alarm = 0; |
| 534 | |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 535 | if (!call->receiving && need_more_data(call)) { |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 536 | op.recv_ops = &call->recv_ops; |
| 537 | op.recv_state = &call->recv_state; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 538 | op.on_done_recv = &call->on_done_recv; |
Craig Tiller | 4efb696 | 2015-06-03 09:32:41 -0700 | [diff] [blame] | 539 | if (grpc_bbq_empty(&call->incoming_queue) && call->reading_message) { |
| 540 | op.max_recv_bytes = call->incoming_message_length - |
| 541 | call->incoming_message.length + MAX_RECV_PEEK_AHEAD; |
| 542 | } else { |
| 543 | buffered_bytes = grpc_bbq_bytes(&call->incoming_queue); |
| 544 | if (buffered_bytes > MAX_RECV_PEEK_AHEAD) { |
| 545 | op.max_recv_bytes = 0; |
| 546 | } else { |
| 547 | op.max_recv_bytes = MAX_RECV_PEEK_AHEAD - buffered_bytes; |
| 548 | } |
| 549 | } |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 550 | call->receiving = 1; |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 551 | GRPC_CALL_INTERNAL_REF(call, "receiving"); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 552 | start_op = 1; |
| 553 | } |
| 554 | |
| 555 | if (!call->sending) { |
| 556 | if (fill_send_ops(call, &op)) { |
| 557 | call->sending = 1; |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 558 | GRPC_CALL_INTERNAL_REF(call, "sending"); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 559 | start_op = 1; |
| 560 | } |
Craig Tiller | cffbcb7 | 2015-01-29 23:16:33 -0800 | [diff] [blame] | 561 | } |
Craig Tiller | 2e10357 | 2015-01-29 14:12:07 -0800 | [diff] [blame] | 562 | |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 563 | if (!call->bound_pollset && call->cq && (!call->is_client || start_op)) { |
| 564 | call->bound_pollset = 1; |
| 565 | op.bind_pollset = grpc_cq_pollset(call->cq); |
| 566 | start_op = 1; |
| 567 | } |
| 568 | |
Craig Tiller | 991ca9f | 2015-03-03 09:59:22 -0800 | [diff] [blame] | 569 | if (!call->completing && call->num_completed_requests != 0) { |
| 570 | completing_requests = call->num_completed_requests; |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 571 | memcpy(completed_requests, call->completed_requests, |
| 572 | sizeof(completed_requests)); |
| 573 | call->num_completed_requests = 0; |
Craig Tiller | 991ca9f | 2015-03-03 09:59:22 -0800 | [diff] [blame] | 574 | call->completing = 1; |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 575 | GRPC_CALL_INTERNAL_REF(call, "completing"); |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 578 | gpr_mu_unlock(&call->mu); |
| 579 | |
Craig Tiller | 77f0461 | 2015-07-01 13:39:45 -0700 | [diff] [blame] | 580 | if (cancel_alarm) { |
| 581 | grpc_alarm_cancel(&call->alarm); |
| 582 | } |
| 583 | |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 584 | if (start_op) { |
| 585 | execute_op(call, &op); |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Craig Tiller | 991ca9f | 2015-03-03 09:59:22 -0800 | [diff] [blame] | 588 | if (completing_requests > 0) { |
| 589 | for (i = 0; i < completing_requests; i++) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 590 | completed_requests[i].on_complete(call, completed_requests[i].success, |
Craig Tiller | 991ca9f | 2015-03-03 09:59:22 -0800 | [diff] [blame] | 591 | completed_requests[i].user_data); |
| 592 | } |
| 593 | lock(call); |
| 594 | call->completing = 0; |
| 595 | unlock(call); |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 596 | GRPC_CALL_INTERNAL_UNREF(call, "completing", 0); |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 597 | } |
| 598 | } |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 599 | |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 600 | static void get_final_status(grpc_call *call, grpc_ioreq_data out) { |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 601 | int i; |
| 602 | for (i = 0; i < STATUS_SOURCE_COUNT; i++) { |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 603 | if (call->status[i].is_set) { |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 604 | out.recv_status.set_value(call->status[i].code, |
| 605 | out.recv_status.user_data); |
| 606 | return; |
| 607 | } |
| 608 | } |
Craig Tiller | de34316 | 2015-02-09 23:37:22 -0800 | [diff] [blame] | 609 | if (call->is_client) { |
| 610 | out.recv_status.set_value(GRPC_STATUS_UNKNOWN, out.recv_status.user_data); |
| 611 | } else { |
| 612 | out.recv_status.set_value(GRPC_STATUS_OK, out.recv_status.user_data); |
| 613 | } |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | static void get_final_details(grpc_call *call, grpc_ioreq_data out) { |
| 617 | int i; |
| 618 | for (i = 0; i < STATUS_SOURCE_COUNT; i++) { |
| 619 | if (call->status[i].is_set) { |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 620 | if (call->status[i].details) { |
| 621 | gpr_slice details = call->status[i].details->slice; |
| 622 | size_t len = GPR_SLICE_LENGTH(details); |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 623 | if (len + 1 > *out.recv_status_details.details_capacity) { |
| 624 | *out.recv_status_details.details_capacity = GPR_MAX( |
| 625 | len + 1, *out.recv_status_details.details_capacity * 3 / 2); |
| 626 | *out.recv_status_details.details = |
| 627 | gpr_realloc(*out.recv_status_details.details, |
| 628 | *out.recv_status_details.details_capacity); |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 629 | } |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 630 | memcpy(*out.recv_status_details.details, GPR_SLICE_START_PTR(details), |
| 631 | len); |
| 632 | (*out.recv_status_details.details)[len] = 0; |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 633 | } else { |
| 634 | goto no_details; |
| 635 | } |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 636 | return; |
| 637 | } |
| 638 | } |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 639 | |
| 640 | no_details: |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 641 | if (0 == *out.recv_status_details.details_capacity) { |
| 642 | *out.recv_status_details.details_capacity = 8; |
| 643 | *out.recv_status_details.details = |
| 644 | gpr_malloc(*out.recv_status_details.details_capacity); |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 645 | } |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 646 | **out.recv_status_details.details = 0; |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 647 | } |
| 648 | |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 649 | static void finish_live_ioreq_op(grpc_call *call, grpc_ioreq_op op, |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 650 | int success) { |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 651 | completed_request *cr; |
| 652 | gpr_uint8 master_set = call->request_set[op]; |
| 653 | reqinfo_master *master; |
| 654 | size_t i; |
| 655 | /* ioreq is live: we need to do something */ |
| 656 | master = &call->masters[master_set]; |
Craig Tiller | d642dcf | 2015-02-03 20:39:09 -0800 | [diff] [blame] | 657 | master->complete_mask |= 1u << op; |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 658 | if (!success) { |
| 659 | master->success = 0; |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 660 | } |
| 661 | if (master->complete_mask == master->need_mask) { |
| 662 | for (i = 0; i < GRPC_IOREQ_OP_COUNT; i++) { |
| 663 | if (call->request_set[i] != master_set) { |
| 664 | continue; |
| 665 | } |
| 666 | call->request_set[i] = REQSET_DONE; |
| 667 | switch ((grpc_ioreq_op)i) { |
| 668 | case GRPC_IOREQ_RECV_MESSAGE: |
| 669 | case GRPC_IOREQ_SEND_MESSAGE: |
Craig Tiller | fd7166d | 2015-05-19 10:23:03 -0700 | [diff] [blame] | 670 | call->request_set[i] = REQSET_EMPTY; |
| 671 | if (!master->success) { |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 672 | call->write_state = WRITE_STATE_WRITE_CLOSED; |
| 673 | } |
| 674 | break; |
Craig Tiller | aea081f | 2015-06-11 14:19:33 -0700 | [diff] [blame] | 675 | case GRPC_IOREQ_SEND_STATUS: |
| 676 | if (call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details != |
| 677 | NULL) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 678 | GRPC_MDSTR_UNREF( |
Craig Tiller | aea081f | 2015-06-11 14:19:33 -0700 | [diff] [blame] | 679 | call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details); |
| 680 | call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details = |
| 681 | NULL; |
| 682 | } |
| 683 | break; |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 684 | case GRPC_IOREQ_RECV_CLOSE: |
| 685 | case GRPC_IOREQ_SEND_INITIAL_METADATA: |
| 686 | case GRPC_IOREQ_SEND_TRAILING_METADATA: |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 687 | case GRPC_IOREQ_SEND_CLOSE: |
| 688 | break; |
| 689 | case GRPC_IOREQ_RECV_STATUS: |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 690 | get_final_status(call, call->request_data[GRPC_IOREQ_RECV_STATUS]); |
| 691 | break; |
| 692 | case GRPC_IOREQ_RECV_STATUS_DETAILS: |
| 693 | get_final_details(call, |
| 694 | call->request_data[GRPC_IOREQ_RECV_STATUS_DETAILS]); |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 695 | break; |
| 696 | case GRPC_IOREQ_RECV_INITIAL_METADATA: |
Craig Tiller | bae41c8 | 2015-04-28 13:22:25 -0700 | [diff] [blame] | 697 | GPR_SWAP(grpc_metadata_array, call->buffered_metadata[0], |
Craig Tiller | f9fae98 | 2015-05-29 23:02:18 -0700 | [diff] [blame] | 698 | *call->request_data[GRPC_IOREQ_RECV_INITIAL_METADATA] |
| 699 | .recv_metadata); |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 700 | break; |
| 701 | case GRPC_IOREQ_RECV_TRAILING_METADATA: |
Craig Tiller | bae41c8 | 2015-04-28 13:22:25 -0700 | [diff] [blame] | 702 | GPR_SWAP(grpc_metadata_array, call->buffered_metadata[1], |
Craig Tiller | f9fae98 | 2015-05-29 23:02:18 -0700 | [diff] [blame] | 703 | *call->request_data[GRPC_IOREQ_RECV_TRAILING_METADATA] |
| 704 | .recv_metadata); |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 705 | break; |
| 706 | case GRPC_IOREQ_OP_COUNT: |
| 707 | abort(); |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | cr = &call->completed_requests[call->num_completed_requests++]; |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 712 | cr->success = master->success; |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 713 | cr->on_complete = master->on_complete; |
| 714 | cr->user_data = master->user_data; |
| 715 | } |
| 716 | } |
| 717 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 718 | static void finish_ioreq_op(grpc_call *call, grpc_ioreq_op op, int success) { |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 719 | if (is_op_live(call, op)) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 720 | finish_live_ioreq_op(call, op, success); |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
Craig Tiller | 27e14f4 | 2015-05-21 14:37:39 -0700 | [diff] [blame] | 724 | static void early_out_write_ops(grpc_call *call) { |
| 725 | switch (call->write_state) { |
| 726 | case WRITE_STATE_WRITE_CLOSED: |
| 727 | finish_ioreq_op(call, GRPC_IOREQ_SEND_MESSAGE, 0); |
| 728 | finish_ioreq_op(call, GRPC_IOREQ_SEND_STATUS, 0); |
| 729 | finish_ioreq_op(call, GRPC_IOREQ_SEND_TRAILING_METADATA, 0); |
| 730 | finish_ioreq_op(call, GRPC_IOREQ_SEND_CLOSE, 1); |
| 731 | /* fallthrough */ |
| 732 | case WRITE_STATE_STARTED: |
| 733 | finish_ioreq_op(call, GRPC_IOREQ_SEND_INITIAL_METADATA, 0); |
| 734 | /* fallthrough */ |
| 735 | case WRITE_STATE_INITIAL: |
| 736 | /* do nothing */ |
| 737 | break; |
| 738 | } |
| 739 | } |
| 740 | |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 741 | static void call_on_done_send(void *pc, int success) { |
| 742 | grpc_call *call = pc; |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 743 | lock(call); |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 744 | if (call->last_send_contains & (1 << GRPC_IOREQ_SEND_INITIAL_METADATA)) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 745 | finish_ioreq_op(call, GRPC_IOREQ_SEND_INITIAL_METADATA, success); |
Craig Tiller | 134e92f | 2015-05-11 14:24:34 -0700 | [diff] [blame] | 746 | call->write_state = WRITE_STATE_STARTED; |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 747 | } |
| 748 | if (call->last_send_contains & (1 << GRPC_IOREQ_SEND_MESSAGE)) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 749 | finish_ioreq_op(call, GRPC_IOREQ_SEND_MESSAGE, success); |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 750 | } |
| 751 | if (call->last_send_contains & (1 << GRPC_IOREQ_SEND_CLOSE)) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 752 | finish_ioreq_op(call, GRPC_IOREQ_SEND_TRAILING_METADATA, success); |
| 753 | finish_ioreq_op(call, GRPC_IOREQ_SEND_STATUS, success); |
| 754 | finish_ioreq_op(call, GRPC_IOREQ_SEND_CLOSE, 1); |
Craig Tiller | 134e92f | 2015-05-11 14:24:34 -0700 | [diff] [blame] | 755 | call->write_state = WRITE_STATE_WRITE_CLOSED; |
| 756 | } |
| 757 | if (!success) { |
| 758 | call->write_state = WRITE_STATE_WRITE_CLOSED; |
Craig Tiller | 27e14f4 | 2015-05-21 14:37:39 -0700 | [diff] [blame] | 759 | early_out_write_ops(call); |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 760 | } |
Craig Tiller | 644cad6 | 2015-05-20 17:57:54 -0700 | [diff] [blame] | 761 | call->send_ops.nops = 0; |
Craig Tiller | 3928c7a | 2015-04-23 16:00:47 -0700 | [diff] [blame] | 762 | call->last_send_contains = 0; |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 763 | call->sending = 0; |
| 764 | unlock(call); |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 765 | GRPC_CALL_INTERNAL_UNREF(call, "sending", 0); |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 766 | } |
| 767 | |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 768 | static void finish_message(grpc_call *call) { |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 769 | if (call->error_status_set == 0) { |
| 770 | /* TODO(ctiller): this could be a lot faster if coded directly */ |
David Garcia Quintas | 8a18709 | 2015-07-01 14:52:44 -0700 | [diff] [blame] | 771 | grpc_byte_buffer *byte_buffer; |
| 772 | /* some aliases for readability */ |
| 773 | gpr_slice *slices = call->incoming_message.slices; |
| 774 | const size_t nslices = call->incoming_message.count; |
David Garcia Quintas | 658b608 | 2015-07-15 13:46:00 -0700 | [diff] [blame] | 775 | |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 776 | if ((call->incoming_message_flags & GRPC_WRITE_INTERNAL_COMPRESS) && |
| 777 | (call->compression_algorithm > GRPC_COMPRESS_NONE)) { |
David Garcia Quintas | 8a18709 | 2015-07-01 14:52:44 -0700 | [diff] [blame] | 778 | byte_buffer = grpc_raw_compressed_byte_buffer_create( |
| 779 | slices, nslices, call->compression_algorithm); |
| 780 | } else { |
| 781 | byte_buffer = grpc_raw_byte_buffer_create(slices, nslices); |
| 782 | } |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 783 | grpc_bbq_push(&call->incoming_queue, byte_buffer); |
| 784 | } |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 785 | gpr_slice_buffer_reset_and_unref(&call->incoming_message); |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 786 | GPR_ASSERT(call->incoming_message.count == 0); |
| 787 | call->reading_message = 0; |
| 788 | } |
| 789 | |
| 790 | static int begin_message(grpc_call *call, grpc_begin_message msg) { |
| 791 | /* can't begin a message when we're still reading a message */ |
| 792 | if (call->reading_message) { |
| 793 | char *message = NULL; |
| 794 | gpr_asprintf( |
| 795 | &message, "Message terminated early; read %d bytes, expected %d", |
| 796 | (int)call->incoming_message.length, (int)call->incoming_message_length); |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 797 | cancel_with_status(call, GRPC_STATUS_INVALID_ARGUMENT, message); |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 798 | gpr_free(message); |
| 799 | return 0; |
| 800 | } |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 801 | /* sanity check: if message flags indicate a compressed message, the |
| 802 | * compression level should already be present in the call, as parsed off its |
| 803 | * corresponding metadata. */ |
| 804 | if ((msg.flags & GRPC_WRITE_INTERNAL_COMPRESS) && |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 805 | (call->compression_algorithm == GRPC_COMPRESS_NONE)) { |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 806 | char *message = NULL; |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 807 | char *alg_name; |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 808 | if (!grpc_compression_algorithm_name(call->compression_algorithm, |
| 809 | &alg_name)) { |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 810 | /* This shouldn't happen, other than due to data corruption */ |
| 811 | alg_name = "<unknown>"; |
| 812 | } |
| 813 | gpr_asprintf(&message, |
| 814 | "Invalid compression algorithm (%s) for compressed message.", |
| 815 | alg_name); |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 816 | cancel_with_status(call, GRPC_STATUS_INTERNAL, message); |
David Garcia Quintas | 45b477e | 2015-07-15 22:13:34 -0700 | [diff] [blame] | 817 | gpr_free(message); |
| 818 | return 0; |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 819 | } |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 820 | /* stash away parameters, and prepare for incoming slices */ |
| 821 | if (msg.length > grpc_channel_get_max_message_length(call->channel)) { |
| 822 | char *message = NULL; |
| 823 | gpr_asprintf( |
| 824 | &message, |
| 825 | "Maximum message length of %d exceeded by a message of length %d", |
| 826 | grpc_channel_get_max_message_length(call->channel), msg.length); |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 827 | cancel_with_status(call, GRPC_STATUS_INVALID_ARGUMENT, message); |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 828 | gpr_free(message); |
| 829 | return 0; |
| 830 | } else if (msg.length > 0) { |
| 831 | call->reading_message = 1; |
| 832 | call->incoming_message_length = msg.length; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 833 | call->incoming_message_flags = msg.flags; |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 834 | return 1; |
| 835 | } else { |
| 836 | finish_message(call); |
| 837 | return 1; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | static int add_slice_to_message(grpc_call *call, gpr_slice slice) { |
| 842 | if (GPR_SLICE_LENGTH(slice) == 0) { |
| 843 | gpr_slice_unref(slice); |
| 844 | return 1; |
| 845 | } |
| 846 | /* we have to be reading a message to know what to do here */ |
| 847 | if (!call->reading_message) { |
Craig Tiller | f9fae98 | 2015-05-29 23:02:18 -0700 | [diff] [blame] | 848 | cancel_with_status(call, GRPC_STATUS_INVALID_ARGUMENT, |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 849 | "Received payload data while not reading a message"); |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 850 | return 0; |
| 851 | } |
| 852 | /* append the slice to the incoming buffer */ |
| 853 | gpr_slice_buffer_add(&call->incoming_message, slice); |
| 854 | if (call->incoming_message.length > call->incoming_message_length) { |
| 855 | /* if we got too many bytes, complain */ |
| 856 | char *message = NULL; |
| 857 | gpr_asprintf( |
| 858 | &message, "Receiving message overflow; read %d bytes, expected %d", |
| 859 | (int)call->incoming_message.length, (int)call->incoming_message_length); |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 860 | cancel_with_status(call, GRPC_STATUS_INVALID_ARGUMENT, message); |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 861 | gpr_free(message); |
| 862 | return 0; |
| 863 | } else if (call->incoming_message.length == call->incoming_message_length) { |
| 864 | finish_message(call); |
| 865 | return 1; |
| 866 | } else { |
| 867 | return 1; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | static void call_on_done_recv(void *pc, int success) { |
| 872 | grpc_call *call = pc; |
| 873 | size_t i; |
David Garcia Quintas | f667f1b | 2015-05-04 13:15:46 -0700 | [diff] [blame] | 874 | GRPC_TIMER_BEGIN(GRPC_PTAG_CALL_ON_DONE_RECV, 0); |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 875 | lock(call); |
Craig Tiller | 6e84aba | 2015-04-23 15:08:17 -0700 | [diff] [blame] | 876 | call->receiving = 0; |
Craig Tiller | 48b9fde | 2015-04-24 08:04:59 -0700 | [diff] [blame] | 877 | if (success) { |
| 878 | for (i = 0; success && i < call->recv_ops.nops; i++) { |
| 879 | grpc_stream_op *op = &call->recv_ops.ops[i]; |
| 880 | switch (op->type) { |
| 881 | case GRPC_NO_OP: |
| 882 | break; |
| 883 | case GRPC_OP_METADATA: |
| 884 | recv_metadata(call, &op->data.metadata); |
| 885 | break; |
| 886 | case GRPC_OP_BEGIN_MESSAGE: |
| 887 | success = begin_message(call, op->data.begin_message); |
| 888 | break; |
| 889 | case GRPC_OP_SLICE: |
| 890 | success = add_slice_to_message(call, op->data.slice); |
| 891 | break; |
| 892 | } |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 893 | } |
Craig Tiller | c5f3e26 | 2015-05-07 10:15:00 -0700 | [diff] [blame] | 894 | if (!success) { |
| 895 | grpc_stream_ops_unref_owned_objects(&call->recv_ops.ops[i], |
| 896 | call->recv_ops.nops - i); |
| 897 | } |
Craig Tiller | 48b9fde | 2015-04-24 08:04:59 -0700 | [diff] [blame] | 898 | if (call->recv_state == GRPC_STREAM_RECV_CLOSED) { |
| 899 | GPR_ASSERT(call->read_state <= READ_STATE_READ_CLOSED); |
| 900 | call->read_state = READ_STATE_READ_CLOSED; |
| 901 | } |
| 902 | if (call->recv_state == GRPC_STREAM_CLOSED) { |
| 903 | GPR_ASSERT(call->read_state <= READ_STATE_STREAM_CLOSED); |
| 904 | call->read_state = READ_STATE_STREAM_CLOSED; |
Craig Tiller | 77f0461 | 2015-07-01 13:39:45 -0700 | [diff] [blame] | 905 | call->cancel_alarm |= call->have_alarm; |
Craig Tiller | f3fba74 | 2015-06-11 09:36:33 -0700 | [diff] [blame] | 906 | GRPC_CALL_INTERNAL_UNREF(call, "closed", 0); |
Craig Tiller | 48b9fde | 2015-04-24 08:04:59 -0700 | [diff] [blame] | 907 | } |
| 908 | finish_read_ops(call); |
| 909 | } else { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 910 | finish_ioreq_op(call, GRPC_IOREQ_RECV_MESSAGE, 0); |
| 911 | finish_ioreq_op(call, GRPC_IOREQ_RECV_STATUS, 0); |
| 912 | finish_ioreq_op(call, GRPC_IOREQ_RECV_CLOSE, 0); |
| 913 | finish_ioreq_op(call, GRPC_IOREQ_RECV_TRAILING_METADATA, 0); |
| 914 | finish_ioreq_op(call, GRPC_IOREQ_RECV_INITIAL_METADATA, 0); |
| 915 | finish_ioreq_op(call, GRPC_IOREQ_RECV_STATUS_DETAILS, 0); |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 916 | } |
Craig Tiller | c1f7560 | 2015-04-24 11:44:53 -0700 | [diff] [blame] | 917 | call->recv_ops.nops = 0; |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 918 | unlock(call); |
| 919 | |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 920 | GRPC_CALL_INTERNAL_UNREF(call, "receiving", 0); |
Yu Zhou | a7c1062 | 2015-05-29 16:22:26 -0700 | [diff] [blame] | 921 | GRPC_TIMER_END(GRPC_PTAG_CALL_ON_DONE_RECV, 0); |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Craig Tiller | b96d001 | 2015-05-06 15:33:23 -0700 | [diff] [blame] | 924 | static int prepare_application_metadata(grpc_call *call, size_t count, |
| 925 | grpc_metadata *metadata) { |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 926 | size_t i; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 927 | for (i = 0; i < count; i++) { |
| 928 | grpc_metadata *md = &metadata[i]; |
| 929 | grpc_metadata *next_md = (i == count - 1) ? NULL : &metadata[i + 1]; |
| 930 | grpc_metadata *prev_md = (i == 0) ? NULL : &metadata[i - 1]; |
| 931 | grpc_linked_mdelem *l = (grpc_linked_mdelem *)&md->internal_data; |
Craig Tiller | d2b11fa | 2015-04-21 13:45:19 -0700 | [diff] [blame] | 932 | GPR_ASSERT(sizeof(grpc_linked_mdelem) == sizeof(md->internal_data)); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 933 | l->md = grpc_mdelem_from_string_and_buffer(call->metadata_context, md->key, |
| 934 | (const gpr_uint8 *)md->value, |
| 935 | md->value_length); |
Craig Tiller | b96d001 | 2015-05-06 15:33:23 -0700 | [diff] [blame] | 936 | if (!grpc_mdstr_is_legal_header(l->md->key)) { |
| 937 | gpr_log(GPR_ERROR, "attempt to send invalid metadata key"); |
| 938 | return 0; |
| 939 | } else if (!grpc_mdstr_is_bin_suffixed(l->md->key) && |
| 940 | !grpc_mdstr_is_legal_header(l->md->value)) { |
| 941 | gpr_log(GPR_ERROR, "attempt to send invalid metadata value"); |
| 942 | return 0; |
| 943 | } |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 944 | l->next = next_md ? (grpc_linked_mdelem *)&next_md->internal_data : NULL; |
| 945 | l->prev = prev_md ? (grpc_linked_mdelem *)&prev_md->internal_data : NULL; |
| 946 | } |
Craig Tiller | b96d001 | 2015-05-06 15:33:23 -0700 | [diff] [blame] | 947 | return 1; |
| 948 | } |
| 949 | |
| 950 | static grpc_mdelem_list chain_metadata_from_app(grpc_call *call, size_t count, |
| 951 | grpc_metadata *metadata) { |
| 952 | grpc_mdelem_list out; |
| 953 | if (count == 0) { |
| 954 | out.head = out.tail = NULL; |
| 955 | return out; |
| 956 | } |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 957 | out.head = (grpc_linked_mdelem *)&(metadata[0].internal_data); |
| 958 | out.tail = (grpc_linked_mdelem *)&(metadata[count - 1].internal_data); |
| 959 | return out; |
| 960 | } |
| 961 | |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 962 | /* Copy the contents of a byte buffer into stream ops */ |
| 963 | static void copy_byte_buffer_to_stream_ops(grpc_byte_buffer *byte_buffer, |
| 964 | grpc_stream_op_buffer *sopb) { |
| 965 | size_t i; |
| 966 | |
| 967 | switch (byte_buffer->type) { |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 968 | case GRPC_BB_RAW: |
| 969 | for (i = 0; i < byte_buffer->data.raw.slice_buffer.count; i++) { |
| 970 | gpr_slice slice = byte_buffer->data.raw.slice_buffer.slices[i]; |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 971 | gpr_slice_ref(slice); |
| 972 | grpc_sopb_add_slice(sopb, slice); |
| 973 | } |
| 974 | break; |
| 975 | } |
| 976 | } |
| 977 | |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 978 | static int fill_send_ops(grpc_call *call, grpc_transport_stream_op *op) { |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 979 | grpc_ioreq_data data; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 980 | gpr_uint32 flags; |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 981 | grpc_metadata_batch mdb; |
| 982 | size_t i; |
| 983 | GPR_ASSERT(op->send_ops == NULL); |
| 984 | |
| 985 | switch (call->write_state) { |
| 986 | case WRITE_STATE_INITIAL: |
| 987 | if (!is_op_live(call, GRPC_IOREQ_SEND_INITIAL_METADATA)) { |
| 988 | break; |
| 989 | } |
| 990 | data = call->request_data[GRPC_IOREQ_SEND_INITIAL_METADATA]; |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 991 | mdb.list = chain_metadata_from_app(call, data.send_metadata.count, |
| 992 | data.send_metadata.metadata); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 993 | mdb.garbage.head = mdb.garbage.tail = NULL; |
| 994 | mdb.deadline = call->send_deadline; |
| 995 | for (i = 0; i < call->send_initial_metadata_count; i++) { |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 996 | grpc_metadata_batch_link_head(&mdb, &call->send_initial_metadata[i]); |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 997 | } |
| 998 | grpc_sopb_add_metadata(&call->send_ops, mdb); |
| 999 | op->send_ops = &call->send_ops; |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 1000 | call->last_send_contains |= 1 << GRPC_IOREQ_SEND_INITIAL_METADATA; |
Craig Tiller | c1f7560 | 2015-04-24 11:44:53 -0700 | [diff] [blame] | 1001 | call->send_initial_metadata_count = 0; |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 1002 | /* fall through intended */ |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 1003 | case WRITE_STATE_STARTED: |
| 1004 | if (is_op_live(call, GRPC_IOREQ_SEND_MESSAGE)) { |
| 1005 | data = call->request_data[GRPC_IOREQ_SEND_MESSAGE]; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1006 | flags = call->request_flags[GRPC_IOREQ_SEND_MESSAGE]; |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 1007 | grpc_sopb_add_begin_message( |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1008 | &call->send_ops, grpc_byte_buffer_length(data.send_message), flags); |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 1009 | copy_byte_buffer_to_stream_ops(data.send_message, &call->send_ops); |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 1010 | op->send_ops = &call->send_ops; |
| 1011 | call->last_send_contains |= 1 << GRPC_IOREQ_SEND_MESSAGE; |
| 1012 | } |
| 1013 | if (is_op_live(call, GRPC_IOREQ_SEND_CLOSE)) { |
| 1014 | op->is_last_send = 1; |
| 1015 | op->send_ops = &call->send_ops; |
| 1016 | call->last_send_contains |= 1 << GRPC_IOREQ_SEND_CLOSE; |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 1017 | if (!call->is_client) { |
| 1018 | /* send trailing metadata */ |
| 1019 | data = call->request_data[GRPC_IOREQ_SEND_TRAILING_METADATA]; |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 1020 | mdb.list = chain_metadata_from_app(call, data.send_metadata.count, |
| 1021 | data.send_metadata.metadata); |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 1022 | mdb.garbage.head = mdb.garbage.tail = NULL; |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 1023 | mdb.deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 1024 | /* send status */ |
| 1025 | /* TODO(ctiller): cache common status values */ |
| 1026 | data = call->request_data[GRPC_IOREQ_SEND_STATUS]; |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 1027 | grpc_metadata_batch_add_tail( |
| 1028 | &mdb, &call->status_link, |
Craig Tiller | 3fc8e82 | 2015-06-08 16:31:28 -0700 | [diff] [blame] | 1029 | grpc_channel_get_reffed_status_elem(call->channel, |
| 1030 | data.send_status.code)); |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 1031 | if (data.send_status.details) { |
| 1032 | grpc_metadata_batch_add_tail( |
| 1033 | &mdb, &call->details_link, |
| 1034 | grpc_mdelem_from_metadata_strings( |
| 1035 | call->metadata_context, |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 1036 | GRPC_MDSTR_REF( |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 1037 | grpc_channel_get_message_string(call->channel)), |
Craig Tiller | aea081f | 2015-06-11 14:19:33 -0700 | [diff] [blame] | 1038 | data.send_status.details)); |
| 1039 | call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details = |
| 1040 | NULL; |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 1041 | } |
Craig Tiller | 7e8489a | 2015-04-23 12:41:16 -0700 | [diff] [blame] | 1042 | grpc_sopb_add_metadata(&call->send_ops, mdb); |
Craig Tiller | de64862 | 2015-02-05 09:32:10 -0800 | [diff] [blame] | 1043 | } |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1044 | } |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 1045 | break; |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1046 | case WRITE_STATE_WRITE_CLOSED: |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 1047 | break; |
Craig Tiller | 62ac155 | 2015-01-27 15:41:44 -0800 | [diff] [blame] | 1048 | } |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 1049 | if (op->send_ops) { |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 1050 | op->on_done_send = &call->on_done_send; |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 1051 | } |
| 1052 | return op->send_ops != NULL; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | static grpc_call_error start_ioreq_error(grpc_call *call, |
| 1056 | gpr_uint32 mutated_ops, |
| 1057 | grpc_call_error ret) { |
| 1058 | size_t i; |
| 1059 | for (i = 0; i < GRPC_IOREQ_OP_COUNT; i++) { |
Craig Tiller | d642dcf | 2015-02-03 20:39:09 -0800 | [diff] [blame] | 1060 | if (mutated_ops & (1u << i)) { |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1061 | call->request_set[i] = REQSET_EMPTY; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1062 | } |
| 1063 | } |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1064 | return ret; |
| 1065 | } |
| 1066 | |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1067 | static void finish_read_ops(grpc_call *call) { |
| 1068 | int empty; |
| 1069 | |
| 1070 | if (is_op_live(call, GRPC_IOREQ_RECV_MESSAGE)) { |
| 1071 | empty = |
| 1072 | (NULL == (*call->request_data[GRPC_IOREQ_RECV_MESSAGE].recv_message = |
| 1073 | grpc_bbq_pop(&call->incoming_queue))); |
| 1074 | if (!empty) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 1075 | finish_live_ioreq_op(call, GRPC_IOREQ_RECV_MESSAGE, 1); |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1076 | empty = grpc_bbq_empty(&call->incoming_queue); |
| 1077 | } |
| 1078 | } else { |
| 1079 | empty = grpc_bbq_empty(&call->incoming_queue); |
| 1080 | } |
| 1081 | |
| 1082 | switch (call->read_state) { |
| 1083 | case READ_STATE_STREAM_CLOSED: |
Craig Tiller | 77f0461 | 2015-07-01 13:39:45 -0700 | [diff] [blame] | 1084 | if (empty && !call->have_alarm) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 1085 | finish_ioreq_op(call, GRPC_IOREQ_RECV_CLOSE, 1); |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1086 | } |
| 1087 | /* fallthrough */ |
| 1088 | case READ_STATE_READ_CLOSED: |
| 1089 | if (empty) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 1090 | finish_ioreq_op(call, GRPC_IOREQ_RECV_MESSAGE, 1); |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1091 | } |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 1092 | finish_ioreq_op(call, GRPC_IOREQ_RECV_STATUS, 1); |
| 1093 | finish_ioreq_op(call, GRPC_IOREQ_RECV_STATUS_DETAILS, 1); |
| 1094 | finish_ioreq_op(call, GRPC_IOREQ_RECV_TRAILING_METADATA, 1); |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1095 | /* fallthrough */ |
| 1096 | case READ_STATE_GOT_INITIAL_METADATA: |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 1097 | finish_ioreq_op(call, GRPC_IOREQ_RECV_INITIAL_METADATA, 1); |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1098 | /* fallthrough */ |
| 1099 | case READ_STATE_INITIAL: |
| 1100 | /* do nothing */ |
| 1101 | break; |
| 1102 | } |
| 1103 | } |
| 1104 | |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 1105 | static grpc_call_error start_ioreq(grpc_call *call, const grpc_ioreq *reqs, |
| 1106 | size_t nreqs, |
| 1107 | grpc_ioreq_completion_func completion, |
Craig Tiller | 9cc6141 | 2015-02-02 14:02:52 -0800 | [diff] [blame] | 1108 | void *user_data) { |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1109 | size_t i; |
| 1110 | gpr_uint32 have_ops = 0; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1111 | grpc_ioreq_op op; |
Craig Tiller | c18c56e | 2015-02-02 15:59:13 -0800 | [diff] [blame] | 1112 | reqinfo_master *master; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1113 | grpc_ioreq_data data; |
Craig Tiller | 1e0d4c4 | 2015-01-30 16:17:29 -0800 | [diff] [blame] | 1114 | gpr_uint8 set; |
| 1115 | |
| 1116 | if (nreqs == 0) { |
| 1117 | return GRPC_CALL_OK; |
| 1118 | } |
| 1119 | |
| 1120 | set = reqs[0].op; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1121 | |
| 1122 | for (i = 0; i < nreqs; i++) { |
| 1123 | op = reqs[i].op; |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1124 | if (call->request_set[op] < GRPC_IOREQ_OP_COUNT) { |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1125 | return start_ioreq_error(call, have_ops, |
| 1126 | GRPC_CALL_ERROR_TOO_MANY_OPERATIONS); |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1127 | } else if (call->request_set[op] == REQSET_DONE) { |
Craig Tiller | 1c14190 | 2015-01-31 08:51:54 -0800 | [diff] [blame] | 1128 | return start_ioreq_error(call, have_ops, GRPC_CALL_ERROR_ALREADY_INVOKED); |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1129 | } |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1130 | data = reqs[i].data; |
Craig Tiller | b96d001 | 2015-05-06 15:33:23 -0700 | [diff] [blame] | 1131 | if (op == GRPC_IOREQ_SEND_INITIAL_METADATA || |
| 1132 | op == GRPC_IOREQ_SEND_TRAILING_METADATA) { |
| 1133 | if (!prepare_application_metadata(call, data.send_metadata.count, |
| 1134 | data.send_metadata.metadata)) { |
| 1135 | return start_ioreq_error(call, have_ops, |
| 1136 | GRPC_CALL_ERROR_INVALID_METADATA); |
| 1137 | } |
| 1138 | } |
Craig Tiller | aea081f | 2015-06-11 14:19:33 -0700 | [diff] [blame] | 1139 | if (op == GRPC_IOREQ_SEND_STATUS) { |
| 1140 | set_status_code(call, STATUS_FROM_SERVER_STATUS, |
| 1141 | reqs[i].data.send_status.code); |
| 1142 | if (reqs[i].data.send_status.details) { |
| 1143 | set_status_details(call, STATUS_FROM_SERVER_STATUS, |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 1144 | GRPC_MDSTR_REF(reqs[i].data.send_status.details)); |
Craig Tiller | aea081f | 2015-06-11 14:19:33 -0700 | [diff] [blame] | 1145 | } |
| 1146 | } |
Craig Tiller | b96d001 | 2015-05-06 15:33:23 -0700 | [diff] [blame] | 1147 | have_ops |= 1u << op; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1148 | |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1149 | call->request_data[op] = data; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1150 | call->request_flags[op] = reqs[i].flags; |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1151 | call->request_set[op] = set; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
Craig Tiller | c18c56e | 2015-02-02 15:59:13 -0800 | [diff] [blame] | 1154 | master = &call->masters[set]; |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 1155 | master->success = 1; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1156 | master->need_mask = have_ops; |
Craig Tiller | 1b40944 | 2015-01-29 14:36:17 -0800 | [diff] [blame] | 1157 | master->complete_mask = 0; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1158 | master->on_complete = completion; |
| 1159 | master->user_data = user_data; |
| 1160 | |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 1161 | finish_read_ops(call); |
| 1162 | early_out_write_ops(call); |
| 1163 | |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1164 | return GRPC_CALL_OK; |
| 1165 | } |
| 1166 | |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1167 | grpc_call_error grpc_call_start_ioreq_and_call_back( |
| 1168 | grpc_call *call, const grpc_ioreq *reqs, size_t nreqs, |
| 1169 | grpc_ioreq_completion_func on_complete, void *user_data) { |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 1170 | grpc_call_error err; |
| 1171 | lock(call); |
Craig Tiller | 9cc6141 | 2015-02-02 14:02:52 -0800 | [diff] [blame] | 1172 | err = start_ioreq(call, reqs, nreqs, on_complete, user_data); |
Craig Tiller | 8eb9d47 | 2015-01-27 17:00:03 -0800 | [diff] [blame] | 1173 | unlock(call); |
| 1174 | return err; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1177 | void grpc_call_destroy(grpc_call *c) { |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 1178 | int cancel; |
Craig Tiller | 9724de8 | 2015-01-28 17:06:29 -0800 | [diff] [blame] | 1179 | lock(c); |
Craig Tiller | f3fba74 | 2015-06-11 09:36:33 -0700 | [diff] [blame] | 1180 | GPR_ASSERT(!c->destroy_called); |
| 1181 | c->destroy_called = 1; |
Craig Tiller | 77f0461 | 2015-07-01 13:39:45 -0700 | [diff] [blame] | 1182 | c->cancel_alarm |= c->have_alarm; |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 1183 | cancel = c->read_state != READ_STATE_STREAM_CLOSED; |
Craig Tiller | 9724de8 | 2015-01-28 17:06:29 -0800 | [diff] [blame] | 1184 | unlock(c); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 1185 | if (cancel) grpc_call_cancel(c); |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 1186 | GRPC_CALL_INTERNAL_UNREF(c, "destroy", 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
Craig Tiller | 58ce3f0 | 2015-04-22 07:54:24 -0700 | [diff] [blame] | 1189 | grpc_call_error grpc_call_cancel(grpc_call *call) { |
Craig Tiller | 48b9fde | 2015-04-24 08:04:59 -0700 | [diff] [blame] | 1190 | return grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED, "Cancelled"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1191 | } |
| 1192 | |
Craig Tiller | 6046dc3 | 2015-01-14 12:55:45 -0800 | [diff] [blame] | 1193 | grpc_call_error grpc_call_cancel_with_status(grpc_call *c, |
| 1194 | grpc_status_code status, |
| 1195 | const char *description) { |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 1196 | grpc_call_error r; |
| 1197 | lock(c); |
| 1198 | r = cancel_with_status(c, status, description); |
| 1199 | unlock(c); |
| 1200 | return r; |
Yang Gao | ff30f8e | 2015-05-04 00:13:39 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Craig Tiller | f9fae98 | 2015-05-29 23:02:18 -0700 | [diff] [blame] | 1203 | static grpc_call_error cancel_with_status(grpc_call *c, grpc_status_code status, |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 1204 | const char *description) { |
Craig Tiller | 6046dc3 | 2015-01-14 12:55:45 -0800 | [diff] [blame] | 1205 | grpc_mdstr *details = |
| 1206 | description ? grpc_mdstr_from_string(c->metadata_context, description) |
| 1207 | : NULL; |
Craig Tiller | 48b9fde | 2015-04-24 08:04:59 -0700 | [diff] [blame] | 1208 | |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 1209 | GPR_ASSERT(status != GRPC_STATUS_OK); |
| 1210 | |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 1211 | set_status_code(c, STATUS_FROM_API_OVERRIDE, status); |
| 1212 | set_status_details(c, STATUS_FROM_API_OVERRIDE, details); |
Craig Tiller | 48b9fde | 2015-04-24 08:04:59 -0700 | [diff] [blame] | 1213 | |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 1214 | c->cancel_with_status = status; |
Craig Tiller | 48b9fde | 2015-04-24 08:04:59 -0700 | [diff] [blame] | 1215 | |
| 1216 | return GRPC_CALL_OK; |
Craig Tiller | d248c24 | 2015-01-14 11:49:12 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 1219 | static void finished_loose_op(void *call, int success_ignored) { |
| 1220 | GRPC_CALL_INTERNAL_UNREF(call, "loose-op", 0); |
| 1221 | } |
| 1222 | |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 1223 | typedef struct { |
| 1224 | grpc_call *call; |
| 1225 | grpc_iomgr_closure closure; |
| 1226 | } finished_loose_op_allocated_args; |
| 1227 | |
| 1228 | static void finished_loose_op_allocated(void *alloc, int success) { |
| 1229 | finished_loose_op_allocated_args *args = alloc; |
| 1230 | finished_loose_op(args->call, success); |
| 1231 | gpr_free(args); |
| 1232 | } |
| 1233 | |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 1234 | static void execute_op(grpc_call *call, grpc_transport_stream_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1235 | grpc_call_element *elem; |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 1236 | |
| 1237 | GPR_ASSERT(op->on_consumed == NULL); |
| 1238 | if (op->cancel_with_status != GRPC_STATUS_OK || op->bind_pollset) { |
| 1239 | GRPC_CALL_INTERNAL_REF(call, "loose-op"); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 1240 | if (op->bind_pollset) { |
| 1241 | op->on_consumed = &call->on_done_bind; |
| 1242 | } else { |
| 1243 | finished_loose_op_allocated_args *args = gpr_malloc(sizeof(*args)); |
| 1244 | args->call = call; |
Craig Tiller | f1bff01 | 2015-07-06 11:20:50 -0700 | [diff] [blame] | 1245 | grpc_iomgr_closure_init(&args->closure, finished_loose_op_allocated, |
| 1246 | args); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 1247 | op->on_consumed = &args->closure; |
| 1248 | } |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1251 | elem = CALL_ELEM_FROM_CALL(call, 0); |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 1252 | op->context = call->context; |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 1253 | elem->filter->start_transport_stream_op(elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1256 | grpc_call *grpc_call_from_top_element(grpc_call_element *elem) { |
| 1257 | return CALL_FROM_TOP_ELEM(elem); |
| 1258 | } |
| 1259 | |
| 1260 | static void call_alarm(void *arg, int success) { |
| 1261 | grpc_call *call = arg; |
Craig Tiller | 77f0461 | 2015-07-01 13:39:45 -0700 | [diff] [blame] | 1262 | lock(call); |
| 1263 | call->have_alarm = 0; |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1264 | if (success) { |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 1265 | cancel_with_status(call, GRPC_STATUS_DEADLINE_EXCEEDED, |
| 1266 | "Deadline Exceeded"); |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1267 | } |
Craig Tiller | 77f0461 | 2015-07-01 13:39:45 -0700 | [diff] [blame] | 1268 | finish_read_ops(call); |
| 1269 | unlock(call); |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 1270 | GRPC_CALL_INTERNAL_UNREF(call, "alarm", 1); |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1271 | } |
| 1272 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 1273 | static void set_deadline_alarm(grpc_call *call, gpr_timespec deadline) { |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1274 | if (call->have_alarm) { |
| 1275 | gpr_log(GPR_ERROR, "Attempt to set deadline alarm twice"); |
Craig Tiller | fa4f994 | 2015-04-23 15:22:09 -0700 | [diff] [blame] | 1276 | assert(0); |
Craig Tiller | 7e8489a | 2015-04-23 12:41:16 -0700 | [diff] [blame] | 1277 | return; |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1278 | } |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 1279 | GRPC_CALL_INTERNAL_REF(call, "alarm"); |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1280 | call->have_alarm = 1; |
Craig Tiller | 6a7626c | 2015-07-19 22:21:41 -0700 | [diff] [blame] | 1281 | grpc_alarm_init(&call->alarm, gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC), call_alarm, call, |
| 1282 | gpr_now(GPR_CLOCK_MONOTONIC)); |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1285 | /* we offset status by a small amount when storing it into transport metadata |
| 1286 | as metadata cannot store a 0 value (which is used as OK for grpc_status_codes |
| 1287 | */ |
| 1288 | #define STATUS_OFFSET 1 |
| 1289 | static void destroy_status(void *ignored) {} |
| 1290 | |
| 1291 | static gpr_uint32 decode_status(grpc_mdelem *md) { |
| 1292 | gpr_uint32 status; |
| 1293 | void *user_data = grpc_mdelem_get_user_data(md, destroy_status); |
| 1294 | if (user_data) { |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 1295 | status = ((gpr_uint32)(gpr_intptr)user_data) - STATUS_OFFSET; |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1296 | } else { |
| 1297 | if (!gpr_parse_bytes_to_uint32(grpc_mdstr_as_c_string(md->value), |
| 1298 | GPR_SLICE_LENGTH(md->value->slice), |
| 1299 | &status)) { |
| 1300 | status = GRPC_STATUS_UNKNOWN; /* could not parse status code */ |
| 1301 | } |
| 1302 | grpc_mdelem_set_user_data(md, destroy_status, |
| 1303 | (void *)(gpr_intptr)(status + STATUS_OFFSET)); |
| 1304 | } |
| 1305 | return status; |
| 1306 | } |
| 1307 | |
David Garcia Quintas | db94b27 | 2015-06-15 18:37:01 -0700 | [diff] [blame] | 1308 | /* just as for status above, we need to offset: metadata userdata can't hold a |
| 1309 | * zero (null), which in this case is used to signal no compression */ |
| 1310 | #define COMPRESS_OFFSET 1 |
| 1311 | static void destroy_compression(void *ignored) {} |
| 1312 | |
| 1313 | static gpr_uint32 decode_compression(grpc_mdelem *md) { |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 1314 | grpc_compression_algorithm algorithm; |
David Garcia Quintas | 92ce588 | 2015-06-23 17:07:12 -0700 | [diff] [blame] | 1315 | void *user_data = grpc_mdelem_get_user_data(md, destroy_compression); |
David Garcia Quintas | db94b27 | 2015-06-15 18:37:01 -0700 | [diff] [blame] | 1316 | if (user_data) { |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 1317 | algorithm = ((grpc_compression_level)(gpr_intptr)user_data) - COMPRESS_OFFSET; |
David Garcia Quintas | db94b27 | 2015-06-15 18:37:01 -0700 | [diff] [blame] | 1318 | } else { |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 1319 | const char *md_c_str = grpc_mdstr_as_c_string(md->value); |
| 1320 | if (!grpc_compression_algorithm_parse(md_c_str, &algorithm)) { |
| 1321 | gpr_log(GPR_ERROR, "Invalid compression algorithm: '%s'", md_c_str); |
| 1322 | assert(0); |
David Garcia Quintas | db94b27 | 2015-06-15 18:37:01 -0700 | [diff] [blame] | 1323 | } |
| 1324 | grpc_mdelem_set_user_data(md, destroy_compression, |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 1325 | (void *)(gpr_intptr)(algorithm + COMPRESS_OFFSET)); |
David Garcia Quintas | db94b27 | 2015-06-15 18:37:01 -0700 | [diff] [blame] | 1326 | } |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 1327 | return algorithm; |
David Garcia Quintas | db94b27 | 2015-06-15 18:37:01 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 1330 | static void recv_metadata(grpc_call *call, grpc_metadata_batch *md) { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 1331 | grpc_linked_mdelem *l; |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1332 | grpc_metadata_array *dest; |
| 1333 | grpc_metadata *mdusr; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 1334 | int is_trailing; |
Craig Tiller | 8b282cb | 2015-04-17 14:57:44 -0700 | [diff] [blame] | 1335 | grpc_mdctx *mdctx = call->metadata_context; |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1336 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 1337 | is_trailing = call->read_state >= READ_STATE_GOT_INITIAL_METADATA; |
Craig Tiller | 48b02ec | 2015-04-21 13:58:36 -0700 | [diff] [blame] | 1338 | for (l = md->list.head; l != NULL; l = l->next) { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 1339 | grpc_mdelem *md = l->md; |
| 1340 | grpc_mdstr *key = md->key; |
| 1341 | if (key == grpc_channel_get_status_string(call->channel)) { |
| 1342 | set_status_code(call, STATUS_FROM_WIRE, decode_status(md)); |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 1343 | } else if (key == grpc_channel_get_message_string(call->channel)) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 1344 | set_status_details(call, STATUS_FROM_WIRE, GRPC_MDSTR_REF(md->value)); |
Craig Tiller | f1bff01 | 2015-07-06 11:20:50 -0700 | [diff] [blame] | 1345 | } else if (key == |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 1346 | grpc_channel_get_compression_algorithm_string(call->channel)) { |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 1347 | set_compression_algorithm(call, decode_compression(md)); |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 1348 | } else { |
| 1349 | dest = &call->buffered_metadata[is_trailing]; |
| 1350 | if (dest->count == dest->capacity) { |
| 1351 | dest->capacity = GPR_MAX(dest->capacity + 8, dest->capacity * 2); |
| 1352 | dest->metadata = |
| 1353 | gpr_realloc(dest->metadata, sizeof(grpc_metadata) * dest->capacity); |
| 1354 | } |
| 1355 | mdusr = &dest->metadata[dest->count++]; |
| 1356 | mdusr->key = grpc_mdstr_as_c_string(md->key); |
| 1357 | mdusr->value = grpc_mdstr_as_c_string(md->value); |
| 1358 | mdusr->value_length = GPR_SLICE_LENGTH(md->value->slice); |
| 1359 | if (call->owned_metadata_count == call->owned_metadata_capacity) { |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 1360 | call->owned_metadata_capacity = |
| 1361 | GPR_MAX(call->owned_metadata_capacity + 8, |
| 1362 | call->owned_metadata_capacity * 2); |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 1363 | call->owned_metadata = |
| 1364 | gpr_realloc(call->owned_metadata, |
| 1365 | sizeof(grpc_mdelem *) * call->owned_metadata_capacity); |
| 1366 | } |
| 1367 | call->owned_metadata[call->owned_metadata_count++] = md; |
Craig Tiller | 8b282cb | 2015-04-17 14:57:44 -0700 | [diff] [blame] | 1368 | l->md = 0; |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1369 | } |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 1370 | } |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 1371 | if (gpr_time_cmp(md->deadline, gpr_inf_future(GPR_CLOCK_REALTIME)) != 0) { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 1372 | set_deadline_alarm(call, md->deadline); |
| 1373 | } |
| 1374 | if (!is_trailing) { |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 1375 | call->read_state = READ_STATE_GOT_INITIAL_METADATA; |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1376 | } |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 1377 | |
Craig Tiller | 8b282cb | 2015-04-17 14:57:44 -0700 | [diff] [blame] | 1378 | grpc_mdctx_lock(mdctx); |
| 1379 | for (l = md->list.head; l; l = l->next) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 1380 | if (l->md) GRPC_MDCTX_LOCKED_MDELEM_UNREF(mdctx, l->md); |
Craig Tiller | 8b282cb | 2015-04-17 14:57:44 -0700 | [diff] [blame] | 1381 | } |
| 1382 | for (l = md->garbage.head; l; l = l->next) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 1383 | GRPC_MDCTX_LOCKED_MDELEM_UNREF(mdctx, l->md); |
Craig Tiller | 8b282cb | 2015-04-17 14:57:44 -0700 | [diff] [blame] | 1384 | } |
| 1385 | grpc_mdctx_unlock(mdctx); |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 1386 | } |
Craig Tiller | 8b282cb | 2015-04-17 14:57:44 -0700 | [diff] [blame] | 1387 | |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1388 | grpc_call_stack *grpc_call_get_call_stack(grpc_call *call) { |
| 1389 | return CALL_STACK_FROM_CALL(call); |
| 1390 | } |
| 1391 | |
| 1392 | /* |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1393 | * BATCH API IMPLEMENTATION |
| 1394 | */ |
| 1395 | |
| 1396 | static void set_status_value_directly(grpc_status_code status, void *dest) { |
| 1397 | *(grpc_status_code *)dest = status; |
| 1398 | } |
| 1399 | |
| 1400 | static void set_cancelled_value(grpc_status_code status, void *dest) { |
| 1401 | *(grpc_status_code *)dest = (status != GRPC_STATUS_OK); |
| 1402 | } |
| 1403 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 1404 | static void finish_batch(grpc_call *call, int success, void *tag) { |
Craig Tiller | 12cf537 | 2015-07-09 13:48:11 -0700 | [diff] [blame] | 1405 | grpc_cq_end_op(call->cq, tag, success, done_completion, call, |
| 1406 | allocate_completion(call)); |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | static void finish_batch_with_close(grpc_call *call, int success, void *tag) { |
Craig Tiller | 12cf537 | 2015-07-09 13:48:11 -0700 | [diff] [blame] | 1410 | grpc_cq_end_op(call->cq, tag, 1, done_completion, call, |
| 1411 | allocate_completion(call)); |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 1412 | } |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1413 | |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1414 | static int are_write_flags_valid(gpr_uint32 flags) { |
| 1415 | /* check that only bits in GRPC_WRITE_(INTERNAL?)_USED_MASK are set */ |
| 1416 | const gpr_uint32 allowed_write_positions = |
| 1417 | (GRPC_WRITE_USED_MASK | GRPC_WRITE_INTERNAL_USED_MASK); |
| 1418 | const gpr_uint32 invalid_positions = ~allowed_write_positions; |
| 1419 | return !(flags & invalid_positions); |
| 1420 | } |
| 1421 | |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1422 | grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, |
| 1423 | size_t nops, void *tag) { |
| 1424 | grpc_ioreq reqs[GRPC_IOREQ_OP_COUNT]; |
| 1425 | size_t in; |
| 1426 | size_t out; |
| 1427 | const grpc_op *op; |
| 1428 | grpc_ioreq *req; |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 1429 | void (*finish_func)(grpc_call *, int, void *) = finish_batch; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1430 | |
murgatroid99 | d47946b | 2015-03-09 14:27:07 -0700 | [diff] [blame] | 1431 | GRPC_CALL_LOG_BATCH(GPR_INFO, call, ops, nops, tag); |
| 1432 | |
murgatroid99 | a8c21e8 | 2015-02-12 13:55:53 -0800 | [diff] [blame] | 1433 | if (nops == 0) { |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1434 | grpc_cq_begin_op(call->cq); |
| 1435 | GRPC_CALL_INTERNAL_REF(call, "completion"); |
Craig Tiller | 12cf537 | 2015-07-09 13:48:11 -0700 | [diff] [blame] | 1436 | grpc_cq_end_op(call->cq, tag, 1, done_completion, call, |
| 1437 | allocate_completion(call)); |
murgatroid99 | a8c21e8 | 2015-02-12 13:55:53 -0800 | [diff] [blame] | 1438 | return GRPC_CALL_OK; |
| 1439 | } |
| 1440 | |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1441 | /* rewrite batch ops into ioreq ops */ |
| 1442 | for (in = 0, out = 0; in < nops; in++) { |
| 1443 | op = &ops[in]; |
| 1444 | switch (op->op) { |
| 1445 | case GRPC_OP_SEND_INITIAL_METADATA: |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1446 | /* Flag validation: currently allow no flags */ |
David Garcia Quintas | de52625 | 2015-06-15 13:15:26 -0700 | [diff] [blame] | 1447 | if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1448 | req = &reqs[out++]; |
| 1449 | req->op = GRPC_IOREQ_SEND_INITIAL_METADATA; |
| 1450 | req->data.send_metadata.count = op->data.send_initial_metadata.count; |
| 1451 | req->data.send_metadata.metadata = |
| 1452 | op->data.send_initial_metadata.metadata; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1453 | req->flags = op->flags; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1454 | break; |
| 1455 | case GRPC_OP_SEND_MESSAGE: |
Craig Tiller | 9a57633 | 2015-06-17 10:21:49 -0700 | [diff] [blame] | 1456 | if (!are_write_flags_valid(op->flags)) { |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1457 | return GRPC_CALL_ERROR_INVALID_FLAGS; |
| 1458 | } |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1459 | req = &reqs[out++]; |
| 1460 | req->op = GRPC_IOREQ_SEND_MESSAGE; |
| 1461 | req->data.send_message = op->data.send_message; |
David Garcia Quintas | 5927aec | 2015-06-18 17:24:44 -0700 | [diff] [blame] | 1462 | req->flags = op->flags; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1463 | break; |
| 1464 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1465 | /* Flag validation: currently allow no flags */ |
David Garcia Quintas | de52625 | 2015-06-15 13:15:26 -0700 | [diff] [blame] | 1466 | if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1467 | if (!call->is_client) { |
| 1468 | return GRPC_CALL_ERROR_NOT_ON_SERVER; |
| 1469 | } |
| 1470 | req = &reqs[out++]; |
| 1471 | req->op = GRPC_IOREQ_SEND_CLOSE; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1472 | req->flags = op->flags; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1473 | break; |
| 1474 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1475 | /* Flag validation: currently allow no flags */ |
David Garcia Quintas | de52625 | 2015-06-15 13:15:26 -0700 | [diff] [blame] | 1476 | if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1477 | if (call->is_client) { |
| 1478 | return GRPC_CALL_ERROR_NOT_ON_CLIENT; |
| 1479 | } |
| 1480 | req = &reqs[out++]; |
| 1481 | req->op = GRPC_IOREQ_SEND_TRAILING_METADATA; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1482 | req->flags = op->flags; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1483 | req->data.send_metadata.count = |
| 1484 | op->data.send_status_from_server.trailing_metadata_count; |
| 1485 | req->data.send_metadata.metadata = |
| 1486 | op->data.send_status_from_server.trailing_metadata; |
| 1487 | req = &reqs[out++]; |
| 1488 | req->op = GRPC_IOREQ_SEND_STATUS; |
| 1489 | req->data.send_status.code = op->data.send_status_from_server.status; |
| 1490 | req->data.send_status.details = |
Craig Tiller | aea081f | 2015-06-11 14:19:33 -0700 | [diff] [blame] | 1491 | op->data.send_status_from_server.status_details != NULL |
| 1492 | ? grpc_mdstr_from_string( |
| 1493 | call->metadata_context, |
| 1494 | op->data.send_status_from_server.status_details) |
| 1495 | : NULL; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1496 | req = &reqs[out++]; |
| 1497 | req->op = GRPC_IOREQ_SEND_CLOSE; |
| 1498 | break; |
| 1499 | case GRPC_OP_RECV_INITIAL_METADATA: |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1500 | /* Flag validation: currently allow no flags */ |
David Garcia Quintas | de52625 | 2015-06-15 13:15:26 -0700 | [diff] [blame] | 1501 | if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1502 | if (!call->is_client) { |
| 1503 | return GRPC_CALL_ERROR_NOT_ON_SERVER; |
| 1504 | } |
| 1505 | req = &reqs[out++]; |
| 1506 | req->op = GRPC_IOREQ_RECV_INITIAL_METADATA; |
| 1507 | req->data.recv_metadata = op->data.recv_initial_metadata; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1508 | req->flags = op->flags; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1509 | break; |
| 1510 | case GRPC_OP_RECV_MESSAGE: |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1511 | /* Flag validation: currently allow no flags */ |
David Garcia Quintas | de52625 | 2015-06-15 13:15:26 -0700 | [diff] [blame] | 1512 | if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1513 | req = &reqs[out++]; |
| 1514 | req->op = GRPC_IOREQ_RECV_MESSAGE; |
| 1515 | req->data.recv_message = op->data.recv_message; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1516 | req->flags = op->flags; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1517 | break; |
| 1518 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1519 | /* Flag validation: currently allow no flags */ |
David Garcia Quintas | de52625 | 2015-06-15 13:15:26 -0700 | [diff] [blame] | 1520 | if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1521 | if (!call->is_client) { |
| 1522 | return GRPC_CALL_ERROR_NOT_ON_SERVER; |
| 1523 | } |
| 1524 | req = &reqs[out++]; |
| 1525 | req->op = GRPC_IOREQ_RECV_STATUS; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1526 | req->flags = op->flags; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1527 | req->data.recv_status.set_value = set_status_value_directly; |
| 1528 | req->data.recv_status.user_data = op->data.recv_status_on_client.status; |
| 1529 | req = &reqs[out++]; |
| 1530 | req->op = GRPC_IOREQ_RECV_STATUS_DETAILS; |
| 1531 | req->data.recv_status_details.details = |
| 1532 | op->data.recv_status_on_client.status_details; |
| 1533 | req->data.recv_status_details.details_capacity = |
| 1534 | op->data.recv_status_on_client.status_details_capacity; |
| 1535 | req = &reqs[out++]; |
| 1536 | req->op = GRPC_IOREQ_RECV_TRAILING_METADATA; |
| 1537 | req->data.recv_metadata = |
| 1538 | op->data.recv_status_on_client.trailing_metadata; |
| 1539 | req = &reqs[out++]; |
| 1540 | req->op = GRPC_IOREQ_RECV_CLOSE; |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 1541 | finish_func = finish_batch_with_close; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1542 | break; |
| 1543 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1544 | /* Flag validation: currently allow no flags */ |
David Garcia Quintas | de52625 | 2015-06-15 13:15:26 -0700 | [diff] [blame] | 1545 | if (op->flags != 0) return GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1546 | req = &reqs[out++]; |
| 1547 | req->op = GRPC_IOREQ_RECV_STATUS; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1548 | req->flags = op->flags; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1549 | req->data.recv_status.set_value = set_cancelled_value; |
| 1550 | req->data.recv_status.user_data = |
| 1551 | op->data.recv_close_on_server.cancelled; |
| 1552 | req = &reqs[out++]; |
| 1553 | req->op = GRPC_IOREQ_RECV_CLOSE; |
Craig Tiller | fbac5f1 | 2015-05-15 14:20:44 -0700 | [diff] [blame] | 1554 | finish_func = finish_batch_with_close; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1555 | break; |
| 1556 | } |
| 1557 | } |
| 1558 | |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1559 | GRPC_CALL_INTERNAL_REF(call, "completion"); |
| 1560 | grpc_cq_begin_op(call->cq); |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1561 | |
Craig Tiller | f9fae98 | 2015-05-29 23:02:18 -0700 | [diff] [blame] | 1562 | return grpc_call_start_ioreq_and_call_back(call, reqs, out, finish_func, tag); |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1563 | } |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 1564 | |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 1565 | void grpc_call_context_set(grpc_call *call, grpc_context_index elem, |
| 1566 | void *value, void (*destroy)(void *value)) { |
Julien Boeuf | 83b0297 | 2015-05-20 22:50:34 -0700 | [diff] [blame] | 1567 | if (call->context[elem].destroy) { |
| 1568 | call->context[elem].destroy(call->context[elem].value); |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 1569 | } |
Julien Boeuf | 83b0297 | 2015-05-20 22:50:34 -0700 | [diff] [blame] | 1570 | call->context[elem].value = value; |
| 1571 | call->context[elem].destroy = destroy; |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | void *grpc_call_context_get(grpc_call *call, grpc_context_index elem) { |
Julien Boeuf | 83b0297 | 2015-05-20 22:50:34 -0700 | [diff] [blame] | 1575 | return call->context[elem].value; |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 1576 | } |
Julien Boeuf | 9f218dd | 2015-04-23 10:24:02 -0700 | [diff] [blame] | 1577 | |
| 1578 | gpr_uint8 grpc_call_is_client(grpc_call *call) { return call->is_client; } |