26
26
#include < mqtt/callable_overlay.hpp>
27
27
#include < mqtt/strand.hpp>
28
28
#include < mqtt/null_strand.hpp>
29
+ #include < mqtt/is_invocable.hpp>
29
30
30
31
namespace MQTT_NS {
31
32
@@ -1444,30 +1445,51 @@ class client : public endpoint<std::mutex, std::lock_guard, PacketIdBytes> {
1444
1445
* When the endpoint disconnects using disconnect(), a will won't send.<BR>
1445
1446
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901205<BR>
1446
1447
* @param timeout after timeout elapsed, force_disconnect() is automatically called.
1448
+ * @param reason_code
1449
+ * DISCONNECT Reason Code<BR>
1450
+ * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901208<BR>
1451
+ * 3.14.2.1 Disconnect Reason Code
1452
+ * @param props
1453
+ * Properties<BR>
1454
+ * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901209<BR>
1455
+ * 3.14.2.2 DISCONNECT Properties
1447
1456
* @param func A callback function that is called when async operation will finish.
1448
1457
*/
1449
- void async_disconnect (
1458
+ template <
1459
+ typename CompletionToken = async_handler_t ,
1460
+ typename std::enable_if_t <
1461
+ is_invocable<CompletionToken, error_code>::value
1462
+ >* = nullptr
1463
+ >
1464
+ auto async_disconnect (
1450
1465
std::chrono::steady_clock::duration timeout,
1451
- async_handler_t func = async_handler_t ()) {
1466
+ v5::disconnect_reason_code reason_code,
1467
+ v5::properties props,
1468
+ CompletionToken&& token = async_handler_t {}
1469
+ ) {
1470
+
1452
1471
if (ping_duration_ != std::chrono::steady_clock::duration::zero ()) tim_ping_.cancel ();
1453
- if (base::connected ()) {
1454
- std::weak_ptr<this_type> wp (std::static_pointer_cast<this_type>(this ->shared_from_this ()));
1455
- tim_close_.expires_after (force_move (timeout));
1456
- tim_close_.async_wait (
1457
- [wp = force_move (wp)](error_code ec) {
1458
- if (auto sp = wp.lock ()) {
1459
- if (!ec) {
1460
- sp->socket ()->post (
1461
- [sp] {
1462
- sp->force_disconnect ();
1463
- }
1464
- );
1465
- }
1472
+ std::weak_ptr<this_type> wp (std::static_pointer_cast<this_type>(this ->shared_from_this ()));
1473
+ tim_close_.expires_after (force_move (timeout));
1474
+ tim_close_.async_wait (
1475
+ [wp = force_move (wp)](error_code ec) {
1476
+ if (auto sp = wp.lock ()) {
1477
+ if (!ec) {
1478
+ sp->socket ()->post (
1479
+ [sp] {
1480
+ sp->force_disconnect ();
1481
+ }
1482
+ );
1466
1483
}
1467
1484
}
1485
+ }
1486
+ );
1487
+ return
1488
+ base::async_disconnect (
1489
+ reason_code,
1490
+ force_move (props),
1491
+ std::forward<CompletionToken>(token)
1468
1492
);
1469
- base::async_disconnect (force_move (func));
1470
- }
1471
1493
}
1472
1494
1473
1495
/* *
@@ -1476,7 +1498,6 @@ class client : public endpoint<std::mutex, std::lock_guard, PacketIdBytes> {
1476
1498
* The broker disconnects the endpoint after receives the disconnect packet.<BR>
1477
1499
* When the endpoint disconnects using disconnect(), a will won't send.<BR>
1478
1500
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901205<BR>
1479
- * @param timeout after timeout elapsed, force_disconnect() is automatically called.
1480
1501
* @param reason_code
1481
1502
* DISCONNECT Reason Code<BR>
1482
1503
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901208<BR>
@@ -1487,30 +1508,24 @@ class client : public endpoint<std::mutex, std::lock_guard, PacketIdBytes> {
1487
1508
* 3.14.2.2 DISCONNECT Properties
1488
1509
* @param func A callback function that is called when async operation will finish.
1489
1510
*/
1490
- void async_disconnect (
1491
- std::chrono::steady_clock::duration timeout,
1511
+ template <
1512
+ typename CompletionToken = async_handler_t ,
1513
+ typename std::enable_if_t <
1514
+ is_invocable<CompletionToken, error_code>::value
1515
+ >* = nullptr
1516
+ >
1517
+ auto async_disconnect (
1492
1518
v5::disconnect_reason_code reason_code,
1493
1519
v5::properties props,
1494
- async_handler_t func = async_handler_t ()) {
1520
+ CompletionToken&& token = async_handler_t {}
1521
+ ) {
1495
1522
if (ping_duration_ != std::chrono::steady_clock::duration::zero ()) tim_ping_.cancel ();
1496
- if (base::connected ()) {
1497
- std::weak_ptr<this_type> wp (std::static_pointer_cast<this_type>(this ->shared_from_this ()));
1498
- tim_close_.expires_after (force_move (timeout));
1499
- tim_close_.async_wait (
1500
- [wp = force_move (wp)](error_code ec) {
1501
- if (auto sp = wp.lock ()) {
1502
- if (!ec) {
1503
- sp->socket ()->post (
1504
- [sp] {
1505
- sp->force_disconnect ();
1506
- }
1507
- );
1508
- }
1509
- }
1510
- }
1523
+ return
1524
+ base::async_disconnect (
1525
+ reason_code,
1526
+ force_move (props),
1527
+ std::forward<CompletionToken>(token)
1511
1528
);
1512
- base::async_disconnect (reason_code, force_move (props), force_move (func));
1513
- }
1514
1529
}
1515
1530
1516
1531
/* *
@@ -1519,22 +1534,39 @@ class client : public endpoint<std::mutex, std::lock_guard, PacketIdBytes> {
1519
1534
* The broker disconnects the endpoint after receives the disconnect packet.<BR>
1520
1535
* When the endpoint disconnects using disconnect(), a will won't send.<BR>
1521
1536
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901205<BR>
1537
+ * @param timeout after timeout elapsed, force_disconnect() is automatically called.
1522
1538
* @param func A callback function that is called when async operation will finish.
1523
1539
*/
1524
- template <typename CompletionToken>
1540
+ template <
1541
+ typename CompletionToken = async_handler_t ,
1542
+ typename std::enable_if_t <
1543
+ is_invocable<CompletionToken, error_code>::value
1544
+ >* = nullptr
1545
+ >
1525
1546
auto async_disconnect (
1526
- CompletionToken&& token = async_handler_t {}
1527
- )
1528
- ->
1529
- typename as::async_result<
1530
- typename std::decay<CompletionToken>::type,
1531
- void(error_code)
1532
- >::return_type
1533
- {
1547
+ std::chrono::steady_clock::duration timeout,
1548
+ CompletionToken&& token = async_handler_t {}) {
1549
+
1534
1550
if (ping_duration_ != std::chrono::steady_clock::duration::zero ()) tim_ping_.cancel ();
1535
- if (base::connected ()) {
1536
- base::async_disconnect (std::forward<CompletionToken>(token));
1537
- }
1551
+ std::weak_ptr<this_type> wp (std::static_pointer_cast<this_type>(this ->shared_from_this ()));
1552
+ tim_close_.expires_after (force_move (timeout));
1553
+ tim_close_.async_wait (
1554
+ [wp = force_move (wp)](error_code ec) {
1555
+ if (auto sp = wp.lock ()) {
1556
+ if (!ec) {
1557
+ sp->socket ()->post (
1558
+ [sp] {
1559
+ sp->force_disconnect ();
1560
+ }
1561
+ );
1562
+ }
1563
+ }
1564
+ }
1565
+ );
1566
+ return
1567
+ base::async_disconnect (
1568
+ std::forward<CompletionToken>(token)
1569
+ );
1538
1570
}
1539
1571
1540
1572
/* *
@@ -1543,24 +1575,19 @@ class client : public endpoint<std::mutex, std::lock_guard, PacketIdBytes> {
1543
1575
* The broker disconnects the endpoint after receives the disconnect packet.<BR>
1544
1576
* When the endpoint disconnects using disconnect(), a will won't send.<BR>
1545
1577
* See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901205<BR>
1546
- * @param reason_code
1547
- * DISCONNECT Reason Code<BR>
1548
- * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901208<BR>
1549
- * 3.14.2.1 Disconnect Reason Code
1550
- * @param props
1551
- * Properties<BR>
1552
- * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901209<BR>
1553
- * 3.14.2.2 DISCONNECT Properties
1554
1578
* @param func A callback function that is called when async operation will finish.
1555
1579
*/
1556
- void async_disconnect (
1557
- v5::disconnect_reason_code reason_code,
1558
- v5::properties props,
1559
- async_handler_t func = async_handler_t ()) {
1580
+ template <
1581
+ typename CompletionToken = async_handler_t ,
1582
+ typename std::enable_if_t <
1583
+ is_invocable<CompletionToken, error_code>::value
1584
+ >* = nullptr
1585
+ >
1586
+ auto async_disconnect (
1587
+ CompletionToken&& token = async_handler_t {}
1588
+ ) {
1560
1589
if (ping_duration_ != std::chrono::steady_clock::duration::zero ()) tim_ping_.cancel ();
1561
- if (base::connected ()) {
1562
- base::async_disconnect (reason_code, force_move (props), force_move (func));
1563
- }
1590
+ return base::async_disconnect (std::forward<CompletionToken>(token));
1564
1591
}
1565
1592
1566
1593
/* *
0 commit comments