@@ -858,14 +858,16 @@ class endpoint : public std::enable_shared_from_this<endpoint<Mutex, LockGuard,
858
858
}
859
859
860
860
/* *
861
- * @brief Set async error notify flag
862
- * @param async send connack/disconnect if error happens
861
+ * @brief Set async notify flag
862
+ * @param async send packet
863
863
*
864
864
* MQTT protocol requests sending connack/disconnect packet with error reason code if some error happens.<BR>
865
865
* This function choose sync/async connack/disconnect.<BR>
866
+ * MQTT protocol requests sending pubrec even if the corresponding publish has already been handled.<BR>
867
+ * This function choose sync/async pubrec.<BR>
866
868
*/
867
- void set_async_error_notify (bool async = true ) {
868
- async_error_notify_ = async;
869
+ void set_async_notify (bool async = true ) {
870
+ async_notify_ = async;
869
871
}
870
872
871
873
/* *
@@ -5023,7 +5025,7 @@ class endpoint : public std::enable_shared_from_this<endpoint<Mutex, LockGuard,
5023
5025
}
5024
5026
5025
5027
void send_error_disconnect (v5::disconnect_reason_code rc) {
5026
- if (async_error_notify_ ) {
5028
+ if (async_notify_ ) {
5027
5029
async_disconnect (rc);
5028
5030
}
5029
5031
else {
@@ -5032,7 +5034,7 @@ class endpoint : public std::enable_shared_from_this<endpoint<Mutex, LockGuard,
5032
5034
}
5033
5035
5034
5036
void send_error_connack (v5::connect_reason_code rc) {
5035
- if (async_error_notify_ ) {
5037
+ if (async_notify_ ) {
5036
5038
async_connack (false , rc);
5037
5039
}
5038
5040
else {
@@ -7342,23 +7344,12 @@ class endpoint : public std::enable_shared_from_this<endpoint<Mutex, LockGuard,
7342
7344
[&] {
7343
7345
switch (ep_.version_ ) {
7344
7346
case protocol_version::v3_1_1:
7345
- if (ep_.on_publish (
7346
- packet_id_,
7347
- publish_options (ep_.fixed_header_ ),
7348
- force_move (topic_name_),
7349
- force_move (force_move (variant_get<buffer>(var))))) {
7350
- ep_.on_mqtt_message_processed (
7351
- force_move (
7352
- std::get<0 >(
7353
- any_cast<
7354
- std::tuple<any, process_type_sp>
7355
- >(session_life_keeper)
7356
- )
7357
- )
7358
- );
7359
- return true ;
7360
- }
7361
- break ;
7347
+ return ep_.on_publish (
7348
+ packet_id_,
7349
+ publish_options (ep_.fixed_header_ ),
7350
+ force_move (topic_name_),
7351
+ force_move (variant_get<buffer>(var))
7352
+ );
7362
7353
case protocol_version::v5:
7363
7354
if (topic_name_.empty ()) {
7364
7355
if (auto topic_alias = get_topic_alias_from_props (props_)) {
@@ -7390,37 +7381,43 @@ class endpoint : public std::enable_shared_from_this<endpoint<Mutex, LockGuard,
7390
7381
}
7391
7382
}
7392
7383
}
7393
- if (ep_.on_v5_publish (
7394
- packet_id_,
7395
- publish_options (ep_.fixed_header_ ),
7396
- force_move (topic_name_),
7397
- force_move (variant_get<buffer>(var)),
7398
- force_move (props_)
7399
- )
7400
- ) {
7401
- ep_.on_mqtt_message_processed (
7402
- force_move (
7403
- std::get<0 >(
7404
- any_cast<
7405
- std::tuple<any, process_type_sp>
7406
- >(session_life_keeper)
7407
- )
7408
- )
7409
- );
7410
- return true ;
7411
- }
7412
- break ;
7384
+ return ep_.on_v5_publish (
7385
+ packet_id_,
7386
+ publish_options (ep_.fixed_header_ ),
7387
+ force_move (topic_name_),
7388
+ force_move (variant_get<buffer>(var)),
7389
+ force_move (props_)
7390
+ );
7413
7391
default :
7414
7392
BOOST_ASSERT (false );
7415
7393
}
7416
7394
return false ;
7417
7395
};
7418
7396
switch (qos_value_) {
7419
7397
case qos::at_most_once:
7420
- handler_call ();
7398
+ if (handler_call ()) {
7399
+ ep_.on_mqtt_message_processed (
7400
+ force_move (
7401
+ std::get<0 >(
7402
+ any_cast<
7403
+ std::tuple<any, process_type_sp>
7404
+ >(session_life_keeper)
7405
+ )
7406
+ )
7407
+ );
7408
+ }
7421
7409
break ;
7422
7410
case qos::at_least_once:
7423
7411
if (handler_call ()) {
7412
+ ep_.on_mqtt_message_processed (
7413
+ force_move (
7414
+ std::get<0 >(
7415
+ any_cast<
7416
+ std::tuple<any, process_type_sp>
7417
+ >(session_life_keeper)
7418
+ )
7419
+ )
7420
+ );
7424
7421
ep_.auto_pub_response (
7425
7422
[this ] {
7426
7423
if (ep_.connected_ ) {
@@ -7445,29 +7442,67 @@ class endpoint : public std::enable_shared_from_this<endpoint<Mutex, LockGuard,
7445
7442
}
7446
7443
break ;
7447
7444
case qos::exactly_once:
7448
- if (handler_call ()) {
7449
- ep_.qos2_publish_handled_ .emplace (*packet_id_);
7450
- ep_.auto_pub_response (
7451
- [this ] {
7452
- if (ep_.connected_ ) {
7453
- ep_.send_pubrec (
7454
- *packet_id_,
7455
- v5::pubrec_reason_code::success,
7456
- v5::properties{}
7457
- );
7458
- }
7459
- },
7460
- [this ] {
7461
- if (ep_.connected_ ) {
7462
- ep_.async_send_pubrec (
7463
- *packet_id_,
7464
- v5::pubrec_reason_code::success,
7465
- v5::properties{},
7466
- [](auto ){}
7467
- );
7445
+ if (ep_.qos2_publish_handled_ .find (*packet_id_) == ep_.qos2_publish_handled_ .end ()) {
7446
+ if (handler_call ()) {
7447
+ ep_.on_mqtt_message_processed (
7448
+ force_move (
7449
+ std::get<0 >(
7450
+ any_cast<
7451
+ std::tuple<any, process_type_sp>
7452
+ >(session_life_keeper)
7453
+ )
7454
+ )
7455
+ );
7456
+ ep_.qos2_publish_handled_ .emplace (*packet_id_);
7457
+ ep_.auto_pub_response (
7458
+ [this ] {
7459
+ if (ep_.connected_ ) {
7460
+ ep_.send_pubrec (
7461
+ *packet_id_,
7462
+ v5::pubrec_reason_code::success,
7463
+ v5::properties{}
7464
+ );
7465
+ }
7466
+ },
7467
+ [this ] {
7468
+ if (ep_.connected_ ) {
7469
+ ep_.async_send_pubrec (
7470
+ *packet_id_,
7471
+ v5::pubrec_reason_code::success,
7472
+ v5::properties{},
7473
+ [](auto ){}
7474
+ );
7475
+ }
7468
7476
}
7469
- }
7477
+ );
7478
+ }
7479
+ }
7480
+ else {
7481
+ // publish has already been handled
7482
+ ep_.on_mqtt_message_processed (
7483
+ force_move (
7484
+ std::get<0 >(
7485
+ any_cast<
7486
+ std::tuple<any, process_type_sp>
7487
+ >(session_life_keeper)
7488
+ )
7489
+ )
7470
7490
);
7491
+ if (ep_.async_notify_ ) {
7492
+ ep_.async_send_pubrec (
7493
+ *packet_id_,
7494
+ v5::pubrec_reason_code::success,
7495
+ v5::properties{},
7496
+ [](auto ){}
7497
+ );
7498
+ }
7499
+ else {
7500
+ ep_.send_pubrec (
7501
+ *packet_id_,
7502
+ v5::pubrec_reason_code::success,
7503
+ v5::properties{}
7504
+ );
7505
+ }
7471
7506
}
7472
7507
break ;
7473
7508
}
@@ -10458,7 +10493,7 @@ class endpoint : public std::enable_shared_from_this<endpoint<Mutex, LockGuard,
10458
10493
std::set<packet_id_t > sub_unsub_inflight_;
10459
10494
bool auto_pub_response_{true };
10460
10495
bool auto_pub_response_async_{false };
10461
- bool async_error_notify_ {false };
10496
+ bool async_notify_ {false };
10462
10497
bool async_send_store_ { false };
10463
10498
bool async_read_on_message_processed_ { true };
10464
10499
bool disconnect_requested_{false };
0 commit comments