mqtt_session_persistence.hpp 3.9 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. #ifndef __ASIO2_MQTT_SESSION_PERSISTENCE_HPP__
  11. #define __ASIO2_MQTT_SESSION_PERSISTENCE_HPP__
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. #pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <unordered_map>
  16. #include <asio2/base/define.hpp>
  17. #include <asio2/base/detail/util.hpp>
  18. #include <asio2/base/detail/shared_mutex.hpp>
  19. namespace asio2::detail
  20. {
  21. ASIO2_CLASS_FORWARD_DECLARE_BASE;
  22. ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE;
  23. ASIO2_CLASS_FORWARD_DECLARE_TCP_SERVER;
  24. ASIO2_CLASS_FORWARD_DECLARE_TCP_SESSION;
  25. ASIO2_CLASS_FORWARD_DECLARE_TCP_CLIENT;
  26. template<class session_t, class args_t>
  27. class mqtt_session_persistence
  28. {
  29. friend session_t;
  30. ASIO2_CLASS_FRIEND_DECLARE_BASE;
  31. ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
  32. ASIO2_CLASS_FRIEND_DECLARE_TCP_SERVER;
  33. ASIO2_CLASS_FRIEND_DECLARE_TCP_SESSION;
  34. ASIO2_CLASS_FRIEND_DECLARE_TCP_CLIENT;
  35. public:
  36. using self = mqtt_session_persistence<session_t, args_t>;
  37. /**
  38. * @brief constructor
  39. */
  40. mqtt_session_persistence()
  41. {
  42. }
  43. /**
  44. * @brief destructor
  45. */
  46. ~mqtt_session_persistence() = default;
  47. public:
  48. /**
  49. * @brief add a mqtt session
  50. */
  51. template<class StringT>
  52. inline self& push_mqtt_session(StringT&& clientid, std::shared_ptr<session_t> session_ptr)
  53. {
  54. asio2::unique_locker guard(this->session_persistence_mutex_);
  55. this->mqtt_sessions_map_[detail::to_string(std::forward<StringT>(clientid))] = std::move(session_ptr);
  56. return (*this);
  57. }
  58. /**
  59. * @brief find the mqtt session by client id
  60. */
  61. inline std::shared_ptr<session_t> find_mqtt_session(const std::string& clientid)
  62. {
  63. asio2::shared_locker guard(this->session_persistence_mutex_);
  64. auto iter = this->mqtt_sessions_map_.find(clientid);
  65. return iter == this->mqtt_sessions_map_.end() ? nullptr : iter->second;
  66. }
  67. /**
  68. * @brief find the mqtt session by client id
  69. */
  70. inline std::shared_ptr<session_t> find_mqtt_session(const std::string_view& clientid)
  71. {
  72. return this->find_mqtt_session(std::string(clientid));
  73. }
  74. /**
  75. * @brief remove the mqtt session by client id
  76. */
  77. inline bool erase_mqtt_session(const std::string& clientid)
  78. {
  79. asio2::unique_locker guard(this->session_persistence_mutex_);
  80. return this->mqtt_sessions_map_.erase(clientid) > 0;
  81. }
  82. /**
  83. * @brief remove the mqtt session by client id
  84. */
  85. inline bool erase_mqtt_session(const std::string_view& clientid)
  86. {
  87. return this->erase_mqtt_session(std::string(clientid));
  88. }
  89. /**
  90. * @brief remove the mqtt session by client id and session itself
  91. */
  92. inline bool erase_mqtt_session(const std::string& clientid, session_t* p)
  93. {
  94. asio2::unique_locker guard(this->session_persistence_mutex_);
  95. auto iter = this->mqtt_sessions_map_.find(clientid);
  96. if (iter != this->mqtt_sessions_map_.end())
  97. {
  98. if (iter->second.get() == p)
  99. {
  100. this->mqtt_sessions_map_.erase(iter);
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. /**
  107. * @brief remove the mqtt session by client id and session itself
  108. */
  109. inline bool erase_mqtt_session(const std::string_view& clientid, session_t* p)
  110. {
  111. return this->erase_mqtt_session(std::string(clientid), p);
  112. }
  113. /**
  114. * @brief remove all mqtt sessions
  115. */
  116. inline self& clear_mqtt_sessions()
  117. {
  118. asio2::unique_locker guard(this->session_persistence_mutex_);
  119. this->mqtt_sessions_map_.clear();
  120. return (*this);
  121. }
  122. protected:
  123. /// use rwlock to make thread safe
  124. mutable asio2::shared_mutexer session_persistence_mutex_;
  125. ///
  126. std::unordered_map<std::string, std::shared_ptr<session_t>> mqtt_sessions_map_ ASIO2_GUARDED_BY(session_persistence_mutex_);
  127. };
  128. }
  129. #endif // !__ASIO2_MQTT_SESSION_PERSISTENCE_HPP__