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