Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, Google Inc. |
| 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 | */ |
| 33 | |
| 34 | #include "src/core/surface/server.h" |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include "src/core/channel/census_filter.h" |
| 40 | #include "src/core/channel/channel_args.h" |
| 41 | #include "src/core/channel/connected_channel.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 42 | #include "src/core/iomgr/iomgr.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 43 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | #include "src/core/surface/call.h" |
| 45 | #include "src/core/surface/channel.h" |
| 46 | #include "src/core/surface/completion_queue.h" |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 47 | #include "src/core/transport/metadata.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | #include <grpc/support/alloc.h> |
| 49 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | #include <grpc/support/useful.h> |
| 51 | |
| 52 | typedef enum { PENDING_START, ALL_CALLS, CALL_LIST_COUNT } call_list; |
| 53 | |
| 54 | typedef struct listener { |
| 55 | void *arg; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 56 | void (*start)(grpc_server *server, void *arg, grpc_pollset **pollsets, |
| 57 | size_t pollset_count); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | void (*destroy)(grpc_server *server, void *arg); |
| 59 | struct listener *next; |
| 60 | } listener; |
| 61 | |
| 62 | typedef struct call_data call_data; |
| 63 | typedef struct channel_data channel_data; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 64 | typedef struct registered_method registered_method; |
| 65 | |
| 66 | typedef struct { |
| 67 | call_data *next; |
| 68 | call_data *prev; |
| 69 | } call_link; |
| 70 | |
| 71 | typedef enum { LEGACY_CALL, BATCH_CALL, REGISTERED_CALL } requested_call_type; |
| 72 | |
| 73 | typedef struct { |
| 74 | requested_call_type type; |
| 75 | void *tag; |
| 76 | union { |
| 77 | struct { |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 78 | grpc_completion_queue *cq_bind; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 79 | grpc_call **call; |
| 80 | grpc_call_details *details; |
| 81 | grpc_metadata_array *initial_metadata; |
| 82 | } batch; |
| 83 | struct { |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 84 | grpc_completion_queue *cq_bind; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 85 | grpc_call **call; |
| 86 | registered_method *registered_method; |
| 87 | gpr_timespec *deadline; |
| 88 | grpc_metadata_array *initial_metadata; |
| 89 | grpc_byte_buffer **optional_payload; |
| 90 | } registered; |
| 91 | } data; |
| 92 | } requested_call; |
| 93 | |
| 94 | typedef struct { |
| 95 | requested_call *calls; |
| 96 | size_t count; |
| 97 | size_t capacity; |
| 98 | } requested_call_array; |
| 99 | |
| 100 | struct registered_method { |
| 101 | char *method; |
| 102 | char *host; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 103 | call_data *pending; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 104 | requested_call_array requested; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 105 | grpc_completion_queue *cq; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 106 | registered_method *next; |
| 107 | }; |
| 108 | |
| 109 | typedef struct channel_registered_method { |
| 110 | registered_method *server_registered_method; |
| 111 | grpc_mdstr *method; |
| 112 | grpc_mdstr *host; |
| 113 | } channel_registered_method; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | |
| 115 | struct channel_data { |
| 116 | grpc_server *server; |
| 117 | grpc_channel *channel; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 118 | grpc_mdstr *path_key; |
| 119 | grpc_mdstr *authority_key; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | /* linked list of all channels on a server */ |
| 121 | channel_data *next; |
| 122 | channel_data *prev; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 123 | channel_registered_method *registered_methods; |
| 124 | gpr_uint32 registered_method_slots; |
| 125 | gpr_uint32 registered_method_max_probes; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | struct grpc_server { |
| 129 | size_t channel_filter_count; |
| 130 | const grpc_channel_filter **channel_filters; |
| 131 | grpc_channel_args *channel_args; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 132 | grpc_completion_queue *unregistered_cq; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 133 | |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 134 | grpc_completion_queue **cqs; |
| 135 | grpc_pollset **pollsets; |
| 136 | size_t cq_count; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 137 | |
| 138 | gpr_mu mu; |
| 139 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 140 | registered_method *registered_methods; |
| 141 | requested_call_array requested_calls; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | |
| 143 | gpr_uint8 shutdown; |
Craig Tiller | 4ffdcd5 | 2015-01-16 11:34:55 -0800 | [diff] [blame] | 144 | gpr_uint8 have_shutdown_tag; |
| 145 | void *shutdown_tag; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 146 | |
| 147 | call_data *lists[CALL_LIST_COUNT]; |
| 148 | channel_data root_channel_data; |
| 149 | |
| 150 | listener *listeners; |
| 151 | gpr_refcount internal_refcount; |
| 152 | }; |
| 153 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 154 | typedef enum { |
| 155 | /* waiting for metadata */ |
| 156 | NOT_STARTED, |
| 157 | /* inital metadata read, not flow controlled in yet */ |
| 158 | PENDING, |
| 159 | /* flow controlled in, on completion queue */ |
| 160 | ACTIVATED, |
| 161 | /* cancelled before being queued */ |
| 162 | ZOMBIED |
| 163 | } call_state; |
| 164 | |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 165 | typedef struct legacy_data { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 166 | grpc_metadata_array initial_metadata; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 167 | } legacy_data; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 168 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 169 | struct call_data { |
| 170 | grpc_call *call; |
| 171 | |
| 172 | call_state state; |
| 173 | gpr_timespec deadline; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 174 | grpc_mdstr *path; |
| 175 | grpc_mdstr *host; |
| 176 | |
| 177 | legacy_data *legacy; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 178 | grpc_completion_queue *cq_new; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 179 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 180 | call_data **root[CALL_LIST_COUNT]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 181 | call_link links[CALL_LIST_COUNT]; |
| 182 | }; |
| 183 | |
| 184 | #define SERVER_FROM_CALL_ELEM(elem) \ |
| 185 | (((channel_data *)(elem)->channel_data)->server) |
| 186 | |
| 187 | static void do_nothing(void *unused, grpc_op_error ignored) {} |
| 188 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 189 | static void begin_call(grpc_server *server, call_data *calld, |
| 190 | requested_call *rc); |
| 191 | static void fail_call(grpc_server *server, requested_call *rc); |
| 192 | |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 193 | static int call_list_join(call_data **root, call_data *call, call_list list) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 194 | GPR_ASSERT(!call->root[list]); |
| 195 | call->root[list] = root; |
| 196 | if (!*root) { |
| 197 | *root = call; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 198 | call->links[list].next = call->links[list].prev = call; |
| 199 | } else { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 200 | call->links[list].next = *root; |
| 201 | call->links[list].prev = (*root)->links[list].prev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 202 | call->links[list].next->links[list].prev = |
| 203 | call->links[list].prev->links[list].next = call; |
| 204 | } |
| 205 | return 1; |
| 206 | } |
| 207 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 208 | static call_data *call_list_remove_head(call_data **root, call_list list) { |
| 209 | call_data *out = *root; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 210 | if (out) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 211 | out->root[list] = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 212 | if (out->links[list].next == out) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 213 | *root = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 214 | } else { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 215 | *root = out->links[list].next; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 216 | out->links[list].next->links[list].prev = out->links[list].prev; |
| 217 | out->links[list].prev->links[list].next = out->links[list].next; |
| 218 | } |
| 219 | } |
| 220 | return out; |
| 221 | } |
| 222 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 223 | static int call_list_remove(call_data *call, call_list list) { |
| 224 | call_data **root = call->root[list]; |
| 225 | if (root == NULL) return 0; |
| 226 | call->root[list] = NULL; |
| 227 | if (*root == call) { |
| 228 | *root = call->links[list].next; |
| 229 | if (*root == call) { |
| 230 | *root = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 231 | return 1; |
| 232 | } |
| 233 | } |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 234 | GPR_ASSERT(*root != call); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 235 | call->links[list].next->links[list].prev = call->links[list].prev; |
| 236 | call->links[list].prev->links[list].next = call->links[list].next; |
| 237 | return 1; |
| 238 | } |
| 239 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 240 | static void requested_call_array_destroy(requested_call_array *array) { |
| 241 | gpr_free(array->calls); |
| 242 | } |
| 243 | |
| 244 | static requested_call *requested_call_array_add(requested_call_array *array) { |
| 245 | requested_call *rc; |
| 246 | if (array->count == array->capacity) { |
| 247 | array->capacity = GPR_MAX(array->capacity + 8, array->capacity * 2); |
| 248 | array->calls = |
| 249 | gpr_realloc(array->calls, sizeof(requested_call) * array->capacity); |
| 250 | } |
| 251 | rc = &array->calls[array->count++]; |
| 252 | memset(rc, 0, sizeof(*rc)); |
| 253 | return rc; |
| 254 | } |
| 255 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 256 | static void server_ref(grpc_server *server) { |
| 257 | gpr_ref(&server->internal_refcount); |
| 258 | } |
| 259 | |
| 260 | static void server_unref(grpc_server *server) { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 261 | registered_method *rm; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 262 | if (gpr_unref(&server->internal_refcount)) { |
| 263 | grpc_channel_args_destroy(server->channel_args); |
| 264 | gpr_mu_destroy(&server->mu); |
| 265 | gpr_free(server->channel_filters); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 266 | requested_call_array_destroy(&server->requested_calls); |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 267 | while ((rm = server->registered_methods) != NULL) { |
| 268 | server->registered_methods = rm->next; |
| 269 | gpr_free(rm->method); |
| 270 | gpr_free(rm->host); |
| 271 | requested_call_array_destroy(&rm->requested); |
| 272 | gpr_free(rm); |
| 273 | } |
| 274 | gpr_free(server->cqs); |
| 275 | gpr_free(server->pollsets); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 276 | gpr_free(server); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | static int is_channel_orphaned(channel_data *chand) { |
| 281 | return chand->next == chand; |
| 282 | } |
| 283 | |
| 284 | static void orphan_channel(channel_data *chand) { |
| 285 | chand->next->prev = chand->prev; |
| 286 | chand->prev->next = chand->next; |
| 287 | chand->next = chand->prev = chand; |
| 288 | } |
| 289 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 290 | static void finish_destroy_channel(void *cd, int success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 291 | channel_data *chand = cd; |
| 292 | grpc_server *server = chand->server; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 293 | grpc_channel_destroy(chand->channel); |
| 294 | server_unref(server); |
| 295 | } |
| 296 | |
| 297 | static void destroy_channel(channel_data *chand) { |
| 298 | if (is_channel_orphaned(chand)) return; |
| 299 | GPR_ASSERT(chand->server != NULL); |
| 300 | orphan_channel(chand); |
| 301 | server_ref(chand->server); |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 302 | grpc_iomgr_add_callback(finish_destroy_channel, chand); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 305 | static void finish_start_new_rpc_and_unlock(grpc_server *server, |
| 306 | grpc_call_element *elem, |
| 307 | call_data **pending_root, |
| 308 | requested_call_array *array) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 309 | requested_call rc; |
| 310 | call_data *calld = elem->call_data; |
| 311 | if (array->count == 0) { |
| 312 | calld->state = PENDING; |
| 313 | call_list_join(pending_root, calld, PENDING_START); |
| 314 | gpr_mu_unlock(&server->mu); |
| 315 | } else { |
Craig Tiller | 0ef1a92 | 2015-02-11 16:23:01 -0800 | [diff] [blame] | 316 | rc = array->calls[--array->count]; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 317 | calld->state = ACTIVATED; |
| 318 | gpr_mu_unlock(&server->mu); |
| 319 | begin_call(server, calld, &rc); |
| 320 | } |
| 321 | } |
| 322 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 323 | static void start_new_rpc(grpc_call_element *elem) { |
| 324 | channel_data *chand = elem->channel_data; |
| 325 | call_data *calld = elem->call_data; |
| 326 | grpc_server *server = chand->server; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 327 | gpr_uint32 i; |
| 328 | gpr_uint32 hash; |
| 329 | channel_registered_method *rm; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 330 | |
| 331 | gpr_mu_lock(&server->mu); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 332 | if (chand->registered_methods && calld->path && calld->host) { |
| 333 | /* check for an exact match with host */ |
| 334 | hash = GRPC_MDSTR_KV_HASH(calld->host->hash, calld->path->hash); |
| 335 | for (i = 0; i < chand->registered_method_max_probes; i++) { |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 336 | rm = &chand->registered_methods[(hash + i) % |
| 337 | chand->registered_method_slots]; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 338 | if (!rm) break; |
| 339 | if (rm->host != calld->host) continue; |
| 340 | if (rm->method != calld->path) continue; |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 341 | finish_start_new_rpc_and_unlock(server, elem, |
| 342 | &rm->server_registered_method->pending, |
| 343 | &rm->server_registered_method->requested); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 344 | return; |
| 345 | } |
| 346 | /* check for a wildcard method definition (no host set) */ |
| 347 | hash = GRPC_MDSTR_KV_HASH(0, calld->path->hash); |
Craig Tiller | 0ef1a92 | 2015-02-11 16:23:01 -0800 | [diff] [blame] | 348 | for (i = 0; i <= chand->registered_method_max_probes; i++) { |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 349 | rm = &chand->registered_methods[(hash + i) % |
| 350 | chand->registered_method_slots]; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 351 | if (!rm) break; |
| 352 | if (rm->host != NULL) continue; |
| 353 | if (rm->method != calld->path) continue; |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 354 | finish_start_new_rpc_and_unlock(server, elem, |
| 355 | &rm->server_registered_method->pending, |
| 356 | &rm->server_registered_method->requested); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 357 | return; |
| 358 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 359 | } |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 360 | finish_start_new_rpc_and_unlock(server, elem, &server->lists[PENDING_START], |
| 361 | &server->requested_calls); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 362 | } |
| 363 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 364 | static void kill_zombie(void *elem, int success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 365 | grpc_call_destroy(grpc_call_from_top_element(elem)); |
| 366 | } |
| 367 | |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 368 | static void stream_closed(grpc_call_element *elem) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 369 | call_data *calld = elem->call_data; |
| 370 | channel_data *chand = elem->channel_data; |
| 371 | gpr_mu_lock(&chand->server->mu); |
| 372 | switch (calld->state) { |
| 373 | case ACTIVATED: |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 374 | break; |
| 375 | case PENDING: |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 376 | call_list_remove(calld, PENDING_START); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 377 | /* fallthrough intended */ |
| 378 | case NOT_STARTED: |
| 379 | calld->state = ZOMBIED; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 380 | grpc_iomgr_add_callback(kill_zombie, elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 381 | break; |
| 382 | case ZOMBIED: |
| 383 | break; |
| 384 | } |
| 385 | gpr_mu_unlock(&chand->server->mu); |
Craig Tiller | 0a927bf | 2015-02-05 10:52:53 -0800 | [diff] [blame] | 386 | grpc_call_stream_closed(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 389 | static void read_closed(grpc_call_element *elem) { |
| 390 | call_data *calld = elem->call_data; |
| 391 | channel_data *chand = elem->channel_data; |
| 392 | gpr_mu_lock(&chand->server->mu); |
| 393 | switch (calld->state) { |
| 394 | case ACTIVATED: |
| 395 | case PENDING: |
| 396 | grpc_call_read_closed(elem); |
| 397 | break; |
| 398 | case NOT_STARTED: |
| 399 | calld->state = ZOMBIED; |
| 400 | grpc_iomgr_add_callback(kill_zombie, elem); |
| 401 | break; |
| 402 | case ZOMBIED: |
| 403 | break; |
| 404 | } |
| 405 | gpr_mu_unlock(&chand->server->mu); |
| 406 | } |
| 407 | |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 408 | static void call_op(grpc_call_element *elem, grpc_call_element *from_elemn, |
| 409 | grpc_call_op *op) { |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 410 | channel_data *chand = elem->channel_data; |
| 411 | call_data *calld = elem->call_data; |
| 412 | grpc_mdelem *md; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 413 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 414 | switch (op->type) { |
| 415 | case GRPC_RECV_METADATA: |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 416 | md = op->data.metadata; |
| 417 | if (md->key == chand->path_key) { |
| 418 | calld->path = grpc_mdstr_ref(md->value); |
| 419 | grpc_mdelem_unref(md); |
| 420 | } else if (md->key == chand->authority_key) { |
| 421 | calld->host = grpc_mdstr_ref(md->value); |
| 422 | grpc_mdelem_unref(md); |
| 423 | } else { |
| 424 | grpc_call_recv_metadata(elem, md); |
| 425 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 426 | break; |
| 427 | case GRPC_RECV_END_OF_INITIAL_METADATA: |
| 428 | start_new_rpc(elem); |
Craig Tiller | 4069b68 | 2015-01-29 14:01:19 -0800 | [diff] [blame] | 429 | grpc_call_initial_metadata_complete(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 430 | break; |
| 431 | case GRPC_RECV_MESSAGE: |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 432 | grpc_call_recv_message(elem, op->data.message); |
| 433 | op->done_cb(op->user_data, GRPC_OP_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 434 | break; |
| 435 | case GRPC_RECV_HALF_CLOSE: |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 436 | read_closed(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 437 | break; |
| 438 | case GRPC_RECV_FINISH: |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 439 | stream_closed(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 440 | break; |
| 441 | case GRPC_RECV_DEADLINE: |
| 442 | grpc_call_set_deadline(elem, op->data.deadline); |
| 443 | ((call_data *)elem->call_data)->deadline = op->data.deadline; |
| 444 | break; |
| 445 | default: |
| 446 | GPR_ASSERT(op->dir == GRPC_CALL_DOWN); |
| 447 | grpc_call_next_op(elem, op); |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 452 | static void channel_op(grpc_channel_element *elem, |
| 453 | grpc_channel_element *from_elem, grpc_channel_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 454 | channel_data *chand = elem->channel_data; |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 455 | grpc_server *server = chand->server; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 456 | |
| 457 | switch (op->type) { |
| 458 | case GRPC_ACCEPT_CALL: |
| 459 | /* create a call */ |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 460 | grpc_call_create(chand->channel, NULL, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 461 | op->data.accept_call.transport_server_data); |
| 462 | break; |
| 463 | case GRPC_TRANSPORT_CLOSED: |
| 464 | /* if the transport is closed for a server channel, we destroy the |
| 465 | channel */ |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 466 | gpr_mu_lock(&server->mu); |
| 467 | server_ref(server); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 468 | destroy_channel(chand); |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 469 | gpr_mu_unlock(&server->mu); |
| 470 | server_unref(server); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 471 | break; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 472 | case GRPC_TRANSPORT_GOAWAY: |
| 473 | gpr_slice_unref(op->data.goaway.message); |
| 474 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 475 | default: |
| 476 | GPR_ASSERT(op->dir == GRPC_CALL_DOWN); |
| 477 | grpc_channel_next_op(elem, op); |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 482 | static void finish_shutdown_channel(void *cd, int success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 483 | channel_data *chand = cd; |
| 484 | grpc_channel_op op; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 485 | op.type = GRPC_CHANNEL_DISCONNECT; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 486 | op.dir = GRPC_CALL_DOWN; |
| 487 | channel_op(grpc_channel_stack_element( |
| 488 | grpc_channel_get_channel_stack(chand->channel), 0), |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 489 | NULL, &op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 490 | grpc_channel_internal_unref(chand->channel); |
| 491 | } |
| 492 | |
| 493 | static void shutdown_channel(channel_data *chand) { |
| 494 | grpc_channel_internal_ref(chand->channel); |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 495 | grpc_iomgr_add_callback(finish_shutdown_channel, chand); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static void init_call_elem(grpc_call_element *elem, |
| 499 | const void *server_transport_data) { |
| 500 | call_data *calld = elem->call_data; |
| 501 | channel_data *chand = elem->channel_data; |
| 502 | memset(calld, 0, sizeof(call_data)); |
| 503 | calld->deadline = gpr_inf_future; |
| 504 | calld->call = grpc_call_from_top_element(elem); |
| 505 | |
| 506 | gpr_mu_lock(&chand->server->mu); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 507 | call_list_join(&chand->server->lists[ALL_CALLS], calld, ALL_CALLS); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 508 | gpr_mu_unlock(&chand->server->mu); |
| 509 | |
| 510 | server_ref(chand->server); |
| 511 | } |
| 512 | |
| 513 | static void destroy_call_elem(grpc_call_element *elem) { |
| 514 | channel_data *chand = elem->channel_data; |
Craig Tiller | db7db99 | 2015-01-29 11:19:01 -0800 | [diff] [blame] | 515 | call_data *calld = elem->call_data; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 516 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 517 | |
| 518 | gpr_mu_lock(&chand->server->mu); |
| 519 | for (i = 0; i < CALL_LIST_COUNT; i++) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 520 | call_list_remove(elem->call_data, i); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 521 | } |
Craig Tiller | 4ffdcd5 | 2015-01-16 11:34:55 -0800 | [diff] [blame] | 522 | if (chand->server->shutdown && chand->server->have_shutdown_tag && |
| 523 | chand->server->lists[ALL_CALLS] == NULL) { |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 524 | for (i = 0; i < chand->server->cq_count; i++) { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 525 | grpc_cq_end_server_shutdown(chand->server->cqs[i], |
| 526 | chand->server->shutdown_tag); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 527 | } |
Craig Tiller | 4ffdcd5 | 2015-01-16 11:34:55 -0800 | [diff] [blame] | 528 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 529 | gpr_mu_unlock(&chand->server->mu); |
| 530 | |
Craig Tiller | 4df31a6 | 2015-01-30 09:44:31 -0800 | [diff] [blame] | 531 | if (calld->host) { |
| 532 | grpc_mdstr_unref(calld->host); |
| 533 | } |
| 534 | if (calld->path) { |
| 535 | grpc_mdstr_unref(calld->path); |
| 536 | } |
| 537 | |
Craig Tiller | db7db99 | 2015-01-29 11:19:01 -0800 | [diff] [blame] | 538 | if (calld->legacy) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 539 | gpr_free(calld->legacy->initial_metadata.metadata); |
Craig Tiller | db7db99 | 2015-01-29 11:19:01 -0800 | [diff] [blame] | 540 | gpr_free(calld->legacy); |
| 541 | } |
| 542 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 543 | server_unref(chand->server); |
| 544 | } |
| 545 | |
| 546 | static void init_channel_elem(grpc_channel_element *elem, |
| 547 | const grpc_channel_args *args, |
| 548 | grpc_mdctx *metadata_context, int is_first, |
| 549 | int is_last) { |
| 550 | channel_data *chand = elem->channel_data; |
| 551 | GPR_ASSERT(is_first); |
| 552 | GPR_ASSERT(!is_last); |
| 553 | chand->server = NULL; |
| 554 | chand->channel = NULL; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 555 | chand->path_key = grpc_mdstr_from_string(metadata_context, ":path"); |
| 556 | chand->authority_key = grpc_mdstr_from_string(metadata_context, ":authority"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 557 | chand->next = chand->prev = chand; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 558 | chand->registered_methods = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static void destroy_channel_elem(grpc_channel_element *elem) { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 562 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 563 | channel_data *chand = elem->channel_data; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 564 | if (chand->registered_methods) { |
| 565 | for (i = 0; i < chand->registered_method_slots; i++) { |
| 566 | if (chand->registered_methods[i].method) { |
| 567 | grpc_mdstr_unref(chand->registered_methods[i].method); |
| 568 | } |
| 569 | if (chand->registered_methods[i].host) { |
| 570 | grpc_mdstr_unref(chand->registered_methods[i].host); |
| 571 | } |
| 572 | } |
| 573 | gpr_free(chand->registered_methods); |
| 574 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 575 | if (chand->server) { |
| 576 | gpr_mu_lock(&chand->server->mu); |
| 577 | chand->next->prev = chand->prev; |
| 578 | chand->prev->next = chand->next; |
| 579 | chand->next = chand->prev = chand; |
| 580 | gpr_mu_unlock(&chand->server->mu); |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 581 | grpc_mdstr_unref(chand->path_key); |
| 582 | grpc_mdstr_unref(chand->authority_key); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 583 | server_unref(chand->server); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | static const grpc_channel_filter server_surface_filter = { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 588 | call_op, channel_op, sizeof(call_data), init_call_elem, destroy_call_elem, |
| 589 | sizeof(channel_data), init_channel_elem, destroy_channel_elem, "server", |
Craig Tiller | 9f28ac2 | 2015-01-27 17:01:29 -0800 | [diff] [blame] | 590 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 591 | |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 592 | static void addcq(grpc_server *server, grpc_completion_queue *cq) { |
| 593 | size_t i, n; |
| 594 | for (i = 0; i < server->cq_count; i++) { |
| 595 | if (server->cqs[i] == cq) return; |
| 596 | } |
| 597 | n = server->cq_count++; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 598 | server->cqs = gpr_realloc(server->cqs, |
| 599 | server->cq_count * sizeof(grpc_completion_queue *)); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 600 | server->cqs[n] = cq; |
| 601 | } |
| 602 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 603 | grpc_server *grpc_server_create_from_filters(grpc_completion_queue *cq, |
| 604 | grpc_channel_filter **filters, |
| 605 | size_t filter_count, |
| 606 | const grpc_channel_args *args) { |
| 607 | size_t i; |
| 608 | int census_enabled = grpc_channel_args_is_census_enabled(args); |
| 609 | |
| 610 | grpc_server *server = gpr_malloc(sizeof(grpc_server)); |
| 611 | memset(server, 0, sizeof(grpc_server)); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 612 | if (cq) addcq(server, cq); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 613 | |
| 614 | gpr_mu_init(&server->mu); |
| 615 | |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 616 | server->unregistered_cq = cq; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 617 | /* decremented by grpc_server_destroy */ |
| 618 | gpr_ref_init(&server->internal_refcount, 1); |
| 619 | server->root_channel_data.next = server->root_channel_data.prev = |
| 620 | &server->root_channel_data; |
| 621 | |
| 622 | /* Server filter stack is: |
| 623 | |
| 624 | server_surface_filter - for making surface API calls |
| 625 | grpc_server_census_filter (optional) - for stats collection and tracing |
| 626 | {passed in filter stack} |
| 627 | grpc_connected_channel_filter - for interfacing with transports */ |
| 628 | server->channel_filter_count = filter_count + 1 + census_enabled; |
| 629 | server->channel_filters = |
| 630 | gpr_malloc(server->channel_filter_count * sizeof(grpc_channel_filter *)); |
| 631 | server->channel_filters[0] = &server_surface_filter; |
| 632 | if (census_enabled) { |
| 633 | server->channel_filters[1] = &grpc_server_census_filter; |
| 634 | } |
| 635 | for (i = 0; i < filter_count; i++) { |
| 636 | server->channel_filters[i + 1 + census_enabled] = filters[i]; |
| 637 | } |
| 638 | |
| 639 | server->channel_args = grpc_channel_args_copy(args); |
| 640 | |
| 641 | return server; |
| 642 | } |
| 643 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 644 | static int streq(const char *a, const char *b) { |
| 645 | if (a == NULL && b == NULL) return 1; |
| 646 | if (a == NULL) return 0; |
| 647 | if (b == NULL) return 0; |
| 648 | return 0 == strcmp(a, b); |
| 649 | } |
| 650 | |
| 651 | void *grpc_server_register_method(grpc_server *server, const char *method, |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 652 | const char *host, |
| 653 | grpc_completion_queue *cq_new_rpc) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 654 | registered_method *m; |
| 655 | if (!method) { |
| 656 | gpr_log(GPR_ERROR, "%s method string cannot be NULL", __FUNCTION__); |
| 657 | return NULL; |
| 658 | } |
| 659 | for (m = server->registered_methods; m; m = m->next) { |
| 660 | if (streq(m->method, method) && streq(m->host, host)) { |
| 661 | gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method, |
| 662 | host ? host : "*"); |
| 663 | return NULL; |
| 664 | } |
| 665 | } |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 666 | addcq(server, cq_new_rpc); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 667 | m = gpr_malloc(sizeof(registered_method)); |
| 668 | memset(m, 0, sizeof(*m)); |
| 669 | m->method = gpr_strdup(method); |
| 670 | m->host = gpr_strdup(host); |
| 671 | m->next = server->registered_methods; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 672 | m->cq = cq_new_rpc; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 673 | server->registered_methods = m; |
| 674 | return m; |
| 675 | } |
| 676 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 677 | void grpc_server_start(grpc_server *server) { |
| 678 | listener *l; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 679 | size_t i; |
| 680 | |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 681 | server->pollsets = gpr_malloc(sizeof(grpc_pollset *) * server->cq_count); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 682 | for (i = 0; i < server->cq_count; i++) { |
| 683 | server->pollsets[i] = grpc_cq_pollset(server->cqs[i]); |
| 684 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 685 | |
| 686 | for (l = server->listeners; l; l = l->next) { |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 687 | l->start(server, l->arg, server->pollsets, server->cq_count); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | grpc_transport_setup_result grpc_server_setup_transport( |
| 692 | grpc_server *s, grpc_transport *transport, |
| 693 | grpc_channel_filter const **extra_filters, size_t num_extra_filters, |
| 694 | grpc_mdctx *mdctx) { |
| 695 | size_t num_filters = s->channel_filter_count + num_extra_filters + 1; |
| 696 | grpc_channel_filter const **filters = |
| 697 | gpr_malloc(sizeof(grpc_channel_filter *) * num_filters); |
| 698 | size_t i; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 699 | size_t num_registered_methods; |
| 700 | size_t alloc; |
| 701 | registered_method *rm; |
| 702 | channel_registered_method *crm; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 703 | grpc_channel *channel; |
| 704 | channel_data *chand; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 705 | grpc_mdstr *host; |
| 706 | grpc_mdstr *method; |
| 707 | gpr_uint32 hash; |
| 708 | gpr_uint32 slots; |
| 709 | gpr_uint32 probes; |
| 710 | gpr_uint32 max_probes = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 711 | |
| 712 | for (i = 0; i < s->channel_filter_count; i++) { |
| 713 | filters[i] = s->channel_filters[i]; |
| 714 | } |
| 715 | for (; i < s->channel_filter_count + num_extra_filters; i++) { |
| 716 | filters[i] = extra_filters[i - s->channel_filter_count]; |
| 717 | } |
| 718 | filters[i] = &grpc_connected_channel_filter; |
| 719 | |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 720 | for (i = 0; i < s->cq_count; i++) { |
| 721 | grpc_transport_add_to_pollset(transport, grpc_cq_pollset(s->cqs[i])); |
| 722 | } |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 723 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 724 | channel = grpc_channel_create_from_filters(filters, num_filters, |
| 725 | s->channel_args, mdctx, 0); |
| 726 | chand = (channel_data *)grpc_channel_stack_element( |
| 727 | grpc_channel_get_channel_stack(channel), 0)->channel_data; |
| 728 | chand->server = s; |
| 729 | server_ref(s); |
| 730 | chand->channel = channel; |
| 731 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 732 | num_registered_methods = 0; |
| 733 | for (rm = s->registered_methods; rm; rm = rm->next) { |
| 734 | num_registered_methods++; |
| 735 | } |
| 736 | /* build a lookup table phrased in terms of mdstr's in this channels context |
| 737 | to quickly find registered methods */ |
| 738 | if (num_registered_methods > 0) { |
| 739 | slots = 2 * num_registered_methods; |
| 740 | alloc = sizeof(channel_registered_method) * slots; |
| 741 | chand->registered_methods = gpr_malloc(alloc); |
| 742 | memset(chand->registered_methods, 0, alloc); |
| 743 | for (rm = s->registered_methods; rm; rm = rm->next) { |
| 744 | host = rm->host ? grpc_mdstr_from_string(mdctx, rm->host) : NULL; |
Craig Tiller | e76c966 | 2015-02-11 13:18:31 -0800 | [diff] [blame] | 745 | method = grpc_mdstr_from_string(mdctx, rm->method); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 746 | hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash); |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 747 | for (probes = 0; chand->registered_methods[(hash + probes) % slots] |
| 748 | .server_registered_method != NULL; |
| 749 | probes++) |
| 750 | ; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 751 | if (probes > max_probes) max_probes = probes; |
| 752 | crm = &chand->registered_methods[(hash + probes) % slots]; |
| 753 | crm->server_registered_method = rm; |
| 754 | crm->host = host; |
| 755 | crm->method = method; |
| 756 | } |
| 757 | chand->registered_method_slots = slots; |
| 758 | chand->registered_method_max_probes = max_probes; |
| 759 | } |
| 760 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 761 | gpr_mu_lock(&s->mu); |
| 762 | chand->next = &s->root_channel_data; |
| 763 | chand->prev = chand->next->prev; |
| 764 | chand->next->prev = chand->prev->next = chand; |
| 765 | gpr_mu_unlock(&s->mu); |
| 766 | |
| 767 | gpr_free(filters); |
| 768 | |
| 769 | return grpc_connected_channel_bind_transport( |
| 770 | grpc_channel_get_channel_stack(channel), transport); |
| 771 | } |
| 772 | |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 773 | static void shutdown_internal(grpc_server *server, gpr_uint8 have_shutdown_tag, |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 774 | void *shutdown_tag) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 775 | listener *l; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 776 | requested_call_array requested_calls; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 777 | channel_data **channels; |
| 778 | channel_data *c; |
| 779 | size_t nchannels; |
| 780 | size_t i; |
| 781 | grpc_channel_op op; |
| 782 | grpc_channel_element *elem; |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 783 | registered_method *rm; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 784 | |
| 785 | /* lock, and gather up some stuff to do */ |
| 786 | gpr_mu_lock(&server->mu); |
| 787 | if (server->shutdown) { |
| 788 | gpr_mu_unlock(&server->mu); |
| 789 | return; |
| 790 | } |
| 791 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 792 | nchannels = 0; |
| 793 | for (c = server->root_channel_data.next; c != &server->root_channel_data; |
| 794 | c = c->next) { |
| 795 | nchannels++; |
| 796 | } |
| 797 | channels = gpr_malloc(sizeof(channel_data *) * nchannels); |
| 798 | i = 0; |
| 799 | for (c = server->root_channel_data.next; c != &server->root_channel_data; |
| 800 | c = c->next) { |
| 801 | grpc_channel_internal_ref(c->channel); |
| 802 | channels[i] = c; |
| 803 | i++; |
| 804 | } |
| 805 | |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 806 | /* collect all unregistered then registered calls */ |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 807 | requested_calls = server->requested_calls; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 808 | memset(&server->requested_calls, 0, sizeof(server->requested_calls)); |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 809 | for (rm = server->registered_methods; rm; rm = rm->next) { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 810 | if (requested_calls.count + rm->requested.count > |
| 811 | requested_calls.capacity) { |
| 812 | requested_calls.capacity = |
| 813 | GPR_MAX(requested_calls.count + rm->requested.count, |
| 814 | 2 * requested_calls.capacity); |
| 815 | requested_calls.calls = |
| 816 | gpr_realloc(requested_calls.calls, sizeof(*requested_calls.calls) * |
| 817 | requested_calls.capacity); |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 818 | } |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 819 | memcpy(requested_calls.calls + requested_calls.count, rm->requested.calls, |
| 820 | sizeof(*requested_calls.calls) * rm->requested.count); |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 821 | requested_calls.count += rm->requested.count; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 822 | gpr_free(rm->requested.calls); |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 823 | memset(&rm->requested, 0, sizeof(rm->requested)); |
| 824 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 825 | |
| 826 | server->shutdown = 1; |
Craig Tiller | 4ffdcd5 | 2015-01-16 11:34:55 -0800 | [diff] [blame] | 827 | server->have_shutdown_tag = have_shutdown_tag; |
| 828 | server->shutdown_tag = shutdown_tag; |
| 829 | if (have_shutdown_tag) { |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 830 | for (i = 0; i < server->cq_count; i++) { |
| 831 | grpc_cq_begin_op(server->cqs[i], NULL, GRPC_SERVER_SHUTDOWN); |
| 832 | if (server->lists[ALL_CALLS] == NULL) { |
| 833 | grpc_cq_end_server_shutdown(server->cqs[i], shutdown_tag); |
| 834 | } |
Craig Tiller | 4ffdcd5 | 2015-01-16 11:34:55 -0800 | [diff] [blame] | 835 | } |
| 836 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 837 | gpr_mu_unlock(&server->mu); |
| 838 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 839 | for (i = 0; i < nchannels; i++) { |
| 840 | c = channels[i]; |
| 841 | elem = grpc_channel_stack_element( |
| 842 | grpc_channel_get_channel_stack(c->channel), 0); |
| 843 | |
| 844 | op.type = GRPC_CHANNEL_GOAWAY; |
| 845 | op.dir = GRPC_CALL_DOWN; |
| 846 | op.data.goaway.status = GRPC_STATUS_OK; |
| 847 | op.data.goaway.message = gpr_slice_from_copied_string("Server shutdown"); |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 848 | elem->filter->channel_op(elem, NULL, &op); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 849 | |
| 850 | grpc_channel_internal_unref(c->channel); |
| 851 | } |
| 852 | gpr_free(channels); |
| 853 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 854 | /* terminate all the requested calls */ |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 855 | for (i = 0; i < requested_calls.count; i++) { |
| 856 | fail_call(server, &requested_calls.calls[i]); |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 857 | } |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 858 | gpr_free(requested_calls.calls); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 859 | |
| 860 | /* Shutdown listeners */ |
| 861 | for (l = server->listeners; l; l = l->next) { |
| 862 | l->destroy(server, l->arg); |
| 863 | } |
| 864 | while (server->listeners) { |
| 865 | l = server->listeners; |
| 866 | server->listeners = l->next; |
| 867 | gpr_free(l); |
| 868 | } |
| 869 | } |
| 870 | |
Craig Tiller | 4ffdcd5 | 2015-01-16 11:34:55 -0800 | [diff] [blame] | 871 | void grpc_server_shutdown(grpc_server *server) { |
| 872 | shutdown_internal(server, 0, NULL); |
| 873 | } |
| 874 | |
| 875 | void grpc_server_shutdown_and_notify(grpc_server *server, void *tag) { |
| 876 | shutdown_internal(server, 1, tag); |
| 877 | } |
| 878 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 879 | void grpc_server_destroy(grpc_server *server) { |
| 880 | channel_data *c; |
| 881 | gpr_mu_lock(&server->mu); |
| 882 | for (c = server->root_channel_data.next; c != &server->root_channel_data; |
| 883 | c = c->next) { |
| 884 | shutdown_channel(c); |
| 885 | } |
| 886 | gpr_mu_unlock(&server->mu); |
| 887 | |
| 888 | server_unref(server); |
| 889 | } |
| 890 | |
| 891 | void grpc_server_add_listener(grpc_server *server, void *arg, |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 892 | void (*start)(grpc_server *server, void *arg, |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 893 | grpc_pollset **pollsets, |
| 894 | size_t pollset_count), |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 895 | void (*destroy)(grpc_server *server, void *arg)) { |
| 896 | listener *l = gpr_malloc(sizeof(listener)); |
| 897 | l->arg = arg; |
| 898 | l->start = start; |
| 899 | l->destroy = destroy; |
| 900 | l->next = server->listeners; |
| 901 | server->listeners = l; |
| 902 | } |
| 903 | |
Craig Tiller | 9f28ac2 | 2015-01-27 17:01:29 -0800 | [diff] [blame] | 904 | static grpc_call_error queue_call_request(grpc_server *server, |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 905 | requested_call *rc) { |
Yang Gao | eb8e7cd | 2015-02-11 11:43:40 -0800 | [diff] [blame] | 906 | call_data *calld = NULL; |
Craig Tiller | 0ef1a92 | 2015-02-11 16:23:01 -0800 | [diff] [blame] | 907 | requested_call_array *requested_calls = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 908 | gpr_mu_lock(&server->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 909 | if (server->shutdown) { |
| 910 | gpr_mu_unlock(&server->mu); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 911 | fail_call(server, rc); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 912 | return GRPC_CALL_OK; |
| 913 | } |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 914 | switch (rc->type) { |
| 915 | case LEGACY_CALL: |
| 916 | case BATCH_CALL: |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 917 | calld = |
| 918 | call_list_remove_head(&server->lists[PENDING_START], PENDING_START); |
Craig Tiller | 0ef1a92 | 2015-02-11 16:23:01 -0800 | [diff] [blame] | 919 | requested_calls = &server->requested_calls; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 920 | break; |
| 921 | case REGISTERED_CALL: |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 922 | calld = call_list_remove_head( |
| 923 | &rc->data.registered.registered_method->pending, PENDING_START); |
Craig Tiller | 0ef1a92 | 2015-02-11 16:23:01 -0800 | [diff] [blame] | 924 | requested_calls = &rc->data.registered.registered_method->requested; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 925 | break; |
| 926 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 927 | if (calld) { |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 928 | GPR_ASSERT(calld->state == PENDING); |
Craig Tiller | bb3f22f | 2015-01-29 16:40:56 -0800 | [diff] [blame] | 929 | calld->state = ACTIVATED; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 930 | gpr_mu_unlock(&server->mu); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 931 | begin_call(server, calld, rc); |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 932 | return GRPC_CALL_OK; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 933 | } else { |
Craig Tiller | 0ef1a92 | 2015-02-11 16:23:01 -0800 | [diff] [blame] | 934 | *requested_call_array_add(requested_calls) = *rc; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 935 | gpr_mu_unlock(&server->mu); |
| 936 | return GRPC_CALL_OK; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 937 | } |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 938 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 939 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 940 | grpc_call_error grpc_server_request_call(grpc_server *server, grpc_call **call, |
| 941 | grpc_call_details *details, |
| 942 | grpc_metadata_array *initial_metadata, |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 943 | grpc_completion_queue *cq_bind, |
| 944 | void *tag) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 945 | requested_call rc; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 946 | grpc_cq_begin_op(server->unregistered_cq, NULL, GRPC_OP_COMPLETE); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 947 | rc.type = BATCH_CALL; |
| 948 | rc.tag = tag; |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 949 | rc.data.batch.cq_bind = cq_bind; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 950 | rc.data.batch.call = call; |
| 951 | rc.data.batch.details = details; |
| 952 | rc.data.batch.initial_metadata = initial_metadata; |
| 953 | return queue_call_request(server, &rc); |
| 954 | } |
| 955 | |
| 956 | grpc_call_error grpc_server_request_registered_call( |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 957 | grpc_server *server, void *rm, grpc_call **call, gpr_timespec *deadline, |
| 958 | grpc_metadata_array *initial_metadata, grpc_byte_buffer **optional_payload, |
| 959 | grpc_completion_queue *cq_bind, void *tag) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 960 | requested_call rc; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 961 | registered_method *registered_method = rm; |
| 962 | grpc_cq_begin_op(registered_method->cq, NULL, GRPC_OP_COMPLETE); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 963 | rc.type = REGISTERED_CALL; |
| 964 | rc.tag = tag; |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 965 | rc.data.registered.cq_bind = cq_bind; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 966 | rc.data.registered.call = call; |
| 967 | rc.data.registered.registered_method = registered_method; |
| 968 | rc.data.registered.deadline = deadline; |
| 969 | rc.data.registered.initial_metadata = initial_metadata; |
| 970 | rc.data.registered.optional_payload = optional_payload; |
| 971 | return queue_call_request(server, &rc); |
| 972 | } |
| 973 | |
| 974 | grpc_call_error grpc_server_request_call_old(grpc_server *server, |
| 975 | void *tag_new) { |
| 976 | requested_call rc; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 977 | grpc_cq_begin_op(server->unregistered_cq, NULL, GRPC_SERVER_RPC_NEW); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 978 | rc.type = LEGACY_CALL; |
| 979 | rc.tag = tag_new; |
| 980 | return queue_call_request(server, &rc); |
| 981 | } |
| 982 | |
| 983 | static void publish_legacy(grpc_call *call, grpc_op_error status, void *tag); |
| 984 | static void publish_registered_or_batch(grpc_call *call, grpc_op_error status, |
| 985 | void *tag); |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 986 | static void publish_was_not_set(grpc_call *call, grpc_op_error status, |
| 987 | void *tag) { |
Yang Gao | eb8e7cd | 2015-02-11 11:43:40 -0800 | [diff] [blame] | 988 | abort(); |
| 989 | } |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 990 | |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 991 | static void cpstr(char **dest, size_t *capacity, grpc_mdstr *value) { |
| 992 | gpr_slice slice = value->slice; |
| 993 | size_t len = GPR_SLICE_LENGTH(slice); |
| 994 | |
| 995 | if (len + 1 > *capacity) { |
| 996 | *capacity = GPR_MAX(len + 1, *capacity * 2); |
| 997 | *dest = gpr_realloc(*dest, *capacity); |
| 998 | } |
| 999 | memcpy(*dest, grpc_mdstr_as_c_string(value), len + 1); |
| 1000 | } |
| 1001 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1002 | static void begin_call(grpc_server *server, call_data *calld, |
| 1003 | requested_call *rc) { |
Yang Gao | eb8e7cd | 2015-02-11 11:43:40 -0800 | [diff] [blame] | 1004 | grpc_ioreq_completion_func publish = publish_was_not_set; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1005 | grpc_ioreq req[2]; |
| 1006 | grpc_ioreq *r = req; |
| 1007 | |
| 1008 | /* called once initial metadata has been read by the call, but BEFORE |
| 1009 | the ioreq to fetch it out of the call has been executed. |
| 1010 | This means metadata related fields can be relied on in calld, but to |
| 1011 | fill in the metadata array passed by the client, we need to perform |
| 1012 | an ioreq op, that should complete immediately. */ |
| 1013 | |
| 1014 | switch (rc->type) { |
| 1015 | case LEGACY_CALL: |
| 1016 | calld->legacy = gpr_malloc(sizeof(legacy_data)); |
| 1017 | memset(calld->legacy, 0, sizeof(legacy_data)); |
| 1018 | r->op = GRPC_IOREQ_RECV_INITIAL_METADATA; |
| 1019 | r->data.recv_metadata = &calld->legacy->initial_metadata; |
| 1020 | r++; |
| 1021 | publish = publish_legacy; |
| 1022 | break; |
| 1023 | case BATCH_CALL: |
| 1024 | cpstr(&rc->data.batch.details->host, |
| 1025 | &rc->data.batch.details->host_capacity, calld->host); |
| 1026 | cpstr(&rc->data.batch.details->method, |
| 1027 | &rc->data.batch.details->method_capacity, calld->path); |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 1028 | grpc_call_set_completion_queue(calld->call, rc->data.batch.cq_bind); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1029 | *rc->data.batch.call = calld->call; |
| 1030 | r->op = GRPC_IOREQ_RECV_INITIAL_METADATA; |
| 1031 | r->data.recv_metadata = rc->data.batch.initial_metadata; |
| 1032 | r++; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 1033 | calld->cq_new = server->unregistered_cq; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1034 | publish = publish_registered_or_batch; |
| 1035 | break; |
| 1036 | case REGISTERED_CALL: |
| 1037 | *rc->data.registered.deadline = calld->deadline; |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 1038 | grpc_call_set_completion_queue(calld->call, rc->data.registered.cq_bind); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1039 | *rc->data.registered.call = calld->call; |
| 1040 | r->op = GRPC_IOREQ_RECV_INITIAL_METADATA; |
| 1041 | r->data.recv_metadata = rc->data.registered.initial_metadata; |
| 1042 | r++; |
| 1043 | if (rc->data.registered.optional_payload) { |
| 1044 | r->op = GRPC_IOREQ_RECV_MESSAGE; |
| 1045 | r->data.recv_message = rc->data.registered.optional_payload; |
| 1046 | r++; |
| 1047 | } |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 1048 | calld->cq_new = rc->data.registered.registered_method->cq; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1049 | publish = publish_registered_or_batch; |
| 1050 | break; |
| 1051 | } |
| 1052 | |
| 1053 | grpc_call_internal_ref(calld->call); |
| 1054 | grpc_call_start_ioreq_and_call_back(calld->call, req, r - req, publish, |
| 1055 | rc->tag); |
| 1056 | } |
| 1057 | |
| 1058 | static void fail_call(grpc_server *server, requested_call *rc) { |
| 1059 | switch (rc->type) { |
| 1060 | case LEGACY_CALL: |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 1061 | grpc_cq_end_new_rpc(server->unregistered_cq, rc->tag, NULL, do_nothing, |
| 1062 | NULL, NULL, NULL, gpr_inf_past, 0, NULL); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1063 | break; |
| 1064 | case BATCH_CALL: |
| 1065 | *rc->data.batch.call = NULL; |
| 1066 | rc->data.batch.initial_metadata->count = 0; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 1067 | grpc_cq_end_op_complete(server->unregistered_cq, rc->tag, NULL, |
| 1068 | do_nothing, NULL, GRPC_OP_ERROR); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1069 | break; |
| 1070 | case REGISTERED_CALL: |
| 1071 | *rc->data.registered.call = NULL; |
| 1072 | rc->data.registered.initial_metadata->count = 0; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame^] | 1073 | grpc_cq_end_op_complete(rc->data.registered.registered_method->cq, |
| 1074 | rc->tag, NULL, do_nothing, NULL, GRPC_OP_ERROR); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | static void publish_legacy(grpc_call *call, grpc_op_error status, void *tag) { |
| 1080 | grpc_call_element *elem = |
| 1081 | grpc_call_stack_element(grpc_call_get_call_stack(call), 0); |
| 1082 | call_data *calld = elem->call_data; |
| 1083 | channel_data *chand = elem->channel_data; |
| 1084 | grpc_server *server = chand->server; |
| 1085 | |
| 1086 | if (status == GRPC_OP_OK) { |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 1087 | grpc_cq_end_new_rpc(server->unregistered_cq, tag, call, do_nothing, NULL, |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1088 | grpc_mdstr_as_c_string(calld->path), |
| 1089 | grpc_mdstr_as_c_string(calld->host), calld->deadline, |
| 1090 | calld->legacy->initial_metadata.count, |
| 1091 | calld->legacy->initial_metadata.metadata); |
| 1092 | } else { |
| 1093 | gpr_log(GPR_ERROR, "should never reach here"); |
| 1094 | abort(); |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | static void publish_registered_or_batch(grpc_call *call, grpc_op_error status, |
| 1099 | void *tag) { |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 1100 | grpc_call_element *elem = |
| 1101 | grpc_call_stack_element(grpc_call_get_call_stack(call), 0); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 1102 | call_data *calld = elem->call_data; |
| 1103 | grpc_cq_end_op_complete(calld->cq_new, tag, call, do_nothing, NULL, status); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1106 | const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) { |
| 1107 | return server->channel_args; |
| 1108 | } |