| 1 |
#ifndef AWS_MQTT_PRIVATE_CLIENT_IMPL_H |
| 2 |
#define AWS_MQTT_PRIVATE_CLIENT_IMPL_H |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <aws/mqtt/client.h> |
| 10 |
|
| 11 |
#include <aws/mqtt/private/client_impl_shared.h> |
| 12 |
#include <aws/mqtt/private/fixed_header.h> |
| 13 |
#include <aws/mqtt/private/mqtt311_decoder.h> |
| 14 |
#include <aws/mqtt/private/mqtt311_listener.h> |
| 15 |
#include <aws/mqtt/private/topic_tree.h> |
| 16 |
|
| 17 |
#include <aws/common/hash_table.h> |
| 18 |
#include <aws/common/mutex.h> |
| 19 |
#include <aws/common/rw_lock.h> |
| 20 |
#include <aws/common/task_scheduler.h> |
| 21 |
|
| 22 |
#include <aws/io/channel.h> |
| 23 |
#include <aws/io/channel_bootstrap.h> |
| 24 |
#include <aws/io/message_pool.h> |
| 25 |
#include <aws/io/socket.h> |
| 26 |
#include <aws/io/tls_channel_handler.h> |
| 27 |
|
| 28 |
struct aws_mqtt_client_connection_311_impl; |
| 29 |
|
| 30 |
#define MQTT_CLIENT_CALL_CALLBACK(client_ptr, callback) \ |
| 31 |
do { \ |
| 32 |
aws_rw_lock_rlock(&(client_ptr)->callback_lock); \ |
| 33 |
if ((client_ptr)->callback) { \ |
| 34 |
(client_ptr)->callback((&client_ptr->base), (client_ptr)->callback##_ud); \ |
| 35 |
} \ |
| 36 |
aws_rw_lock_runlock(&(client_ptr)->callback_lock); \ |
| 37 |
} while (false) |
| 38 |
#define MQTT_CLIENT_CALL_CALLBACK_ARGS(client_ptr, callback, ...) \ |
| 39 |
do { \ |
| 40 |
aws_rw_lock_rlock(&(client_ptr)->callback_lock); \ |
| 41 |
if ((client_ptr)->callback) { \ |
| 42 |
(client_ptr)->callback((&client_ptr->base), __VA_ARGS__, (client_ptr)->callback##_ud); \ |
| 43 |
} \ |
| 44 |
aws_rw_lock_runlock(&(client_ptr)->callback_lock); \ |
| 45 |
} while (false) |
| 46 |
|
| 47 |
#if ASSERT_LOCK_HELD |
| 48 |
# define ASSERT_SYNCED_DATA_LOCK_HELD(object) \ |
| 49 |
{ \ |
| 50 |
int cached_error = aws_last_error(); \ |
| 51 |
AWS_ASSERT(aws_mutex_try_lock(&(object)->synced_data.lock) == AWS_OP_ERR); \ |
| 52 |
aws_raise_error(cached_error); \ |
| 53 |
} |
| 54 |
#else |
| 55 |
# define ASSERT_SYNCED_DATA_LOCK_HELD(object) |
| 56 |
#endif |
| 57 |
|
| 58 |
enum aws_mqtt_client_connection_state { |
| 59 |
AWS_MQTT_CLIENT_STATE_CONNECTING, |
| 60 |
AWS_MQTT_CLIENT_STATE_CONNECTED, |
| 61 |
AWS_MQTT_CLIENT_STATE_RECONNECTING, |
| 62 |
AWS_MQTT_CLIENT_STATE_DISCONNECTING, |
| 63 |
AWS_MQTT_CLIENT_STATE_DISCONNECTED, |
| 64 |
}; |
| 65 |
|
| 66 |
enum aws_mqtt_client_request_state { |
| 67 |
AWS_MQTT_CLIENT_REQUEST_ONGOING, |
| 68 |
AWS_MQTT_CLIENT_REQUEST_COMPLETE, |
| 69 |
AWS_MQTT_CLIENT_REQUEST_ERROR, |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
struct aws_mqtt_connection_operation_statistics_impl { |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
struct aws_atomic_var incomplete_operation_count_atomic; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
struct aws_atomic_var incomplete_operation_size_atomic; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
struct aws_atomic_var unacked_operation_count_atomic; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
struct aws_atomic_var unacked_operation_size_atomic; |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
typedef enum aws_mqtt_client_request_state( |
| 109 |
aws_mqtt_send_request_fn)(uint16_t packet_id, bool is_first_attempt, void *userdata); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
typedef void( |
| 115 |
aws_mqtt_on_operation_statistics_fn)(struct aws_mqtt_client_connection_311_impl *connection, void *userdata); |
| 116 |
|
| 117 |
|
| 118 |
enum aws_mqtt_operation_statistic_state_flags { |
| 119 |
|
| 120 |
AWS_MQTT_OSS_NONE = 0, |
| 121 |
|
| 122 |
|
| 123 |
AWS_MQTT_OSS_INCOMPLETE = 1 << 0, |
| 124 |
|
| 125 |
|
| 126 |
AWS_MQTT_OSS_UNACKED = 1 << 1, |
| 127 |
}; |
| 128 |
|
| 129 |
struct aws_mqtt_request { |
| 130 |
struct aws_linked_list_node list_node; |
| 131 |
|
| 132 |
struct aws_allocator *allocator; |
| 133 |
struct aws_mqtt_client_connection_311_impl *connection; |
| 134 |
|
| 135 |
struct aws_channel_task outgoing_task; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
uint64_t request_send_timestamp; |
| 141 |
|
| 142 |
|
| 143 |
enum aws_mqtt_operation_statistic_state_flags statistic_state_flags; |
| 144 |
|
| 145 |
uint64_t packet_size; |
| 146 |
|
| 147 |
uint16_t packet_id; |
| 148 |
bool retryable; |
| 149 |
bool initiated; |
| 150 |
aws_mqtt_send_request_fn *send_request; |
| 151 |
void *send_request_ud; |
| 152 |
aws_mqtt_op_complete_fn *on_complete; |
| 153 |
void *on_complete_ud; |
| 154 |
}; |
| 155 |
|
| 156 |
struct aws_mqtt_reconnect_task { |
| 157 |
struct aws_task task; |
| 158 |
struct aws_atomic_var connection_ptr; |
| 159 |
struct aws_allocator *allocator; |
| 160 |
}; |
| 161 |
|
| 162 |
struct request_timeout_wrapper; |
| 163 |
|
| 164 |
|
| 165 |
struct request_timeout_task_arg { |
| 166 |
uint16_t packet_id; |
| 167 |
struct aws_mqtt_client_connection_311_impl *connection; |
| 168 |
struct request_timeout_wrapper *task_arg_wrapper; |
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
struct request_timeout_wrapper { |
| 178 |
struct request_timeout_task_arg *timeout_task_arg; |
| 179 |
}; |
| 180 |
|
| 181 |
|
| 182 |
struct subscribe_task_arg { |
| 183 |
|
| 184 |
struct aws_mqtt_client_connection_311_impl *connection; |
| 185 |
|
| 186 |
|
| 187 |
struct aws_array_list topics; |
| 188 |
|
| 189 |
|
| 190 |
struct aws_mqtt_packet_subscribe subscribe; |
| 191 |
|
| 192 |
|
| 193 |
bool tree_updated; |
| 194 |
|
| 195 |
struct { |
| 196 |
aws_mqtt_suback_multi_fn *multi; |
| 197 |
aws_mqtt_suback_fn *single; |
| 198 |
} on_suback; |
| 199 |
void *on_suback_ud; |
| 200 |
|
| 201 |
struct request_timeout_wrapper timeout_wrapper; |
| 202 |
uint64_t timeout_duration_in_ns; |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
struct subscribe_task_topic { |
| 207 |
struct aws_mqtt_client_connection_311_impl *connection; |
| 208 |
|
| 209 |
struct aws_mqtt_topic_subscription request; |
| 210 |
struct aws_string *filter; |
| 211 |
|
| 212 |
struct aws_ref_count ref_count; |
| 213 |
}; |
| 214 |
|
| 215 |
struct aws_mqtt_client_connection_311_impl { |
| 216 |
struct aws_allocator *allocator; |
| 217 |
|
| 218 |
struct aws_mqtt_client_connection base; |
| 219 |
|
| 220 |
struct aws_ref_count ref_count; |
| 221 |
|
| 222 |
struct aws_mqtt_client *client; |
| 223 |
|
| 224 |
|
| 225 |
struct aws_channel_handler handler; |
| 226 |
struct aws_channel_slot *slot; |
| 227 |
|
| 228 |
|
| 229 |
struct aws_string *host_name; |
| 230 |
uint32_t port; |
| 231 |
struct aws_tls_connection_options tls_options; |
| 232 |
struct aws_socket_options socket_options; |
| 233 |
struct aws_http_proxy_config *http_proxy_config; |
| 234 |
struct aws_event_loop *loop; |
| 235 |
struct aws_host_resolution_config host_resolution_config; |
| 236 |
|
| 237 |
|
| 238 |
struct aws_byte_buf client_id; |
| 239 |
bool clean_session; |
| 240 |
uint16_t keep_alive_time_secs; |
| 241 |
uint64_t keep_alive_time_ns; |
| 242 |
uint64_t ping_timeout_ns; |
| 243 |
uint64_t operation_timeout_ns; |
| 244 |
struct aws_string *username; |
| 245 |
struct aws_string *password; |
| 246 |
struct { |
| 247 |
struct aws_byte_buf topic; |
| 248 |
enum aws_mqtt_qos qos; |
| 249 |
bool retain; |
| 250 |
struct aws_byte_buf payload; |
| 251 |
} will; |
| 252 |
struct { |
| 253 |
uint64_t current_sec; |
| 254 |
uint64_t min_sec; |
| 255 |
uint64_t max_sec; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
uint64_t channel_successful_connack_timestamp_ns; |
| 262 |
} reconnect_timeouts; |
| 263 |
|
| 264 |
|
| 265 |
aws_mqtt_client_on_connection_complete_fn *on_connection_complete; |
| 266 |
void *on_connection_complete_ud; |
| 267 |
aws_mqtt_client_on_connection_success_fn *on_connection_success; |
| 268 |
void *on_connection_success_ud; |
| 269 |
aws_mqtt_client_on_connection_failure_fn *on_connection_failure; |
| 270 |
void *on_connection_failure_ud; |
| 271 |
aws_mqtt_client_on_connection_interrupted_fn *on_interrupted; |
| 272 |
void *on_interrupted_ud; |
| 273 |
aws_mqtt_client_on_connection_resumed_fn *on_resumed; |
| 274 |
void *on_resumed_ud; |
| 275 |
aws_mqtt_client_on_connection_closed_fn *on_closed; |
| 276 |
void *on_closed_ud; |
| 277 |
aws_mqtt_client_publish_received_fn *on_any_publish; |
| 278 |
void *on_any_publish_ud; |
| 279 |
aws_mqtt_client_on_disconnect_fn *on_disconnect; |
| 280 |
void *on_disconnect_ud; |
| 281 |
aws_mqtt_client_on_connection_termination_fn *on_termination; |
| 282 |
void *on_termination_ud; |
| 283 |
aws_mqtt_on_operation_statistics_fn *on_any_operation_statistics; |
| 284 |
void *on_any_operation_statistics_ud; |
| 285 |
|
| 286 |
|
| 287 |
struct aws_rw_lock callback_lock; |
| 288 |
|
| 289 |
|
| 290 |
struct aws_mqtt311_callback_set_manager callback_manager; |
| 291 |
|
| 292 |
|
| 293 |
struct aws_mqtt_reconnect_task *reconnect_task; |
| 294 |
struct aws_channel_task ping_task; |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
size_t connection_count; |
| 302 |
bool use_tls; |
| 303 |
|
| 304 |
|
| 305 |
struct { |
| 306 |
struct aws_mqtt311_decoder decoder; |
| 307 |
|
| 308 |
bool waiting_on_ping_response; |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
struct aws_mqtt_topic_tree subscriptions; |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
struct aws_linked_list ongoing_requests_list; |
| 319 |
} thread_data; |
| 320 |
|
| 321 |
|
| 322 |
struct { |
| 323 |
|
| 324 |
struct aws_mutex lock; |
| 325 |
|
| 326 |
|
| 327 |
enum aws_mqtt_client_connection_state state; |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
struct aws_memory_pool requests_pool; |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
struct aws_hash_table outstanding_requests_table; |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
struct aws_linked_list pending_requests_list; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
uint16_t packet_id; |
| 351 |
|
| 352 |
} synced_data; |
| 353 |
|
| 354 |
struct { |
| 355 |
aws_mqtt_transform_websocket_handshake_fn *handshake_transformer; |
| 356 |
void *handshake_transformer_ud; |
| 357 |
aws_mqtt_validate_websocket_handshake_fn *handshake_validator; |
| 358 |
void *handshake_validator_ud; |
| 359 |
bool enabled; |
| 360 |
|
| 361 |
struct aws_http_message *handshake_request; |
| 362 |
} websocket; |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
uint64_t next_ping_time; |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
struct aws_mqtt_connection_operation_statistics_impl operation_statistics_impl; |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
struct aws_mqtt_iot_metrics_storage *metrics_storage; |
| 381 |
}; |
| 382 |
|
| 383 |
struct aws_channel_handler_vtable *aws_mqtt_get_client_channel_vtable(void); |
| 384 |
|
| 385 |
|
| 386 |
struct aws_io_message *mqtt_get_message_for_packet( |
| 387 |
struct aws_mqtt_client_connection_311_impl *connection, |
| 388 |
struct aws_mqtt_fixed_header *header); |
| 389 |
|
| 390 |
void mqtt_connection_lock_synced_data(struct aws_mqtt_client_connection_311_impl *connection); |
| 391 |
void mqtt_connection_unlock_synced_data(struct aws_mqtt_client_connection_311_impl *connection); |
| 392 |
|
| 393 |
|
| 394 |
void mqtt_connection_set_state( |
| 395 |
struct aws_mqtt_client_connection_311_impl *connection, |
| 396 |
enum aws_mqtt_client_connection_state state); |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
AWS_MQTT_API uint16_t mqtt_create_request( |
| 405 |
struct aws_mqtt_client_connection_311_impl *connection, |
| 406 |
aws_mqtt_send_request_fn *send_request, |
| 407 |
void *send_request_ud, |
| 408 |
aws_mqtt_op_complete_fn *on_complete, |
| 409 |
void *on_complete_ud, |
| 410 |
bool noRetry, |
| 411 |
uint64_t packet_size); |
| 412 |
|
| 413 |
|
| 414 |
AWS_MQTT_API void mqtt_request_complete( |
| 415 |
struct aws_mqtt_client_connection_311_impl *connection, |
| 416 |
int error_code, |
| 417 |
uint16_t packet_id); |
| 418 |
|
| 419 |
|
| 420 |
AWS_MQTT_API void mqtt_disconnect_impl(struct aws_mqtt_client_connection_311_impl *connection, int error_code); |
| 421 |
|
| 422 |
|
| 423 |
AWS_MQTT_API void aws_create_reconnect_task(struct aws_mqtt_client_connection_311_impl *connection); |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
AWS_MQTT_API int aws_mqtt_client_connection_set_on_operation_statistics_handler( |
| 433 |
struct aws_mqtt_client_connection_311_impl *connection, |
| 434 |
aws_mqtt_on_operation_statistics_fn *on_operation_statistics, |
| 435 |
void *on_operation_statistics_ud); |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
int aws_mqtt_client_connection_ping(struct aws_mqtt_client_connection_311_impl *connection); |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
void aws_mqtt_connection_statistics_change_operation_statistic_state( |
| 461 |
struct aws_mqtt_client_connection_311_impl *connection, |
| 462 |
struct aws_mqtt_request *request, |
| 463 |
enum aws_mqtt_operation_statistic_state_flags new_state_flags); |
| 464 |
|
| 465 |
AWS_MQTT_API const struct aws_mqtt_client_connection_packet_handlers *aws_mqtt311_get_default_packet_handlers(void); |
| 466 |
|
| 467 |
AWS_MQTT_API uint16_t aws_mqtt_client_connection_311_unsubscribe( |
| 468 |
struct aws_mqtt_client_connection_311_impl *connection, |
| 469 |
const struct aws_byte_cursor *topic_filter, |
| 470 |
aws_mqtt_op_complete_fn *on_unsuback, |
| 471 |
void *on_unsuback_ud, |
| 472 |
uint64_t timeout_ns); |
| 473 |
|
| 474 |
AWS_MQTT_API uint16_t aws_mqtt_client_connection_311_subscribe( |
| 475 |
struct aws_mqtt_client_connection_311_impl *connection, |
| 476 |
const struct aws_byte_cursor *topic_filter, |
| 477 |
enum aws_mqtt_qos qos, |
| 478 |
aws_mqtt_client_publish_received_fn *on_publish, |
| 479 |
void *on_publish_ud, |
| 480 |
aws_mqtt_userdata_cleanup_fn *on_ud_cleanup, |
| 481 |
aws_mqtt_suback_fn *on_suback, |
| 482 |
void *on_suback_ud, |
| 483 |
uint64_t timeout_ns); |
| 484 |
|
| 485 |
AWS_MQTT_API uint16_t aws_mqtt_client_connection_311_publish( |
| 486 |
struct aws_mqtt_client_connection_311_impl *connection, |
| 487 |
const struct aws_byte_cursor *topic, |
| 488 |
enum aws_mqtt_qos qos, |
| 489 |
bool retain, |
| 490 |
const struct aws_byte_cursor *payload, |
| 491 |
aws_mqtt_op_complete_fn *on_complete, |
| 492 |
void *userdata, |
| 493 |
uint64_t timeout_ns); |
| 494 |
|
| 495 |
#endif |