mqtts_server.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #if defined(ASIO2_ENABLE_SSL) || defined(ASIO2_USE_SSL)
  11. #ifndef __ASIO2_MQTTS_SERVER_HPP__
  12. #define __ASIO2_MQTTS_SERVER_HPP__
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. #pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <asio2/base/detail/push_options.hpp>
  17. #include <asio2/mqtt/mqtt_server.hpp>
  18. #include <asio2/mqtt/mqtts_session.hpp>
  19. namespace asio2::detail
  20. {
  21. struct template_args_mqtts_server
  22. {
  23. static constexpr bool is_session = false;
  24. static constexpr bool is_client = false;
  25. static constexpr bool is_server = true;
  26. };
  27. ASIO2_CLASS_FORWARD_DECLARE_BASE;
  28. ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE;
  29. ASIO2_CLASS_FORWARD_DECLARE_TCP_SERVER;
  30. template<class derived_t, class session_t>
  31. class mqtts_server_impl_t
  32. : public ssl_context_cp <derived_t, template_args_mqtts_server>
  33. , public mqtt_server_impl_t<derived_t, session_t>
  34. {
  35. ASIO2_CLASS_FRIEND_DECLARE_BASE;
  36. ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
  37. ASIO2_CLASS_FRIEND_DECLARE_TCP_SERVER;
  38. public:
  39. using super = mqtt_server_impl_t <derived_t, session_t>;
  40. using self = mqtts_server_impl_t<derived_t, session_t>;
  41. using session_type = session_t;
  42. public:
  43. /**
  44. * @brief constructor
  45. */
  46. template<class... Args>
  47. explicit mqtts_server_impl_t(
  48. asio::ssl::context::method method,
  49. Args&&... args
  50. )
  51. : ssl_context_cp<derived_t, template_args_mqtts_server>(method)
  52. , super(std::forward<Args>(args)...)
  53. {
  54. }
  55. /**
  56. * @brief constructor
  57. */
  58. template<class... Args>
  59. explicit mqtts_server_impl_t(Args&&... args)
  60. : ssl_context_cp<derived_t, template_args_mqtts_server>(ASIO2_DEFAULT_SSL_METHOD)
  61. , super(std::forward<Args>(args)...)
  62. {
  63. }
  64. /**
  65. * @brief constructor
  66. */
  67. explicit mqtts_server_impl_t()
  68. : ssl_context_cp<derived_t, template_args_mqtts_server>(ASIO2_DEFAULT_SSL_METHOD)
  69. , super()
  70. {
  71. }
  72. /**
  73. * @brief destructor
  74. */
  75. ~mqtts_server_impl_t()
  76. {
  77. this->stop();
  78. }
  79. public:
  80. /**
  81. * @brief bind ssl handshake listener
  82. * @param fun - a user defined callback function.
  83. * @param obj - a pointer or reference to a class object, this parameter can be none.
  84. * if fun is nonmember function, the obj param must be none, otherwise the obj must be the
  85. * the class object's pointer or reference.
  86. * Function signature : void(std::shared_ptr<asio2::mqtts_session>& session_ptr)
  87. */
  88. template<class F, class ...C>
  89. inline derived_t & bind_handshake(F&& fun, C&&... obj)
  90. {
  91. this->listener_.bind(event_type::handshake,
  92. observer_t<std::shared_ptr<session_t>&>(
  93. std::forward<F>(fun), std::forward<C>(obj)...));
  94. return (this->derived());
  95. }
  96. protected:
  97. template<typename... Args>
  98. inline std::shared_ptr<session_t> _make_session(Args&&... args)
  99. {
  100. return super::_make_session(std::forward<Args>(args)..., *this);
  101. }
  102. };
  103. }
  104. namespace asio2
  105. {
  106. template<class derived_t, class session_t>
  107. using mqtts_server_impl_t = detail::mqtts_server_impl_t<derived_t, session_t>;
  108. /**
  109. * @brief ssl mqtt server
  110. * @throws constructor maybe throw exception "Too many open files" (exception code : 24)
  111. * asio::error::no_descriptors - Too many open files
  112. */
  113. template<class session_t>
  114. class mqtts_server_t : public detail::mqtts_server_impl_t<mqtts_server_t<session_t>, session_t>
  115. {
  116. public:
  117. using detail::mqtts_server_impl_t<mqtts_server_t<session_t>, session_t>::mqtts_server_impl_t;
  118. };
  119. /**
  120. * @brief ssl mqtt server
  121. * @throws constructor maybe throw exception "Too many open files" (exception code : 24)
  122. * asio::error::no_descriptors - Too many open files
  123. */
  124. using mqtts_server = mqtts_server_t<mqtts_session>;
  125. }
  126. #if defined(ASIO2_INCLUDE_RATE_LIMIT)
  127. #include <asio2/tcp/tcp_stream.hpp>
  128. namespace asio2
  129. {
  130. template<class session_t>
  131. class mqtts_rate_server_t : public asio2::mqtts_server_impl_t<mqtts_rate_server_t<session_t>, session_t>
  132. {
  133. public:
  134. using asio2::mqtts_server_impl_t<mqtts_rate_server_t<session_t>, session_t>::mqtts_server_impl_t;
  135. };
  136. using mqtts_rate_server = mqtts_rate_server_t<mqtts_rate_session>;
  137. }
  138. #endif
  139. #include <asio2/base/detail/pop_options.hpp>
  140. #endif // !__ASIO2_MQTTS_SERVER_HPP__
  141. #endif