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