async_mqtt 9.0.1
Loading...
Searching...
No Matches
client.hpp
1// Copyright Takatoshi Kondo 2024
2//
3// Distributed under the Boost Software License, Version 1.0.
4// (See accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7#if !defined(ASYNC_MQTT_CLIENT_HPP)
8#define ASYNC_MQTT_CLIENT_HPP
9
10#include <async_mqtt/detail/client_impl.hpp>
11
17namespace async_mqtt {
18
19namespace as = boost::asio;
20
21template <protocol_version Version, typename NextLayer>
22class client {
23 using this_type = client<Version, NextLayer>;
24 using impl_type = detail::client_impl<Version, NextLayer>;
25
26public:
29
32
35
38
39 ASYNC_MQTT_PACKET_TYPE(Version, connect)
40 ASYNC_MQTT_PACKET_TYPE(Version, connack)
41 ASYNC_MQTT_PACKET_TYPE(Version, subscribe)
42 ASYNC_MQTT_PACKET_TYPE(Version, suback)
43 ASYNC_MQTT_PACKET_TYPE(Version, unsubscribe)
44 ASYNC_MQTT_PACKET_TYPE(Version, unsuback)
45 ASYNC_MQTT_PACKET_TYPE(Version, publish)
46 ASYNC_MQTT_PACKET_TYPE(Version, puback)
47 ASYNC_MQTT_PACKET_TYPE(Version, pubrec)
48 ASYNC_MQTT_PACKET_TYPE(Version, pubrel)
49 ASYNC_MQTT_PACKET_TYPE(Version, pubcomp)
50 ASYNC_MQTT_PACKET_TYPE(Version, pingreq)
51 ASYNC_MQTT_PACKET_TYPE(Version, pingresp)
52 ASYNC_MQTT_PACKET_TYPE(Version, disconnect)
53
54
57 struct pubres_type {
61 std::optional<puback_packet> puback_opt;
65 std::optional<pubrec_packet> pubrec_opt;
69 std::optional<pubcomp_packet> pubcomp_opt;
70 };
71
82 template <typename... Args>
83 explicit
85 Args&&... args
86 );
87
91 client(this_type const&) = delete;
92
96 client(this_type&&) = default;
97
103 ~client() = default;
104
108 this_type& operator=(this_type const&) = delete;
109
114
159 template <typename... Args>
160 auto async_start(Args&&... args);
161
206 template <typename... Args>
207 auto async_subscribe(Args&&... args);
208
253 template <typename... Args>
254 auto async_unsubscribe(Args&&... args);
255
302 template <typename... Args>
303 auto async_publish(Args&&... args);
304
333 template <typename... Args>
334 auto async_disconnect(Args&&... args);
335
363 template <typename... Args>
364 auto async_auth(Args&&... args);
365
377 template <
378 typename CompletionToken = as::default_completion_token_t<executor_type>
379 >
380#if !defined(GENERATING_DOCUMENTATION)
381 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
382 CompletionToken,
383 void()
384 )
385#endif // !defined(GENERATING_DOCUMENTATION)
387 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
388 );
389
411 template <
412 typename CompletionToken = as::default_completion_token_t<executor_type>
413 >
414#if !defined(GENERATING_DOCUMENTATION)
415 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
416 CompletionToken,
417 void(error_code, packet_variant)
418 )
419#endif // !defined(GENERATING_DOCUMENTATION)
421 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
422 );
423
428 as::any_io_executor get_executor();
429
435
441
447
453
460
467
476
485
496 void set_pingresp_recv_timeout(std::chrono::milliseconds duration);
497
507 void set_bulk_write(bool val);
508
519 void set_bulk_read_buffer_size(std::size_t val);
520
536 template <
537 typename CompletionToken = as::default_completion_token_t<executor_type>
538 >
539#if !defined(GENERATING_DOCUMENTATION)
540 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
541 CompletionToken,
543 )
544#endif // !defined(GENERATING_DOCUMENTATION)
546 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
547 );
548
565 template <
566 typename CompletionToken = as::default_completion_token_t<executor_type>
567 >
568#if !defined(GENERATING_DOCUMENTATION)
569 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
570 CompletionToken,
572 )
573#endif // !defined(GENERATING_DOCUMENTATION)
575 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
576 );
577
595 template <
596 typename CompletionToken = as::default_completion_token_t<executor_type>
597 >
598#if !defined(GENERATING_DOCUMENTATION)
599 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
600 CompletionToken,
601 void(error_code)
602 )
603#endif // !defined(GENERATING_DOCUMENTATION)
605 packet_id_type packet_id,
606 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
607 );
608
625 template <
626 typename CompletionToken = as::default_completion_token_t<executor_type>
627 >
628#if !defined(GENERATING_DOCUMENTATION)
629 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
630 CompletionToken,
631 void()
632 )
633#endif // !defined(GENERATING_DOCUMENTATION)
635 packet_id_type packet_id,
636 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
637 );
638
643 std::optional<packet_id_type> acquire_unique_packet_id();
644
651
657
661 template <typename Executor1>
663 using other = client<
664 Version,
665 typename NextLayer::template rebind_executor<Executor1>::other
666 >;
667 };
668
669private:
675 template <typename Other>
676 explicit
677 client(
679 );
680
681 template <
682 typename CompletionToken = as::default_completion_token_t<executor_type>
683 >
684#if !defined(GENERATING_DOCUMENTATION)
685 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
686 CompletionToken,
687 void(error_code, std::optional<connack_packet>)
688 )
689#endif // !defined(GENERATING_DOCUMENTATION)
690 async_start_impl(
691 error_code ec,
692 std::optional<connect_packet> packet,
693 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
694 );
695
696 template <
697 typename CompletionToken = as::default_completion_token_t<executor_type>
698 >
699#if !defined(GENERATING_DOCUMENTATION)
700 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
701 CompletionToken,
702 void(error_code, std::optional<suback_packet>)
703 )
704#endif // !defined(GENERATING_DOCUMENTATION)
705 async_subscribe_impl(
706 error_code ec,
707 std::optional<subscribe_packet> packet,
708 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
709 );
710
711 template <
712 typename CompletionToken = as::default_completion_token_t<executor_type>
713 >
714#if !defined(GENERATING_DOCUMENTATION)
715 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
716 CompletionToken,
717 void(error_code, std::optional<suback_packet>)
718 )
719#endif // !defined(GENERATING_DOCUMENTATION)
720 async_unsubscribe_impl(
721 error_code ec,
722 std::optional<unsubscribe_packet> packet,
723 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
724 );
725
726 template <
727 typename CompletionToken = as::default_completion_token_t<executor_type>
728 >
729#if !defined(GENERATING_DOCUMENTATION)
730 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
731 CompletionToken,
732 void(error_code, pubres_type)
733 )
734#endif // !defined(GENERATING_DOCUMENTATION)
735 async_publish_impl(
736 error_code ec,
737 std::optional<publish_packet> packet,
738 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
739 );
740
741 template <
742 typename CompletionToken = as::default_completion_token_t<executor_type>
743 >
744#if !defined(GENERATING_DOCUMENTATION)
745 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
746 CompletionToken,
747 void(error_code)
748 )
749#endif // !defined(GENERATING_DOCUMENTATION)
750 async_disconnect_impl(
751 error_code ec,
752 std::optional<disconnect_packet> packet,
753 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
754 );
755
756 template <
757 typename CompletionToken = as::default_completion_token_t<executor_type>
758 >
759#if !defined(GENERATING_DOCUMENTATION)
760 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
761 CompletionToken,
762 void(error_code)
763 )
764#endif // !defined(GENERATING_DOCUMENTATION)
765 async_auth_impl(
766 error_code ec,
767 std::optional<v5::auth_packet> packet,
768 CompletionToken&& token = as::default_completion_token_t<executor_type>{}
769 );
770
771 void recv_loop();
772
773private:
774 std::shared_ptr<impl_type> impl_;
775};
776
777} // namespace async_mqtt
778
779#include <async_mqtt/impl/client_impl.hpp>
780#include <async_mqtt/impl/client_start.hpp>
781#include <async_mqtt/impl/client_subscribe.hpp>
782#include <async_mqtt/impl/client_unsubscribe.hpp>
783#include <async_mqtt/impl/client_publish.hpp>
784#include <async_mqtt/impl/client_disconnect.hpp>
785#include <async_mqtt/impl/client_auth.hpp>
786#include <async_mqtt/impl/client_close.hpp>
787#include <async_mqtt/impl/client_recv.hpp>
788#include <async_mqtt/impl/client_acquire_unique_packet_id.hpp>
789#include <async_mqtt/impl/client_acquire_unique_packet_id_wait_until.hpp>
790#include <async_mqtt/impl/client_register_packet_id.hpp>
791#include <async_mqtt/impl/client_release_packet_id.hpp>
792
793#endif // ASYNC_MQTT_CLIENT_HPP
MQTT endpoint corresponding to the connection.
Definition endpoint_fwd.hpp:32
typename next_layer_type::executor_type executor_type
executor_type of the given NextLayer
Definition endpoint.hpp:48
typename stream_type::lowest_layer_type lowest_layer_type
lowest_layer_type of the given NextLayer
Definition endpoint.hpp:45
typename stream_type::next_layer_type next_layer_type
type of the given NextLayer
Definition endpoint.hpp:42
MQTT client for casual usecases.
Definition client_fwd.hpp:82
void set_pingresp_recv_timeout(std::chrono::milliseconds duration)
Set timeout for receiving PINGRESP packet after PINGREQ packet is sent. If the timer is fired,...
client(this_type const &)=delete
copy constructor deleted
void set_auto_replace_topic_alias_send(bool val)
auto replace topic with corresponding topic alias on send PUBLISH packet. Registering topic alias nee...
as::any_io_executor get_executor()
executor getter
async_acquire_unique_packet_id_wait_until(CompletionToken &&token=as::default_completion_token_t< executor_type >{})
acuire unique packet_id. If packet_id is fully acquired, then wait until released.
auto async_subscribe(Args &&... args)
send SUBSCRIBE packet
next_layer_type const & next_layer() const
next_layer getter
async_release_packet_id(packet_id_type packet_id, CompletionToken &&token=as::default_completion_token_t< executor_type >{})
release packet_id.
auto async_unsubscribe(Args &&... args)
send UNSUBSCRIBE packet
bool register_packet_id(packet_id_type packet_id)
register packet_id.
next_layer_type & next_layer()
next_layer getter
endpoint_type const & get_endpoint() const
get endpoint This is for detail operation
endpoint_type & get_endpoint()
get endpoint This is for detail operation
void set_auto_map_topic_alias_send(bool val)
auto map (allocate) topic alias on send PUBLISH packet. If all topic aliases are used,...
lowest_layer_type & lowest_layer()
lowest_layer getter
void release_packet_id(packet_id_type packet_id)
release packet_id.
auto async_start(Args &&... args)
send CONNECT packet and start packet receive loop
typename endpoint_type::next_layer_type next_layer_type
type of the given NextLayer
Definition client.hpp:31
async_recv(CompletionToken &&token=as::default_completion_token_t< executor_type >{})
receive PUBLISH or DISCONNECT packet users CANNOT call recv() before the previous recv()'s Completion...
lowest_layer_type const & lowest_layer() const
lowest_layer getter
typename endpoint_type::executor_type executor_type
executor_type of the given NextLayer
Definition client.hpp:37
auto async_auth(Args &&... args)
send AUTH packet
client(Args &&... args)
constructor
async_close(CompletionToken &&token=as::default_completion_token_t< executor_type >{})
close the underlying connection
void set_bulk_write(bool val)
Set bulk write mode. If true, then concatenate multiple packets' const buffer sequence when send() is...
this_type & operator=(this_type &&)=default
move assign operator
std::optional< packet_id_type > acquire_unique_packet_id()
acuire unique packet_id.
client(this_type &&)=default
move constructor
~client()=default
destructor This function destroys the client, cancelling any outstanding asynchronous operations asso...
async_register_packet_id(packet_id_type packet_id, CompletionToken &&token=as::default_completion_token_t< executor_type >{})
register packet_id.
auto async_disconnect(Args &&... args)
send DISCONNECT packet
async_acquire_unique_packet_id(CompletionToken &&token=as::default_completion_token_t< executor_type >{})
acuire unique packet_id.
auto async_publish(Args &&... args)
send PUBLISH packet
typename endpoint_type::lowest_layer_type lowest_layer_type
lowest_layer_type of the given NextLayer
Definition client.hpp:34
void set_bulk_read_buffer_size(std::size_t val)
Set the bulk read buffer size. If bulk read is enabled, the val parameter specifies the size of the i...
this_type & operator=(this_type const &)=delete
copy assign operator deleted
sys::error_code error_code
sys is a namespace alias of boost::sytem.
Definition error.hpp:56
typename basic_packet_id_type< 2 >::type packet_id_type
packet idenfitifer type
Definition packet_id_type.hpp:46
@ client
as client. Can't send CONNACK, SUBACK, UNSUBACK, PINGRESP. Can send Other packets.
publish completion handler parameter class
Definition client.hpp:57
std::optional< pubrec_packet > pubrec_opt
Definition client.hpp:65
std::optional< pubcomp_packet > pubcomp_opt
Definition client.hpp:69
std::optional< puback_packet > puback_opt
Definition client.hpp:61
rebinds the client type to another executor
Definition client.hpp:662