Debian Codegraph @

crt/aws-c-event-stream/source/event_stream_rpc_server.c

raw symbols SCIP
1 /*
2 * Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "aws/io/future.h"
17
18 #include <aws/event-stream/event_stream_channel_handler.h>
19 #include <aws/event-stream/event_stream_rpc_server.h>
20 #include <aws/event-stream/private/event_stream_rpc_priv.h>
21
22 #include <aws/common/atomics.h>
23 #include <aws/common/clock.h>
24 #include <aws/common/hash_table.h>
25
26 #include <aws/io/channel.h>
27 #include <aws/io/channel_bootstrap.h>
28 #include <aws/io/socket.h>
29
30 #include <inttypes.h>
31
32 #if defined(_MSC_VER)
33 /* allow non-constant aggregate initializer */
34 # pragma warning(disable : 4204)
35 /* allow passing a pointer to an automatically allocated variable around, cause I'm smarter than the compiler. */
36 # pragma warning(disable : 4221)
37 #endif
38
39 static const struct aws_byte_cursor s_missing_operation_name_error = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(
40 "{ \"message\": \"The first message for on a non-zero :stream-id must contain an operation header value.\"; }");
41
42 struct aws_event_stream_rpc_server_listener {
43 struct aws_allocator *allocator;
44 struct aws_socket *listener;
45 struct aws_server_bootstrap *bootstrap;
46 struct aws_atomic_var ref_count;
47 aws_event_stream_rpc_server_on_new_connection_fn *on_new_connection;
48 aws_event_stream_rpc_server_on_connection_shutdown_fn *on_connection_shutdown;
49 aws_event_stream_rpc_server_on_listener_destroy_fn *on_destroy_callback;
50 size_t initial_window_size;
51 bool enable_read_backpressure;
52 bool initialized;
53 void *user_data;
54 struct aws_future_void *setup_future;
55 };
56
57 struct aws_event_stream_rpc_server_connection {
58 struct aws_allocator *allocator;
59 struct aws_hash_table continuation_table;
60 struct aws_event_stream_rpc_server_listener *server;
61 struct aws_atomic_var ref_count;
62 aws_event_stream_rpc_server_on_incoming_stream_fn *on_incoming_stream;
63 aws_event_stream_rpc_server_connection_protocol_message_fn *on_connection_protocol_message;
64 struct aws_channel *channel;
65 struct aws_channel_handler *event_stream_handler;
66 uint32_t latest_stream_id;
67 void *user_data;
68 struct aws_atomic_var is_open;
69 struct aws_atomic_var handshake_state;
70 bool bootstrap_owned;
71 };
72
73 struct aws_event_stream_rpc_server_continuation_token {
74 uint32_t stream_id;
75 struct aws_event_stream_rpc_server_connection *connection;
76 aws_event_stream_rpc_server_stream_continuation_fn *continuation_fn;
77 aws_event_stream_rpc_server_stream_continuation_closed_fn *closed_fn;
78 void *user_data;
79 struct aws_atomic_var ref_count;
80 struct aws_atomic_var is_closed;
81 };
82
83 /** This is the destructor callback invoked by the connections continuation table when a continuation is removed
84 * from the hash table.
85 */
86 void s_continuation_destroy(void *value) {
87 struct aws_event_stream_rpc_server_continuation_token *continuation = value;
88 AWS_LOGF_DEBUG(AWS_LS_EVENT_STREAM_RPC_SERVER, "id=%p: destroying continuation", (void *)continuation);
89
90 /*
91 * When creating a stream, we end up putting the continuation in the table before we finish initializing it.
92 * If an error occurs in the on incoming stream callback, we end up with a continuation with no user data or
93 * callbacks. This means we have to check closed_fn for validity even though the success path does a fatal assert
94 * on validity.
95 */
96 if (continuation->closed_fn != NULL) {
97 continuation->closed_fn(continuation, continuation->user_data);
98 }
99
100 aws_event_stream_rpc_server_continuation_release(continuation);
101 }
102
103 static void s_on_message_received(struct aws_event_stream_message *message, int error_code, void *user_data);
104
105 /* We have two paths for creating a connection on a channel. The first is an incoming connection on the server listener.
106 * The second is adding a connection to an already existing channel. This is the code common to both cases. */
107 static struct aws_event_stream_rpc_server_connection *s_create_connection_on_channel(
108 struct aws_event_stream_rpc_server_listener *server,
109 struct aws_channel *channel) {
110
111 AWS_LOGF_TRACE(
112 AWS_LS_EVENT_STREAM_RPC_SERVER, "id=%p: creating connection on channel %p", (void *)server, (void *)channel);
113 struct aws_event_stream_rpc_server_connection *connection =
114 aws_mem_calloc(server->allocator, 1, sizeof(struct aws_event_stream_rpc_server_connection));
115 struct aws_channel_handler *event_stream_handler = NULL;
116 struct aws_channel_slot *slot = NULL;
117
118 if (!connection) {
119 AWS_LOGF_ERROR(
120 AWS_LS_EVENT_STREAM_RPC_SERVER,
121 "id=%p: allocation failed for connection with error %s",
122 (void *)server,
123 aws_error_debug_str(aws_last_error()));
124 return NULL;
125 }
126
127 AWS_LOGF_DEBUG(AWS_LS_EVENT_STREAM_RPC_SERVER, "id=%p: new connection is %p", (void *)server, (void *)connection);
128 aws_atomic_init_int(&connection->ref_count, 1u);
129 aws_atomic_init_int(&connection->is_open, 1u);
130 /* handshake step 1 is a connect message being received. Handshake 2 is the connect ack being sent.
131 * no messages other than connect and connect ack are allowed until this count reaches 2. */
132 aws_atomic_init_int(&connection->handshake_state, CONNECTION_HANDSHAKE_STATE_INITIALIZED);
133 connection->allocator = server->allocator;
134
135 if (aws_hash_table_init(
136 &connection->continuation_table,
137 server->allocator,
138 64,
139 aws_event_stream_rpc_hash_streamid,
140 aws_event_stream_rpc_streamid_eq,
141 NULL,
142 s_continuation_destroy)) {
143 AWS_LOGF_ERROR(
144 AWS_LS_EVENT_STREAM_RPC_SERVER,
145 "id=%p: initialization of connection stream table failed with error %s",
146 (void *)connection,
147 aws_error_debug_str(aws_last_error()));
148 goto error;
149 }
150
151 struct aws_event_stream_channel_handler_options handler_options = {
152 .on_message_received = s_on_message_received,
153 .user_data = connection,
154 .initial_window_size = server->initial_window_size,
155 .manual_window_management = server->enable_read_backpressure,
156 };
157
158 event_stream_handler = aws_event_stream_channel_handler_new(server->allocator, &handler_options);
159
160 if (!event_stream_handler) {
161 AWS_LOGF_ERROR(
162 AWS_LS_EVENT_STREAM_RPC_SERVER,
163 "id=%p: initialization of event-stream handler failed with error %s",
164 (void *)connection,
165 aws_error_debug_str(aws_last_error()));
166 goto error;
167 }
168
169 slot = aws_channel_slot_new(channel);
170
171 if (!slot) {
172 AWS_LOGF_ERROR(
173 AWS_LS_EVENT_STREAM_RPC_SERVER,
174 "id=%p: initialization of channel slot failed with error %s",
175 (void *)connection,
176 aws_error_debug_str(aws_last_error()));
177 goto error;
178 }
179
180 aws_channel_slot_insert_end(channel, slot);
181 if (aws_channel_slot_set_handler(slot, event_stream_handler)) {
182 AWS_LOGF_ERROR(
183 AWS_LS_EVENT_STREAM_RPC_SERVER,
184 "id=%p: setting the handler on the slot failed with error %s",
185 (void *)connection,
186 aws_error_debug_str(aws_last_error()));
187 goto error;
188 }
189
190 aws_event_stream_rpc_server_listener_acquire(server);
191 connection->server = server;
192
193 connection->event_stream_handler = event_stream_handler;
194 connection->channel = channel;
195 aws_channel_acquire_hold(channel);
196
197 return connection;
198
199 error:
200 if (!slot && event_stream_handler) {
201 aws_channel_handler_destroy(event_stream_handler);
202 }
203
204 if (connection) {
205 aws_event_stream_rpc_server_connection_release(connection);
206 }
207
208 return NULL;
209 }
210
211 struct aws_event_stream_rpc_server_connection *aws_event_stream_rpc_server_connection_from_existing_channel(
212 struct aws_event_stream_rpc_server_listener *server,
213 struct aws_channel *channel,
214 const struct aws_event_stream_rpc_connection_options *connection_options) {
215 AWS_FATAL_ASSERT(
216 connection_options->on_connection_protocol_message && "on_connection_protocol_message must be specified!");
217 AWS_FATAL_ASSERT(connection_options->on_incoming_stream && "on_incoming_stream must be specified");
218
219 struct aws_event_stream_rpc_server_connection *connection = s_create_connection_on_channel(server, channel);
220
221 if (!connection) {
222 return NULL;
223 }
224
225 connection->on_incoming_stream = connection_options->on_incoming_stream;
226 connection->on_connection_protocol_message = connection_options->on_connection_protocol_message;
227 connection->user_data = connection_options->user_data;
228 aws_event_stream_rpc_server_connection_acquire(connection);
229
230 return connection;
231 }
232
233 void aws_event_stream_rpc_server_connection_acquire(struct aws_event_stream_rpc_server_connection *connection) {
234 size_t current_count = aws_atomic_fetch_add_explicit(&connection->ref_count, 1, aws_memory_order_relaxed);
235 AWS_LOGF_TRACE(
236 AWS_LS_EVENT_STREAM_RPC_SERVER,
237 "id=%p: connection acquired, new ref count is %zu.",
238 (void *)connection,
239 current_count + 1);
240 }
241
242 void aws_event_stream_rpc_server_connection_release(struct aws_event_stream_rpc_server_connection *connection) {
243 if (!connection) {
244 return;
245 }
246
247 size_t value = aws_atomic_fetch_sub_explicit(&connection->ref_count, 1, aws_memory_order_seq_cst);
248
249 AWS_LOGF_TRACE(
250 AWS_LS_EVENT_STREAM_RPC_SERVER,
251 "id=%p: connection released, new ref count is %zu.",
252 (void *)connection,
253 value - 1);
254 if (value == 1) {
255 AWS_LOGF_DEBUG(AWS_LS_EVENT_STREAM_RPC_SERVER, "id=%p: destroying connection.", (void *)connection);
256 aws_channel_release_hold(connection->channel);
257 aws_hash_table_clean_up(&connection->continuation_table);
258 aws_event_stream_rpc_server_listener_release(connection->server);
259 aws_mem_release(connection->allocator, connection);
260 }
261 }
262
263 static void s_on_server_listener_setup(struct aws_server_bootstrap *bootstrap, int error_code, void *user_data) {
264 (void)bootstrap;
265 struct aws_event_stream_rpc_server_listener *server = user_data;
266 AWS_LOGF_DEBUG(
267 AWS_LS_EVENT_STREAM_RPC_SERVER,
268 "id=%p: listener setup completed with error %s",
269 (void *)server,
270 aws_error_debug_str(error_code));
271
272 /* Ensure that setup_future will be alive during aws_future_void_set_* call even if a waiter thread releases
273 * setup_future immediately after aws_future_void_wait returns result. Otherwise, aws_future_void_set_* might
274 * use already freed memory. */
275 aws_future_void_acquire(server->setup_future);
276
277 if (error_code) {
278 aws_future_void_set_error(server->setup_future, error_code);
279 } else {
280 aws_future_void_set_result(server->setup_future);
281 }
282
283 aws_future_void_release(server->setup_future);
284 }
285
286 /* incoming from a socket on this listener. */
287 static void s_on_accept_channel_setup(
288 struct aws_server_bootstrap *bootstrap,
289 int error_code,
290 struct aws_channel *channel,
291 void *user_data) {
292 (void)bootstrap;
293
294 struct aws_event_stream_rpc_server_listener *server = user_data;
295
296 if (!error_code) {
297 AWS_LOGF_INFO(
298 AWS_LS_EVENT_STREAM_RPC_SERVER,
299 "id=%p: incoming connection with channel %p.",
300 (void *)server,
301 (void *)channel);
302 AWS_FATAL_ASSERT(channel && "Channel should never be null with a 0 error code.");
303
304 struct aws_event_stream_rpc_server_connection *connection = s_create_connection_on_channel(server, channel);
305
306 if (!connection) {
307 int error = aws_last_error();
308 server->on_new_connection(NULL, error, NULL, server->user_data);
309 aws_channel_shutdown(channel, error);
310 }
311
312 struct aws_event_stream_rpc_connection_options connection_options;
313 AWS_ZERO_STRUCT(connection_options);
314
315 aws_event_stream_rpc_server_connection_acquire(connection);
316 AWS_LOGF_TRACE(
317 AWS_LS_EVENT_STREAM_RPC_SERVER,
318 "id=%p: invoking on_new_connection with connection %p.",
319 (void *)server,
320 (void *)connection);
321
322 if (server->on_new_connection(connection, AWS_ERROR_SUCCESS, &connection_options, server->user_data)) {
323 aws_channel_shutdown(channel, aws_last_error());
324 aws_event_stream_rpc_server_connection_release(connection);
325 return;
326 }
327
328 AWS_FATAL_ASSERT(
329 connection_options.on_connection_protocol_message && "on_connection_protocol_message must be specified!");
330 AWS_FATAL_ASSERT(connection_options.on_incoming_stream && "on_incoming_stream must be specified");
331 connection->on_incoming_stream = connection_options.on_incoming_stream;
332 connection->on_connection_protocol_message = connection_options.on_connection_protocol_message;
333 connection->user_data = connection_options.user_data;
334 connection->bootstrap_owned = true;
335 aws_event_stream_rpc_server_connection_release(connection);
336
337 } else {
338 AWS_LOGF_ERROR(
339 AWS_LS_EVENT_STREAM_RPC_SERVER,
340 "id=%p: invoking on_new_connection with error %s",
341 (void *)server,
342 aws_error_debug_str(error_code));
343 server->on_new_connection(NULL, error_code, NULL, server->user_data);
344 }
345 }
346
347 /* this is just to get the connection object off of the channel. */
348 static inline struct aws_event_stream_rpc_server_connection *s_rpc_connection_from_channel(
349 struct aws_channel *channel) {
350 struct aws_channel_slot *our_slot = NULL;
351 struct aws_channel_slot *current_slot = aws_channel_get_first_slot(channel);
352 AWS_FATAL_ASSERT(
353 current_slot &&
354 "It should be logically impossible to have a channel in this callback that doesn't have a slot in it");
355 while (current_slot->adj_right) {
356 current_slot = current_slot->adj_right;
357 }
358 our_slot = current_slot;
359 struct aws_channel_handler *our_handler = our_slot->handler;
360 return aws_event_stream_channel_handler_get_user_data(our_handler);
361 }
362
363 static void s_on_accept_channel_shutdown(
364 struct aws_server_bootstrap *bootstrap,
365 int error_code,
366 struct aws_channel *channel,
367 void *user_data) {
368 (void)bootstrap;
369
370 struct aws_event_stream_rpc_server_listener *server = user_data;
371 struct aws_event_stream_rpc_server_connection *connection = s_rpc_connection_from_channel(channel);
372
373 AWS_LOGF_DEBUG(
374 AWS_LS_EVENT_STREAM_RPC_SERVER,
375 "id=%p: channel %p and connection %p shutdown occurred with error %s",
376 (void *)server,
377 (void *)channel,
378 (void *)connection,
379 aws_error_debug_str(error_code));
380
381 aws_atomic_store_int(&connection->is_open, 0U);
382 aws_hash_table_clear(&connection->continuation_table);
383 aws_event_stream_rpc_server_connection_acquire(connection);
384 server->on_connection_shutdown(connection, error_code, server->user_data);
385 aws_event_stream_rpc_server_connection_release(connection);
386 aws_event_stream_rpc_server_connection_release(connection);
387 }
388
389 static void s_on_server_listener_destroy(struct aws_server_bootstrap *bootstrap, void *user_data) {
390 (void)bootstrap;
391 struct aws_event_stream_rpc_server_listener *server = user_data;
392
393 AWS_LOGF_INFO(AWS_LS_EVENT_STREAM_RPC_SERVER, "id=%p: destroying server", (void *)server);
394
395 /* server bootstrap invokes this callback regardless of if the listener was successfully created, so
396 * just check that we successfully set it up before freeing anything. When that's fixed in aws-c-io, this
397 * code will still be correct, so just leave it here for now. */
398
399 /* Call a user-provided destruction callback only if the server initialization was successful. If initialization
400 * failed, the calling side got NULL from aws_event_stream_rpc_server_new_listener and doesn't expect anything to
401 * be called from the event stream server's internals. */
402 if (server->initialized && server->on_destroy_callback) {
403 server->on_destroy_callback(server, server->user_data);
404 }
405
406 aws_mem_release(server->allocator, server);
407 }
408
409 struct aws_event_stream_rpc_server_listener *aws_event_stream_rpc_server_new_listener(
410 struct aws_allocator *allocator,
411 struct aws_event_stream_rpc_server_listener_options *options) {
412 struct aws_event_stream_rpc_server_listener *result = NULL;
413 struct aws_event_stream_rpc_server_listener *server =
414 aws_mem_calloc(allocator, 1, sizeof(struct aws_event_stream_rpc_server_listener));
415
416 AWS_LOGF_DEBUG(AWS_LS_EVENT_STREAM_RPC_SERVER, "static: new server is %p", (void *)server);
417 aws_atomic_init_int(&server->ref_count, 1);
418
419 struct aws_server_socket_channel_bootstrap_options bootstrap_options = {
420 .bootstrap = options->bootstrap,
421 .socket_options = options->socket_options,
422 .tls_options = options->tls_options,
423 .enable_read_back_pressure = false,
424 .host_name = options->host_name,
425 .port = options->port,
426 .setup_callback = s_on_server_listener_setup,
427 .incoming_callback = s_on_accept_channel_setup,
428 .shutdown_callback = s_on_accept_channel_shutdown,
429 .destroy_callback = s_on_server_listener_destroy,
430 .user_data = server,
431 };
432
433 server->bootstrap = options->bootstrap;
434 server->allocator = allocator;
435 server->on_destroy_callback = options->on_destroy_callback;
436 server->on_new_connection = options->on_new_connection;
437 server->on_connection_shutdown = options->on_connection_shutdown;
438 server->user_data = options->user_data;
439 server->setup_future = aws_future_void_new(allocator);
440 struct aws_future_void *setup_future = server->setup_future;
441
442 struct aws_socket *listener = aws_server_bootstrap_new_socket_listener(&bootstrap_options);
443 if (!listener) {
444 AWS_LOGF_ERROR(
445 AWS_LS_EVENT_STREAM_RPC_SERVER,
446 "static: failed to allocate new socket listener with error %s",
447 aws_error_debug_str(aws_last_error()));
448 goto done;
449 }
450
451 /* With all the possible event loop hiccups, 60 seconds is still more than enough for completing binding and
452 * listening, successfully or not. */
453 uint64_t timeout_sec = 60;
454 /* Handle async nw_socket (Apple Network framework socket) case when the actual work (i.e. binding and listening) is
455 * happening asynchronously in the dispatch queue event loop. In case of a failure, the server destruction can be
456 * already in progress in the event loop thread.
457 * For POSIX sockets the error handling is happening in a synchronous manner.
458 */
459 aws_future_void_wait(setup_future, timeout_sec * AWS_TIMESTAMP_NANOS);
460 int listen_error = aws_future_void_get_error(setup_future);
461
462 if (listen_error) {
463 AWS_LOGF_ERROR(
464 AWS_LS_EVENT_STREAM_RPC_SERVER,
465 "static: failed to setup new socket listener with error %s",
466 aws_error_debug_str(listen_error));
467 aws_raise_error(listen_error);
468 goto done;
469 }
470
471 /* At this point a listening socket is successfully initialized, so we complete the server initialization. */
472 server->listener = listener;
473 server->initialized = true;
474 result = server;
475
476 done:
477 /* Even if aws_server_bootstrap_new_socket_listener fails, the bootstrap_options.destroy_callback will still
478 * be fired. So, to avoid a race condition we delegate server's destruction to that callback. */
479 aws_future_void_release(setup_future);
480 return result;
481 }
482
483 uint32_t aws_event_stream_rpc_server_listener_get_bound_port(
484 const struct aws_event_stream_rpc_server_listener *server) {
485
486 struct aws_socket_endpoint address;
487 AWS_ZERO_STRUCT(address);
488 /* not checking error code because it can't fail when called on a listening socket */
489 aws_socket_get_bound_address(server->listener, &address);
490 return address.port;
491 }
492
493 void aws_event_stream_rpc_server_listener_acquire(struct aws_event_stream_rpc_server_listener *server) {
494 size_t current_count = aws_atomic_fetch_add_explicit(&server->ref_count, 1, aws_memory_order_relaxed);
495
496 AWS_LOGF_TRACE(
497 AWS_LS_EVENT_STREAM_RPC_SERVER,
498 "id=%p: server acquired, new ref count is %zu.",
499 (void *)server,
500 current_count + 1);
501 }
502
503 void aws_event_stream_rpc_server_listener_release(struct aws_event_stream_rpc_server_listener *server) {
504 if (!server) {
505 return;
506 }
507
508 size_t ref_count = aws_atomic_fetch_sub_explicit(&server->ref_count, 1, aws_memory_order_seq_cst);
509 AWS_LOGF_TRACE(
510 AWS_LS_EVENT_STREAM_RPC_SERVER, "id=%p: server released, new ref count is %zu.", (void *)server, ref_count - 1);
511
512 if (ref_count == 1) {
513 AWS_LOGF_INFO(AWS_LS_EVENT_STREAM_RPC_SERVER, "id=%p: ref count reached 0, destroying server", (void *)server);
514 /* the memory for this is cleaned up in the listener shutdown complete callback. */
515 aws_server_bootstrap_destroy_socket_listener(server->bootstrap, server->listener);
516 }
517 }
518
519 struct event_stream_connection_send_message_args {
520 struct aws_allocator *allocator;
521 struct aws_event_stream_message message;
522 enum aws_event_stream_rpc_message_type message_type;
523 struct aws_event_stream_rpc_server_connection *connection;
524 struct aws_event_stream_rpc_server_continuation_token *continuation;
525 aws_event_stream_rpc_server_message_flush_fn *flush_fn;
526 void *user_data;
527 bool end_stream;
528 bool terminate_connection;
529 };
530
531 static void s_on_protocol_message_written_fn(
532 struct aws_event_stream_message *message,
533 int error_code,
534 void *user_data) {
535 (void)message;
536
537 struct event_stream_connection_send_message_args *message_args = user_data;
538
539 AWS_LOGF_TRACE(
540 AWS_LS_EVENT_STREAM_RPC_SERVER,
541 "id=%p: message flushed to channel with error %s",
542 (void *)message_args->connection,
543 aws_error_debug_str(error_code));
544
545 if (message_args->message_type == AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK) {
546 AWS_LOGF_TRACE(
547 AWS_LS_EVENT_STREAM_RPC_SERVER,
548 "id=%p: connect ack message flushed to wire",
549 (void *)message_args->connection);
550 }
551
552 if (message_args->end_stream) {
553 AWS_FATAL_ASSERT(message_args->continuation && "end stream flag was set but it wasn't on a continuation");
554 AWS_LOGF_DEBUG(
555 AWS_LS_EVENT_STREAM_RPC_SERVER,
556 "id=%p: end_stream flag for continuation %p was set, closing",
557 (void *)message_args->connection,
558 (void *)message_args->continuation);
559 aws_atomic_store_int(&message_args->continuation->is_closed, 1U);
560 aws_hash_table_remove(
561 &message_args->connection->continuation_table, &message_args->continuation->stream_id, NULL, NULL);
562 }
563
564 message_args->flush_fn(error_code, message_args->user_data);
565
566 if (message_args->terminate_connection) {
567 AWS_LOGF_INFO(
568 AWS_LS_EVENT_STREAM_RPC_SERVER,
569 "id=%p: terminate connection flag was set. closing",
570 (void *)message_args->connection);
571 aws_event_stream_rpc_server_connection_close(message_args->connection, AWS_ERROR_SUCCESS);
572 }
573
574 aws_event_stream_rpc_server_connection_release(message_args->connection);
575
576 if (message_args->continuation) {
577 aws_event_stream_rpc_server_continuation_release(message_args->continuation);
578 }
579
580 aws_event_stream_message_clean_up(&message_args->message);
581 aws_mem_release(message_args->allocator, message_args);
582 }
583
584 static int s_send_protocol_message(
585 struct aws_event_stream_rpc_server_connection *connection,
586 struct aws_event_stream_rpc_server_continuation_token *continuation,
587 const struct aws_event_stream_rpc_message_args *message_args,
588 int32_t stream_id,
589 aws_event_stream_rpc_server_message_flush_fn *flush_fn,
590 void *user_data) {
591
592 size_t connect_handshake_state = aws_atomic_load_int(&connection->handshake_state);
593 AWS_LOGF_TRACE(
594 AWS_LS_EVENT_STREAM_RPC_SERVER,
595 "id=%p: connect handshake state %zu",
596 (void *)connection,
597 connect_handshake_state);
598 /* handshake step 1 is a connect message being received. Handshake 2 is the connect ack being sent.
599 * no messages other than connect and connect ack are allowed until this count reaches 2. */
600 if (connect_handshake_state != CONNECTION_HANDSHAKE_STATE_CONNECT_ACK_PROCESSED &&
601 message_args->message_type < AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK) {
602 AWS_LOGF_TRACE(
603 AWS_LS_EVENT_STREAM_RPC_SERVER,
604 "id=%p: invalid state, a message was received prior to connect handshake completion",
605 (void *)connection);
606 return aws_raise_error(AWS_ERROR_EVENT_STREAM_RPC_PROTOCOL_ERROR);
607 }
608
609 struct event_stream_connection_send_message_args *args =
610 aws_mem_calloc(connection->allocator, 1, sizeof(struct event_stream_connection_send_message_args));
611 args->allocator = connection->allocator;
612 args->user_data = user_data;
613 args->message_type = message_args->message_type;
614 args->connection = connection;
615 args->flush_fn = flush_fn;
616
617 if (continuation) {
618 args->continuation = continuation;
619 aws_event_stream_rpc_server_continuation_acquire(continuation);
620
621 if (message_args->message_flags & AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_TERMINATE_STREAM) {
622 AWS_LOGF_DEBUG(
623 AWS_LS_EVENT_STREAM_RPC_SERVER,
624 "id=%p: continuation with terminate stream flag was specified closing",
625 (void *)continuation);
626 args->end_stream = true;
627 }
628 }
629
630 if (message_args->message_type == AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK) {
631 AWS_LOGF_INFO(
632 AWS_LS_EVENT_STREAM_RPC_SERVER,
633 "id=%p: sending connect ack message, the connect handshake is completed",
634 (void *)connection);
635 aws_atomic_store_int(&connection->handshake_state, CONNECTION_HANDSHAKE_STATE_CONNECT_ACK_PROCESSED);
636
637 if (!(message_args->message_flags & AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_CONNECTION_ACCEPTED)) {
638 AWS_LOGF_DEBUG(
639 AWS_LS_EVENT_STREAM_RPC_SERVER,
640 "id=%p: connection ack was rejected closing connection",
641 (void *)connection);
642 args->terminate_connection = true;
643 }
644 }
645
646 args->flush_fn = flush_fn;
647
648 size_t headers_count = 0;
649
650 if (aws_add_size_checked(message_args->headers_count, 3, &headers_count)) {
651 AWS_LOGF_ERROR(
652 AWS_LS_EVENT_STREAM_RPC_SERVER,
653 "id=%p: integer overflow detected when using headers_count %zu",
654 (void *)connection,
655 message_args->headers_count);
656 goto args_allocated_before_failure;
657 }
658
659 struct aws_array_list headers_list;
660 AWS_ZERO_STRUCT(headers_list);
661
662 if (aws_array_list_init_dynamic(
663 &headers_list, connection->allocator, headers_count, sizeof(struct aws_event_stream_header_value_pair))) {
664 AWS_LOGF_ERROR(
665 AWS_LS_EVENT_STREAM_RPC_SERVER,
666 "id=%p: allocation of headers failed with error %s",
667 (void *)connection,
668 aws_error_debug_str(aws_last_error()));
669 goto args_allocated_before_failure;
670 }
671
672 /* since we preallocated the space for the headers, these can't fail, but we'll go ahead an assert on them just in
673 * case */
674 for (size_t i = 0; i < message_args->headers_count; ++i) {
675 AWS_FATAL_ASSERT(!aws_array_list_push_back(&headers_list, &message_args->headers[i]));
676 }
677
678 AWS_FATAL_ASSERT(!aws_event_stream_add_int32_header(
679 &headers_list,
680 (const char *)aws_event_stream_rpc_message_type_name.ptr,
681 (uint8_t)aws_event_stream_rpc_message_type_name.len,
682 message_args->message_type));
683 AWS_FATAL_ASSERT(!aws_event_stream_add_int32_header(
684 &headers_list,
685 (const char *)aws_event_stream_rpc_message_flags_name.ptr,
686 (uint8_t)aws_event_stream_rpc_message_flags_name.len,
687 message_args->message_flags));
688 AWS_FATAL_ASSERT(!aws_event_stream_add_int32_header(
689 &headers_list,
690 (const char *)aws_event_stream_rpc_stream_id_name.ptr,
691 (uint8_t)aws_event_stream_rpc_stream_id_name.len,
692 stream_id));
693
694 int message_init_err_code =
695 aws_event_stream_message_init(&args->message, connection->allocator, &headers_list, message_args->payload);
696 aws_array_list_clean_up(&headers_list);
697
698 if (message_init_err_code) {
699 AWS_LOGF_ERROR(
700 AWS_LS_EVENT_STREAM_RPC_SERVER,
701 "id=%p: initialization of message failed with error %s",
702 (void *)connection,
703 aws_error_debug_str(aws_last_error()));
704 goto args_allocated_before_failure;
705 }
706
707 aws_event_stream_rpc_server_connection_acquire(connection);
708
709 if (aws_event_stream_channel_handler_write_message(
710 connection->event_stream_handler, &args->message, s_on_protocol_message_written_fn, args)) {
711 AWS_LOGF_ERROR(
712 AWS_LS_EVENT_STREAM_RPC_SERVER,
713 "id=%p: message send failed with error %s",
714 (void *)connection,
715 aws_error_debug_str(aws_last_error()));
716 goto message_initialized_before_failure;
717 }
718
719 return AWS_OP_SUCCESS;
720
721 message_initialized_before_failure:
722 aws_event_stream_message_clean_up(&args->message);
723
724 args_allocated_before_failure:
725 aws_mem_release(args->allocator, args);
726 aws_event_stream_rpc_server_connection_release(connection);
727
728 return AWS_OP_ERR;
729 }
730
731 int aws_event_stream_rpc_server_connection_send_protocol_message(
732 struct aws_event_stream_rpc_server_connection *connection,
733 const struct aws_event_stream_rpc_message_args *message_args,
734 aws_event_stream_rpc_server_message_flush_fn *flush_fn,
735 void *user_data) {
736 if (!aws_event_stream_rpc_server_connection_is_open(connection)) {
737 return aws_raise_error(AWS_ERROR_EVENT_STREAM_RPC_CONNECTION_CLOSED);
738 }
739
740 return s_send_protocol_message(connection, NULL, message_args, 0, flush_fn, user_data);
741 }
742
743 void *aws_event_stream_rpc_server_connection_get_user_data(struct aws_event_stream_rpc_server_connection *connection) {
744 return connection->user_data;
745 }
746
747 AWS_EVENT_STREAM_API void aws_event_stream_rpc_server_override_last_stream_id(
748 struct aws_event_stream_rpc_server_connection *connection,
749 int32_t value) {
750 connection->latest_stream_id = value;
751 }
752
753 void aws_event_stream_rpc_server_connection_close(
754 struct aws_event_stream_rpc_server_connection *connection,
755 int shutdown_error_code) {
756
757 size_t expect_open = 1U;
758 if (aws_atomic_compare_exchange_int(&connection->is_open, &expect_open, 0U)) {
759 AWS_LOGF_DEBUG(
760 AWS_LS_EVENT_STREAM_RPC_SERVER,
761 "id=%p: closing connection with error %s",
762 (void *)connection,
763 aws_error_debug_str(shutdown_error_code));
764 aws_channel_shutdown(connection->channel, shutdown_error_code);
765
766 if (!connection->bootstrap_owned) {
767 aws_hash_table_clear(&connection->continuation_table);
768 aws_event_stream_rpc_server_connection_release(connection);
769 }
770 }
771 }
772
773 bool aws_event_stream_rpc_server_continuation_is_closed(
774 struct aws_event_stream_rpc_server_continuation_token *continuation) {
775 return aws_atomic_load_int(&continuation->is_closed) == 1U;
776 }
777
778 bool aws_event_stream_rpc_server_connection_is_open(struct aws_event_stream_rpc_server_connection *connection) {
779 return aws_atomic_load_int(&connection->is_open) == 1U;
780 }
781
782 void aws_event_stream_rpc_server_continuation_acquire(
783 struct aws_event_stream_rpc_server_continuation_token *continuation) {
784 size_t current_count = aws_atomic_fetch_add_explicit(&continuation->ref_count, 1, aws_memory_order_relaxed);
785 AWS_LOGF_TRACE(
786 AWS_LS_EVENT_STREAM_RPC_SERVER,
787 "id=%p: continuation acquired, new ref count is %zu.",
788 (void *)continuation,
789 current_count + 1);
790 }
791
792 void aws_event_stream_rpc_server_continuation_release(
793 struct aws_event_stream_rpc_server_continuation_token *continuation) {
794 size_t value = aws_atomic_fetch_sub_explicit(&continuation->ref_count, 1, aws_memory_order_seq_cst);
795
796 AWS_LOGF_TRACE(
797 AWS_LS_EVENT_STREAM_RPC_SERVER,
798 "id=%p: continuation released, new ref count is %zu.",
799 (void *)continuation,
800 value - 1);
801
802 if (value == 1) {
803 AWS_LOGF_DEBUG(AWS_LS_EVENT_STREAM_RPC_SERVER, "id=%p: destroying continuation.", (void *)continuation);
804 struct aws_allocator *allocator = continuation->connection->allocator;
805 aws_event_stream_rpc_server_connection_release(continuation->connection);
806 aws_mem_release(allocator, continuation);
807 }
808 }
809
810 int aws_event_stream_rpc_server_continuation_send_message(
811 struct aws_event_stream_rpc_server_continuation_token *continuation,
812 const struct aws_event_stream_rpc_message_args *message_args,
813 aws_event_stream_rpc_server_message_flush_fn *flush_fn,
814 void *user_data) {
815 AWS_FATAL_PRECONDITION(continuation->continuation_fn);
816 AWS_FATAL_PRECONDITION(continuation->closed_fn);
817
818 if (aws_event_stream_rpc_server_continuation_is_closed(continuation)) {
819 return aws_raise_error(AWS_ERROR_EVENT_STREAM_RPC_STREAM_CLOSED);
820 }
821
822 return s_send_protocol_message(
823 continuation->connection, continuation, message_args, continuation->stream_id, flush_fn, user_data);
824 }
825
826 static void s_connection_error_message_flush_fn(int error_code, void *user_data) {
827 (void)error_code;
828
829 struct aws_event_stream_rpc_server_connection *connection = user_data;
830 aws_event_stream_rpc_server_connection_close(connection, AWS_ERROR_EVENT_STREAM_RPC_PROTOCOL_ERROR);
831 }
832
833 static void s_send_connection_level_error(
834 struct aws_event_stream_rpc_server_connection *connection,
835 uint32_t message_type,
836 uint32_t message_flags,
837 const struct aws_byte_cursor *message) {
838 struct aws_byte_buf payload_buf = aws_byte_buf_from_array(message->ptr, message->len);
839
840 AWS_LOGF_DEBUG(
841 AWS_LS_EVENT_STREAM_RPC_SERVER,
842 "id=%p: sending connection-level error\n" PRInSTR,
843 (void *)connection,
844 AWS_BYTE_BUF_PRI(payload_buf));
845
846 struct aws_event_stream_header_value_pair content_type_header =
847 aws_event_stream_create_string_header(s_json_content_type_name, s_json_content_type_value);
848
849 struct aws_event_stream_header_value_pair headers[] = {
850 content_type_header,
851 };
852
853 struct aws_event_stream_rpc_message_args message_args = {
854 .message_type = message_type,
855 .message_flags = message_flags,
856 .payload = &payload_buf,
857 .headers_count = 1,
858 .headers = headers,
859 };
860
861 aws_event_stream_rpc_server_connection_send_protocol_message(
862 connection, &message_args, s_connection_error_message_flush_fn, connection);
863 }
864
865 /* TODO: come back and make this a proper state pattern. For now it's branches all over the place until we nail
866 * down the spec. */
867 static void s_route_message_by_type(
868 struct aws_event_stream_rpc_server_connection *connection,
869 struct aws_event_stream_message *message,
870 struct aws_array_list *headers_list,
871 uint32_t stream_id,
872 uint32_t message_type,
873 uint32_t message_flags,
874 struct aws_byte_cursor operation_name) {
875 struct aws_byte_buf payload_buf = aws_byte_buf_from_array(
876 aws_event_stream_message_payload(message), aws_event_stream_message_payload_len(message));
877
878 struct aws_event_stream_rpc_message_args message_args = {
879 .headers = headers_list->data,
880 .headers_count = aws_array_list_length(headers_list),
881 .payload = &payload_buf,
882 .message_flags = message_flags,
883 .message_type = message_type,
884 };
885
886 size_t handshake_state = aws_atomic_load_int(&connection->handshake_state);
887
888 /* make sure if this is not a CONNECT message being received, the handshake has been completed. */
889 if (handshake_state < CONNECTION_HANDSHAKE_STATE_CONNECT_ACK_PROCESSED &&
890 message_type != AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT) {
891 AWS_LOGF_ERROR(
892 AWS_LS_EVENT_STREAM_RPC_SERVER,
893 "id=%p: a message was received on this connection prior to the "
894 "connect handshake completing",
895 (void *)connection);
896 aws_raise_error(AWS_ERROR_EVENT_STREAM_RPC_PROTOCOL_ERROR);
897 s_send_connection_level_error(
898 connection, AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PROTOCOL_ERROR, 0, &s_connect_not_completed_error);
899 return;
900 }
901
902 /* stream_id being non zero ALWAYS indicates APPLICATION_DATA or APPLICATION_ERROR. */
903 if (stream_id > 0) {
904 AWS_LOGF_TRACE(AWS_LS_EVENT_STREAM_RPC_SERVER, "id=%p: stream id %" PRIu32, (void *)connection, stream_id);
905
906 struct aws_event_stream_rpc_server_continuation_token *continuation = NULL;
907 if (message_type > AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_ERROR) {
908 AWS_LOGF_ERROR(
909 AWS_LS_EVENT_STREAM_RPC_SERVER,
910 "id=%p: only application messages can be sent on a stream id, "
911 "but this message is the incorrect type",
912 (void *)connection);
913 aws_raise_error(AWS_ERROR_EVENT_STREAM_RPC_PROTOCOL_ERROR);
914 s_send_connection_level_error(
915 connection, AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PROTOCOL_ERROR, 0, &s_invalid_stream_id_error);
916 return;
917 }
918
919 /* INT32_MAX is the max stream id. */
920 if (stream_id > INT32_MAX) {
921 AWS_LOGF_ERROR(
922 AWS_LS_EVENT_STREAM_RPC_SERVER,
923 "id=%p: stream_id is larger than the max acceptable value",
924 (void *)connection);
925 aws_raise_error(AWS_ERROR_EVENT_STREAM_RPC_PROTOCOL_ERROR);
926 s_send_connection_level_error(
927 connection, AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PROTOCOL_ERROR, 0, &s_invalid_stream_id_error);
928 return;
929 }
930
931 /* if the stream is in the past, look it up from the continuation table. If it's not there, that's an error.
932 * if it is, find it and notify the user a message arrived */
933 if (stream_id <= connection->latest_stream_id) {
934 AWS_LOGF_ERROR(
935 AWS_LS_EVENT_STREAM_RPC_SERVER,
936 "id=%p: stream_id is an already seen stream_id, looking for existing continuation",
937 (void *)connection);
938
939 struct aws_hash_element *continuation_element = NULL;
940 if (aws_hash_table_find(&connection->continuation_table, &stream_id, &continuation_element) ||
941 !continuation_element) {
942 if ((message_flags & AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_TERMINATE_STREAM) == 0) {
943 AWS_LOGF_ERROR(
944 AWS_LS_EVENT_STREAM_RPC_SERVER,
945 "id=%p: stream_id does not have a corresponding continuation",
946 (void *)connection);
947 aws_raise_error(AWS_ERROR_EVENT_STREAM_RPC_PROTOCOL_ERROR);
948 s_send_connection_level_error(
949 connection,
950 AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PROTOCOL_ERROR,
951 0,
952 &s_invalid_client_stream_id_error);
953 } else {
954 /* Simultaneous close can trip this condition */
955 AWS_LOGF_DEBUG(
956 AWS_LS_EVENT_STREAM_RPC_SERVER,
957 "id=%p: received a terminate stream message for stream_id %d, which no longer has a "
958 "corresponding continuation",
959 (void *)connection,
960 (int)stream_id);
961 }
962 return;
963 }
964
965 continuation = continuation_element->value;
966 AWS_LOGF_TRACE(
967 AWS_LS_EVENT_STREAM_RPC_SERVER,
968 "id=%p: stream_id corresponds to continuation %p",
969 (void *)connection,
970 (void *)continuation);
971
972 /*
973 * I don't think it's possible for the continuation_fn to be NULL at this point, but given the
974 * multiple partially-initialized object crashes we've had, let's be safe.
975 */
976 if (continuation->continuation_fn != NULL) {
977 aws_event_stream_rpc_server_continuation_acquire(continuation);
978 continuation->continuation_fn(continuation, &message_args, continuation->user_data);
979 aws_event_stream_rpc_server_continuation_release(continuation);
980 }
981 /* now these are potentially new streams. Make sure they're in bounds, create a new continuation
982 * and notify the user the stream has been created, then send them the message. */
983 } else {
984 AWS_LOGF_TRACE(
985 AWS_LS_EVENT_STREAM_RPC_SERVER,
986 "id=%p: stream_id is unknown, attempting to create a continuation for it",
987 (void *)connection);
988 if (stream_id != connection->latest_stream_id + 1) {
989 AWS_LOGF_ERROR(
990 AWS_LS_EVENT_STREAM_RPC_SERVER,
991 "id=%p: stream_id is invalid because it's not sequentially increasing",
992 (void *)connection);
993
994 aws_raise_error(AWS_ERROR_EVENT_STREAM_RPC_PROTOCOL_ERROR);
995 s_send_connection_level_error(
996 connection,
997 AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PROTOCOL_ERROR,
998 0,
999 &s_invalid_new_client_stream_id_error);
1000 return;
1001 }
1002
1003 /* new streams must always have an operation name. */
1004 if (operation_name.len == 0) {
1005 AWS_LOGF_ERROR(
1006 AWS_LS_EVENT_STREAM_RPC_SERVER,
1007 "id=%p: new stream_id encountered, but an operation name was not received",
1008 (void *)connection);
1009 aws_raise_error(AWS_ERROR_EVENT_STREAM_RPC_PROTOCOL_ERROR);
1010 s_send_connection_level_error(
1011 connection, AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PROTOCOL_ERROR, 0, &s_missing_operation_name_error);
1012 return;
1013 }
1014
1015 AWS_LOGF_DEBUG(
1016 AWS_LS_EVENT_STREAM_RPC_SERVER,
1017 "id=%p: stream_id is a valid new stream. Creating continuation",
1018 (void *)connection);
1019 continuation =
1020 aws_mem_calloc(connection->allocator, 1, sizeof(struct aws_event_stream_rpc_server_continuation_token));
1021 if (!continuation) {
1022 AWS_LOGF_ERROR(
1023 AWS_LS_EVENT_STREAM_RPC_SERVER,
1024 "id=%p: continuation allocation failed with error %s",
1025 (void *)connection,
1026 aws_error_debug_str(aws_last_error()));
1027 s_send_connection_level_error(
1028 connection, AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_INTERNAL_ERROR, 0, &s_internal_error);
1029 return;
1030 }
1031
1032 AWS_LOGF_DEBUG(
1033 AWS_LS_EVENT_STREAM_RPC_SERVER,
1034 "id=%p: new continuation is %p",
1035 (void *)connection,
1036 (void *)continuation);
1037
1038 continuation->stream_id = stream_id;
1039 continuation->connection = connection;
1040 aws_event_stream_rpc_server_connection_acquire(continuation->connection);
1041 aws_atomic_init_int(&continuation->ref_count, 1);
1042
1043 if (aws_hash_table_put(&connection->continuation_table, &continuation->stream_id, continuation, NULL)) {
1044 AWS_LOGF_ERROR(
1045 AWS_LS_EVENT_STREAM_RPC_SERVER,
1046 "id=%p: continuation table update failed with error %s",
1047 (void *)connection,
1048 aws_error_debug_str(aws_last_error()));
1049 /* continuation release will drop the connection reference as well */
1050 aws_event_stream_rpc_server_continuation_release(continuation);
1051 s_send_connection_level_error(
1052 connection, AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_INTERNAL_ERROR, 0, &s_internal_error);
1053 return;
1054 }
1055
1056 struct aws_event_stream_rpc_server_stream_continuation_options options;
1057 AWS_ZERO_STRUCT(options);
1058
1059 aws_event_stream_rpc_server_continuation_acquire(continuation);
1060 AWS_LOGF_TRACE(
1061 AWS_LS_EVENT_STREAM_RPC_SERVER, "id=%p: invoking on_incoming_stream callback", (void *)connection);
1062 /*
1063 * This callback must only keep a ref to the continuation on a success path. On a failure, it must
1064 * leave the ref count alone so that the release + removal destroys the continuation
1065 */
1066 if (connection->on_incoming_stream == NULL ||
1067 connection->on_incoming_stream(
1068 continuation->connection, continuation, operation_name, &options, connection->user_data)) {
1069
1070 AWS_FATAL_ASSERT(aws_atomic_load_int(&continuation->ref_count) == 2);
1071
1072 /* undo the continuation acquire that was done a few lines above */
1073 aws_event_stream_rpc_server_continuation_release(continuation);
1074
1075 /* removing the continuation from the table will do the final decref on the continuation */
1076 aws_hash_table_remove(&connection->continuation_table, &continuation->stream_id, NULL, NULL);
1077 s_send_connection_level_error(
1078 connection, AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_INTERNAL_ERROR, 0, &s_internal_error);
1079 return;
1080 }
1081 AWS_FATAL_ASSERT(options.on_continuation);
1082 AWS_FATAL_ASSERT(options.on_continuation_closed);
1083
1084 continuation->continuation_fn = options.on_continuation;
1085 continuation->closed_fn = options.on_continuation_closed;
1086 continuation->user_data = options.user_data;
1087
1088 connection->latest_stream_id = stream_id;
1089 continuation->continuation_fn(continuation, &message_args, continuation->user_data);
1090
1091 /* undo the acquire made before the on_incoming_stream callback invocation */
1092 aws_event_stream_rpc_server_continuation_release(continuation);
1093 }
1094
1095 /* if it was a terminal stream message purge it from the hash table. The delete will decref the continuation. */
1096 if (message_flags & AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_TERMINATE_STREAM) {
1097 AWS_LOGF_DEBUG(
1098 AWS_LS_EVENT_STREAM_RPC_SERVER,
1099 "id=%p: the terminate_stream flag was received for continuation %p, closing",
1100 (void *)connection,
1101 (void *)continuation);
1102 aws_atomic_store_int(&continuation->is_closed, 1U);
1103 aws_hash_table_remove(&connection->continuation_table, &stream_id, NULL, NULL);
1104 }
1105 } else {
1106 if (message_type <= AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_ERROR ||
1107 message_type >= AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_COUNT) {
1108 AWS_LOGF_ERROR(
1109 AWS_LS_EVENT_STREAM_RPC_SERVER,
1110 "id=%p: a zero stream id was received with an invalid message-type %" PRIu32,
1111 (void *)connection,
1112 message_type);
1113 s_send_connection_level_error(
1114 connection, AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PROTOCOL_ERROR, 0, &s_invalid_message_type_error);
1115 return;
1116 }
1117
1118 if (message_type == AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT) {
1119 if (handshake_state) {
1120 AWS_LOGF_ERROR(
1121 AWS_LS_EVENT_STREAM_RPC_SERVER,
1122 "id=%p: connect received but the handshake is already completed. Only one is allowed.",
1123 (void *)connection);
1124 /* only one connect is allowed. This would be a duplicate. */
1125 s_send_connection_level_error(
1126 connection, AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PROTOCOL_ERROR, 0, &s_connect_not_completed_error);
1127 return;
1128 }
1129 aws_atomic_store_int(&connection->handshake_state, CONNECTION_HANDSHAKE_STATE_CONNECT_PROCESSED);
1130 AWS_LOGF_INFO(
1131 AWS_LS_EVENT_STREAM_RPC_SERVER,
1132 "id=%p: connect received, connection handshake completion pending the server sending an ack.",
1133 (void *)connection);
1134 }
1135
1136 if (connection->on_connection_protocol_message != NULL) {
1137 connection->on_connection_protocol_message(connection, &message_args, connection->user_data);
1138 }
1139 }
1140 }
1141
1142 /* invoked by the event stream channel handler when a complete message has been read from the channel. */
1143 static void s_on_message_received(struct aws_event_stream_message *message, int error_code, void *user_data) {
1144
1145 if (!error_code) {
1146 struct aws_event_stream_rpc_server_connection *connection = user_data;
1147 AWS_LOGF_TRACE(
1148 AWS_LS_EVENT_STREAM_RPC_SERVER,
1149 "id=%p: message received on connection of length %" PRIu32,
1150 (void *)connection,
1151 aws_event_stream_message_total_length(message));
1152
1153 struct aws_array_list headers;
1154 if (aws_array_list_init_dynamic(
1155 &headers, connection->allocator, 8, sizeof(struct aws_event_stream_header_value_pair))) {
1156 AWS_LOGF_ERROR(
1157 AWS_LS_EVENT_STREAM_RPC_SERVER,
1158 "id=%p: error initializing headers %s",
1159 (void *)connection,
1160 aws_error_debug_str(aws_last_error()));
1161 s_send_connection_level_error(
1162 connection, AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_INTERNAL_ERROR, 0, &s_internal_error);
1163 return;
1164 }
1165
1166 if (aws_event_stream_message_headers(message, &headers)) {
1167 AWS_LOGF_ERROR(
1168 AWS_LS_EVENT_STREAM_RPC_SERVER,
1169 "id=%p: error fetching headers %s",
1170 (void *)connection,
1171 aws_error_debug_str(aws_last_error()));
1172 s_send_connection_level_error(
1173 connection, AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_INTERNAL_ERROR, 0, &s_internal_error);
1174 goto clean_up;
1175 }
1176
1177 int32_t stream_id = -1;
1178 int32_t message_type = -1;
1179 int32_t message_flags = -1;
1180
1181 struct aws_byte_buf operation_name_buf;
1182 AWS_ZERO_STRUCT(operation_name_buf);
1183 if (aws_event_stream_rpc_extract_message_metadata(
1184 &headers, &stream_id, &message_type, &message_flags, &operation_name_buf)) {
1185 AWS_LOGF_ERROR(
1186 AWS_LS_EVENT_STREAM_RPC_SERVER,
1187 "id=%p: invalid protocol message with error %s",
1188 (void *)connection,
1189 aws_error_debug_str(aws_last_error()));
1190 s_send_connection_level_error(
1191 connection, AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PROTOCOL_ERROR, 0, &s_invalid_message_error);
1192 goto clean_up;
1193 }
1194
1195 AWS_LOGF_TRACE(AWS_LS_EVENT_STREAM_RPC_SERVER, "id=%p: routing message", (void *)connection);
1196
1197 s_route_message_by_type(
1198 connection,
1199 message,
1200 &headers,
1201 stream_id,
1202 message_type,
1203 message_flags,
1204 aws_byte_cursor_from_buf(&operation_name_buf));
1205
1206 clean_up:
1207 aws_event_stream_headers_list_cleanup(&headers);
1208 }
1209 }