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