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