-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathendpoint.hpp
10977 lines (10293 loc) · 409 KB
/
endpoint.hpp
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 Takatoshi Kondo 2015
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if !defined(MQTT_ENDPOINT_HPP)
#define MQTT_ENDPOINT_HPP
#include <mqtt/variant.hpp> // should be top to configure variant limit
#include <string>
#include <vector>
#include <deque>
#include <functional>
#include <set>
#include <memory>
#include <mutex>
#include <atomic>
#include <boost/any.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/system/error_code.hpp>
#include <boost/assert.hpp>
#include <mqtt/any.hpp>
#include <mqtt/fixed_header.hpp>
#include <mqtt/remaining_length.hpp>
#include <mqtt/utf8encoded_strings.hpp>
#include <mqtt/connect_flags.hpp>
#include <mqtt/will.hpp>
#include <mqtt/session_present.hpp>
#include <mqtt/qos.hpp>
#include <mqtt/publish.hpp>
#include <mqtt/connect_return_code.hpp>
#include <mqtt/exception.hpp>
#include <mqtt/tcp_endpoint.hpp>
#include <mqtt/unique_scope_guard.hpp>
#include <mqtt/shared_scope_guard.hpp>
#include <mqtt/message_variant.hpp>
#include <mqtt/two_byte_util.hpp>
#include <mqtt/four_byte_util.hpp>
#include <mqtt/packet_id_type.hpp>
#include <mqtt/optional.hpp>
#include <mqtt/property_variant.hpp>
#include <mqtt/property_parse.hpp>
#include <mqtt/protocol_version.hpp>
#include <mqtt/reason_code.hpp>
#include <mqtt/subscribe.hpp>
#if defined(MQTT_USE_WS)
#include <mqtt/ws_endpoint.hpp>
#endif // defined(MQTT_USE_WS)
namespace mqtt {
namespace as = boost::asio;
namespace mi = boost::multi_index;
template <typename Socket, typename Mutex = std::mutex, template<typename...> class LockGuard = std::lock_guard, std::size_t PacketIdBytes = 2>
class endpoint : public std::enable_shared_from_this<endpoint<Socket, Mutex, LockGuard, PacketIdBytes>> {
using this_type = endpoint<Socket, Mutex, LockGuard, PacketIdBytes>;
public:
using async_handler_t = std::function<void(boost::system::error_code const& ec)>;
using packet_id_t = typename packet_id_type<PacketIdBytes>::type;
/**
* @brief Constructor for client
*/
endpoint(protocol_version version = protocol_version::undetermined, bool async_send_store = false)
:async_send_store_{async_send_store},
h_mqtt_message_processed_(
[this]
(async_handler_t func) {
async_read_control_packet_type(std::move(func));
}
),
version_(version)
{}
/**
* @brief Constructor for server.
* socket should have already been connected with another endpoint.
*/
explicit endpoint(std::shared_ptr<Socket> socket, protocol_version version = protocol_version::undetermined, bool async_send_store = false)
:socket_(std::move(socket)),
connected_(true),
async_send_store_{async_send_store},
h_mqtt_message_processed_(
[this]
(async_handler_t func) {
async_read_control_packet_type(std::move(func));
}
),
version_(version)
{}
// MQTT Common handlers
/**
* @brief Pingreq handler
* See http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718086<BR>
* 3.13 PINGREQ – PING request
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using pingreq_handler = std::function<bool()>;
/**
* @brief Pingresp handler
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901200<BR>
* 3.13 PINGRESP – PING response
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using pingresp_handler = std::function<bool()>;
// MQTT v3_1_1 handlers
/**
* @brief Connect handler
* @param client_id
* Client Identifier.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349245<BR>
* 3.1.3.1 Client Identifier
* @param username
* User Name.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349245<BR>
* 3.1.3.4 User Name
* @param password
* Password.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349246<BR>
* 3.1.3.5 Password
* @param will
* Will. It contains retain, QoS, topic, and message.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349232<BR>
* 3.1.2.5 Will Flag<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349233<BR>
* 3.1.2.6 Will QoS<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349234<BR>
* 3.1.2.7 Will Retain<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349243<BR>
* 3.1.3.2 Will Topic<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349244<BR>
* 3.1.3.3 Will Message<BR>
* @param clean_session
* Clean Session<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349231<BR>
* 3.1.2.4 Clean Session
* @param keep_alive
* Keep Alive<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349237<BR>
* 3.1.2.10 Keep Alive
* @return if the handler returns true, then continue receiving, otherwise quit.
*
*/
using connect_handler = std::function<
bool(mqtt::string_view client_id,
mqtt::optional<mqtt::string_view> username,
mqtt::optional<mqtt::string_view> password,
mqtt::optional<will> will,
bool clean_session,
std::uint16_t keep_alive)>;
/**
* @brief Connack handler
* @param session_present
* Session present flag.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc398718035<BR>
* 3.2.2.2 Session Present
* @param return_code
* connect_return_code<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc398718035<BR>
* 3.2.2.3 Connect Return code
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using connack_handler = std::function<bool(bool session_present, std::uint8_t return_code)>;
/**
* @brief Publish handler
* @param fixed_header
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc398718038<BR>
* 3.3.1 Fixed header<BR>
* You can check the fixed header using mqtt::publish functions.
* @param packet_id
* packet identifier<BR>
* If received publish's QoS is 0, packet_id is mqtt::nullopt.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc398718039<BR>
* 3.3.2 Variable header
* @param topic_name
* Topic name
* @param contents
* Published contents
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using publish_handler = std::function<bool(std::uint8_t fixed_header,
mqtt::optional<packet_id_t> packet_id,
mqtt::string_view topic_name,
mqtt::string_view contents)>;
/**
* @brief Puback handler
* @param packet_id
* packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc398718045<BR>
* 3.4.2 Variable header
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using puback_handler = std::function<bool(packet_id_t packet_id)>;
/**
* @brief Pubrec handler
* @param packet_id
* packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc398718050<BR>
* 3.5.2 Variable header
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using pubrec_handler = std::function<bool(packet_id_t packet_id)>;
/**
* @brief Pubrel handler
* @param packet_id
* packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349791<BR>
* 3.6.2 Variable header
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using pubrel_handler = std::function<bool(packet_id_t packet_id)>;
/**
* @brief Pubcomp handler
* @param packet_id
* packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc398718060<BR>
* 3.7.2 Variable header
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using pubcomp_handler = std::function<bool(packet_id_t packet_id)>;
/**
* @brief Subscribe handler
* @param packet_id packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349801<BR>
* 3.8.2 Variable header
* @param entries
* Collection of a pair of Topic Filter and QoS.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349802<BR>
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using subscribe_handler = std::function<bool(packet_id_t packet_id,
std::vector<std::tuple<mqtt::string_view, std::uint8_t>> entries)>;
/**
* @brief Suback handler
* @param packet_id packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc398718070<BR>
* 3.9.2 Variable header
* @param qoss
* Collection of QoS that is corresponding to subscribed topic order.<BR>
* If subscription is failure, the value is mqtt::nullopt.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc398718071<BR>
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using suback_handler = std::function<bool(packet_id_t packet_id,
std::vector<mqtt::optional<std::uint8_t>> qoss)>;
/**
* @brief Unsubscribe handler
* @param packet_id packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc385349810<BR>
* 3.10.2 Variable header
* @param topics
* Collection of Topic Filters<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc384800448<BR>
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using unsubscribe_handler = std::function<bool(packet_id_t packet_id,
std::vector<mqtt::string_view> topics)>;
/**
* @brief Unsuback handler
* @param packet_id packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc398718045<BR>
* 3.11.2 Variable header
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using unsuback_handler = std::function<bool(packet_id_t)>;
/**
* @brief Disconnect handler
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc384800463<BR>
* 3.14 DISCONNECT – Disconnect notification
*/
using disconnect_handler = std::function<void()>;
// MQTT v5 handlers
/**
* @brief Connect handler
* @param client_id
* Client Identifier.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901059<BR>
* 3.1.3.1 Client Identifier
* @param username
* User Name.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901071<BR>
* 3.1.3.4 User Name
* @param password
* Password.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901072<BR>
* 3.1.3.5 Password
* @param will
* Will. It contains retain, QoS, propertied, topic, and message.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901040<BR>
* 3.1.2.5 Will Flag<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901041<BR>
* 3.1.2.6 Will QoS<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901042<BR>
* 3.1.2.7 Will Retain<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901060<BR>
* 3.1.3.2 Will Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901069<BR>
* 3.1.3.3 Will Topic<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901070<BR>
* 3.1.3.3 Will Payload<BR>
* @param clean_start
* Clean Start<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901039<BR>
* 3.1.2.4 Clean Session
* @param keep_alive
* Keep Alive<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901045<BR>
* 3.1.2.10 Keep Alive
* @param props
* Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901046<BR>
* 3.1.2.11 CONNECT Properties
* @return if the handler returns true, then continue receiving, otherwise quit.
*
*/
using v5_connect_handler = std::function<
bool(mqtt::string_view client_id,
mqtt::optional<mqtt::string_view> username,
mqtt::optional<mqtt::string_view> password,
mqtt::optional<will> will,
bool clean_start,
std::uint16_t keep_alive,
std::vector<v5::property_variant> props)
>;
/**
* @brief Connack handler
* @param session_present
* Session present flag.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901078<BR>
* 3.2.2.1.1 Session Present
* @param reason_code
* Connect Reason Code<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901079<BR>
* 3.2.2.2 Connect Reason code
* @param props
* Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901080<BR>
* 3.2.2.3 CONNACK Properties
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using v5_connack_handler = std::function<
bool(bool session_present,
std::uint8_t reason_code,
std::vector<v5::property_variant> props)
>;
/**
* @brief Publish handler
* @param fixed_header
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901101<BR>
* 3.3.1 Fixed header<BR>
* You can check the fixed header using mqtt::publish functions.
* @param packet_id
* packet identifier<BR>
* If received publish's QoS is 0, packet_id is mqtt::nullopt.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901108<BR>
* 3.3.2.2 Packet Identifier
* @param topic_name
* Topic name<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901107<BR>
* 3.3.2.1 Topic Name<BR>
* @param contents
* Publish Payload<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901119<BR>
* 3.3.3 PUBLISH Payload
* @param props
* Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901109<BR>
* 3.3.2.3 PUBLISH Properties
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using v5_publish_handler = std::function<
bool(std::uint8_t fixed_header,
mqtt::optional<packet_id_t> packet_id,
mqtt::string_view topic_name,
mqtt::string_view contents,
std::vector<v5::property_variant> props)
>;
/**
* @brief Puback handler
* @param packet_id
* packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901123<BR>
* 3.4.2 Variable header
* @param reason_code
* PUBACK Reason Code<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901124<BR>
* 3.4.2.1 PUBACK Reason Code
* @param props
* Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901125<BR>
* 3.4.2.2 PUBACK Properties
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using v5_puback_handler = std::function<
bool(packet_id_t packet_id,
std::uint8_t reason_code,
std::vector<v5::property_variant> props)
>;
/**
* @brief Pubrec handler
* @param packet_id
* packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901133<BR>
* 3.5.2 Variable header
* @param reason_code
* PUBREC Reason Code<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901134<BR>
* 3.5.2.1 PUBREC Reason Code
* @param props
* Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901135<BR>
* 3.5.2.2 PUBREC Properties
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using v5_pubrec_handler = std::function<
bool(packet_id_t packet_id,
std::uint8_t reason_code,
std::vector<v5::property_variant> props)
>;
/**
* @brief Pubrel handler
* @param packet_id
* packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901143<BR>
* 3.6.2 Variable header
* @param reason_code
* PUBREL Reason Code<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901144<BR>
* 3.6.2.1 PUBREL Reason Code
* @param props
* Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901145<BR>
* 3.6.2.2 PUBREL Properties
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using v5_pubrel_handler = std::function<
bool(packet_id_t packet_id,
std::uint8_t reason_code,
std::vector<v5::property_variant> props)
>;
/**
* @brief Pubcomp handler
* @param packet_id
* packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901153<BR>
* 3.7.2 Variable header
* @param reason_code
* PUBCOMP Reason Code<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901154<BR>
* 3.7.2.1 PUBCOMP Reason Code
* @param props
* Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901155<BR>
* 3.7.2.2 PUBCOMP Properties
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using v5_pubcomp_handler = std::function<
bool(packet_id_t packet_id,
std::uint8_t reason_code,
std::vector<v5::property_variant> props)
>;
/**
* @brief Subscribe handler
* @param packet_id packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901163<BR>
* 3.8.2 Variable header
* @param entries
* Collection of a pair of Topic Filter and QoS.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901168<BR>
* @param props
* Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901164<BR>
* 3.8.2.1 SUBSCRIBE Properties
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using v5_subscribe_handler = std::function<
bool(packet_id_t packet_id,
std::vector<std::tuple<mqtt::string_view, std::uint8_t>> entries,
std::vector<v5::property_variant> props)
>;
/**
* @brief Suback handler
* @param packet_id packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901173<BR>
* 3.9.2 Variable header
* @param reasons
* Collection of reason_code.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901178<BR>
* 3.9.3 SUBACK Payload
* @param props
* Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901174<BR>
* 3.9.2.1 SUBACK Properties
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using v5_suback_handler = std::function<
bool(packet_id_t packet_id,
std::vector<std::uint8_t> reasons,
std::vector<v5::property_variant> props)
>;
/**
* @brief Unsubscribe handler
* @param packet_id packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901181<BR>
* 3.10.2 Variable header
* @param topics
* Collection of Topic Filters<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901185<BR>
* 3.10.3 UNSUBSCRIBE Payload
* @param props
* Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901182<BR>
* 3.10.2.1 UNSUBSCRIBE Properties
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using v5_unsubscribe_handler = std::function<
bool(packet_id_t packet_id,
std::vector<mqtt::string_view> topics,
std::vector<v5::property_variant> props)
>;
/**
* @brief Unsuback handler
* @param packet_id packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901189<BR>
* 3.11.2 Variable header
* @param reasons
* Collection of reason_code.<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901194<BR>
* 3.11.3 UNSUBACK Payload
* @param props
* Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901190<BR>
* 3.11.2.1 UNSUBACK Properties
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using v5_unsuback_handler = std::function<
bool(packet_id_t,
std::vector<std::uint8_t> reasons,
std::vector<v5::property_variant> props)
>;
/**
* @brief Disconnect handler
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901205<BR>
* 3.14 DISCONNECT – Disconnect notification
* @param reason_code
* DISCONNECT Reason Code<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901208<BR>
* 3.14.2.1 Disconnect Reason Code
* @param props
* Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901209<BR>
* 3.14.2.2 DISCONNECT Properties
*/
using v5_disconnect_handler = std::function<
void(std::uint8_t reason_code,
std::vector<v5::property_variant> props)
>;
/**
* @brief Auth handler
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901217<BR>
* 3.15 AUTH – Authentication exchange
* @param reason_code
* AUTH Reason Code<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901220<BR>
* 3.15.2.1 Authenticate Reason Code
* @param props
* Properties<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901221<BR>
* 3.15.2.2 AUTH Properties
* @return if the handler returns true, then continue receiving, otherwise quit.
*/
using v5_auth_handler = std::function<
bool(std::uint8_t reason_code,
std::vector<v5::property_variant> props)
>;
// Original handlers
/**
* @brief Close handler
*
* This handler is called if the client called `disconnect()` and the server closed the socket cleanly.
* If the socket is closed by other reasons, error_handler is called.
*/
using close_handler = std::function<void()>;
/**
* @brief Error handler
*
* This handler is called if the socket is closed without client's `disconnect()` call.
*
* @param ec error code
*/
using error_handler = std::function<void(boost::system::error_code const& ec)>;
/**
* @brief Publish response sent handler
* This function is called just after puback sent on QoS1, or pubcomp sent on QoS2.
* @param packet_id
* packet identifier<BR>
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901026<BR>
* 2.2.1 Packet Identifier
*/
using pub_res_sent_handler = std::function<void(packet_id_t packet_id)>;
/**
* @brief Serialize publish handler
* You can serialize the publish message.
* To restore the message, use restore_serialized_message().
* @param msg publish message
*/
using serialize_publish_message_handler = std::function<void(basic_publish_message<PacketIdBytes> msg)>;
/**
* @brief Serialize publish handler
* You can serialize the publish message.
* To restore the message, use restore_serialized_message().
* @param msg v5::publish message
*/
using serialize_v5_publish_message_handler = std::function<void(v5::basic_publish_message<PacketIdBytes> msg)>;
/**
* @brief Serialize publish handler
* You can serialize the publish message.
* To restore the message, use restore_serialized_message().
* @param packet_id packet identifier of the serializing message
* @param data pointer to the serializing message
* @param size size of the serializing message
*/
using serialize_publish_handler = std::function<void(packet_id_t packet_id, char const* data, std::size_t size)>;
/**
* @brief Serialize pubrel handler
* You can serialize the pubrel message.
* If your storage has already had the publish message that has the same packet_id,
* then you need to replace the publish message to pubrel message.
* To restore the message, use restore_serialized_message().
* @param msg pubrel message
*/
using serialize_pubrel_message_handler = std::function<void(basic_pubrel_message<PacketIdBytes> msg)>;
/**
* @brief Serialize pubrel handler
* You can serialize the pubrel message.
* If your storage has already had the publish message that has the same packet_id,
* then you need to replace the publish message to pubrel message.
* To restore the message, use restore_serialized_message().
* @param msg pubrel message
*/
using serialize_v5_pubrel_message_handler = std::function<void(v5::basic_pubrel_message<PacketIdBytes> msg)>;
/**
* @brief Serialize pubrel handler
* You can serialize the pubrel message.
* If your storage has already had the publish message that has the same packet_id,
* then you need to replace the publish message to pubrel message.
* To restore the message, use restore_serialized_message().
* @param packet_id packet identifier of the serializing message
* @param data pointer to the serializing message
* @param size size of the serializing message
*/
using serialize_pubrel_handler = std::function<void(packet_id_t packet_id, char const* data, std::size_t size)>;
/**
* @brief Remove serialized message
* @param packet_id packet identifier of the removing message
*/
using serialize_remove_handler = std::function<void(packet_id_t packet_id)>;
/**
* @brief Pre-send handler
* This handler is called when any mqtt control packet is decided to send.
*/
using pre_send_handler = std::function<void()>;
/**
* @brief is valid length handler
* This handler is called when remaining length is received.
* @param control_packet_type control_packet_type that has variable length
* @param remaining length
* @return true if check is success, otherwise false
*/
using is_valid_length_handler =
std::function<bool(std::uint8_t control_packet_type, std::size_t remaining_length)>;
/**
* @brief next read handler
* This handler is called when the current mqtt message has been processed.
* @param func A callback function that is called when async operation will finish.
*/
using mqtt_message_processed_handler =
std::function<void(async_handler_t func)>;
endpoint(this_type const&) = delete;
endpoint(this_type&&) = delete;
endpoint& operator=(this_type const&) = delete;
endpoint& operator=(this_type&&) = delete;
/**
* @brief Get clean session.
*
* See http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349231<BR>
* 3.1.2.4 Clean Session<BR>
* After constructing a endpoint, the clean session is set to false.
* @return clean session
*/
bool clean_session() const {
return clean_session_;
}
/**
* @brief Get clean start.
*
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901039<BR>
* 3.1.2.4 Clean Start<BR>
* After constructing a endpoint, the clean start is set to false.
* @return clean start
*/
bool clean_start() const {
return clean_session();
}
/**
* @brief Set auto publish response mode.
* @param b set value
* @param async auto publish ressponse send asynchronous
*
* When set auto publish response mode to true, puback, pubrec, pubrel,and pub comp automatically send.<BR>
*/
void set_auto_pub_response(bool b = true, bool async = true) {
auto_pub_response_ = b;
auto_pub_response_async_ = async;
}
// MQTT Common handlers
/**
* @brief Set pingreq handler
* @param h handler
*/
void set_pingreq_handler(pingreq_handler h = pingreq_handler()) {
h_pingreq_ = std::move(h);
}
/**
* @brief Set pingresp handler
* @param h handler
*/
void set_pingresp_handler(pingresp_handler h = pingresp_handler()) {
h_pingresp_ = std::move(h);
}
/**
* @brief Get pingreq handler
* @return handler
*/
pingreq_handler const& get_pingreq_handler() const {
return h_pingreq_;
}
/**
* @brief Get pingresp handler
* @return handler
*/
pingresp_handler const& get_pingresp_handler() const {
return h_pingresp_;
}
// MQTT v3_1_1 handlers
/**
* @brief Set connect handler
* @param h handler
*/
void set_connect_handler(connect_handler h = connect_handler()) {
h_connect_ = std::move(h);
}
/**
* @brief Set connack handler
* @param h handler
*/
void set_connack_handler(connack_handler h = connack_handler()) {
h_connack_ = std::move(h);
}
/**
* @brief Set publish handler
* @param h handler
*/
void set_publish_handler(publish_handler h = publish_handler()) {
h_publish_ = std::move(h);
}
/**
* @brief Set puback handler
* @param h handler
*/
void set_puback_handler(puback_handler h = puback_handler()) {
h_puback_ = std::move(h);
}
/**
* @brief Set pubrec handler
* @param h handler
*/
void set_pubrec_handler(pubrec_handler h = pubrec_handler()) {
h_pubrec_ = std::move(h);
}
/**
* @brief Set pubrel handler
* @param h handler
*/
void set_pubrel_handler(pubrel_handler h = pubrel_handler()) {
h_pubrel_ = std::move(h);
}
/**
* @brief Set pubcomp handler
* @param h handler
*/
void set_pubcomp_handler(pubcomp_handler h = pubcomp_handler()) {
h_pubcomp_ = std::move(h);
}
/**
* @brief Set subscribe handler
* @param h handler
*/
void set_subscribe_handler(subscribe_handler h = subscribe_handler()) {
h_subscribe_ = std::move(h);
}
/**
* @brief Set suback handler
* @param h handler
*/
void set_suback_handler(suback_handler h = suback_handler()) {
h_suback_ = std::move(h);
}
/**
* @brief Set unsubscribe handler
* @param h handler
*/
void set_unsubscribe_handler(unsubscribe_handler h = unsubscribe_handler()) {
h_unsubscribe_ = std::move(h);
}
/**
* @brief Set unsuback handler
* @param h handler
*/
void set_unsuback_handler(unsuback_handler h = unsuback_handler()) {
h_unsuback_ = std::move(h);
}
/**
* @brief Set disconnect handler
* @param h handler
*/
void set_disconnect_handler(disconnect_handler h = disconnect_handler()) {
h_disconnect_ = std::move(h);
}
/**
* @brief Get connect handler
* @return handler
*/
connect_handler const& get_connect_handler() const {
return h_connect_;
}
/**
* @brief Get connack handler
* @return handler
*/
connack_handler const& get_connack_handler() const {
return h_connack_;
}
/**
* @brief Set publish handler
* @return handler
*/
publish_handler const& get_publish_handler() const {
return h_publish_;
}
/**
* @brief Get puback handler
* @return handler
*/
puback_handler const& get_puback_handler() const {
return h_puback_;
}
/**
* @brief Get pubrec handler
* @return handler
*/
pubrec_handler const& get_pubrec_handler() const {
return h_pubrec_;
}
/**
* @brief Get pubrel handler
* @return handler
*/
pubrel_handler const& get_pubrel_handler() const {
return h_pubrel_;
}
/**
* @brief Get pubcomp handler
* @return handler
*/
pubcomp_handler const& get_pubcomp_handler() const {
return h_pubcomp_;
}
/**
* @brief Get subscribe handler
* @return handler
*/
subscribe_handler const& get_subscribe_handler() const {
return h_subscribe_;
}
/**
* @brief Get suback handler
* @return handler
*/
suback_handler const& get_suback_handler() const {
return h_suback_;
}
/**
* @brief Get unsubscribe handler
* @return handler
*/
unsubscribe_handler const& get_unsubscribe_handler() const {
return h_unsubscribe_;
}
/**
* @brief Get unsuback handler
* @return handler
*/
unsuback_handler const& get_unsuback_handler() const {
return h_unsuback_;