Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [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 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/surface/server.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame] | 36 | #include <limits.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
| 42 | #include <grpc/support/string_util.h> |
| 43 | #include <grpc/support/useful.h> |
| 44 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 45 | #include "src/core/lib/channel/channel_args.h" |
| 46 | #include "src/core/lib/channel/connected_channel.h" |
| 47 | #include "src/core/lib/iomgr/iomgr.h" |
| 48 | #include "src/core/lib/support/stack_lockfree.h" |
| 49 | #include "src/core/lib/support/string.h" |
| 50 | #include "src/core/lib/surface/api_trace.h" |
| 51 | #include "src/core/lib/surface/call.h" |
| 52 | #include "src/core/lib/surface/channel.h" |
| 53 | #include "src/core/lib/surface/completion_queue.h" |
| 54 | #include "src/core/lib/surface/init.h" |
| 55 | #include "src/core/lib/transport/metadata.h" |
| 56 | #include "src/core/lib/transport/static_metadata.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 58 | typedef struct listener { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | void *arg; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 60 | void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg, |
| 61 | grpc_pollset **pollsets, size_t pollset_count); |
| 62 | void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg, |
| 63 | grpc_closure *closure); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | struct listener *next; |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 65 | grpc_closure destroy_done; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | } listener; |
| 67 | |
| 68 | typedef struct call_data call_data; |
| 69 | typedef struct channel_data channel_data; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 70 | typedef struct registered_method registered_method; |
| 71 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 72 | typedef struct { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 73 | call_data *next; |
| 74 | call_data *prev; |
| 75 | } call_link; |
| 76 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 77 | typedef enum { BATCH_CALL, REGISTERED_CALL } requested_call_type; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 78 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 79 | typedef struct requested_call { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 80 | requested_call_type type; |
| 81 | void *tag; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 82 | grpc_server *server; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 83 | grpc_completion_queue *cq_bound_to_call; |
| 84 | grpc_completion_queue *cq_for_notification; |
| 85 | grpc_call **call; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 86 | grpc_cq_completion completion; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 87 | grpc_metadata_array *initial_metadata; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | union { |
| 89 | struct { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 90 | grpc_call_details *details; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 91 | } batch; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 92 | struct { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 93 | registered_method *registered_method; |
| 94 | gpr_timespec *deadline; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 95 | grpc_byte_buffer **optional_payload; |
| 96 | } registered; |
| 97 | } data; |
| 98 | } requested_call; |
| 99 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | typedef struct channel_registered_method { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 101 | registered_method *server_registered_method; |
Craig Tiller | b290686 | 2016-03-10 06:50:07 -0800 | [diff] [blame] | 102 | uint32_t flags; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 103 | grpc_mdstr *method; |
| 104 | grpc_mdstr *host; |
| 105 | } channel_registered_method; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 106 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 107 | struct channel_data { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 108 | grpc_server *server; |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 109 | grpc_connectivity_state connectivity_state; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | grpc_channel *channel; |
| 111 | /* linked list of all channels on a server */ |
| 112 | channel_data *next; |
| 113 | channel_data *prev; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 114 | channel_registered_method *registered_methods; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 115 | uint32_t registered_method_slots; |
| 116 | uint32_t registered_method_max_probes; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 117 | grpc_closure finish_destroy_channel_closure; |
| 118 | grpc_closure channel_connectivity_changed; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 121 | typedef struct shutdown_tag { |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 122 | void *tag; |
| 123 | grpc_completion_queue *cq; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 124 | grpc_cq_completion completion; |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 125 | } shutdown_tag; |
| 126 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 127 | typedef enum { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 128 | /* waiting for metadata */ |
| 129 | NOT_STARTED, |
| 130 | /* inital metadata read, not flow controlled in yet */ |
| 131 | PENDING, |
| 132 | /* flow controlled in, on completion queue */ |
| 133 | ACTIVATED, |
| 134 | /* cancelled before being queued */ |
| 135 | ZOMBIED |
| 136 | } call_state; |
| 137 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 138 | typedef struct request_matcher request_matcher; |
| 139 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 140 | struct call_data { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 141 | grpc_call *call; |
| 142 | |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 143 | /** protects state */ |
| 144 | gpr_mu mu_state; |
| 145 | /** the current state of a call - see call_state */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 146 | call_state state; |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 147 | |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 148 | grpc_mdstr *path; |
| 149 | grpc_mdstr *host; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 150 | gpr_timespec deadline; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 151 | |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 152 | grpc_completion_queue *cq_new; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 153 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 154 | grpc_metadata_batch *recv_initial_metadata; |
Craig Tiller | 4d40ba3 | 2016-03-09 17:48:40 -0800 | [diff] [blame] | 155 | bool recv_idempotent_request; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 156 | grpc_metadata_array initial_metadata; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 157 | |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 158 | request_matcher *request_matcher; |
| 159 | grpc_byte_buffer *payload; |
| 160 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 161 | grpc_closure got_initial_metadata; |
| 162 | grpc_closure server_on_recv_initial_metadata; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 163 | grpc_closure kill_zombie_closure; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 164 | grpc_closure *on_done_recv_initial_metadata; |
David Garcia Quintas | 284488b | 2015-05-28 16:27:39 -0700 | [diff] [blame] | 165 | |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 166 | grpc_closure publish; |
| 167 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 168 | call_data *pending_next; |
| 169 | }; |
| 170 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 171 | struct request_matcher { |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 172 | grpc_server *server; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 173 | call_data *pending_head; |
| 174 | call_data *pending_tail; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 175 | gpr_stack_lockfree *requests; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 178 | struct registered_method { |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 179 | char *method; |
| 180 | char *host; |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 181 | grpc_server_register_method_payload_handling payload_handling; |
Craig Tiller | b290686 | 2016-03-10 06:50:07 -0800 | [diff] [blame] | 182 | uint32_t flags; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 183 | request_matcher request_matcher; |
| 184 | registered_method *next; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 185 | }; |
| 186 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 187 | typedef struct { |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 188 | grpc_channel **channels; |
| 189 | size_t num_channels; |
| 190 | } channel_broadcaster; |
| 191 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 192 | struct grpc_server { |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 193 | grpc_channel_args *channel_args; |
| 194 | |
| 195 | grpc_completion_queue **cqs; |
| 196 | grpc_pollset **pollsets; |
| 197 | size_t cq_count; |
| 198 | |
| 199 | /* The two following mutexes control access to server-state |
| 200 | mu_global controls access to non-call-related state (e.g., channel state) |
| 201 | mu_call controls access to call-related state (e.g., the call lists) |
| 202 | |
| 203 | If they are ever required to be nested, you must lock mu_global |
| 204 | before mu_call. This is currently used in shutdown processing |
| 205 | (grpc_server_shutdown_and_notify and maybe_finish_shutdown) */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 206 | gpr_mu mu_global; /* mutex for server and channel state */ |
| 207 | gpr_mu mu_call; /* mutex for call-specific state */ |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 208 | |
| 209 | registered_method *registered_methods; |
| 210 | request_matcher unregistered_request_matcher; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 211 | /** free list of available requested_calls indices */ |
| 212 | gpr_stack_lockfree *request_freelist; |
| 213 | /** requested call backing data */ |
| 214 | requested_call *requested_calls; |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 215 | size_t max_requested_calls; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 216 | |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 217 | gpr_atm shutdown_flag; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 218 | uint8_t shutdown_published; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 219 | size_t num_shutdown_tags; |
| 220 | shutdown_tag *shutdown_tags; |
| 221 | |
| 222 | channel_data root_channel_data; |
| 223 | |
| 224 | listener *listeners; |
| 225 | int listeners_destroyed; |
| 226 | gpr_refcount internal_refcount; |
| 227 | |
| 228 | /** when did we print the last shutdown progress message */ |
| 229 | gpr_timespec last_shutdown_message_time; |
| 230 | }; |
| 231 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 232 | #define SERVER_FROM_CALL_ELEM(elem) \ |
| 233 | (((channel_data *)(elem)->channel_data)->server) |
| 234 | |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 235 | static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *calld, |
| 236 | grpc_error *error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 237 | static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server, |
| 238 | requested_call *rc); |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 239 | /* Before calling maybe_finish_shutdown, we must hold mu_global and not |
| 240 | hold mu_call */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 241 | static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_server *server); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 242 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 243 | /* |
| 244 | * channel broadcaster |
| 245 | */ |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 246 | |
| 247 | /* assumes server locked */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 248 | static void channel_broadcaster_init(grpc_server *s, channel_broadcaster *cb) { |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 249 | channel_data *c; |
| 250 | size_t count = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 251 | for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) { |
| 252 | count++; |
| 253 | } |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 254 | cb->num_channels = count; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 255 | cb->channels = gpr_malloc(sizeof(*cb->channels) * cb->num_channels); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 256 | count = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 257 | for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) { |
| 258 | cb->channels[count++] = c->channel; |
| 259 | GRPC_CHANNEL_INTERNAL_REF(c->channel, "broadcast"); |
| 260 | } |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 263 | struct shutdown_cleanup_args { |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 264 | grpc_closure closure; |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 265 | gpr_slice slice; |
| 266 | }; |
| 267 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 268 | static void shutdown_cleanup(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 269 | grpc_error *error) { |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 270 | struct shutdown_cleanup_args *a = arg; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 271 | gpr_slice_unref(a->slice); |
| 272 | gpr_free(a); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 275 | static void send_shutdown(grpc_exec_ctx *exec_ctx, grpc_channel *channel, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame^] | 276 | int send_goaway, grpc_error *send_disconnect) { |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 277 | grpc_transport_op op; |
| 278 | struct shutdown_cleanup_args *sc; |
| 279 | grpc_channel_element *elem; |
| 280 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 281 | memset(&op, 0, sizeof(op)); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 282 | op.send_goaway = send_goaway; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 283 | sc = gpr_malloc(sizeof(*sc)); |
| 284 | sc->slice = gpr_slice_from_copied_string("Server shutdown"); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 285 | op.goaway_message = &sc->slice; |
| 286 | op.goaway_status = GRPC_STATUS_OK; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame^] | 287 | op.disconnect_with_error = send_disconnect; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 288 | grpc_closure_init(&sc->closure, shutdown_cleanup, sc); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 289 | op.on_consumed = &sc->closure; |
| 290 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 291 | elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0); |
| 292 | elem->filter->start_transport_op(exec_ctx, elem, &op); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 295 | static void channel_broadcaster_shutdown(grpc_exec_ctx *exec_ctx, |
| 296 | channel_broadcaster *cb, |
| 297 | int send_goaway, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame^] | 298 | grpc_error *force_disconnect) { |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 299 | size_t i; |
| 300 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 301 | for (i = 0; i < cb->num_channels; i++) { |
| 302 | send_shutdown(exec_ctx, cb->channels[i], send_goaway, force_disconnect); |
| 303 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, cb->channels[i], "broadcast"); |
| 304 | } |
| 305 | gpr_free(cb->channels); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 308 | /* |
| 309 | * request_matcher |
| 310 | */ |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 311 | |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 312 | static void request_matcher_init(request_matcher *rm, size_t entries, |
| 313 | grpc_server *server) { |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 314 | memset(rm, 0, sizeof(*rm)); |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 315 | rm->server = server; |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 316 | rm->requests = gpr_stack_lockfree_create(entries); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 319 | static void request_matcher_destroy(request_matcher *rm) { |
| 320 | GPR_ASSERT(gpr_stack_lockfree_pop(rm->requests) == -1); |
| 321 | gpr_stack_lockfree_destroy(rm->requests); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 324 | static void kill_zombie(grpc_exec_ctx *exec_ctx, void *elem, |
| 325 | grpc_error *error) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 326 | grpc_call_destroy(grpc_call_from_top_element(elem)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Craig Tiller | 8dc0971 | 2015-09-24 13:58:16 -0700 | [diff] [blame] | 329 | static void request_matcher_zombify_all_pending_calls(grpc_exec_ctx *exec_ctx, |
| 330 | request_matcher *rm) { |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 331 | while (rm->pending_head) { |
| 332 | call_data *calld = rm->pending_head; |
| 333 | rm->pending_head = calld->pending_next; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 334 | gpr_mu_lock(&calld->mu_state); |
| 335 | calld->state = ZOMBIED; |
| 336 | gpr_mu_unlock(&calld->mu_state); |
| 337 | grpc_closure_init( |
| 338 | &calld->kill_zombie_closure, kill_zombie, |
| 339 | grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0)); |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 340 | grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE, |
| 341 | NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 342 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 345 | static void request_matcher_kill_requests(grpc_exec_ctx *exec_ctx, |
| 346 | grpc_server *server, |
| 347 | request_matcher *rm) { |
Craig Tiller | 1191e21 | 2015-07-30 14:49:02 -0700 | [diff] [blame] | 348 | int request_id; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 349 | while ((request_id = gpr_stack_lockfree_pop(rm->requests)) != -1) { |
| 350 | fail_call(exec_ctx, server, &server->requested_calls[request_id]); |
| 351 | } |
Craig Tiller | 1191e21 | 2015-07-30 14:49:02 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 354 | /* |
| 355 | * server proper |
| 356 | */ |
| 357 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 358 | static void server_ref(grpc_server *server) { |
| 359 | gpr_ref(&server->internal_refcount); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 362 | static void server_delete(grpc_exec_ctx *exec_ctx, grpc_server *server) { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 363 | registered_method *rm; |
Craig Tiller | 8950461 | 2015-04-27 11:48:46 -0700 | [diff] [blame] | 364 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 365 | grpc_channel_args_destroy(server->channel_args); |
| 366 | gpr_mu_destroy(&server->mu_global); |
| 367 | gpr_mu_destroy(&server->mu_call); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 368 | while ((rm = server->registered_methods) != NULL) { |
| 369 | server->registered_methods = rm->next; |
| 370 | request_matcher_destroy(&rm->request_matcher); |
| 371 | gpr_free(rm->method); |
| 372 | gpr_free(rm->host); |
| 373 | gpr_free(rm); |
| 374 | } |
| 375 | for (i = 0; i < server->cq_count; i++) { |
| 376 | GRPC_CQ_INTERNAL_UNREF(server->cqs[i], "server"); |
| 377 | } |
| 378 | request_matcher_destroy(&server->unregistered_request_matcher); |
| 379 | gpr_stack_lockfree_destroy(server->request_freelist); |
| 380 | gpr_free(server->cqs); |
| 381 | gpr_free(server->pollsets); |
| 382 | gpr_free(server->shutdown_tags); |
| 383 | gpr_free(server->requested_calls); |
| 384 | gpr_free(server); |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 387 | static void server_unref(grpc_exec_ctx *exec_ctx, grpc_server *server) { |
| 388 | if (gpr_unref(&server->internal_refcount)) { |
| 389 | server_delete(exec_ctx, server); |
| 390 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 393 | static int is_channel_orphaned(channel_data *chand) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 394 | return chand->next == chand; |
| 395 | } |
| 396 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 397 | static void orphan_channel(channel_data *chand) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 398 | chand->next->prev = chand->prev; |
| 399 | chand->prev->next = chand->next; |
| 400 | chand->next = chand->prev = chand; |
| 401 | } |
| 402 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 403 | static void finish_destroy_channel(grpc_exec_ctx *exec_ctx, void *cd, |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 404 | grpc_error *error) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 405 | channel_data *chand = cd; |
| 406 | grpc_server *server = chand->server; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 407 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "server"); |
| 408 | server_unref(exec_ctx, server); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 411 | static void destroy_channel(grpc_exec_ctx *exec_ctx, channel_data *chand) { |
| 412 | if (is_channel_orphaned(chand)) return; |
| 413 | GPR_ASSERT(chand->server != NULL); |
| 414 | orphan_channel(chand); |
| 415 | server_ref(chand->server); |
| 416 | maybe_finish_shutdown(exec_ctx, chand->server); |
David Garcia Quintas | 284488b | 2015-05-28 16:27:39 -0700 | [diff] [blame] | 417 | chand->finish_destroy_channel_closure.cb = finish_destroy_channel; |
| 418 | chand->finish_destroy_channel_closure.cb_arg = chand; |
Craig Tiller | d7f12e3 | 2016-03-03 10:08:31 -0800 | [diff] [blame] | 419 | |
| 420 | grpc_transport_op op; |
| 421 | memset(&op, 0, sizeof(op)); |
| 422 | op.set_accept_stream = true; |
| 423 | op.on_consumed = &chand->finish_destroy_channel_closure; |
| 424 | grpc_channel_next_op(exec_ctx, |
| 425 | grpc_channel_stack_element( |
| 426 | grpc_channel_get_channel_stack(chand->channel), 0), |
| 427 | &op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 430 | static void cpstr(char **dest, size_t *capacity, grpc_mdstr *value) { |
| 431 | gpr_slice slice = value->slice; |
| 432 | size_t len = GPR_SLICE_LENGTH(slice); |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 433 | |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 434 | if (len + 1 > *capacity) { |
| 435 | *capacity = GPR_MAX(len + 1, *capacity * 2); |
| 436 | *dest = gpr_realloc(*dest, *capacity); |
| 437 | } |
| 438 | memcpy(*dest, grpc_mdstr_as_c_string(value), len + 1); |
| 439 | } |
| 440 | |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 441 | static void done_request_event(grpc_exec_ctx *exec_ctx, void *req, |
| 442 | grpc_cq_completion *c) { |
| 443 | requested_call *rc = req; |
| 444 | grpc_server *server = rc->server; |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 445 | |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 446 | if (rc >= server->requested_calls && |
| 447 | rc < server->requested_calls + server->max_requested_calls) { |
| 448 | GPR_ASSERT(rc - server->requested_calls <= INT_MAX); |
| 449 | gpr_stack_lockfree_push(server->request_freelist, |
| 450 | (int)(rc - server->requested_calls)); |
| 451 | } else { |
| 452 | gpr_free(req); |
| 453 | } |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 454 | |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 455 | server_unref(exec_ctx, server); |
| 456 | } |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 457 | |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 458 | static void publish_call(grpc_exec_ctx *exec_ctx, grpc_server *server, |
| 459 | call_data *calld, requested_call *rc) { |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 460 | grpc_call_set_completion_queue(exec_ctx, calld->call, rc->cq_bound_to_call); |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 461 | grpc_call *call = calld->call; |
| 462 | *rc->call = call; |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 463 | calld->cq_new = rc->cq_for_notification; |
| 464 | GPR_SWAP(grpc_metadata_array, *rc->initial_metadata, calld->initial_metadata); |
| 465 | switch (rc->type) { |
| 466 | case BATCH_CALL: |
| 467 | GPR_ASSERT(calld->host != NULL); |
| 468 | GPR_ASSERT(calld->path != NULL); |
| 469 | cpstr(&rc->data.batch.details->host, |
| 470 | &rc->data.batch.details->host_capacity, calld->host); |
| 471 | cpstr(&rc->data.batch.details->method, |
| 472 | &rc->data.batch.details->method_capacity, calld->path); |
| 473 | rc->data.batch.details->deadline = calld->deadline; |
| 474 | rc->data.batch.details->flags = |
| 475 | 0 | (calld->recv_idempotent_request |
| 476 | ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST |
| 477 | : 0); |
| 478 | break; |
| 479 | case REGISTERED_CALL: |
| 480 | *rc->data.registered.deadline = calld->deadline; |
| 481 | if (rc->data.registered.optional_payload) { |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 482 | *rc->data.registered.optional_payload = calld->payload; |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 483 | } |
| 484 | break; |
| 485 | default: |
| 486 | GPR_UNREACHABLE_CODE(return ); |
| 487 | } |
| 488 | |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 489 | grpc_call_element *elem = |
| 490 | grpc_call_stack_element(grpc_call_get_call_stack(call), 0); |
| 491 | channel_data *chand = elem->channel_data; |
| 492 | server_ref(chand->server); |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 493 | grpc_cq_end_op(exec_ctx, calld->cq_new, rc->tag, GRPC_ERROR_NONE, |
| 494 | done_request_event, rc, &rc->completion); |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 497 | static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *arg, |
| 498 | grpc_error *error) { |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 499 | call_data *calld = arg; |
| 500 | request_matcher *rm = calld->request_matcher; |
| 501 | grpc_server *server = rm->server; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 502 | |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 503 | if (error != GRPC_ERROR_NONE || gpr_atm_acq_load(&server->shutdown_flag)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 504 | gpr_mu_lock(&calld->mu_state); |
| 505 | calld->state = ZOMBIED; |
| 506 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 507 | grpc_closure_init( |
| 508 | &calld->kill_zombie_closure, kill_zombie, |
| 509 | grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0)); |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 510 | grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, error, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 511 | return; |
| 512 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 513 | |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 514 | int request_id = gpr_stack_lockfree_pop(rm->requests); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 515 | if (request_id == -1) { |
| 516 | gpr_mu_lock(&server->mu_call); |
| 517 | gpr_mu_lock(&calld->mu_state); |
| 518 | calld->state = PENDING; |
| 519 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 520 | if (rm->pending_head == NULL) { |
| 521 | rm->pending_tail = rm->pending_head = calld; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 522 | } else { |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 523 | rm->pending_tail->pending_next = calld; |
| 524 | rm->pending_tail = calld; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 525 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 526 | calld->pending_next = NULL; |
| 527 | gpr_mu_unlock(&server->mu_call); |
| 528 | } else { |
| 529 | gpr_mu_lock(&calld->mu_state); |
| 530 | calld->state = ACTIVATED; |
| 531 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 532 | publish_call(exec_ctx, server, calld, &server->requested_calls[request_id]); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | static void finish_start_new_rpc( |
| 537 | grpc_exec_ctx *exec_ctx, grpc_server *server, grpc_call_element *elem, |
| 538 | request_matcher *rm, |
| 539 | grpc_server_register_method_payload_handling payload_handling) { |
| 540 | call_data *calld = elem->call_data; |
| 541 | |
| 542 | if (gpr_atm_acq_load(&server->shutdown_flag)) { |
| 543 | gpr_mu_lock(&calld->mu_state); |
| 544 | calld->state = ZOMBIED; |
| 545 | gpr_mu_unlock(&calld->mu_state); |
| 546 | grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem); |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 547 | grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE, |
| 548 | NULL); |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 549 | return; |
| 550 | } |
| 551 | |
| 552 | calld->request_matcher = rm; |
| 553 | |
| 554 | switch (payload_handling) { |
| 555 | case GRPC_SRM_PAYLOAD_NONE: |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 556 | publish_new_rpc(exec_ctx, calld, GRPC_ERROR_NONE); |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 557 | break; |
| 558 | case GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER: { |
| 559 | grpc_op op; |
| 560 | memset(&op, 0, sizeof(op)); |
| 561 | op.op = GRPC_OP_RECV_MESSAGE; |
| 562 | op.data.recv_message = &calld->payload; |
| 563 | grpc_closure_init(&calld->publish, publish_new_rpc, calld); |
| 564 | grpc_call_start_batch_and_execute(exec_ctx, calld->call, &op, 1, |
| 565 | &calld->publish); |
| 566 | break; |
| 567 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 568 | } |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 571 | static void start_new_rpc(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 572 | channel_data *chand = elem->channel_data; |
| 573 | call_data *calld = elem->call_data; |
| 574 | grpc_server *server = chand->server; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 575 | uint32_t i; |
| 576 | uint32_t hash; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 577 | channel_registered_method *rm; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 578 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 579 | if (chand->registered_methods && calld->path && calld->host) { |
| 580 | /* TODO(ctiller): unify these two searches */ |
| 581 | /* check for an exact match with host */ |
| 582 | hash = GRPC_MDSTR_KV_HASH(calld->host->hash, calld->path->hash); |
| 583 | for (i = 0; i <= chand->registered_method_max_probes; i++) { |
| 584 | rm = &chand->registered_methods[(hash + i) % |
| 585 | chand->registered_method_slots]; |
| 586 | if (!rm) break; |
| 587 | if (rm->host != calld->host) continue; |
| 588 | if (rm->method != calld->path) continue; |
Craig Tiller | b290686 | 2016-03-10 06:50:07 -0800 | [diff] [blame] | 589 | if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) && |
| 590 | !calld->recv_idempotent_request) |
| 591 | continue; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 592 | finish_start_new_rpc(exec_ctx, server, elem, |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 593 | &rm->server_registered_method->request_matcher, |
| 594 | rm->server_registered_method->payload_handling); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 595 | return; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 596 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 597 | /* check for a wildcard method definition (no host set) */ |
| 598 | hash = GRPC_MDSTR_KV_HASH(0, calld->path->hash); |
| 599 | for (i = 0; i <= chand->registered_method_max_probes; i++) { |
| 600 | rm = &chand->registered_methods[(hash + i) % |
| 601 | chand->registered_method_slots]; |
| 602 | if (!rm) break; |
| 603 | if (rm->host != NULL) continue; |
| 604 | if (rm->method != calld->path) continue; |
Craig Tiller | b290686 | 2016-03-10 06:50:07 -0800 | [diff] [blame] | 605 | if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) && |
| 606 | !calld->recv_idempotent_request) |
| 607 | continue; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 608 | finish_start_new_rpc(exec_ctx, server, elem, |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 609 | &rm->server_registered_method->request_matcher, |
| 610 | rm->server_registered_method->payload_handling); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 611 | return; |
| 612 | } |
| 613 | } |
| 614 | finish_start_new_rpc(exec_ctx, server, elem, |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 615 | &server->unregistered_request_matcher, |
| 616 | GRPC_SRM_PAYLOAD_NONE); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 619 | static int num_listeners(grpc_server *server) { |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 620 | listener *l; |
| 621 | int n = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 622 | for (l = server->listeners; l; l = l->next) { |
| 623 | n++; |
| 624 | } |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 625 | return n; |
| 626 | } |
| 627 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 628 | static void done_shutdown_event(grpc_exec_ctx *exec_ctx, void *server, |
| 629 | grpc_cq_completion *completion) { |
| 630 | server_unref(exec_ctx, server); |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 633 | static int num_channels(grpc_server *server) { |
Craig Tiller | ab54f79 | 2015-07-08 08:34:20 -0700 | [diff] [blame] | 634 | channel_data *chand; |
| 635 | int n = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 636 | for (chand = server->root_channel_data.next; |
| 637 | chand != &server->root_channel_data; chand = chand->next) { |
| 638 | n++; |
| 639 | } |
Craig Tiller | ab54f79 | 2015-07-08 08:34:20 -0700 | [diff] [blame] | 640 | return n; |
| 641 | } |
| 642 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 643 | static void kill_pending_work_locked(grpc_exec_ctx *exec_ctx, |
| 644 | grpc_server *server) { |
Craig Tiller | 1191e21 | 2015-07-30 14:49:02 -0700 | [diff] [blame] | 645 | registered_method *rm; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 646 | request_matcher_kill_requests(exec_ctx, server, |
| 647 | &server->unregistered_request_matcher); |
| 648 | request_matcher_zombify_all_pending_calls( |
| 649 | exec_ctx, &server->unregistered_request_matcher); |
| 650 | for (rm = server->registered_methods; rm; rm = rm->next) { |
| 651 | request_matcher_kill_requests(exec_ctx, server, &rm->request_matcher); |
| 652 | request_matcher_zombify_all_pending_calls(exec_ctx, &rm->request_matcher); |
| 653 | } |
Craig Tiller | dc62772 | 2015-05-26 15:27:02 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 656 | static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx, |
| 657 | grpc_server *server) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 658 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 659 | if (!gpr_atm_acq_load(&server->shutdown_flag) || server->shutdown_published) { |
| 660 | return; |
| 661 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 662 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 663 | kill_pending_work_locked(exec_ctx, server); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 664 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 665 | if (server->root_channel_data.next != &server->root_channel_data || |
| 666 | server->listeners_destroyed < num_listeners(server)) { |
| 667 | if (gpr_time_cmp(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), |
| 668 | server->last_shutdown_message_time), |
| 669 | gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) { |
| 670 | server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME); |
| 671 | gpr_log(GPR_DEBUG, |
| 672 | "Waiting for %d channels and %d/%d listeners to be destroyed" |
| 673 | " before shutting down server", |
| 674 | num_channels(server), |
| 675 | num_listeners(server) - server->listeners_destroyed, |
| 676 | num_listeners(server)); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 677 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 678 | return; |
| 679 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 680 | server->shutdown_published = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 681 | for (i = 0; i < server->num_shutdown_tags; i++) { |
| 682 | server_ref(server); |
| 683 | grpc_cq_end_op(exec_ctx, server->shutdown_tags[i].cq, |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 684 | server->shutdown_tags[i].tag, GRPC_ERROR_NONE, |
| 685 | done_shutdown_event, server, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 686 | &server->shutdown_tags[i].completion); |
| 687 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 690 | static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 691 | grpc_call_element *elem = user_data; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 692 | call_data *calld = elem->call_data; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 693 | if (md->key == GRPC_MDSTR_PATH) { |
Craig Tiller | 9cac2a1 | 2016-04-04 20:56:56 -0700 | [diff] [blame] | 694 | if (calld->path == NULL) { |
| 695 | calld->path = GRPC_MDSTR_REF(md->value); |
| 696 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 697 | return NULL; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 698 | } else if (md->key == GRPC_MDSTR_AUTHORITY) { |
Craig Tiller | 9cac2a1 | 2016-04-04 20:56:56 -0700 | [diff] [blame] | 699 | if (calld->host == NULL) { |
| 700 | calld->host = GRPC_MDSTR_REF(md->value); |
| 701 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 702 | return NULL; |
| 703 | } |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 704 | return md; |
| 705 | } |
| 706 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 707 | static void server_on_recv_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr, |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 708 | grpc_error *error) { |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 709 | grpc_call_element *elem = ptr; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 710 | call_data *calld = elem->call_data; |
Craig Tiller | 94329d0 | 2015-07-23 09:52:11 -0700 | [diff] [blame] | 711 | gpr_timespec op_deadline; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 712 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 713 | grpc_metadata_batch_filter(calld->recv_initial_metadata, server_filter, elem); |
| 714 | op_deadline = calld->recv_initial_metadata->deadline; |
| 715 | if (0 != gpr_time_cmp(op_deadline, gpr_inf_future(op_deadline.clock_type))) { |
| 716 | calld->deadline = op_deadline; |
| 717 | } |
| 718 | if (calld->host && calld->path) { |
| 719 | /* do nothing */ |
| 720 | } else { |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 721 | error = |
| 722 | GRPC_ERROR_CREATE_REFERENCING("Missing :authority or :path", &error, 1); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 723 | } |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 724 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 725 | calld->on_done_recv_initial_metadata->cb( |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 726 | exec_ctx, calld->on_done_recv_initial_metadata->cb_arg, error); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 729 | static void server_mutate_op(grpc_call_element *elem, |
| 730 | grpc_transport_stream_op *op) { |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 731 | call_data *calld = elem->call_data; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 732 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 733 | if (op->recv_initial_metadata != NULL) { |
Craig Tiller | 4d40ba3 | 2016-03-09 17:48:40 -0800 | [diff] [blame] | 734 | GPR_ASSERT(op->recv_idempotent_request == NULL); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 735 | calld->recv_initial_metadata = op->recv_initial_metadata; |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 736 | calld->on_done_recv_initial_metadata = op->recv_initial_metadata_ready; |
| 737 | op->recv_initial_metadata_ready = &calld->server_on_recv_initial_metadata; |
Craig Tiller | 4d40ba3 | 2016-03-09 17:48:40 -0800 | [diff] [blame] | 738 | op->recv_idempotent_request = &calld->recv_idempotent_request; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 739 | } |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 740 | } |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 741 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 742 | static void server_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 743 | grpc_call_element *elem, |
| 744 | grpc_transport_stream_op *op) { |
| 745 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 746 | server_mutate_op(elem, op); |
| 747 | grpc_call_next_op(exec_ctx, elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 748 | } |
| 749 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 750 | static void got_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr, |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 751 | grpc_error *error) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 752 | grpc_call_element *elem = ptr; |
| 753 | call_data *calld = elem->call_data; |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 754 | if (error == GRPC_ERROR_NONE) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 755 | start_new_rpc(exec_ctx, elem); |
| 756 | } else { |
| 757 | gpr_mu_lock(&calld->mu_state); |
| 758 | if (calld->state == NOT_STARTED) { |
| 759 | calld->state = ZOMBIED; |
| 760 | gpr_mu_unlock(&calld->mu_state); |
| 761 | grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem); |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 762 | grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE, |
| 763 | NULL); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 764 | } else if (calld->state == PENDING) { |
| 765 | calld->state = ZOMBIED; |
| 766 | gpr_mu_unlock(&calld->mu_state); |
| 767 | /* zombied call will be destroyed when it's removed from the pending |
| 768 | queue... later */ |
| 769 | } else { |
| 770 | gpr_mu_unlock(&calld->mu_state); |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | static void accept_stream(grpc_exec_ctx *exec_ctx, void *cd, |
| 776 | grpc_transport *transport, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 777 | const void *transport_server_data) { |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 778 | channel_data *chand = cd; |
| 779 | /* create a call */ |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 780 | grpc_call *call = |
| 781 | grpc_call_create(chand->channel, NULL, 0, NULL, transport_server_data, |
| 782 | NULL, 0, gpr_inf_future(GPR_CLOCK_MONOTONIC)); |
| 783 | grpc_call_element *elem = |
| 784 | grpc_call_stack_element(grpc_call_get_call_stack(call), 0); |
| 785 | call_data *calld = elem->call_data; |
| 786 | grpc_op op; |
| 787 | memset(&op, 0, sizeof(op)); |
| 788 | op.op = GRPC_OP_RECV_INITIAL_METADATA; |
| 789 | op.data.recv_initial_metadata = &calld->initial_metadata; |
| 790 | grpc_closure_init(&calld->got_initial_metadata, got_initial_metadata, elem); |
| 791 | grpc_call_start_batch_and_execute(exec_ctx, call, &op, 1, |
| 792 | &calld->got_initial_metadata); |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 795 | static void channel_connectivity_changed(grpc_exec_ctx *exec_ctx, void *cd, |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 796 | grpc_error *error) { |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 797 | channel_data *chand = cd; |
| 798 | grpc_server *server = chand->server; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 799 | if (chand->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE) { |
| 800 | grpc_transport_op op; |
| 801 | memset(&op, 0, sizeof(op)); |
| 802 | op.on_connectivity_state_change = &chand->channel_connectivity_changed, |
| 803 | op.connectivity_state = &chand->connectivity_state; |
| 804 | grpc_channel_next_op(exec_ctx, |
| 805 | grpc_channel_stack_element( |
| 806 | grpc_channel_get_channel_stack(chand->channel), 0), |
| 807 | &op); |
| 808 | } else { |
| 809 | gpr_mu_lock(&server->mu_global); |
| 810 | destroy_channel(exec_ctx, chand); |
| 811 | gpr_mu_unlock(&server->mu_global); |
| 812 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "connectivity"); |
| 813 | } |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 816 | static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 817 | grpc_call_element_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 818 | call_data *calld = elem->call_data; |
| 819 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 820 | memset(calld, 0, sizeof(call_data)); |
| 821 | calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
| 822 | calld->call = grpc_call_from_top_element(elem); |
| 823 | gpr_mu_init(&calld->mu_state); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 824 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 825 | grpc_closure_init(&calld->server_on_recv_initial_metadata, |
| 826 | server_on_recv_initial_metadata, elem); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 827 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 828 | server_ref(chand->server); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 829 | } |
| 830 | |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 831 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 832 | void *ignored) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 833 | channel_data *chand = elem->channel_data; |
Craig Tiller | db7db99 | 2015-01-29 11:19:01 -0800 | [diff] [blame] | 834 | call_data *calld = elem->call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 835 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 836 | GPR_ASSERT(calld->state != PENDING); |
Craig Tiller | 092d8d1 | 2015-07-04 22:35:00 -0700 | [diff] [blame] | 837 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 838 | if (calld->host) { |
| 839 | GRPC_MDSTR_UNREF(calld->host); |
| 840 | } |
| 841 | if (calld->path) { |
| 842 | GRPC_MDSTR_UNREF(calld->path); |
| 843 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 844 | grpc_metadata_array_destroy(&calld->initial_metadata); |
Craig Tiller | 4df31a6 | 2015-01-30 09:44:31 -0800 | [diff] [blame] | 845 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 846 | gpr_mu_destroy(&calld->mu_state); |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 847 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 848 | server_unref(exec_ctx, chand->server); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 849 | } |
| 850 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 851 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 852 | grpc_channel_element *elem, |
| 853 | grpc_channel_element_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 854 | channel_data *chand = elem->channel_data; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 855 | GPR_ASSERT(args->is_first); |
| 856 | GPR_ASSERT(!args->is_last); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 857 | chand->server = NULL; |
| 858 | chand->channel = NULL; |
| 859 | chand->next = chand->prev = chand; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 860 | chand->registered_methods = NULL; |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 861 | chand->connectivity_state = GRPC_CHANNEL_IDLE; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 862 | grpc_closure_init(&chand->channel_connectivity_changed, |
| 863 | channel_connectivity_changed, chand); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 864 | } |
| 865 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 866 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 867 | grpc_channel_element *elem) { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 868 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 869 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 870 | if (chand->registered_methods) { |
| 871 | for (i = 0; i < chand->registered_method_slots; i++) { |
| 872 | if (chand->registered_methods[i].method) { |
| 873 | GRPC_MDSTR_UNREF(chand->registered_methods[i].method); |
| 874 | } |
| 875 | if (chand->registered_methods[i].host) { |
| 876 | GRPC_MDSTR_UNREF(chand->registered_methods[i].host); |
| 877 | } |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 878 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 879 | gpr_free(chand->registered_methods); |
| 880 | } |
| 881 | if (chand->server) { |
| 882 | gpr_mu_lock(&chand->server->mu_global); |
| 883 | chand->next->prev = chand->prev; |
| 884 | chand->prev->next = chand->next; |
| 885 | chand->next = chand->prev = chand; |
| 886 | maybe_finish_shutdown(exec_ctx, chand->server); |
| 887 | gpr_mu_unlock(&chand->server->mu_global); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 888 | server_unref(exec_ctx, chand->server); |
| 889 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 890 | } |
| 891 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 892 | const grpc_channel_filter grpc_server_top_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 893 | server_start_transport_stream_op, |
| 894 | grpc_channel_next_op, |
| 895 | sizeof(call_data), |
| 896 | init_call_elem, |
| 897 | grpc_call_stack_ignore_set_pollset, |
| 898 | destroy_call_elem, |
| 899 | sizeof(channel_data), |
| 900 | init_channel_elem, |
| 901 | destroy_channel_elem, |
| 902 | grpc_call_next_get_peer, |
| 903 | "server", |
Craig Tiller | 9f28ac2 | 2015-01-27 17:01:29 -0800 | [diff] [blame] | 904 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 905 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 906 | void grpc_server_register_completion_queue(grpc_server *server, |
| 907 | grpc_completion_queue *cq, |
| 908 | void *reserved) { |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 909 | size_t i, n; |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 910 | GRPC_API_TRACE( |
| 911 | "grpc_server_register_completion_queue(server=%p, cq=%p, reserved=%p)", 3, |
| 912 | (server, cq, reserved)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 913 | GPR_ASSERT(!reserved); |
| 914 | for (i = 0; i < server->cq_count; i++) { |
| 915 | if (server->cqs[i] == cq) return; |
| 916 | } |
| 917 | GRPC_CQ_INTERNAL_REF(cq, "server"); |
| 918 | grpc_cq_mark_server_cq(cq); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 919 | n = server->cq_count++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 920 | server->cqs = gpr_realloc(server->cqs, |
| 921 | server->cq_count * sizeof(grpc_completion_queue *)); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 922 | server->cqs[n] = cq; |
| 923 | } |
| 924 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 925 | grpc_server *grpc_server_create(const grpc_channel_args *args, void *reserved) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 926 | size_t i; |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 927 | |
| 928 | GRPC_API_TRACE("grpc_server_create(%p, %p)", 2, (args, reserved)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 929 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 930 | grpc_server *server = gpr_malloc(sizeof(grpc_server)); |
Craig Tiller | 60fd361 | 2015-03-05 16:24:22 -0800 | [diff] [blame] | 931 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 932 | GPR_ASSERT(grpc_is_initialized() && "call grpc_init()"); |
Craig Tiller | 60fd361 | 2015-03-05 16:24:22 -0800 | [diff] [blame] | 933 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 934 | memset(server, 0, sizeof(grpc_server)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 935 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 936 | gpr_mu_init(&server->mu_global); |
| 937 | gpr_mu_init(&server->mu_call); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 938 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 939 | /* decremented by grpc_server_destroy */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 940 | gpr_ref_init(&server->internal_refcount, 1); |
| 941 | server->root_channel_data.next = server->root_channel_data.prev = |
| 942 | &server->root_channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 943 | |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 944 | /* TODO(ctiller): expose a channel_arg for this */ |
| 945 | server->max_requested_calls = 32768; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 946 | server->request_freelist = |
| 947 | gpr_stack_lockfree_create(server->max_requested_calls); |
| 948 | for (i = 0; i < (size_t)server->max_requested_calls; i++) { |
| 949 | gpr_stack_lockfree_push(server->request_freelist, (int)i); |
| 950 | } |
| 951 | request_matcher_init(&server->unregistered_request_matcher, |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 952 | server->max_requested_calls, server); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 953 | server->requested_calls = gpr_malloc(server->max_requested_calls * |
| 954 | sizeof(*server->requested_calls)); |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 955 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 956 | server->channel_args = grpc_channel_args_copy(args); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 957 | |
| 958 | return server; |
| 959 | } |
| 960 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 961 | static int streq(const char *a, const char *b) { |
| 962 | if (a == NULL && b == NULL) return 1; |
| 963 | if (a == NULL) return 0; |
| 964 | if (b == NULL) return 0; |
| 965 | return 0 == strcmp(a, b); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 966 | } |
| 967 | |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 968 | void *grpc_server_register_method( |
| 969 | grpc_server *server, const char *method, const char *host, |
| 970 | grpc_server_register_method_payload_handling payload_handling, |
| 971 | uint32_t flags) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 972 | registered_method *m; |
Craig Tiller | b290686 | 2016-03-10 06:50:07 -0800 | [diff] [blame] | 973 | GRPC_API_TRACE( |
| 974 | "grpc_server_register_method(server=%p, method=%s, host=%s, " |
| 975 | "flags=0x%08x)", |
| 976 | 4, (server, method, host, flags)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 977 | if (!method) { |
| 978 | gpr_log(GPR_ERROR, |
| 979 | "grpc_server_register_method method string cannot be NULL"); |
| 980 | return NULL; |
| 981 | } |
| 982 | for (m = server->registered_methods; m; m = m->next) { |
| 983 | if (streq(m->method, method) && streq(m->host, host)) { |
| 984 | gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method, |
| 985 | host ? host : "*"); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 986 | return NULL; |
| 987 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 988 | } |
Craig Tiller | b290686 | 2016-03-10 06:50:07 -0800 | [diff] [blame] | 989 | if ((flags & ~GRPC_INITIAL_METADATA_USED_MASK) != 0) { |
| 990 | gpr_log(GPR_ERROR, "grpc_server_register_method invalid flags 0x%08x", |
| 991 | flags); |
| 992 | return NULL; |
| 993 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 994 | m = gpr_malloc(sizeof(registered_method)); |
| 995 | memset(m, 0, sizeof(*m)); |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 996 | request_matcher_init(&m->request_matcher, server->max_requested_calls, |
| 997 | server); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 998 | m->method = gpr_strdup(method); |
| 999 | m->host = gpr_strdup(host); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1000 | m->next = server->registered_methods; |
Craig Tiller | 5ada3d2 | 2016-04-04 08:30:59 -0700 | [diff] [blame] | 1001 | m->payload_handling = payload_handling; |
Craig Tiller | b290686 | 2016-03-10 06:50:07 -0800 | [diff] [blame] | 1002 | m->flags = flags; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1003 | server->registered_methods = m; |
| 1004 | return m; |
| 1005 | } |
| 1006 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1007 | void grpc_server_start(grpc_server *server) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1008 | listener *l; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 1009 | size_t i; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 1010 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 1011 | |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 1012 | GRPC_API_TRACE("grpc_server_start(server=%p)", 1, (server)); |
| 1013 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1014 | server->pollsets = gpr_malloc(sizeof(grpc_pollset *) * server->cq_count); |
| 1015 | for (i = 0; i < server->cq_count; i++) { |
| 1016 | server->pollsets[i] = grpc_cq_pollset(server->cqs[i]); |
| 1017 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1018 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1019 | for (l = server->listeners; l; l = l->next) { |
| 1020 | l->start(&exec_ctx, server, l->arg, server->pollsets, server->cq_count); |
| 1021 | } |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1022 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1023 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1026 | void grpc_server_setup_transport(grpc_exec_ctx *exec_ctx, grpc_server *s, |
| 1027 | grpc_transport *transport, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1028 | const grpc_channel_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1029 | size_t i; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1030 | size_t num_registered_methods; |
| 1031 | size_t alloc; |
| 1032 | registered_method *rm; |
| 1033 | channel_registered_method *crm; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1034 | grpc_channel *channel; |
| 1035 | channel_data *chand; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1036 | grpc_mdstr *host; |
| 1037 | grpc_mdstr *method; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1038 | uint32_t hash; |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame] | 1039 | size_t slots; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1040 | uint32_t probes; |
| 1041 | uint32_t max_probes = 0; |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 1042 | grpc_transport_op op; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1043 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1044 | for (i = 0; i < s->cq_count; i++) { |
| 1045 | memset(&op, 0, sizeof(op)); |
| 1046 | op.bind_pollset = grpc_cq_pollset(s->cqs[i]); |
| 1047 | grpc_transport_perform_op(exec_ctx, transport, &op); |
| 1048 | } |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 1049 | |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 1050 | channel = |
| 1051 | grpc_channel_create(exec_ctx, NULL, args, GRPC_SERVER_CHANNEL, transport); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1052 | chand = (channel_data *)grpc_channel_stack_element( |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1053 | grpc_channel_get_channel_stack(channel), 0) |
| 1054 | ->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1055 | chand->server = s; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1056 | server_ref(s); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1057 | chand->channel = channel; |
| 1058 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1059 | num_registered_methods = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1060 | for (rm = s->registered_methods; rm; rm = rm->next) { |
| 1061 | num_registered_methods++; |
| 1062 | } |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1063 | /* build a lookup table phrased in terms of mdstr's in this channels context |
| 1064 | to quickly find registered methods */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1065 | if (num_registered_methods > 0) { |
| 1066 | slots = 2 * num_registered_methods; |
| 1067 | alloc = sizeof(channel_registered_method) * slots; |
| 1068 | chand->registered_methods = gpr_malloc(alloc); |
| 1069 | memset(chand->registered_methods, 0, alloc); |
| 1070 | for (rm = s->registered_methods; rm; rm = rm->next) { |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 1071 | host = rm->host ? grpc_mdstr_from_string(rm->host) : NULL; |
| 1072 | method = grpc_mdstr_from_string(rm->method); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1073 | hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash); |
| 1074 | for (probes = 0; chand->registered_methods[(hash + probes) % slots] |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1075 | .server_registered_method != NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1076 | probes++) |
| 1077 | ; |
| 1078 | if (probes > max_probes) max_probes = probes; |
| 1079 | crm = &chand->registered_methods[(hash + probes) % slots]; |
| 1080 | crm->server_registered_method = rm; |
Craig Tiller | b290686 | 2016-03-10 06:50:07 -0800 | [diff] [blame] | 1081 | crm->flags = rm->flags; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1082 | crm->host = host; |
| 1083 | crm->method = method; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1084 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1085 | GPR_ASSERT(slots <= UINT32_MAX); |
| 1086 | chand->registered_method_slots = (uint32_t)slots; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1087 | chand->registered_method_max_probes = max_probes; |
| 1088 | } |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1089 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1090 | gpr_mu_lock(&s->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1091 | chand->next = &s->root_channel_data; |
| 1092 | chand->prev = chand->next->prev; |
| 1093 | chand->next->prev = chand->prev->next = chand; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1094 | gpr_mu_unlock(&s->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1095 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1096 | GRPC_CHANNEL_INTERNAL_REF(channel, "connectivity"); |
| 1097 | memset(&op, 0, sizeof(op)); |
Craig Tiller | d7f12e3 | 2016-03-03 10:08:31 -0800 | [diff] [blame] | 1098 | op.set_accept_stream = true; |
| 1099 | op.set_accept_stream_fn = accept_stream; |
Craig Tiller | 4b80410 | 2015-06-26 16:16:12 -0700 | [diff] [blame] | 1100 | op.set_accept_stream_user_data = chand; |
| 1101 | op.on_connectivity_state_change = &chand->channel_connectivity_changed; |
| 1102 | op.connectivity_state = &chand->connectivity_state; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame^] | 1103 | if (gpr_atm_acq_load(&s->shutdown_flag) != 0) { |
| 1104 | op.disconnect_with_error = GRPC_ERROR_CREATE("Server shutdown"); |
| 1105 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1106 | grpc_transport_perform_op(exec_ctx, transport, &op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1107 | } |
| 1108 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1109 | void done_published_shutdown(grpc_exec_ctx *exec_ctx, void *done_arg, |
| 1110 | grpc_cq_completion *storage) { |
| 1111 | (void)done_arg; |
| 1112 | gpr_free(storage); |
murgatroid99 | 00a3dab | 2015-08-19 11:15:38 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1115 | static void listener_destroy_done(grpc_exec_ctx *exec_ctx, void *s, |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 1116 | grpc_error *error) { |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1117 | grpc_server *server = s; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1118 | gpr_mu_lock(&server->mu_global); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1119 | server->listeners_destroyed++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1120 | maybe_finish_shutdown(exec_ctx, server); |
| 1121 | gpr_mu_unlock(&server->mu_global); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1124 | void grpc_server_shutdown_and_notify(grpc_server *server, |
| 1125 | grpc_completion_queue *cq, void *tag) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1126 | listener *l; |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 1127 | shutdown_tag *sdt; |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 1128 | channel_broadcaster broadcaster; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 1129 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1130 | |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 1131 | GRPC_API_TRACE("grpc_server_shutdown_and_notify(server=%p, cq=%p, tag=%p)", 3, |
| 1132 | (server, cq, tag)); |
| 1133 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1134 | /* lock, and gather up some stuff to do */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1135 | gpr_mu_lock(&server->mu_global); |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 1136 | grpc_cq_begin_op(cq, tag); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1137 | if (server->shutdown_published) { |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 1138 | grpc_cq_end_op(&exec_ctx, cq, tag, GRPC_ERROR_NONE, done_published_shutdown, |
| 1139 | NULL, gpr_malloc(sizeof(grpc_cq_completion))); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1140 | gpr_mu_unlock(&server->mu_global); |
| 1141 | goto done; |
| 1142 | } |
| 1143 | server->shutdown_tags = |
| 1144 | gpr_realloc(server->shutdown_tags, |
| 1145 | sizeof(shutdown_tag) * (server->num_shutdown_tags + 1)); |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 1146 | sdt = &server->shutdown_tags[server->num_shutdown_tags++]; |
| 1147 | sdt->tag = tag; |
| 1148 | sdt->cq = cq; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1149 | if (gpr_atm_acq_load(&server->shutdown_flag)) { |
| 1150 | gpr_mu_unlock(&server->mu_global); |
| 1151 | goto done; |
| 1152 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1153 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1154 | server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME); |
Craig Tiller | ab54f79 | 2015-07-08 08:34:20 -0700 | [diff] [blame] | 1155 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1156 | channel_broadcaster_init(server, &broadcaster); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1157 | |
Craig Tiller | fc193e1 | 2015-09-24 15:29:03 -0700 | [diff] [blame] | 1158 | gpr_atm_rel_store(&server->shutdown_flag, 1); |
| 1159 | |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 1160 | /* collect all unregistered then registered calls */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1161 | gpr_mu_lock(&server->mu_call); |
| 1162 | kill_pending_work_locked(&exec_ctx, server); |
| 1163 | gpr_mu_unlock(&server->mu_call); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1164 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1165 | maybe_finish_shutdown(&exec_ctx, server); |
| 1166 | gpr_mu_unlock(&server->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1167 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1168 | /* Shutdown listeners */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1169 | for (l = server->listeners; l; l = l->next) { |
| 1170 | grpc_closure_init(&l->destroy_done, listener_destroy_done, server); |
| 1171 | l->destroy(&exec_ctx, server, l->arg, &l->destroy_done); |
| 1172 | } |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 1173 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1174 | channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 1, 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1175 | |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1176 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1177 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1180 | void grpc_server_cancel_all_calls(grpc_server *server) { |
Craig Tiller | 092d8d1 | 2015-07-04 22:35:00 -0700 | [diff] [blame] | 1181 | channel_broadcaster broadcaster; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 1182 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | afa2d63 | 2015-05-26 16:39:13 -0700 | [diff] [blame] | 1183 | |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 1184 | GRPC_API_TRACE("grpc_server_cancel_all_calls(server=%p)", 1, (server)); |
| 1185 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1186 | gpr_mu_lock(&server->mu_global); |
| 1187 | channel_broadcaster_init(server, &broadcaster); |
| 1188 | gpr_mu_unlock(&server->mu_global); |
Craig Tiller | afa2d63 | 2015-05-26 16:39:13 -0700 | [diff] [blame] | 1189 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame^] | 1190 | channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 0, |
| 1191 | GRPC_ERROR_CREATE("Cancelling all calls")); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1192 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | afa2d63 | 2015-05-26 16:39:13 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1195 | void grpc_server_destroy(grpc_server *server) { |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1196 | listener *l; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 1197 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | 872af02 | 2015-04-24 15:57:52 -0700 | [diff] [blame] | 1198 | |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 1199 | GRPC_API_TRACE("grpc_server_destroy(server=%p)", 1, (server)); |
| 1200 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1201 | gpr_mu_lock(&server->mu_global); |
| 1202 | GPR_ASSERT(gpr_atm_acq_load(&server->shutdown_flag) || !server->listeners); |
| 1203 | GPR_ASSERT(server->listeners_destroyed == num_listeners(server)); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1204 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1205 | while (server->listeners) { |
| 1206 | l = server->listeners; |
| 1207 | server->listeners = l->next; |
| 1208 | gpr_free(l); |
| 1209 | } |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1210 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1211 | gpr_mu_unlock(&server->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1212 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1213 | server_unref(&exec_ctx, server); |
| 1214 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1217 | void grpc_server_add_listener( |
| 1218 | grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg, |
| 1219 | void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg, |
| 1220 | grpc_pollset **pollsets, size_t pollset_count), |
| 1221 | void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg, |
| 1222 | grpc_closure *on_done)) { |
| 1223 | listener *l = gpr_malloc(sizeof(listener)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1224 | l->arg = arg; |
| 1225 | l->start = start; |
| 1226 | l->destroy = destroy; |
| 1227 | l->next = server->listeners; |
| 1228 | server->listeners = l; |
| 1229 | } |
| 1230 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1231 | static grpc_call_error queue_call_request(grpc_exec_ctx *exec_ctx, |
| 1232 | grpc_server *server, |
| 1233 | requested_call *rc) { |
Yang Gao | eb8e7cd | 2015-02-11 11:43:40 -0800 | [diff] [blame] | 1234 | call_data *calld = NULL; |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 1235 | request_matcher *rm = NULL; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1236 | int request_id; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1237 | if (gpr_atm_acq_load(&server->shutdown_flag)) { |
| 1238 | fail_call(exec_ctx, server, rc); |
| 1239 | return GRPC_CALL_OK; |
| 1240 | } |
| 1241 | request_id = gpr_stack_lockfree_pop(server->request_freelist); |
| 1242 | if (request_id == -1) { |
| 1243 | /* out of request ids: just fail this one */ |
| 1244 | fail_call(exec_ctx, server, rc); |
| 1245 | return GRPC_CALL_OK; |
| 1246 | } |
| 1247 | switch (rc->type) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1248 | case BATCH_CALL: |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 1249 | rm = &server->unregistered_request_matcher; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1250 | break; |
| 1251 | case REGISTERED_CALL: |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 1252 | rm = &rc->data.registered.registered_method->request_matcher; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1253 | break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1254 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1255 | server->requested_calls[request_id] = *rc; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1256 | gpr_free(rc); |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 1257 | if (gpr_stack_lockfree_push(rm->requests, request_id)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1258 | /* this was the first queued request: we need to lock and start |
| 1259 | matching calls */ |
| 1260 | gpr_mu_lock(&server->mu_call); |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 1261 | while ((calld = rm->pending_head) != NULL) { |
| 1262 | request_id = gpr_stack_lockfree_pop(rm->requests); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1263 | if (request_id == -1) break; |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 1264 | rm->pending_head = calld->pending_next; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1265 | gpr_mu_unlock(&server->mu_call); |
| 1266 | gpr_mu_lock(&calld->mu_state); |
| 1267 | if (calld->state == ZOMBIED) { |
| 1268 | gpr_mu_unlock(&calld->mu_state); |
| 1269 | grpc_closure_init( |
| 1270 | &calld->kill_zombie_closure, kill_zombie, |
| 1271 | grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0)); |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 1272 | grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, |
| 1273 | GRPC_ERROR_NONE, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1274 | } else { |
| 1275 | GPR_ASSERT(calld->state == PENDING); |
| 1276 | calld->state = ACTIVATED; |
| 1277 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | 8851269 | 2016-04-04 09:32:52 -0700 | [diff] [blame] | 1278 | publish_call(exec_ctx, server, calld, |
| 1279 | &server->requested_calls[request_id]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1280 | } |
| 1281 | gpr_mu_lock(&server->mu_call); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1282 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1283 | gpr_mu_unlock(&server->mu_call); |
| 1284 | } |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1285 | return GRPC_CALL_OK; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1286 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1287 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1288 | grpc_call_error grpc_server_request_call( |
| 1289 | grpc_server *server, grpc_call **call, grpc_call_details *details, |
| 1290 | grpc_metadata_array *initial_metadata, |
| 1291 | grpc_completion_queue *cq_bound_to_call, |
| 1292 | grpc_completion_queue *cq_for_notification, void *tag) { |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1293 | grpc_call_error error; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 1294 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1295 | requested_call *rc = gpr_malloc(sizeof(*rc)); |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 1296 | GRPC_API_TRACE( |
| 1297 | "grpc_server_request_call(" |
Craig Tiller | 4de3e4f | 2015-10-05 08:55:50 -0700 | [diff] [blame] | 1298 | "server=%p, call=%p, details=%p, initial_metadata=%p, " |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1299 | "cq_bound_to_call=%p, cq_for_notification=%p, tag=%p)", |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 1300 | 7, (server, call, details, initial_metadata, cq_bound_to_call, |
| 1301 | cq_for_notification, tag)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1302 | if (!grpc_cq_is_server_cq(cq_for_notification)) { |
| 1303 | gpr_free(rc); |
| 1304 | error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE; |
| 1305 | goto done; |
| 1306 | } |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 1307 | grpc_cq_begin_op(cq_for_notification, tag); |
Craig Tiller | 9928d39 | 2015-08-18 09:40:24 -0700 | [diff] [blame] | 1308 | details->reserved = NULL; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1309 | rc->type = BATCH_CALL; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1310 | rc->server = server; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1311 | rc->tag = tag; |
| 1312 | rc->cq_bound_to_call = cq_bound_to_call; |
| 1313 | rc->cq_for_notification = cq_for_notification; |
| 1314 | rc->call = call; |
| 1315 | rc->data.batch.details = details; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1316 | rc->initial_metadata = initial_metadata; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1317 | error = queue_call_request(&exec_ctx, server, rc); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1318 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1319 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1320 | return error; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1323 | grpc_call_error grpc_server_request_registered_call( |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 1324 | grpc_server *server, void *rmp, grpc_call **call, gpr_timespec *deadline, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1325 | grpc_metadata_array *initial_metadata, grpc_byte_buffer **optional_payload, |
| 1326 | grpc_completion_queue *cq_bound_to_call, |
| 1327 | grpc_completion_queue *cq_for_notification, void *tag) { |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1328 | grpc_call_error error; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 1329 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1330 | requested_call *rc = gpr_malloc(sizeof(*rc)); |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 1331 | registered_method *rm = rmp; |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 1332 | GRPC_API_TRACE( |
| 1333 | "grpc_server_request_registered_call(" |
Craig Tiller | 4de3e4f | 2015-10-05 08:55:50 -0700 | [diff] [blame] | 1334 | "server=%p, rmp=%p, call=%p, deadline=%p, initial_metadata=%p, " |
| 1335 | "optional_payload=%p, cq_bound_to_call=%p, cq_for_notification=%p, " |
| 1336 | "tag=%p)", |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 1337 | 9, (server, rmp, call, deadline, initial_metadata, optional_payload, |
| 1338 | cq_bound_to_call, cq_for_notification, tag)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1339 | if (!grpc_cq_is_server_cq(cq_for_notification)) { |
| 1340 | gpr_free(rc); |
| 1341 | error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE; |
| 1342 | goto done; |
| 1343 | } |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 1344 | if ((optional_payload == NULL) != |
| 1345 | (rm->payload_handling == GRPC_SRM_PAYLOAD_NONE)) { |
| 1346 | gpr_free(rc); |
| 1347 | error = GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH; |
| 1348 | goto done; |
| 1349 | } |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 1350 | grpc_cq_begin_op(cq_for_notification, tag); |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1351 | rc->type = REGISTERED_CALL; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1352 | rc->server = server; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1353 | rc->tag = tag; |
| 1354 | rc->cq_bound_to_call = cq_bound_to_call; |
| 1355 | rc->cq_for_notification = cq_for_notification; |
| 1356 | rc->call = call; |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 1357 | rc->data.registered.registered_method = rm; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1358 | rc->data.registered.deadline = deadline; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1359 | rc->initial_metadata = initial_metadata; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1360 | rc->data.registered.optional_payload = optional_payload; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1361 | error = queue_call_request(&exec_ctx, server, rc); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1362 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1363 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1364 | return error; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1365 | } |
| 1366 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1367 | static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server, |
| 1368 | requested_call *rc) { |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 1369 | *rc->call = NULL; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1370 | rc->initial_metadata->count = 0; |
| 1371 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1372 | server_ref(server); |
| 1373 | grpc_cq_end_op(exec_ctx, rc->cq_for_notification, rc->tag, 0, |
| 1374 | done_request_event, rc, &rc->completion); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1375 | } |
| 1376 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1377 | const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1378 | return server->channel_args; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 1379 | } |
Craig Tiller | ba3c3cd | 2015-05-26 06:28:10 -0700 | [diff] [blame] | 1380 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1381 | int grpc_server_has_open_connections(grpc_server *server) { |
Craig Tiller | ba3c3cd | 2015-05-26 06:28:10 -0700 | [diff] [blame] | 1382 | int r; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1383 | gpr_mu_lock(&server->mu_global); |
Craig Tiller | ba3c3cd | 2015-05-26 06:28:10 -0700 | [diff] [blame] | 1384 | r = server->root_channel_data.next != &server->root_channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1385 | gpr_mu_unlock(&server->mu_global); |
Craig Tiller | ba3c3cd | 2015-05-26 06:28:10 -0700 | [diff] [blame] | 1386 | return r; |
| 1387 | } |