55#include <aws/mqtt/client.h>
66
77#include <aws/mqtt/private/client_impl.h>
8- #include <aws/mqtt/private/client_impl_shared.h>
98#include <aws/mqtt/private/mqtt_client_test_helper.h>
9+ #include <aws/mqtt/private/mqtt_iot_sdk_metrics.h>
1010#include <aws/mqtt/private/packets.h>
1111#include <aws/mqtt/private/shared.h>
1212#include <aws/mqtt/private/topic_tree.h>
@@ -504,6 +504,8 @@ static void s_mqtt_client_init(
504504 AWS_FATAL_ASSERT ((error_code != 0 ) == (channel == NULL ));
505505
506506 struct aws_mqtt_client_connection_311_impl * connection = user_data ;
507+ struct aws_byte_buf username_with_metrics_buf ;
508+ aws_byte_buf_init (& username_with_metrics_buf , connection -> allocator , 0 );
507509
508510 if (error_code != AWS_OP_SUCCESS ) {
509511 /* client shutdown already handles this case, so just call that. */
@@ -624,7 +626,7 @@ static void s_mqtt_client_init(
624626 & connect , topic_cur , connection -> will .qos , connection -> will .retain , payload_cur );
625627 }
626628
627- if (connection -> username || connection -> metrics ) {
629+ if (connection -> username || connection -> metrics_storage ) {
628630 struct aws_byte_cursor username_cur ;
629631 if (connection -> username ) {
630632 username_cur = aws_byte_cursor_from_string (connection -> username );
@@ -633,17 +635,15 @@ static void s_mqtt_client_init(
633635 username_cur = aws_byte_cursor_from_c_str ("" );
634636 }
635637
636- aws_byte_buf_clean_up (& connection -> username_with_metrics_buf );
637-
638638 /* Apply metrics to username if configured */
639- if (connection -> metrics ) {
639+ if (connection -> metrics_storage ) {
640640 if (aws_mqtt_append_sdk_metrics_to_username (
641641 connection -> allocator ,
642642 & username_cur ,
643- connection -> metrics -> storage_view ,
644- & connection -> username_with_metrics_buf ,
643+ connection -> metrics_storage -> storage_view ,
644+ & username_with_metrics_buf ,
645645 NULL ) == AWS_OP_SUCCESS ) {
646- username_cur = aws_byte_cursor_from_buf (& connection -> username_with_metrics_buf );
646+ username_cur = aws_byte_cursor_from_buf (& username_with_metrics_buf );
647647 } else {
648648 AWS_LOGF_WARN (
649649 AWS_LS_MQTT_CLIENT ,
@@ -689,13 +689,21 @@ static void s_mqtt_client_init(
689689 goto handle_error ;
690690 }
691691
692+ if (aws_byte_buf_is_valid (& username_with_metrics_buf )) {
693+ aws_byte_buf_clean_up (& username_with_metrics_buf );
694+ }
695+
692696 return ;
693697
694698handle_error :
695699 MQTT_CLIENT_CALL_CALLBACK_ARGS (connection , on_connection_complete , aws_last_error (), 0 , false);
696700 MQTT_CLIENT_CALL_CALLBACK_ARGS (connection , on_connection_failure , aws_last_error ());
697701 aws_channel_shutdown (channel , aws_last_error ());
698702
703+ if (aws_byte_buf_is_valid (& username_with_metrics_buf )) {
704+ aws_byte_buf_clean_up (& username_with_metrics_buf );
705+ }
706+
699707 if (message ) {
700708 aws_mem_release (message -> allocator , message );
701709 }
@@ -871,13 +879,11 @@ static void s_mqtt_client_connection_destroy_final(struct aws_mqtt_client_connec
871879 }
872880
873881 /* Clean up metrics */
874- if (connection -> metrics ) {
875- aws_mqtt_iot_sdk_metrics_storage_destroy (connection -> metrics );
876- connection -> metrics = NULL ;
882+ if (connection -> metrics_storage ) {
883+ aws_mqtt_iot_sdk_metrics_storage_destroy (connection -> metrics_storage );
884+ connection -> metrics_storage = NULL ;
877885 }
878886
879- aws_byte_buf_clean_up (& connection -> username_with_metrics_buf );
880-
881887 aws_mqtt_client_release (connection -> client );
882888
883889 /* Frees all allocated memory */
@@ -3395,14 +3401,14 @@ static int s_aws_mqtt_client_connection_311_set_metrics(void *impl, const struct
33953401 AWS_LOGF_TRACE (AWS_LS_MQTT_CLIENT , "id=%p: Setting IoT SDK metrics" , (void * )connection );
33963402
33973403 /* Clean up existing metrics if any */
3398- if (connection -> metrics ) {
3399- aws_mqtt_iot_sdk_metrics_storage_destroy (connection -> metrics );
3400- connection -> metrics = NULL ;
3404+ if (connection -> metrics_storage ) {
3405+ aws_mqtt_iot_sdk_metrics_storage_destroy (connection -> metrics_storage );
3406+ connection -> metrics_storage = NULL ;
34013407 }
34023408
34033409 if (metrics ) {
34043410 /* Initialize metrics storage */
3405- connection -> metrics = aws_mqtt_iot_sdk_metrics_storage_new (connection -> allocator , metrics );
3411+ connection -> metrics_storage = aws_mqtt_iot_sdk_metrics_storage_new (connection -> allocator , metrics );
34063412 }
34073413
34083414 return AWS_OP_SUCCESS ;
@@ -3561,8 +3567,6 @@ struct aws_mqtt_client_connection *aws_mqtt_client_connection_new(struct aws_mqt
35613567 connection -> handler .vtable = aws_mqtt_get_client_channel_vtable ();
35623568 connection -> handler .impl = connection ;
35633569
3564- aws_byte_buf_init (& connection -> username_with_metrics_buf , connection -> allocator , 0 );
3565-
35663570 aws_mqtt311_callback_set_manager_init (& connection -> callback_manager , connection -> allocator , & connection -> base );
35673571
35683572 return & connection -> base ;
0 commit comments