tcps_session.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_TCPS_SESSION_HPP__
  12. #define __ASIO2_TCPS_SESSION_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/tcp/tcp_session.hpp>
  18. #include <asio2/tcp/impl/ssl_stream_cp.hpp>
  19. #include <asio2/tcp/impl/ssl_context_cp.hpp>
  20. namespace asio2::detail
  21. {
  22. ASIO2_CLASS_FORWARD_DECLARE_BASE;
  23. ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE;
  24. ASIO2_CLASS_FORWARD_DECLARE_TCP_SERVER;
  25. ASIO2_CLASS_FORWARD_DECLARE_TCP_SESSION;
  26. template<class derived_t, class args_t = template_args_tcp_session>
  27. class tcps_session_impl_t
  28. : public tcp_session_impl_t<derived_t, args_t>
  29. , public ssl_stream_cp <derived_t, args_t>
  30. {
  31. ASIO2_CLASS_FRIEND_DECLARE_BASE;
  32. ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
  33. ASIO2_CLASS_FRIEND_DECLARE_TCP_SERVER;
  34. ASIO2_CLASS_FRIEND_DECLARE_TCP_SESSION;
  35. public:
  36. using super = tcp_session_impl_t <derived_t, args_t>;
  37. using self = tcps_session_impl_t<derived_t, args_t>;
  38. using args_type = args_t;
  39. using key_type = std::size_t;
  40. using buffer_type = typename args_t::buffer_t;
  41. using ssl_stream_comp = ssl_stream_cp<derived_t, args_t>;
  42. using super::send;
  43. using super::async_send;
  44. public:
  45. /**
  46. * @brief constructor
  47. */
  48. explicit tcps_session_impl_t(
  49. asio::ssl::context & ctx,
  50. session_mgr_t<derived_t> & sessions,
  51. listener_t & listener,
  52. std::shared_ptr<io_t> rwio,
  53. std::size_t init_buf_size,
  54. std::size_t max_buf_size
  55. )
  56. : super(sessions, listener, std::move(rwio), init_buf_size, max_buf_size)
  57. , ssl_stream_comp(ctx, asio::ssl::stream_base::server)
  58. , ctx_(ctx)
  59. {
  60. }
  61. /**
  62. * @brief destructor
  63. */
  64. ~tcps_session_impl_t()
  65. {
  66. }
  67. public:
  68. /**
  69. * @brief destroy the content of all member variables, this is used for solve the memory leaks.
  70. * After this function is called, this class object cannot be used again.
  71. */
  72. inline void destroy()
  73. {
  74. derived_t& derive = this->derived();
  75. derive.ssl_stream_.reset();
  76. super::destroy();
  77. }
  78. /**
  79. * @brief get this object hash key,used for session map
  80. */
  81. inline key_type hash_key() const noexcept
  82. {
  83. return reinterpret_cast<key_type>(this);
  84. }
  85. /**
  86. * @brief get the stream object reference
  87. */
  88. inline typename ssl_stream_comp::ssl_stream_type& stream() noexcept
  89. {
  90. return this->derived().ssl_stream();
  91. }
  92. /**
  93. * @brief get the stream object reference
  94. */
  95. inline typename ssl_stream_comp::ssl_stream_type const& stream() const noexcept
  96. {
  97. return this->derived().ssl_stream();
  98. }
  99. protected:
  100. template<typename C>
  101. inline void _do_init(std::shared_ptr<derived_t>& this_ptr, std::shared_ptr<ecs_t<C>>& ecs)
  102. {
  103. super::_do_init(this_ptr, ecs);
  104. this->derived()._ssl_init(ecs, this->derived().socket(), this->ctx_);
  105. }
  106. template<typename DeferEvent>
  107. inline void _post_shutdown(const error_code& ec, std::shared_ptr<derived_t> this_ptr, DeferEvent chain)
  108. {
  109. ASIO2_LOG_DEBUG("tcps_session::_post_shutdown: {} {}", ec.value(), ec.message());
  110. this->derived()._ssl_stop(this_ptr, defer_event
  111. {
  112. [this, ec, this_ptr, e = chain.move_event()] (event_queue_guard<derived_t> g) mutable
  113. {
  114. super::_post_shutdown(ec, std::move(this_ptr), defer_event(std::move(e), std::move(g)));
  115. }, chain.move_guard()
  116. });
  117. }
  118. template<typename C, typename DeferEvent>
  119. inline void _handle_connect(
  120. const error_code& ec,
  121. std::shared_ptr<derived_t> this_ptr, std::shared_ptr<ecs_t<C>> ecs, DeferEvent chain)
  122. {
  123. detail::ignore_unused(ec);
  124. derived_t& derive = this->derived();
  125. ASIO2_ASSERT(!ec);
  126. ASIO2_ASSERT(derive.sessions_.io_->running_in_this_thread());
  127. asio::dispatch(derive.io_->context(), make_allocator(derive.wallocator(),
  128. [&derive, this_ptr = std::move(this_ptr), ecs = std::move(ecs), chain = std::move(chain)]
  129. () mutable
  130. {
  131. derive._ssl_start(this_ptr, ecs, derive.socket(), derive.ctx_);
  132. derive._post_handshake(std::move(this_ptr), std::move(ecs), std::move(chain));
  133. }));
  134. }
  135. inline void _fire_handshake(std::shared_ptr<derived_t>& this_ptr)
  136. {
  137. // the _fire_handshake must be executed in the thread 0.
  138. ASIO2_ASSERT(this->sessions_.io_->running_in_this_thread());
  139. this->listener_.notify(event_type::handshake, this_ptr);
  140. }
  141. protected:
  142. asio::ssl::context & ctx_;
  143. };
  144. }
  145. namespace asio2
  146. {
  147. using tcps_session_args = detail::template_args_tcp_session;
  148. template<class derived_t, class args_t>
  149. using tcps_session_impl_t = detail::tcps_session_impl_t<derived_t, args_t>;
  150. /**
  151. * @brief ssl tcp session
  152. */
  153. template<class derived_t>
  154. class tcps_session_t : public detail::tcps_session_impl_t<derived_t, detail::template_args_tcp_session>
  155. {
  156. public:
  157. using detail::tcps_session_impl_t<derived_t, detail::template_args_tcp_session>::tcps_session_impl_t;
  158. };
  159. /**
  160. * @brief ssl tcp session
  161. */
  162. class tcps_session : public tcps_session_t<tcps_session>
  163. {
  164. public:
  165. using tcps_session_t<tcps_session>::tcps_session_t;
  166. };
  167. }
  168. #if defined(ASIO2_INCLUDE_RATE_LIMIT)
  169. #include <asio2/tcp/tcp_stream.hpp>
  170. namespace asio2
  171. {
  172. struct tcps_rate_session_args : public tcps_session_args
  173. {
  174. using socket_t = asio2::tcp_stream<asio2::simple_rate_policy>;
  175. };
  176. template<class derived_t>
  177. class tcps_rate_session_t : public asio2::tcps_session_impl_t<derived_t, tcps_rate_session_args>
  178. {
  179. public:
  180. using asio2::tcps_session_impl_t<derived_t, tcps_rate_session_args>::tcps_session_impl_t;
  181. };
  182. class tcps_rate_session : public asio2::tcps_rate_session_t<tcps_rate_session>
  183. {
  184. public:
  185. using asio2::tcps_rate_session_t<tcps_rate_session>::tcps_rate_session_t;
  186. };
  187. }
  188. #endif
  189. #include <asio2/base/detail/pop_options.hpp>
  190. #endif // !__ASIO2_TCPS_SESSION_HPP__
  191. #endif