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