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