| 1 | #ifndef AWS_MQTT_MQTT5_CLIENT_IMPL_H |
| 2 | #define AWS_MQTT_MQTT5_CLIENT_IMPL_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | |
| 9 | #include <aws/mqtt/mqtt.h> |
| 10 | |
| 11 | #include <aws/common/hash_table.h> |
| 12 | #include <aws/common/mutex.h> |
| 13 | #include <aws/common/ref_count.h> |
| 14 | #include <aws/io/channel.h> |
| 15 | #include <aws/mqtt/private/v5/mqtt5_callbacks.h> |
| 16 | #include <aws/mqtt/private/v5/mqtt5_decoder.h> |
| 17 | #include <aws/mqtt/private/v5/mqtt5_encoder.h> |
| 18 | #include <aws/mqtt/private/v5/mqtt5_topic_alias.h> |
| 19 | #include <aws/mqtt/private/v5/rate_limiters.h> |
| 20 | #include <aws/mqtt/v5/mqtt5_types.h> |
| 21 | |
| 22 | struct aws_event_loop; |
| 23 | struct aws_http_message; |
| 24 | struct aws_http_proxy_options; |
| 25 | struct aws_mqtt5_client_options_storage; |
| 26 | struct aws_mqtt5_operation; |
| 27 | struct aws_websocket_client_connection_options; |
| 28 | |
| 29 | /** |
| 30 | * The various states that the client can be in. A client has both a current state and a desired state. |
| 31 | * Desired state is only allowed to be one of {STOPPED, CONNECTED, TERMINATED}. The client transitions states |
| 32 | * based on either |
| 33 | * (1) changes in desired state, or |
| 34 | * (2) external events. |
| 35 | * |
| 36 | * Most states are interruptible (in the sense of a change in desired state causing an immediate change in state) but |
| 37 | * CONNECTING and CHANNEL_SHUTDOWN cannot be interrupted due to waiting for an asynchronous callback (that has no |
| 38 | * cancel) to complete. |
| 39 | */ |
| 40 | enum aws_mqtt5_client_state { |
| 41 | |
| 42 | /* |
| 43 | * The client is not connected and not waiting for anything to happen. |
| 44 | * |
| 45 | * Next States: |
| 46 | * CONNECTING - if the user invokes Start() on the client |
| 47 | * TERMINATED - if the user releases the last ref count on the client |
| 48 | */ |
| 49 | AWS_MCS_STOPPED, |
| 50 | |
| 51 | /* |
| 52 | * The client is attempting to connect to a remote endpoint, and is waiting for channel setup to complete. This |
| 53 | * state is not interruptible by any means other than channel setup completion. |
| 54 | * |
| 55 | * Next States: |
| 56 | * MQTT_CONNECT - if the channel completes setup with no error and desired state is still CONNECTED |
| 57 | * CHANNEL_SHUTDOWN - if the channel completes setup with no error, but desired state is not CONNECTED |
| 58 | * PENDING_RECONNECT - if the channel fails to complete setup and desired state is still CONNECTED |
| 59 | * STOPPED - if the channel fails to complete setup and desired state is not CONNECTED |
| 60 | */ |
| 61 | AWS_MCS_CONNECTING, |
| 62 | |
| 63 | /* |
| 64 | * The client is sending a CONNECT packet and waiting on a CONNACK packet. |
| 65 | * |
| 66 | * Next States: |
| 67 | * CONNECTED - if a successful CONNACK is received and desired state is still CONNECTED |
| 68 | * CHANNEL_SHUTDOWN - On send/encode errors, read/decode errors, unsuccessful CONNACK, timeout to receive |
| 69 | * CONNACK, desired state is no longer CONNECTED |
| 70 | * PENDING_RECONNECT - unexpected channel shutdown completion and desired state still CONNECTED |
| 71 | * STOPPED - unexpected channel shutdown completion and desired state no longer CONNECTED |
| 72 | */ |
| 73 | AWS_MCS_MQTT_CONNECT, |
| 74 | |
| 75 | /* |
| 76 | * The client is ready to perform user-requested mqtt operations. |
| 77 | * |
| 78 | * Next States: |
| 79 | * CHANNEL_SHUTDOWN - On send/encode errors, read/decode errors, DISCONNECT packet received, desired state |
| 80 | * no longer CONNECTED, PINGRESP timeout |
| 81 | * PENDING_RECONNECT - unexpected channel shutdown completion and desired state still CONNECTED |
| 82 | * STOPPED - unexpected channel shutdown completion and desired state no longer CONNECTED |
| 83 | */ |
| 84 | AWS_MCS_CONNECTED, |
| 85 | |
| 86 | /* |
| 87 | * The client is attempt to shut down a connection cleanly by finishing the current operation and then |
| 88 | * transmitting an outbound DISCONNECT. |
| 89 | * |
| 90 | * Next States: |
| 91 | * CHANNEL_SHUTDOWN - on successful (or unsuccessful) send of the DISCONNECT |
| 92 | * PENDING_RECONNECT - unexpected channel shutdown completion and desired state still CONNECTED |
| 93 | * STOPPED - unexpected channel shutdown completion and desired state no longer CONNECTED |
| 94 | */ |
| 95 | AWS_MCS_CLEAN_DISCONNECT, |
| 96 | |
| 97 | /* |
| 98 | * The client is waiting for the io channel to completely shut down. This state is not interruptible. |
| 99 | * |
| 100 | * Next States: |
| 101 | * PENDING_RECONNECT - the io channel has shut down and desired state is still CONNECTED |
| 102 | * STOPPED - the io channel has shut down and desired state is not CONNECTED |
| 103 | */ |
| 104 | AWS_MCS_CHANNEL_SHUTDOWN, |
| 105 | |
| 106 | /* |
| 107 | * The client is waiting for the reconnect timer to expire before attempting to connect again. |
| 108 | * |
| 109 | * Next States: |
| 110 | * CONNECTING - the reconnect timer has expired and desired state is still CONNECTED |
| 111 | * STOPPED - desired state is no longer CONNECTED |
| 112 | */ |
| 113 | AWS_MCS_PENDING_RECONNECT, |
| 114 | |
| 115 | /* |
| 116 | * The client is performing final shutdown and release of all resources. This state is only realized for |
| 117 | * a non-observable instant of time (transition out of STOPPED). |
| 118 | */ |
| 119 | AWS_MCS_TERMINATED, |
| 120 | }; |
| 121 | |
| 122 | /** |
| 123 | * Table of overridable external functions to allow mocking and monitoring of the client. |
| 124 | */ |
| 125 | struct aws_mqtt5_client_vtable { |
| 126 | /* aws_high_res_clock_get_ticks */ |
| 127 | uint64_t (*get_current_time_fn)(void); |
| 128 | |
| 129 | /* aws_channel_shutdown */ |
| 130 | int (*channel_shutdown_fn)(struct aws_channel *channel, int error_code); |
| 131 | |
| 132 | /* aws_websocket_client_connect */ |
| 133 | int (*websocket_connect_fn)(const struct aws_websocket_client_connection_options *options); |
| 134 | |
| 135 | /* aws_client_bootstrap_new_socket_channel */ |
| 136 | int (*client_bootstrap_new_socket_channel_fn)(struct aws_socket_channel_bootstrap_options *options); |
| 137 | |
| 138 | /* aws_http_proxy_new_socket_channel */ |
| 139 | int (*http_proxy_new_socket_channel_fn)( |
| 140 | struct aws_socket_channel_bootstrap_options *channel_options, |
| 141 | const struct aws_http_proxy_options *proxy_options); |
| 142 | |
| 143 | /* This doesn't replace anything, it's just for test verification of state changes */ |
| 144 | void (*on_client_state_change_callback_fn)( |
| 145 | struct aws_mqtt5_client *client, |
| 146 | enum aws_mqtt5_client_state old_state, |
| 147 | enum aws_mqtt5_client_state new_state, |
| 148 | void *vtable_user_data); |
| 149 | |
| 150 | /* This doesn't replace anything, it's just for test verification of statistic changes */ |
| 151 | void (*on_client_statistics_changed_callback_fn)( |
| 152 | struct aws_mqtt5_client *client, |
| 153 | struct aws_mqtt5_operation *operation, |
| 154 | void *vtable_user_data); |
| 155 | |
| 156 | /* aws_channel_acquire_message_from_pool */ |
| 157 | struct aws_io_message *(*aws_channel_acquire_message_from_pool_fn)( |
| 158 | struct aws_channel *channel, |
| 159 | enum aws_io_message_type message_type, |
| 160 | size_t size_hint, |
| 161 | void *user_data); |
| 162 | |
| 163 | /* aws_channel_slot_send_message */ |
| 164 | int (*aws_channel_slot_send_message_fn)( |
| 165 | struct aws_channel_slot *slot, |
| 166 | struct aws_io_message *message, |
| 167 | enum aws_channel_direction dir, |
| 168 | void *user_data); |
| 169 | |
| 170 | void *vtable_user_data; |
| 171 | }; |
| 172 | |
| 173 | /* |
| 174 | * In order to make it easier to guarantee the lifecycle events are properly paired and emitted, we track |
| 175 | * a separate state (from aws_mqtt5_client_state) and emit lifecycle events based on it. |
| 176 | * |
| 177 | * For example, if our lifecycle event is state CONNECTING, than anything going wrong becomes a CONNECTION_FAILED event |
| 178 | * whereas if we were in CONNECTED, it must be a DISCONNECTED event. By setting the state to NONE after emitting |
| 179 | * a CONNECTION_FAILED or DISCONNECTED event, then emission spots further down the execution pipeline will not |
| 180 | * accidentally emit an additional event. This also allows us to emit immediately when an event happens, if |
| 181 | * appropriate, without having to persist additional event data (like packet views) until some singular point. |
| 182 | * |
| 183 | * For example: |
| 184 | * |
| 185 | * If I'm in CONNECTING and the channel shuts down, I want to emit a CONNECTION_FAILED event with the error code. |
| 186 | * If I'm in CONNECTING and I receive a failed CONNACK, I want to emit a CONNECTION_FAILED event immediately with |
| 187 | * the CONNACK view in it and then invoke channel shutdown (and channel shutdown completing later should not emit an |
| 188 | * event). |
| 189 | * If I'm in CONNECTED and the channel shuts down, I want to emit a DISCONNECTED event with the error code. |
| 190 | * If I'm in CONNECTED and get a DISCONNECT packet from the server, I want to emit a DISCONNECTED event with |
| 191 | * the DISCONNECT packet in it, invoke channel shutdown, and then I *don't* want to emit a DISCONNECTED event |
| 192 | * when the channel finishes shutting down. |
| 193 | */ |
| 194 | enum aws_mqtt5_lifecycle_state { |
| 195 | AWS_MQTT5_LS_NONE, |
| 196 | AWS_MQTT5_LS_CONNECTING, |
| 197 | AWS_MQTT5_LS_CONNECTED, |
| 198 | }; |
| 199 | |
| 200 | /* |
| 201 | * Operation-related state notes |
| 202 | * |
| 203 | * operation flow: |
| 204 | * (qos 0 publish, disconnect, connect) |
| 205 | * user (via cross thread task) -> |
| 206 | * queued_operations -> (on front of queue) |
| 207 | * current_operation -> (on completely encoded and passed to next handler) |
| 208 | * write_completion_operations -> (on socket write complete) |
| 209 | * release |
| 210 | * |
| 211 | * (qos 1+ publish, sub/unsub) |
| 212 | * user (via cross thread task) -> |
| 213 | * queued_operations -> (on front of queue) |
| 214 | * current_operation (allocate packet id if necessary) -> (on completely encoded and passed to next handler) |
| 215 | * unacked_operations && unacked_operations_table -> (on ack received) |
| 216 | * release |
| 217 | * |
| 218 | * QoS 1+ requires both a table and a list holding the same operations in order to support fast lookups by |
| 219 | * mqtt packet id and in-order re-queueing in the case of a disconnection (required by spec) |
| 220 | * |
| 221 | * On Qos 1 PUBLISH completely received (and final callback invoked): |
| 222 | * Add PUBACK at head of queued_operations |
| 223 | * |
| 224 | * On disconnect (on transition to PENDING_RECONNECT or STOPPED): |
| 225 | * If current_operation, move current_operation to head of queued_operations |
| 226 | * Fail all operations in the pending write completion list |
| 227 | * Fail, remove, and release operations in queued_operations where |
| 228 | * (1) They fail the offline queue policy OR |
| 229 | * (2) They are a PUBACK, PINGREQ, or DISCONNECT |
| 230 | * Fail, remove, and release unacked_operations if: |
| 231 | * (1) They fail the offline queue policy AND |
| 232 | * (2) operation is not Qos 1+ publish |
| 233 | * |
| 234 | * On reconnect (post CONNACK): |
| 235 | * if rejoined_session: |
| 236 | * Move-and-append all non-qos1+-publishes in unacked_operations to the front of queued_operations |
| 237 | * Move-and-append remaining operations (qos1+ publishes) to the front of queued_operations |
| 238 | * else: |
| 239 | * Fail, remove, and release unacked_operations that fail the offline queue policy |
| 240 | * Move and append unacked operations to front of queued_operations |
| 241 | * |
| 242 | * Clear unacked_operations_table |
| 243 | */ |
| 244 | struct aws_mqtt5_client_operational_state { |
| 245 | |
| 246 | /* back pointer to the client */ |
| 247 | struct aws_mqtt5_client *client; |
| 248 | |
| 249 | /* |
| 250 | * One more than the most recently used packet id. This is the best starting point for a forward search through |
| 251 | * the id space for a free id. |
| 252 | */ |
| 253 | aws_mqtt5_packet_id_t next_mqtt_packet_id; |
| 254 | |
| 255 | /* |
| 256 | * One more than the most recently used control packet id. |
| 257 | */ |
| 258 | uint64_t next_mqtt5_pub_ack_control_id; |
| 259 | /* These tables are used for fast search of a packet_id and control_id pair. They both set an |
| 260 | * aws_mqtt5_manual_pub_ack_entry as its value */ |
| 261 | struct aws_hash_table manual_pub_ack_packet_id_table; |
| 262 | struct aws_hash_table manual_pub_ack_control_id_table; |
| 263 | /* This table contains control ids known to be cancelled due to client disconnect for better failure messaging */ |
| 264 | struct aws_hash_table manual_pub_ack_cancelled_control_id_table; |
| 265 | |
| 266 | struct aws_linked_list queued_operations; |
| 267 | struct aws_mqtt5_operation *current_operation; |
| 268 | struct aws_hash_table unacked_operations_table; |
| 269 | struct aws_linked_list unacked_operations; |
| 270 | struct aws_linked_list write_completion_operations; |
| 271 | |
| 272 | /* |
| 273 | * heap of operation pointers where the timeout is the sort value. Elements are added/removed from this |
| 274 | * data structure in exact synchronization with unacked_operations_table. |
| 275 | */ |
| 276 | struct aws_priority_queue operations_by_ack_timeout; |
| 277 | |
| 278 | /* |
| 279 | * Is there an io message in transit (to the socket) that has not invoked its write completion callback yet? |
| 280 | * The client implementation only allows one in-transit message at a time, and so if this is true, we don't |
| 281 | * send additional ones/ |
| 282 | */ |
| 283 | bool pending_write_completion; |
| 284 | }; |
| 285 | |
| 286 | /* |
| 287 | * State related to flow-control rules for the mqtt5 client |
| 288 | * |
| 289 | * Includes: |
| 290 | * (1) Mqtt5 ReceiveMaximum support |
| 291 | * (2) AWS IoT Core limit support: |
| 292 | * (a) Publish TPS rate limit |
| 293 | * (b) Total outbound throughput limit |
| 294 | */ |
| 295 | struct aws_mqtt5_client_flow_control_state { |
| 296 | |
| 297 | /* |
| 298 | * Mechanically follows the mqtt5 suggested implementation: |
| 299 | * |
| 300 | * Starts at the server's receive maximum. |
| 301 | * 1. Decrement every time we send a QoS1+ publish |
| 302 | * 2. Increment every time we receive a PUBACK |
| 303 | * |
| 304 | * Qos1+ publishes (and all operations behind them in the queue) are blocked while this value is zero. |
| 305 | * |
| 306 | * Qos 2 support will require additional work here to match the spec. |
| 307 | */ |
| 308 | uint32_t unacked_publish_token_count; |
| 309 | |
| 310 | /* |
| 311 | * Optional throttle (extended validation) that prevents the client from exceeding Iot Core's default throughput |
| 312 | * limit |
| 313 | */ |
| 314 | struct aws_rate_limiter_token_bucket throughput_throttle; |
| 315 | |
| 316 | /* |
| 317 | * Optional throttle (extended validation) that prevents the client from exceeding Iot Core's default publish |
| 318 | * rate limit. |
| 319 | */ |
| 320 | struct aws_rate_limiter_token_bucket publish_throttle; |
| 321 | }; |
| 322 | |
| 323 | /** |
| 324 | * Contains some simple statistics about the current state of the client's queue of operations |
| 325 | */ |
| 326 | struct aws_mqtt5_client_operation_statistics_impl { |
| 327 | /* |
| 328 | * total number of operations submitted to the client that have not yet been completed. Unacked operations |
| 329 | * are a subset of this. |
| 330 | */ |
| 331 | struct aws_atomic_var incomplete_operation_count_atomic; |
| 332 | |
| 333 | /* |
| 334 | * total packet size of operations submitted to the client that have not yet been completed. Unacked operations |
| 335 | * are a subset of this. |
| 336 | */ |
| 337 | struct aws_atomic_var incomplete_operation_size_atomic; |
| 338 | |
| 339 | /* |
| 340 | * total number of operations that have been sent to the server and are waiting for a corresponding ACK before |
| 341 | * they can be completed. |
| 342 | */ |
| 343 | struct aws_atomic_var unacked_operation_count_atomic; |
| 344 | |
| 345 | /* |
| 346 | * total packet size of operations that have been sent to the server and are waiting for a corresponding ACK before |
| 347 | * they can be completed. |
| 348 | */ |
| 349 | struct aws_atomic_var unacked_operation_size_atomic; |
| 350 | }; |
| 351 | |
| 352 | struct aws_mqtt5_client { |
| 353 | |
| 354 | struct aws_allocator *allocator; |
| 355 | struct aws_ref_count ref_count; |
| 356 | |
| 357 | const struct aws_mqtt5_client_vtable *vtable; |
| 358 | |
| 359 | /* |
| 360 | * Client configuration |
| 361 | */ |
| 362 | struct aws_mqtt5_client_options_storage *config; |
| 363 | |
| 364 | /* |
| 365 | * The recurrent task that runs all client logic outside of external event callbacks. Bound to the client's |
| 366 | * event loop. |
| 367 | */ |
| 368 | struct aws_task service_task; |
| 369 | |
| 370 | /* |
| 371 | * Tracks when the client's service task is next schedule to run. Is zero if the task is not scheduled to run or |
| 372 | * we are in the middle of a service (so technically not scheduled too). |
| 373 | */ |
| 374 | uint64_t next_service_task_run_time; |
| 375 | |
| 376 | /* |
| 377 | * True if the client's service task is running. Used to skip service task reevaluation due to state changes |
| 378 | * while running the service task. Reevaluation will occur at the very end of the service. |
| 379 | */ |
| 380 | bool in_service; |
| 381 | |
| 382 | /* |
| 383 | * The final mqtt5 settings negotiated between defaults, CONNECT, and CONNACK. Only valid while in |
| 384 | * CONNECTED or CLEAN_DISCONNECT states. |
| 385 | */ |
| 386 | struct aws_mqtt5_negotiated_settings negotiated_settings; |
| 387 | |
| 388 | /* |
| 389 | * Event loop all the client's connections and any related tasks will be pinned to, ensuring serialization and |
| 390 | * concurrency safety. |
| 391 | */ |
| 392 | struct aws_event_loop *loop; |
| 393 | |
| 394 | /* Channel handler information */ |
| 395 | struct aws_channel_handler handler; |
| 396 | struct aws_channel_slot *slot; |
| 397 | |
| 398 | /* |
| 399 | * What state is the client working towards? |
| 400 | */ |
| 401 | enum aws_mqtt5_client_state desired_state; |
| 402 | |
| 403 | /* |
| 404 | * What is the client's current state? |
| 405 | */ |
| 406 | enum aws_mqtt5_client_state current_state; |
| 407 | |
| 408 | /* |
| 409 | * The client's lifecycle state. Used to correctly emit lifecycle events in spite of the complicated |
| 410 | * async execution pathways that are possible. |
| 411 | */ |
| 412 | enum aws_mqtt5_lifecycle_state lifecycle_state; |
| 413 | |
| 414 | /* |
| 415 | * The client's MQTT packet encoder |
| 416 | */ |
| 417 | struct aws_mqtt5_encoder encoder; |
| 418 | |
| 419 | /* |
| 420 | * The client's MQTT packet decoder |
| 421 | */ |
| 422 | struct aws_mqtt5_decoder decoder; |
| 423 | |
| 424 | /* |
| 425 | * Cache of inbound topic aliases |
| 426 | */ |
| 427 | struct aws_mqtt5_inbound_topic_alias_resolver inbound_topic_alias_resolver; |
| 428 | |
| 429 | /* |
| 430 | * Cache of outbound topic aliases |
| 431 | */ |
| 432 | struct aws_mqtt5_outbound_topic_alias_resolver *outbound_topic_alias_resolver; |
| 433 | |
| 434 | /* |
| 435 | * Temporary state-related data. |
| 436 | * |
| 437 | * clean_disconnect_error_code - the CLEAN_DISCONNECT state takes time to complete and we want to be able |
| 438 | * to pass an error code from a prior event to the channel shutdown. This holds the "override" error code |
| 439 | * that we'd like to shut down the channel with while CLEAN_DISCONNECT is processed. |
| 440 | * |
| 441 | * handshake exists on websocket-configured clients between the transform completion timepoint and the |
| 442 | * websocket setup callback. |
| 443 | */ |
| 444 | int clean_disconnect_error_code; |
| 445 | struct aws_http_message *handshake; |
| 446 | |
| 447 | /* |
| 448 | * Wraps all state related to pending and in-progress MQTT operations within the client. |
| 449 | */ |
| 450 | struct aws_mqtt5_client_operational_state operational_state; |
| 451 | |
| 452 | /* Statistics tracking operational state */ |
| 453 | struct aws_mqtt5_client_operation_statistics_impl operation_statistics_impl; |
| 454 | |
| 455 | /* |
| 456 | * Wraps all state related to outbound flow control. |
| 457 | */ |
| 458 | struct aws_mqtt5_client_flow_control_state flow_control_state; |
| 459 | |
| 460 | /* |
| 461 | * Manages notification listener chains for lifecycle events and incoming publishes |
| 462 | */ |
| 463 | struct aws_mqtt5_callback_set_manager callback_manager; |
| 464 | |
| 465 | /* |
| 466 | * When should the next PINGREQ be sent? |
| 467 | */ |
| 468 | uint64_t next_ping_time; |
| 469 | |
| 470 | /* |
| 471 | * When should we shut down the channel due to failure to receive a PINGRESP? Only non-zero when an outstanding |
| 472 | * PINGREQ has not been answered. |
| 473 | */ |
| 474 | uint64_t next_ping_timeout_time; |
| 475 | |
| 476 | /* |
| 477 | * When should the client next attempt to reconnect? Only used by PENDING_RECONNECT state. |
| 478 | */ |
| 479 | uint64_t next_reconnect_time_ns; |
| 480 | |
| 481 | /* |
| 482 | * How many consecutive reconnect failures have we experienced? |
| 483 | */ |
| 484 | uint64_t reconnect_count; |
| 485 | |
| 486 | /* |
| 487 | * How much should we wait before our next reconnect attempt? |
| 488 | */ |
| 489 | uint64_t current_reconnect_delay_ms; |
| 490 | |
| 491 | /* |
| 492 | * When should the client reset current_reconnect_delay_interval_ms to the minimum value? Only relevant to the |
| 493 | * CONNECTED state. |
| 494 | */ |
| 495 | uint64_t next_reconnect_delay_reset_time_ns; |
| 496 | |
| 497 | /* |
| 498 | * When should we shut down the channel due to failure to receive a CONNACK? Only relevant during the MQTT_CONNECT |
| 499 | * state. |
| 500 | */ |
| 501 | uint64_t next_mqtt_connect_packet_timeout_time; |
| 502 | |
| 503 | /* |
| 504 | * Starts false and set to true as soon as a successful connection is established. If the session resumption |
| 505 | * behavior is AWS_MQTT5_CSBT_REJOIN_POST_SUCCESS then this must be true before the client sends CONNECT packets |
| 506 | * with clean start set to false. |
| 507 | */ |
| 508 | bool has_connected_successfully; |
| 509 | |
| 510 | /* |
| 511 | * A flag that allows in-thread observers (currently the mqtt3_to_5 adapter) to signal that the connection |
| 512 | * should be torn down and re-established. Only relevant to the CONNECTING state which is not interruptible: |
| 513 | * |
| 514 | * If the mqtt5 client is in the CONNECTING state (ie waiting for bootstrap to complete) and the 3-adapter |
| 515 | * is asked to connect, then we *MUST* discard the in-progress connection attempt in order to guarantee the |
| 516 | * connection we establish uses all of the configuration parameters that are passed during the mqtt3 API's connect |
| 517 | * call (host, port, tls options, socket options, etc...). Since we can't interrupt the CONNECTING state, we |
| 518 | * instead set a flag that tells the mqtt5 client to tear down the connection as soon as the initial bootstrap |
| 519 | * completes. The reconnect will establish the requested connection using the parameters passed to |
| 520 | * the mqtt3 API. |
| 521 | * |
| 522 | * Rather than try and catch every escape path from CONNECTING, we lazily reset this flag to false when we |
| 523 | * enter the CONNECTING state. On a similar note, we only check this flag as we transition to MQTT_CONNECT. |
| 524 | * |
| 525 | * This flag is ultimately only needed when the 3 adapter and 5 client are used out-of-sync. If you use the |
| 526 | * 3 adapter exclusively after 5 client creation, it never comes into play. |
| 527 | * |
| 528 | * Even the adapter shouldn't manipulate this directly. Instead, use the aws_mqtt5_client_reset_connection private |
| 529 | * API to tear down an in-progress or established connection in response to a connect() request on the adapter. |
| 530 | */ |
| 531 | bool should_reset_connection; |
| 532 | }; |
| 533 | |
| 534 | AWS_EXTERN_C_BEGIN |
| 535 | |
| 536 | /* |
| 537 | * A number of private APIs which are either set up for mocking parts of the client or testing subsystems within it by |
| 538 | * exposing what would normally be static functions internal to the implementation. |
| 539 | */ |
| 540 | |
| 541 | /* |
| 542 | * Override the vtable used by the client; useful for mocking certain scenarios. |
| 543 | */ |
| 544 | AWS_MQTT_API void aws_mqtt5_client_set_vtable( |
| 545 | struct aws_mqtt5_client *client, |
| 546 | const struct aws_mqtt5_client_vtable *vtable); |
| 547 | |
| 548 | /* |
| 549 | * Gets the default vtable used by the client. In order to mock something, we start with the default and then |
| 550 | * mutate it selectively to achieve the scenario we're interested in. |
| 551 | */ |
| 552 | AWS_MQTT_API const struct aws_mqtt5_client_vtable *aws_mqtt5_client_get_default_vtable(void); |
| 553 | |
| 554 | /* |
| 555 | * Sets the packet id, if necessary, on an operation based on the current pending acks table. The caller is |
| 556 | * responsible for adding the operation to the unacked table when the packet has been encoding in an io message. |
| 557 | * |
| 558 | * There is an argument that the operation should go into the table only on socket write completion, but that breaks |
| 559 | * allocation unless an additional, independent table is added, which I'd prefer not to do presently. Also, socket |
| 560 | * write completion callbacks can be a bit delayed which could lead to a situation where the response from a local |
| 561 | * server could arrive before the write completion runs which would be a disaster. |
| 562 | */ |
| 563 | AWS_MQTT_API int aws_mqtt5_operation_bind_packet_id( |
| 564 | struct aws_mqtt5_operation *operation, |
| 565 | struct aws_mqtt5_client_operational_state *client_operational_state); |
| 566 | |
| 567 | /* |
| 568 | * Initialize and clean up of the client operational state. Exposed (privately) to enabled tests to reuse the |
| 569 | * init/cleanup used by the client itself. |
| 570 | */ |
| 571 | AWS_MQTT_API int aws_mqtt5_client_operational_state_init( |
| 572 | struct aws_mqtt5_client_operational_state *client_operational_state, |
| 573 | struct aws_allocator *allocator, |
| 574 | struct aws_mqtt5_client *client); |
| 575 | |
| 576 | AWS_MQTT_API void aws_mqtt5_client_operational_state_clean_up( |
| 577 | struct aws_mqtt5_client_operational_state *client_operational_state); |
| 578 | |
| 579 | /* |
| 580 | * Resets the client's operational state based on a disconnection (from above comment): |
| 581 | * |
| 582 | * If current_operation |
| 583 | * move current_operation to head of queued_operations |
| 584 | * Fail all operations in the pending write completion list |
| 585 | * Fail, remove, and release operations in queued_operations where they fail the offline queue policy |
| 586 | * Iterate unacked_operations: |
| 587 | * If qos1+ publish |
| 588 | * set dup flag |
| 589 | * else |
| 590 | * unset/release packet id |
| 591 | * Fail, remove, and release unacked_operations if: |
| 592 | * (1) They fail the offline queue policy AND |
| 593 | * (2) the operation is not Qos 1+ publish |
| 594 | */ |
| 595 | AWS_MQTT_API void aws_mqtt5_client_on_disconnection_update_operational_state(struct aws_mqtt5_client *client); |
| 596 | |
| 597 | /* |
| 598 | * Updates the client's operational state based on a successfully established connection event: |
| 599 | * |
| 600 | * if rejoined_session: |
| 601 | * Move-and-append all non-qos1+-publishes in unacked_operations to the front of queued_operations |
| 602 | * Move-and-append remaining operations (qos1+ publishes) to the front of queued_operations |
| 603 | * else: |
| 604 | * Fail, remove, and release unacked_operations that fail the offline queue policy |
| 605 | * Move and append unacked operations to front of queued_operations |
| 606 | */ |
| 607 | AWS_MQTT_API void aws_mqtt5_client_on_connection_update_operational_state(struct aws_mqtt5_client *client); |
| 608 | |
| 609 | /* |
| 610 | * Processes the pending operation queue based on the current state of the associated client |
| 611 | */ |
| 612 | AWS_MQTT_API int aws_mqtt5_client_service_operational_state( |
| 613 | struct aws_mqtt5_client_operational_state *client_operational_state); |
| 614 | |
| 615 | /* |
| 616 | * Updates the client's operational state based on the receipt of an ACK packet from the server. In general this |
| 617 | * means looking up the original operation in the pending ack table, completing it, removing it from both the |
| 618 | * pending ack table and list, and then destroying it. |
| 619 | */ |
| 620 | AWS_MQTT_API void aws_mqtt5_client_operational_state_handle_ack( |
| 621 | struct aws_mqtt5_client_operational_state *client_operational_state, |
| 622 | aws_mqtt5_packet_id_t packet_id, |
| 623 | enum aws_mqtt5_packet_type packet_type, |
| 624 | const void *packet_view, |
| 625 | int error_code); |
| 626 | |
| 627 | /* |
| 628 | * Helper function that returns whether or not the current value of the negotiated settings can be used. Primarily |
| 629 | * a client state check (received CONNACK, not yet disconnected) |
| 630 | */ |
| 631 | AWS_MQTT_API bool aws_mqtt5_client_are_negotiated_settings_valid(const struct aws_mqtt5_client *client); |
| 632 | |
| 633 | /* |
| 634 | * Initializes the client's flow control state. This state governs the rates and delays between processing |
| 635 | * operations and sending packets. |
| 636 | */ |
| 637 | AWS_MQTT_API void aws_mqtt5_client_flow_control_state_init(struct aws_mqtt5_client *client); |
| 638 | |
| 639 | /* |
| 640 | * Resets the client's flow control state to a known baseline. Invoked right after entering the connected state. |
| 641 | */ |
| 642 | AWS_MQTT_API void aws_mqtt5_client_flow_control_state_reset(struct aws_mqtt5_client *client); |
| 643 | |
| 644 | /* |
| 645 | * Updates the client's flow control state based on the receipt of a PUBACK for a Qos1 publish. |
| 646 | */ |
| 647 | AWS_MQTT_API void aws_mqtt5_client_flow_control_state_on_puback(struct aws_mqtt5_client *client); |
| 648 | |
| 649 | /* |
| 650 | * Updates the client's flow control state based on successfully encoding an operation into a channel message. |
| 651 | */ |
| 652 | AWS_MQTT_API void aws_mqtt5_client_flow_control_state_on_outbound_operation( |
| 653 | struct aws_mqtt5_client *client, |
| 654 | struct aws_mqtt5_operation *operation); |
| 655 | |
| 656 | /* |
| 657 | * Given the next operation in the queue, examines the flow control state to determine when is the earliest time |
| 658 | * it should be processed. |
| 659 | */ |
| 660 | AWS_MQTT_API uint64_t aws_mqtt5_client_flow_control_state_get_next_operation_service_time( |
| 661 | struct aws_mqtt5_client *client, |
| 662 | struct aws_mqtt5_operation *operation, |
| 663 | uint64_t now); |
| 664 | |
| 665 | /* |
| 666 | * Updates the client's operation statistics based on a change in the state of an operation. |
| 667 | */ |
| 668 | AWS_MQTT_API void aws_mqtt5_client_statistics_change_operation_statistic_state( |
| 669 | struct aws_mqtt5_client *client, |
| 670 | struct aws_mqtt5_operation *operation, |
| 671 | enum aws_mqtt5_operation_statistic_state_flags new_state_flags); |
| 672 | |
| 673 | /** |
| 674 | * Converts a client state type to a readable description. |
| 675 | * |
| 676 | * @param state client state |
| 677 | * @return short string describing the client state |
| 678 | */ |
| 679 | AWS_MQTT_API const char *aws_mqtt5_client_state_to_c_string(enum aws_mqtt5_client_state state); |
| 680 | |
| 681 | /** |
| 682 | * An internal API used by the MQTT3 adapter to force any existing-or-in-progress connection to |
| 683 | * be torn down and re-established. Necessary because the MQTT3 interface allows overrides on a large number |
| 684 | * of configuration parameters through the connect() call. We must honor those parameters and the safest thing |
| 685 | * to do is to just throw away the current connection (if it exists) and make a new one. In the case that an MQTT5 |
| 686 | * client is being driven entirely by the MQTT3 adapter, this case never actually happens. |
| 687 | * |
| 688 | * @param client client to reset an existing or in-progress connection for |
| 689 | * @return true if a connection reset was triggered, false if there was nothing to do |
| 690 | */ |
| 691 | AWS_MQTT_API bool aws_mqtt5_client_reset_connection(struct aws_mqtt5_client *client); |
| 692 | |
| 693 | /** |
| 694 | * Event-loop-internal API used to switch the client's desired state. Used by both start() and stop() cross-thread |
| 695 | * tasks as well as by the 3-to-5 adapter to make changes synchronously (when in the event loop). |
| 696 | * |
| 697 | * @param client mqtt5 client to update desired state for |
| 698 | * @param desired_state new desired state |
| 699 | * @param disconnect_op optional description of a DISCONNECT packet to send as part of a stop command |
| 700 | */ |
| 701 | AWS_MQTT_API void aws_mqtt5_client_change_desired_state( |
| 702 | struct aws_mqtt5_client *client, |
| 703 | enum aws_mqtt5_client_state desired_state, |
| 704 | struct aws_mqtt5_operation_disconnect *disconnect_op); |
| 705 | |
| 706 | /** |
| 707 | * Event-loop-internal API to add an operation to the client's queue. Used by the 3-to-5 adapter to synchnrously |
| 708 | * inject the MQTT5 operation once the adapter operation has reached the event loop. |
| 709 | * |
| 710 | * @param client MQTT5 client to submit an operation to |
| 711 | * @param operation MQTT5 operation to submit |
| 712 | * @param is_terminated flag that indicates whether the submitter is shutting down or not. Needed to differentiate |
| 713 | * between adapter submissions and MQTT5 client API submissions and correctly handle ref count adjustments. |
| 714 | */ |
| 715 | AWS_MQTT_API void aws_mqtt5_client_submit_operation_internal( |
| 716 | struct aws_mqtt5_client *client, |
| 717 | struct aws_mqtt5_operation *operation, |
| 718 | bool is_terminated); |
| 719 | |
| 720 | AWS_EXTERN_C_END |
| 721 | |
| 722 | #endif /* AWS_MQTT_MQTT5_CLIENT_IMPL_H */ |