mqtt_inflight_message.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. * refrenced from : mqtt_cpp/include/mqtt/broker/inflight_message.hpp
  11. */
  12. #ifndef __ASIO2_MQTT_INFLIGHT_MESSAGE_HPP__
  13. #define __ASIO2_MQTT_INFLIGHT_MESSAGE_HPP__
  14. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #pragma once
  16. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  17. #include <cstdint>
  18. #include <string>
  19. #include <string_view>
  20. #include <type_traits>
  21. #include <unordered_map>
  22. #include <algorithm>
  23. #include <variant>
  24. #include <asio2/base/iopool.hpp>
  25. #include <asio2/mqtt/message.hpp>
  26. #include <asio2/mqtt/detail/mqtt_topic_util.hpp>
  27. namespace asio2::mqtt
  28. {
  29. //class inflight_messages
  30. //{
  31. //public:
  32. // void insert(
  33. // store_message_variant msg,
  34. // any life_keeper,
  35. // std::shared_ptr<asio::steady_timer> tim_message_expiry
  36. // )
  37. // {
  38. // messages_.emplace_back(
  39. // std::move(msg),
  40. // std::move(life_keeper),
  41. // std::move(tim_message_expiry)
  42. // );
  43. // }
  44. // void send_all_messages(endpoint_t& ep)
  45. // {
  46. // for (auto const& ifm : messages_)
  47. // {
  48. // ifm.send(ep);
  49. // }
  50. // }
  51. // void clear()
  52. // {
  53. // messages_.clear();
  54. // }
  55. // template <typename Tag>
  56. // decltype(auto) get()
  57. // {
  58. // return messages_.get<Tag>();
  59. // }
  60. // template <typename Tag>
  61. // decltype(auto) get() const
  62. // {
  63. // return messages_.get<Tag>();
  64. // }
  65. //protected:
  66. // using mi_inflight_message = mi::multi_index_container<
  67. // imnode,
  68. // mi::indexed_by<
  69. // mi::sequenced<
  70. // mi::tag<tag_seq>
  71. // >,
  72. // mi::ordered_unique<
  73. // mi::tag<tag_pid>,
  74. // BOOST_MULTI_INDEX_CONST_MEM_FUN(imnode, packet_id_t, packet_id)
  75. // >,
  76. // mi::ordered_non_unique<
  77. // mi::tag<tag_tim>,
  78. // BOOST_MULTI_INDEX_MEMBER(imnode, std::shared_ptr<asio::steady_timer>, message_expiry_timer_)
  79. // >
  80. // >
  81. // >;
  82. // mi_inflight_message messages_;
  83. //};
  84. //struct imnode
  85. //{
  86. // template<class Message>
  87. // imnode(Message&& msg,
  88. // any life_keeper,
  89. // std::shared_ptr<asio::steady_timer> tim_message_expiry
  90. // )
  91. // : message(std::forward<Message>(msg))
  92. // , life_keeper_{ std::move(life_keeper) }
  93. // , message_expiry_timer_{ std::move(tim_message_expiry) }
  94. // {}
  95. // packet_id_t packet_id() const
  96. // {
  97. // return std::visit(make_lambda_visitor(
  98. // [](auto const& m) {
  99. // return m.packet_id();
  100. // }
  101. // ),
  102. // msg_
  103. // );
  104. // }
  105. // void send(endpoint_t& ep) const
  106. // {
  107. // optional<store_message_variant> msg_opt;
  108. // if (message_expiry_timer_) {
  109. // MQTT_NS::visit(
  110. // make_lambda_visitor(
  111. // [&](v5::basic_publish_message<sizeof(packet_id_t)> const& m) {
  112. // auto updated_msg = m;
  113. // auto d =
  114. // std::chrono::duration_cast<std::chrono::seconds>(
  115. // message_expiry_timer_->expiry() - std::chrono::steady_clock::now()
  116. // ).count();
  117. // if (d < 0) d = 0;
  118. // updated_msg.update_prop(
  119. // v5::property::message_expiry_interval(
  120. // static_cast<uint32_t>(d)
  121. // )
  122. // );
  123. // msg_opt.emplace(std::move(updated_msg));
  124. // },
  125. // [](auto const&) {
  126. // }
  127. // ),
  128. // msg_
  129. // );
  130. // }
  131. // // packet_id_exhausted never happen because inflight message has already
  132. // // allocated packet_id at the previous connection.
  133. // // In send_store_message(), packet_id is registered.
  134. // ep.send_store_message(msg_opt ? msg_opt.value() : msg_, life_keeper_);
  135. // }
  136. //protected:
  137. // mqtt::message message;
  138. // //any life_keeper_;
  139. // std::shared_ptr<asio::steady_timer> message_expiry_timer_;
  140. //};
  141. }
  142. #endif // !__ASIO2_MQTT_INFLIGHT_MESSAGE_HPP__