-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmqtt5_client.c
More file actions
3686 lines (2995 loc) · 142 KB
/
mqtt5_client.c
File metadata and controls
3686 lines (2995 loc) · 142 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include "mqtt5_client.h"
#include "http_connection.h"
#include "http_message.h"
#include "io.h"
#include <aws/http/proxy.h>
#include <aws/io/socket.h>
#include <aws/io/tls_channel_handler.h>
#include <aws/mqtt/mqtt.h>
#include <aws/mqtt/v5/mqtt5_client.h>
#include <aws/mqtt/v5/mqtt5_packet_storage.h>
#include <aws/mqtt/v5/mqtt5_types.h>
/* object key names for referencing mqtt5-related properties on napi objects */
static const char *AWS_NAPI_KEY_NAME = "name";
static const char *AWS_NAPI_KEY_VALUE = "value";
static const char *AWS_NAPI_KEY_USER_PROPERTIES = "userProperties";
static const char *AWS_NAPI_KEY_SESSION_PRESENT = "sessionPresent";
static const char *AWS_NAPI_KEY_REASON_CODE = "reasonCode";
static const char *AWS_NAPI_KEY_REASON_CODES = "reasonCodes";
static const char *AWS_NAPI_KEY_SESSION_EXPIRY_INTERVAL = "sessionExpiryInterval";
static const char *AWS_NAPI_KEY_RECEIVE_MAXIMUM = "receiveMaximum";
static const char *AWS_NAPI_KEY_MAXIMUM_QOS = "maximumQos";
static const char *AWS_NAPI_KEY_RETAIN_AVAILABLE = "retainAvailable";
static const char *AWS_NAPI_KEY_MAXIMUM_PACKET_SIZE = "maximumPacketSize";
static const char *AWS_NAPI_KEY_ASSIGNED_CLIENT_IDENTIFIER = "assignedClientIdentifier";
static const char *AWS_NAPI_KEY_TOPIC_ALIAS_MAXIMUM = "topicAliasMaximum";
static const char *AWS_NAPI_KEY_REASON_STRING = "reasonString";
static const char *AWS_NAPI_KEY_WILDCARD_SUBSCRIPTIONS_AVAILABLE = "wildcardSubscriptionsAvailable";
static const char *AWS_NAPI_KEY_SUBSCRIPTION_IDENTIFIERS_AVAILABLE = "subscriptionIdentifiersAvailable";
static const char *AWS_NAPI_KEY_SHARED_SUBSCRIPTIONS_AVAILABLE = "sharedSubscriptionsAvailable";
static const char *AWS_NAPI_KEY_SERVER_KEEP_ALIVE = "serverKeepAlive";
static const char *AWS_NAPI_KEY_RESPONSE_INFORMATION = "responseInformation";
static const char *AWS_NAPI_KEY_SERVER_REFERENCE = "serverReference";
static const char *AWS_NAPI_KEY_RECEIVE_MAXIMUM_FROM_SERVER = "receiveMaximumFromServer";
static const char *AWS_NAPI_KEY_MAXIMUM_PACKET_SIZE_TO_SERVER = "maximumPacketSizeToServer";
static const char *AWS_NAPI_KEY_TOPIC_ALIAS_MAXIMUM_TO_SERVER = "topicAliasMaximumToServer";
static const char *AWS_NAPI_KEY_TOPIC_ALIAS_MAXIMUM_TO_CLIENT = "topicAliasMaximumToClient";
static const char *AWS_NAPI_KEY_REJOINED_SESSION = "rejoinedSession";
static const char *AWS_NAPI_KEY_CLIENT_ID = "clientId";
static const char *AWS_NAPI_KEY_SESSION_EXPIRY_INTERVAL_SECONDS = "sessionExpiryIntervalSeconds";
static const char *AWS_NAPI_KEY_TOPIC_NAME = "topicName";
static const char *AWS_NAPI_KEY_PAYLOAD = "payload";
static const char *AWS_NAPI_KEY_QOS = "qos";
static const char *AWS_NAPI_KEY_RETAIN = "retain";
static const char *AWS_NAPI_KEY_PAYLOAD_FORMAT = "payloadFormat";
static const char *AWS_NAPI_KEY_MESSAGE_EXPIRY_INTERVAL_SECONDS = "messageExpiryIntervalSeconds";
static const char *AWS_NAPI_KEY_TOPIC_ALIAS = "topicAlias";
static const char *AWS_NAPI_KEY_RESPONSE_TOPIC = "responseTopic";
static const char *AWS_NAPI_KEY_CORRELATION_DATA = "correlationData";
static const char *AWS_NAPI_KEY_CONTENT_TYPE = "contentType";
static const char *AWS_NAPI_KEY_KEEP_ALIVE_INTERVAL_SECONDS = "keepAliveIntervalSeconds";
static const char *AWS_NAPI_KEY_USERNAME = "username";
static const char *AWS_NAPI_KEY_PASSWORD = "password";
static const char *AWS_NAPI_KEY_REQUEST_RESPONSE_INFORMATION = "requestResponseInformation";
static const char *AWS_NAPI_KEY_REQUEST_PROBLEM_INFORMATION = "requestProblemInformation";
static const char *AWS_NAPI_KEY_MAXIMUM_PACKET_SIZE_BYTES = "maximumPacketSizeBytes";
static const char *AWS_NAPI_KEY_WILL_DELAY_INTERVAL_SECONDS = "willDelayIntervalSeconds";
static const char *AWS_NAPI_KEY_WILL = "will";
static const char *AWS_NAPI_KEY_HOST_NAME = "hostName";
static const char *AWS_NAPI_KEY_PORT = "port";
static const char *AWS_NAPI_KEY_SESSION_BEHAVIOR = "sessionBehavior";
static const char *AWS_NAPI_KEY_EXTENDED_VALIDATION_AND_FLOW_CONTROL_OPTIONS =
"extendedValidationAndFlowControlOptions";
static const char *AWS_NAPI_KEY_OFFLINE_QUEUE_BEHAVIOR = "offlineQueueBehavior";
static const char *AWS_NAPI_KEY_RETRY_JITTER_MODE = "retryJitterMode";
static const char *AWS_NAPI_KEY_MIN_RECONNECT_DELAY_MS = "minReconnectDelayMs";
static const char *AWS_NAPI_KEY_MAX_RECONNECT_DELAY_MS = "maxReconnectDelayMs";
static const char *AWS_NAPI_KEY_MIN_CONNECTED_TIME_TO_RESET_RECONNECT_DELAY_MS =
"minConnectedTimeToResetReconnectDelayMs";
static const char *AWS_NAPI_KEY_PING_TIMEOUT_MS = "pingTimeoutMs";
static const char *AWS_NAPI_KEY_CONNACK_TIMEOUT_MS = "connackTimeoutMs";
static const char *AWS_NAPI_KEY_ACK_TIMEOUT_SECONDS = "ackTimeoutSeconds";
static const char *AWS_NAPI_KEY_CONNECT_PROPERTIES = "connectProperties";
static const char *AWS_NAPI_KEY_WEBSOCKET_HANDSHAKE_TRANSFORM = "websocketHandshakeTransform";
static const char *AWS_NAPI_KEY_SUBSCRIPTIONS = "subscriptions";
static const char *AWS_NAPI_KEY_TOPIC_FILTER = "topicFilter";
static const char *AWS_NAPI_KEY_TOPIC_FILTERS = "topicFilters";
static const char *AWS_NAPI_KEY_NO_LOCAL = "noLocal";
static const char *AWS_NAPI_KEY_RETAIN_AS_PUBLISHED = "retainAsPublished";
static const char *AWS_NAPI_KEY_RETAIN_HANDLING_TYPE = "retainHandlingType";
static const char *AWS_NAPI_KEY_SUBSCRIPTION_IDENTIFIER = "subscriptionIdentifier";
static const char *AWS_NAPI_KEY_SUSBCRIPTION_IDENTIFIERS = "subscriptionIdentifiers";
static const char *AWS_NAPI_KEY_INCOMPLETE_OPERATION_COUNT = "incompleteOperationCount";
static const char *AWS_NAPI_KEY_INCOMPLETE_OPERATION_SIZE = "incompleteOperationSize";
static const char *AWS_NAPI_KEY_UNACKED_OPERATION_COUNT = "unackedOperationCount";
static const char *AWS_NAPI_KEY_UNACKED_OPERATION_SIZE = "unackedOperationSize";
static const char *AWS_NAPI_KEY_TYPE = "type";
static const char *AWS_NAPI_KEY_TOPIC_ALIASING_OPTIONS = "topicAliasingOptions";
static const char *AWS_NAPI_KEY_OUTBOUND_BEHAVIOR = "outboundBehavior";
static const char *AWS_NAPI_KEY_OUTBOUND_CACHE_MAX_SIZE = "outboundCacheMaxSize";
static const char *AWS_NAPI_KEY_INBOUND_BEHAVIOR = "inboundBehavior";
static const char *AWS_NAPI_KEY_INBOUND_CACHE_MAX_SIZE = "inboundCacheMaxSize";
/*
* Binding object that outlives the associated napi wrapper object. When that object finalizes, then it's a signal
* to this object to destroy the client (and itself, afterwards).
*/
struct aws_mqtt5_client_binding {
struct aws_allocator *allocator;
/*
* We ref count the binding itself because there are anomalous situations where the binding must outlive even
* the native client. In particular, if we have a native client being destroyed it may emit lifecycle events
* or completion callbacks for submitted operations as it does so. Those events get marshalled across to the
* node/libuv thread and in the time it takes to do so, the native client may have completed destruction. But
* we still need the binding when we're processing those events/callbacks in the libuv thread so the binding
* must not simply destroy itself as soon as the native client has destroyed itself.
*
* We handle this by having all operations/events inc/dec this ref count as well as the base of one from
* creating the client. In this way, the binding will only destroy itself when the native client is completely
* gone and all callbacks and events have been successfully emitted to node.
*/
struct aws_ref_count ref_count;
struct aws_mqtt5_client *client;
struct aws_tls_connection_options tls_connection_options;
/*
* Single count ref to the JS mqtt 5 client object.
*/
napi_ref node_mqtt5_client_ref;
/*
* Single count ref to the node external managed by the client.
*/
napi_ref node_client_external_ref;
napi_threadsafe_function on_stopped;
napi_threadsafe_function on_attempting_connect;
napi_threadsafe_function on_connection_success;
napi_threadsafe_function on_connection_failure;
napi_threadsafe_function on_disconnection;
napi_threadsafe_function on_message_received;
napi_threadsafe_function transform_websocket;
};
struct aws_mqtt5_client *aws_napi_get_mqtt5_client_from_binding(struct aws_mqtt5_client_binding *binding) {
if (binding == NULL) {
return NULL;
}
return binding->client;
}
static void s_aws_mqtt5_client_binding_destroy(struct aws_mqtt5_client_binding *binding) {
if (binding == NULL) {
return;
}
aws_tls_connection_options_clean_up(&binding->tls_connection_options);
AWS_CLEAN_THREADSAFE_FUNCTION(binding, on_stopped);
AWS_CLEAN_THREADSAFE_FUNCTION(binding, on_attempting_connect);
AWS_CLEAN_THREADSAFE_FUNCTION(binding, on_connection_success);
AWS_CLEAN_THREADSAFE_FUNCTION(binding, on_connection_failure);
AWS_CLEAN_THREADSAFE_FUNCTION(binding, on_disconnection);
AWS_CLEAN_THREADSAFE_FUNCTION(binding, on_message_received);
AWS_CLEAN_THREADSAFE_FUNCTION(binding, transform_websocket);
aws_mem_release(binding->allocator, binding);
}
static void s_aws_mqtt5_client_binding_on_zero(void *object) {
s_aws_mqtt5_client_binding_destroy(object);
}
static struct aws_mqtt5_client_binding *s_aws_mqtt5_client_binding_acquire(struct aws_mqtt5_client_binding *binding) {
if (binding == NULL) {
return NULL;
}
aws_ref_count_acquire(&binding->ref_count);
return binding;
}
static struct aws_mqtt5_client_binding *s_aws_mqtt5_client_binding_release(struct aws_mqtt5_client_binding *binding) {
if (binding != NULL) {
aws_ref_count_release(&binding->ref_count);
}
return NULL;
}
static void s_aws_mqtt5_client_binding_on_client_terminate(void *user_data) {
struct aws_mqtt5_client_binding *binding = user_data;
s_aws_mqtt5_client_binding_release(binding);
}
/*
* Invoked when the node mqtt5 client is garbage collected or if fails construction partway through
*/
static void s_aws_mqtt5_client_extern_finalize(napi_env env, void *finalize_data, void *finalize_hint) {
(void)finalize_hint;
(void)env;
struct aws_mqtt5_client_binding *binding = finalize_data;
AWS_LOGF_INFO(
AWS_LS_NODEJS_CRT_GENERAL,
"id=%p s_aws_mqtt5_client_extern_finalize - mqtt5_client node wrapper is being finalized",
(void *)binding->client);
if (binding->client != NULL) {
/* if client is not null, then this is a successfully constructed client which should shutdown normally */
aws_mqtt5_client_release(binding->client);
binding->client = NULL;
} else {
/*
* no client, this must be a creation attempt that failed partway through and we should directly clean up the
* binding
*/
s_aws_mqtt5_client_binding_on_client_terminate(binding);
}
}
struct on_message_received_user_data {
struct aws_allocator *allocator;
struct aws_mqtt5_client_binding *binding;
struct aws_mqtt5_packet_publish_storage publish_storage;
struct aws_byte_buf *payload;
struct aws_byte_buf *correlation_data;
};
static void s_on_message_received_user_data_destroy(struct on_message_received_user_data *user_data) {
if (user_data == NULL) {
return;
}
user_data->binding = s_aws_mqtt5_client_binding_release(user_data->binding);
aws_mqtt5_packet_publish_storage_clean_up(&user_data->publish_storage);
if (user_data->payload != NULL) {
aws_byte_buf_clean_up(user_data->payload);
aws_mem_release(user_data->allocator, user_data->payload);
}
if (user_data->correlation_data != NULL) {
aws_byte_buf_clean_up(user_data->correlation_data);
aws_mem_release(user_data->allocator, user_data->correlation_data);
}
aws_mem_release(user_data->allocator, user_data);
}
static struct on_message_received_user_data *s_on_message_received_user_data_new(
struct aws_mqtt5_client_binding *binding,
const struct aws_mqtt5_packet_publish_view *publish_packet) {
struct on_message_received_user_data *user_data =
aws_mem_calloc(binding->allocator, 1, sizeof(struct on_message_received_user_data));
user_data->allocator = binding->allocator;
/*
* Binary data needs to be separately pinned and tracked so that it can be individually finalized. In order to not
* make even more redundant copies of it, we do some hacky nonsense to "split" it out of the storage into separate
* buffers. We can then "take" the buffer pointers when we're calling into node and manage them separately.
*/
struct aws_mqtt5_packet_publish_view publish_copy = *publish_packet;
user_data->payload = aws_mem_calloc(binding->allocator, 1, sizeof(struct aws_byte_buf));
if (aws_byte_buf_init_copy_from_cursor(user_data->payload, binding->allocator, publish_copy.payload)) {
goto error;
}
AWS_ZERO_STRUCT(publish_copy.payload);
if (publish_copy.correlation_data != NULL) {
user_data->correlation_data = aws_mem_calloc(binding->allocator, 1, sizeof(struct aws_byte_buf));
if (aws_byte_buf_init_copy_from_cursor(
user_data->correlation_data, binding->allocator, *publish_copy.correlation_data)) {
goto error;
}
publish_copy.correlation_data = NULL;
}
/*
* We've saved off correlation data and payload separately and erased them from the packet copy. Now we can make
* a persistent copy of the packet without copying the binary data twice.
*/
if (aws_mqtt5_packet_publish_storage_init(&user_data->publish_storage, user_data->allocator, &publish_copy)) {
goto error;
}
user_data->binding = s_aws_mqtt5_client_binding_acquire(binding);
return user_data;
error:
s_on_message_received_user_data_destroy(user_data);
return NULL;
}
static void s_on_publish_received(const struct aws_mqtt5_packet_publish_view *publish_packet, void *user_data) {
struct aws_mqtt5_client_binding *binding = user_data;
if (!binding->on_message_received) {
return;
}
struct on_message_received_user_data *message_received_ud =
s_on_message_received_user_data_new(binding, publish_packet);
if (message_received_ud == NULL) {
return;
}
/* queue a callback in node's libuv thread */
AWS_NAPI_ENSURE(NULL, aws_napi_queue_threadsafe_function(binding->on_message_received, message_received_ud));
}
struct on_simple_event_user_data {
struct aws_allocator *allocator;
struct aws_mqtt5_client_binding *binding;
};
static void s_on_simple_event_user_data_destroy(struct on_simple_event_user_data *user_data) {
if (user_data == NULL) {
return;
}
user_data->binding = s_aws_mqtt5_client_binding_release(user_data->binding);
aws_mem_release(user_data->allocator, user_data);
}
static struct on_simple_event_user_data *s_on_simple_event_user_data_new(struct aws_mqtt5_client_binding *binding) {
struct on_simple_event_user_data *user_data =
aws_mem_calloc(binding->allocator, 1, sizeof(struct on_simple_event_user_data));
user_data->allocator = binding->allocator;
user_data->binding = s_aws_mqtt5_client_binding_acquire(binding);
return user_data;
}
static void s_on_stopped(struct aws_mqtt5_client_binding *binding) {
if (!binding->on_stopped) {
return;
}
/* queue a callback in node's libuv thread */
AWS_NAPI_ENSURE(
NULL, aws_napi_queue_threadsafe_function(binding->on_stopped, s_on_simple_event_user_data_new(binding)));
}
static void s_on_attempting_connect(struct aws_mqtt5_client_binding *binding) {
if (!binding->on_attempting_connect) {
return;
}
/* queue a callback in node's libuv thread */
AWS_NAPI_ENSURE(
NULL,
aws_napi_queue_threadsafe_function(binding->on_attempting_connect, s_on_simple_event_user_data_new(binding)));
}
/* unions callback data needed for connection succes and failure as a convenience */
struct on_connection_result_user_data {
struct aws_allocator *allocator;
struct aws_mqtt5_client_binding *binding;
struct aws_mqtt5_packet_connack_storage connack_storage;
bool is_connack_valid;
int error_code;
struct aws_mqtt5_negotiated_settings settings;
};
static void s_on_connection_result_user_data_destroy(struct on_connection_result_user_data *connection_result_ud) {
if (connection_result_ud == NULL) {
return;
}
connection_result_ud->binding = s_aws_mqtt5_client_binding_release(connection_result_ud->binding);
aws_mqtt5_packet_connack_storage_clean_up(&connection_result_ud->connack_storage);
aws_mqtt5_negotiated_settings_clean_up(&connection_result_ud->settings);
aws_mem_release(connection_result_ud->allocator, connection_result_ud);
}
static struct on_connection_result_user_data *s_on_connection_result_user_data_new(
struct aws_allocator *allocator,
struct aws_mqtt5_client_binding *binding,
const struct aws_mqtt5_packet_connack_view *connack,
const struct aws_mqtt5_negotiated_settings *settings,
int error_code) {
struct on_connection_result_user_data *connection_result_ud =
aws_mem_calloc(allocator, 1, sizeof(struct on_connection_result_user_data));
connection_result_ud->allocator = allocator;
connection_result_ud->error_code = error_code;
connection_result_ud->binding = s_aws_mqtt5_client_binding_acquire(binding);
if (connack != NULL) {
if (aws_mqtt5_packet_connack_storage_init(&connection_result_ud->connack_storage, allocator, connack)) {
goto error;
}
connection_result_ud->is_connack_valid = true;
}
if (settings != NULL) {
if (aws_mqtt5_negotiated_settings_copy(settings, &connection_result_ud->settings)) {
goto error;
}
}
return connection_result_ud;
error:
s_on_connection_result_user_data_destroy(connection_result_ud);
return NULL;
}
static void s_on_connection_success(
struct aws_mqtt5_client_binding *binding,
const struct aws_mqtt5_packet_connack_view *connack,
const struct aws_mqtt5_negotiated_settings *settings) {
if (!binding->on_connection_success) {
return;
}
struct on_connection_result_user_data *connection_result_ud =
s_on_connection_result_user_data_new(binding->allocator, binding, connack, settings, AWS_ERROR_SUCCESS);
if (connection_result_ud == NULL) {
return;
}
/* queue a callback in node's libuv thread */
AWS_NAPI_ENSURE(NULL, aws_napi_queue_threadsafe_function(binding->on_connection_success, connection_result_ud));
}
static void s_on_connection_failure(
struct aws_mqtt5_client_binding *binding,
const struct aws_mqtt5_packet_connack_view *connack,
int error_code) {
if (!binding->on_connection_failure) {
return;
}
struct on_connection_result_user_data *connection_result_ud =
s_on_connection_result_user_data_new(binding->allocator, binding, connack, NULL, error_code);
if (connection_result_ud == NULL) {
return;
}
/* queue a callback in node's libuv thread */
AWS_NAPI_ENSURE(NULL, aws_napi_queue_threadsafe_function(binding->on_connection_failure, connection_result_ud));
}
struct on_disconnection_user_data {
struct aws_allocator *allocator;
struct aws_mqtt5_client_binding *binding;
struct aws_mqtt5_packet_disconnect_storage disconnect_storage;
bool is_disconnect_valid;
int error_code;
};
static void s_on_disconnection_user_data_destroy(struct on_disconnection_user_data *disconnection_ud) {
if (disconnection_ud == NULL) {
return;
}
disconnection_ud->binding = s_aws_mqtt5_client_binding_release(disconnection_ud->binding);
aws_mqtt5_packet_disconnect_storage_clean_up(&disconnection_ud->disconnect_storage);
aws_mem_release(disconnection_ud->allocator, disconnection_ud);
}
static struct on_disconnection_user_data *s_on_disconnection_user_data_new(
struct aws_allocator *allocator,
struct aws_mqtt5_client_binding *binding,
const struct aws_mqtt5_packet_disconnect_view *disconnect,
int error_code) {
struct on_disconnection_user_data *disconnection_ud =
aws_mem_calloc(allocator, 1, sizeof(struct on_disconnection_user_data));
disconnection_ud->allocator = allocator;
disconnection_ud->error_code = error_code;
disconnection_ud->binding = s_aws_mqtt5_client_binding_acquire(binding);
if (disconnect != NULL) {
if (aws_mqtt5_packet_disconnect_storage_init(&disconnection_ud->disconnect_storage, allocator, disconnect)) {
goto error;
}
disconnection_ud->is_disconnect_valid = true;
}
return disconnection_ud;
error:
s_on_disconnection_user_data_destroy(disconnection_ud);
return NULL;
}
static void s_on_disconnection(
struct aws_mqtt5_client_binding *binding,
const struct aws_mqtt5_packet_disconnect_view *disconnect,
int error_code) {
if (!binding->on_disconnection) {
return;
}
struct on_disconnection_user_data *disconnection_ud =
s_on_disconnection_user_data_new(binding->allocator, binding, disconnect, error_code);
if (disconnection_ud == NULL) {
return;
}
/* queue a callback in node's libuv thread */
AWS_NAPI_ENSURE(NULL, aws_napi_queue_threadsafe_function(binding->on_disconnection, disconnection_ud));
}
static void s_lifecycle_event_callback(const struct aws_mqtt5_client_lifecycle_event *event) {
struct aws_mqtt5_client_binding *binding = event->user_data;
switch (event->event_type) {
case AWS_MQTT5_CLET_STOPPED:
s_on_stopped(binding);
break;
case AWS_MQTT5_CLET_ATTEMPTING_CONNECT:
s_on_attempting_connect(binding);
break;
case AWS_MQTT5_CLET_CONNECTION_SUCCESS:
s_on_connection_success(binding, event->connack_data, event->settings);
break;
case AWS_MQTT5_CLET_CONNECTION_FAILURE:
s_on_connection_failure(binding, event->connack_data, event->error_code);
break;
case AWS_MQTT5_CLET_DISCONNECTION:
s_on_disconnection(binding, event->disconnect_data, event->error_code);
break;
default:
break;
}
}
typedef void(napi_threadsafe_function_type)(napi_env env, napi_value function, void *context, void *user_data);
/* in-node/libuv-thread function to trigger the emission of a STOPPED client lifecycle event */
static void s_napi_on_stopped(napi_env env, napi_value function, void *context, void *user_data) {
(void)context;
struct on_simple_event_user_data *simple_ud = user_data;
struct aws_mqtt5_client_binding *binding = simple_ud->binding;
if (env) {
napi_value params[1];
const size_t num_params = AWS_ARRAY_SIZE(params);
/*
* If we can't resolve the weak ref to the mqtt5 client, then it's been garbage collected and we should not
* do anything.
*/
params[0] = NULL;
if (napi_get_reference_value(env, binding->node_mqtt5_client_ref, ¶ms[0]) != napi_ok || params[0] == NULL) {
AWS_LOGF_INFO(
AWS_LS_NODEJS_CRT_GENERAL,
"id=%p s_on_stopped_call - mqtt5_client node wrapper no longer resolvable",
(void *)binding->client);
goto done;
}
AWS_NAPI_ENSURE(
env, aws_napi_dispatch_threadsafe_function(env, binding->on_stopped, NULL, function, num_params, params));
}
done:
s_on_simple_event_user_data_destroy(simple_ud);
}
/* in-node/libuv-thread function to trigger the emission of an ATTEMPTING_CONNECT client lifecycle event */
static void s_napi_on_attempting_connect(napi_env env, napi_value function, void *context, void *user_data) {
(void)context;
struct on_simple_event_user_data *simple_ud = user_data;
struct aws_mqtt5_client_binding *binding = simple_ud->binding;
if (env) {
napi_value params[1];
const size_t num_params = AWS_ARRAY_SIZE(params);
/*
* If we can't resolve the weak ref to the mqtt5 client, then it's been garbage collected and we should not
* do anything.
*/
params[0] = NULL;
if (napi_get_reference_value(env, binding->node_mqtt5_client_ref, ¶ms[0]) != napi_ok || params[0] == NULL) {
AWS_LOGF_INFO(
AWS_LS_NODEJS_CRT_GENERAL,
"id=%p s_on_attempting_connect_call - mqtt5_client node wrapper no longer resolvable",
(void *)binding->client);
goto done;
}
AWS_NAPI_ENSURE(
env,
aws_napi_dispatch_threadsafe_function(
env, binding->on_attempting_connect, NULL, function, num_params, params));
}
done:
s_on_simple_event_user_data_destroy(simple_ud);
}
/* utility function to attach native-specified user properties to a napi object as an array of user property objects */
static int s_attach_object_property_user_properties(
napi_value napi_packet,
napi_env env,
size_t user_property_count,
const struct aws_mqtt5_user_property *user_properties) {
if (env == NULL) {
return aws_raise_error(AWS_CRT_NODEJS_ERROR_THREADSAFE_FUNCTION_NULL_NAPI_ENV);
}
napi_value user_property_array = NULL;
AWS_NAPI_CALL(env, napi_create_array_with_length(env, user_property_count, &user_property_array), {
return aws_raise_error(AWS_CRT_NODEJS_ERROR_NAPI_FAILURE);
});
for (size_t i = 0; i < user_property_count; ++i) {
const struct aws_mqtt5_user_property *property = &user_properties[i];
napi_value user_property_value = NULL;
AWS_NAPI_CALL(env, napi_create_object(env, &user_property_value), {
return aws_raise_error(AWS_CRT_NODEJS_ERROR_NAPI_FAILURE);
});
if (aws_napi_attach_object_property_string(user_property_value, env, AWS_NAPI_KEY_NAME, property->name) ||
aws_napi_attach_object_property_string(user_property_value, env, AWS_NAPI_KEY_VALUE, property->value)) {
return AWS_OP_ERR;
}
AWS_NAPI_CALL(env, napi_set_element(env, user_property_array, (uint32_t)i, user_property_value), {
return aws_raise_error(AWS_CRT_NODEJS_ERROR_NAPI_FAILURE);
});
}
AWS_NAPI_CALL(env, napi_set_named_property(env, napi_packet, AWS_NAPI_KEY_USER_PROPERTIES, user_property_array), {
return aws_raise_error(AWS_CRT_NODEJS_ERROR_NAPI_FAILURE);
});
return AWS_OP_SUCCESS;
}
/* Builds a napi object that represents a CONNACK packet, matching the AwsMqtt5PacketConnack interface */
static int s_create_napi_connack_packet(
napi_env env,
const struct on_connection_result_user_data *connection_result_ud,
napi_value *packet_out) {
if (env == NULL) {
return aws_raise_error(AWS_CRT_NODEJS_ERROR_THREADSAFE_FUNCTION_NULL_NAPI_ENV);
}
if (!connection_result_ud->is_connack_valid) {
AWS_NAPI_CALL(
env, napi_get_null(env, packet_out), { return aws_raise_error(AWS_CRT_NODEJS_ERROR_NAPI_FAILURE); });
return AWS_OP_SUCCESS;
}
napi_value packet = NULL;
AWS_NAPI_CALL(
env, napi_create_object(env, &packet), { return aws_raise_error(AWS_CRT_NODEJS_ERROR_NAPI_FAILURE); });
const struct aws_mqtt5_packet_connack_view *connack_view = &connection_result_ud->connack_storage.storage_view;
if (aws_napi_attach_object_property_u32(packet, env, AWS_NAPI_KEY_TYPE, (uint32_t)AWS_MQTT5_PT_CONNACK)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_boolean(
packet, env, AWS_NAPI_KEY_SESSION_PRESENT, connack_view->session_present)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_u32(
packet, env, AWS_NAPI_KEY_REASON_CODE, (uint32_t)connack_view->reason_code)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_optional_u32(
packet, env, AWS_NAPI_KEY_SESSION_EXPIRY_INTERVAL, connack_view->session_expiry_interval)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_optional_u16(
packet, env, AWS_NAPI_KEY_RECEIVE_MAXIMUM, connack_view->receive_maximum)) {
return AWS_OP_ERR;
}
if (connack_view->maximum_qos != NULL) {
uint32_t maximum_qos = *connack_view->maximum_qos;
if (aws_napi_attach_object_property_u32(packet, env, AWS_NAPI_KEY_MAXIMUM_QOS, maximum_qos)) {
return AWS_OP_ERR;
}
}
if (aws_napi_attach_object_property_optional_boolean(
packet, env, AWS_NAPI_KEY_RETAIN_AVAILABLE, connack_view->retain_available)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_optional_u32(
packet, env, AWS_NAPI_KEY_MAXIMUM_PACKET_SIZE, connack_view->maximum_packet_size)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_optional_string(
packet, env, AWS_NAPI_KEY_ASSIGNED_CLIENT_IDENTIFIER, connack_view->assigned_client_identifier)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_optional_u16(
packet, env, AWS_NAPI_KEY_TOPIC_ALIAS_MAXIMUM, connack_view->topic_alias_maximum)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_optional_string(
packet, env, AWS_NAPI_KEY_REASON_STRING, connack_view->reason_string)) {
return AWS_OP_ERR;
}
if (s_attach_object_property_user_properties(
packet, env, connack_view->user_property_count, connack_view->user_properties)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_optional_boolean(
packet,
env,
AWS_NAPI_KEY_WILDCARD_SUBSCRIPTIONS_AVAILABLE,
connack_view->wildcard_subscriptions_available)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_optional_boolean(
packet,
env,
AWS_NAPI_KEY_SUBSCRIPTION_IDENTIFIERS_AVAILABLE,
connack_view->subscription_identifiers_available)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_optional_boolean(
packet, env, AWS_NAPI_KEY_SHARED_SUBSCRIPTIONS_AVAILABLE, connack_view->shared_subscriptions_available)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_optional_u16(
packet, env, AWS_NAPI_KEY_SERVER_KEEP_ALIVE, connack_view->server_keep_alive)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_optional_string(
packet, env, AWS_NAPI_KEY_RESPONSE_INFORMATION, connack_view->response_information)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_optional_string(
packet, env, AWS_NAPI_KEY_SERVER_REFERENCE, connack_view->server_reference)) {
return AWS_OP_ERR;
}
*packet_out = packet;
return AWS_OP_SUCCESS;
}
/* Builds a napi object that represents connection negotiated settings, using the Mqtt5NegotiatedSettings interface */
static int s_create_napi_negotiated_settings(
napi_env env,
const struct aws_mqtt5_negotiated_settings *settings,
napi_value *value_out) {
if (env == NULL) {
return aws_raise_error(AWS_CRT_NODEJS_ERROR_THREADSAFE_FUNCTION_NULL_NAPI_ENV);
}
napi_value napi_settings = NULL;
AWS_NAPI_CALL(
env, napi_create_object(env, &napi_settings), { return aws_raise_error(AWS_CRT_NODEJS_ERROR_NAPI_FAILURE); });
uint32_t maximum_qos = settings->maximum_qos;
if (aws_napi_attach_object_property_u32(napi_settings, env, AWS_NAPI_KEY_MAXIMUM_QOS, maximum_qos)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_u32(
napi_settings, env, AWS_NAPI_KEY_SESSION_EXPIRY_INTERVAL, settings->session_expiry_interval)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_u32(
napi_settings,
env,
AWS_NAPI_KEY_RECEIVE_MAXIMUM_FROM_SERVER,
(uint32_t)settings->receive_maximum_from_server)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_u32(
napi_settings, env, AWS_NAPI_KEY_MAXIMUM_PACKET_SIZE_TO_SERVER, settings->maximum_packet_size_to_server)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_u16(
napi_settings, env, AWS_NAPI_KEY_TOPIC_ALIAS_MAXIMUM_TO_SERVER, settings->topic_alias_maximum_to_server)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_u16(
napi_settings, env, AWS_NAPI_KEY_TOPIC_ALIAS_MAXIMUM_TO_CLIENT, settings->topic_alias_maximum_to_client)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_u32(
napi_settings, env, AWS_NAPI_KEY_SERVER_KEEP_ALIVE, (uint32_t)settings->server_keep_alive)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_boolean(
napi_settings, env, AWS_NAPI_KEY_RETAIN_AVAILABLE, settings->retain_available)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_boolean(
napi_settings,
env,
AWS_NAPI_KEY_WILDCARD_SUBSCRIPTIONS_AVAILABLE,
settings->wildcard_subscriptions_available)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_boolean(
napi_settings,
env,
AWS_NAPI_KEY_SUBSCRIPTION_IDENTIFIERS_AVAILABLE,
settings->subscription_identifiers_available)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_boolean(
napi_settings,
env,
AWS_NAPI_KEY_SHARED_SUBSCRIPTIONS_AVAILABLE,
settings->shared_subscriptions_available)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_boolean(
napi_settings, env, AWS_NAPI_KEY_REJOINED_SESSION, settings->rejoined_session)) {
return AWS_OP_ERR;
}
if (aws_napi_attach_object_property_string(
napi_settings, env, AWS_NAPI_KEY_CLIENT_ID, aws_byte_cursor_from_buf(&settings->client_id_storage))) {
return AWS_OP_ERR;
}
*value_out = napi_settings;
return AWS_OP_SUCCESS;
}
/* in-node/libuv-thread function to trigger the emission of a CONNECTION_SUCCESS client lifecycle event */
static void s_napi_on_connection_success(napi_env env, napi_value function, void *context, void *user_data) {
(void)context;
struct on_connection_result_user_data *connection_result_ud = user_data;
struct aws_mqtt5_client_binding *binding = connection_result_ud->binding;
if (env) {
napi_value params[3];
const size_t num_params = AWS_ARRAY_SIZE(params);
/*
* If we can't resolve the weak ref to the mqtt5 client, then it's been garbage collected and we should not
* do anything.
*/
params[0] = NULL;
if (napi_get_reference_value(env, binding->node_mqtt5_client_ref, ¶ms[0]) != napi_ok || params[0] == NULL) {
AWS_LOGF_INFO(
AWS_LS_NODEJS_CRT_GENERAL,
"id=%p s_on_connection_success_call - mqtt5_client node wrapper no longer resolvable",
(void *)binding->client);
goto done;
}
if (s_create_napi_connack_packet(env, connection_result_ud, ¶ms[1])) {
AWS_LOGF_ERROR(
AWS_LS_NODEJS_CRT_GENERAL,
"id=%p s_on_connection_success_call - failed to create connack object",
(void *)binding->client);
goto done;
}
if (s_create_napi_negotiated_settings(env, &connection_result_ud->settings, ¶ms[2])) {
AWS_LOGF_ERROR(
AWS_LS_NODEJS_CRT_GENERAL,
"id=%p s_on_connection_success_call - failed to create negotiated settings object",
(void *)binding->client);
goto done;
}
AWS_NAPI_ENSURE(
env,
aws_napi_dispatch_threadsafe_function(
env, binding->on_connection_success, NULL, function, num_params, params));
}
done:
s_on_connection_result_user_data_destroy(connection_result_ud);
}
/* in-node/libuv-thread function to trigger the emission of a CONNECTION_FAILURE client lifecycle event */
static void s_napi_on_connection_failure(napi_env env, napi_value function, void *context, void *user_data) {
(void)context;
struct on_connection_result_user_data *connection_result_ud = user_data;
struct aws_mqtt5_client_binding *binding = connection_result_ud->binding;
if (env) {
napi_value params[3];
const size_t num_params = AWS_ARRAY_SIZE(params);
/*
* If we can't resolve the weak ref to the mqtt5 client, then it's been garbage collected and we should not
* do anything.
*/
params[0] = NULL;
if (napi_get_reference_value(env, binding->node_mqtt5_client_ref, ¶ms[0]) != napi_ok || params[0] == NULL) {
AWS_LOGF_INFO(
AWS_LS_NODEJS_CRT_GENERAL,
"id=%p s_on_connection_failure_call - mqtt5_client node wrapper no longer resolvable",
(void *)binding->client);
goto done;
}
AWS_NAPI_CALL(env, napi_create_uint32(env, connection_result_ud->error_code, ¶ms[1]), { goto done; });
if (s_create_napi_connack_packet(env, connection_result_ud, ¶ms[2])) {
AWS_LOGF_ERROR(
AWS_LS_NODEJS_CRT_GENERAL,
"id=%p s_on_connection_failure_call - failed to create connack object",
(void *)binding->client);
goto done;
}
AWS_NAPI_ENSURE(
env,
aws_napi_dispatch_threadsafe_function(
env, binding->on_connection_failure, NULL, function, num_params, params));
}
done:
s_on_connection_result_user_data_destroy(connection_result_ud);
}
/* Builds a napi object that represents DISCONNECT packet, using the AwsMqtt5PacketDisconnect interface */
static int s_create_napi_disconnect_packet(
napi_env env,
const struct on_disconnection_user_data *disconnection_ud,
napi_value *packet_out) {
if (env == NULL) {
return aws_raise_error(AWS_CRT_NODEJS_ERROR_THREADSAFE_FUNCTION_NULL_NAPI_ENV);
}
if (!disconnection_ud->is_disconnect_valid) {
AWS_NAPI_CALL(
env, napi_get_null(env, packet_out), { return aws_raise_error(AWS_CRT_NODEJS_ERROR_NAPI_FAILURE); });
return AWS_OP_SUCCESS;
}
napi_value packet = NULL;