wss_client.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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_WSS_CLIENT_HPP__
  12. #define __ASIO2_WSS_CLIENT_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/tcps_client.hpp>
  18. #include <asio2/http/ws_client.hpp>
  19. namespace asio2::detail
  20. {
  21. struct template_args_wss_client : public template_args_ws_client
  22. {
  23. using stream_t = websocket::stream<asio::ssl::stream<typename template_args_ws_client::socket_t&>&>;
  24. };
  25. ASIO2_CLASS_FORWARD_DECLARE_BASE;
  26. ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE;
  27. ASIO2_CLASS_FORWARD_DECLARE_TCP_CLIENT;
  28. template<class derived_t, class args_t = template_args_wss_client>
  29. class wss_client_impl_t
  30. : public tcps_client_impl_t<derived_t, args_t>
  31. , public ws_stream_cp <derived_t, args_t>
  32. , public ws_send_op <derived_t, args_t>
  33. {
  34. ASIO2_CLASS_FRIEND_DECLARE_BASE;
  35. ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
  36. ASIO2_CLASS_FRIEND_DECLARE_TCP_CLIENT;
  37. public:
  38. using super = tcps_client_impl_t<derived_t, args_t>;
  39. using self = wss_client_impl_t <derived_t, args_t>;
  40. using args_type = args_t;
  41. using body_type = typename args_t::body_t;
  42. using buffer_type = typename args_t::buffer_t;
  43. using ws_stream_comp = ws_stream_cp<derived_t, args_t>;
  44. using super::send;
  45. using super::async_send;
  46. public:
  47. /**
  48. * @brief constructor
  49. */
  50. template<class... Args>
  51. explicit wss_client_impl_t(
  52. asio::ssl::context::method method,
  53. Args&&... args
  54. )
  55. : super(method, std::forward<Args>(args)...)
  56. , ws_stream_cp<derived_t, args_t>()
  57. , ws_send_op <derived_t, args_t>()
  58. {
  59. }
  60. /**
  61. * @brief constructor
  62. */
  63. template<class... Args>
  64. explicit wss_client_impl_t(Args&&... args)
  65. : super(ASIO2_DEFAULT_SSL_METHOD, std::forward<Args>(args)...)
  66. , ws_stream_cp<derived_t, args_t>()
  67. , ws_send_op <derived_t, args_t>()
  68. {
  69. }
  70. /**
  71. * @brief constructor
  72. */
  73. explicit wss_client_impl_t()
  74. : super(ASIO2_DEFAULT_SSL_METHOD)
  75. , ws_stream_cp<derived_t, args_t>()
  76. , ws_send_op <derived_t, args_t>()
  77. {
  78. }
  79. /**
  80. * @brief destructor
  81. */
  82. ~wss_client_impl_t()
  83. {
  84. this->stop();
  85. }
  86. /**
  87. * @brief destroy the content of all member variables, this is used for solve the memory leaks.
  88. * After this function is called, this class object cannot be used again.
  89. */
  90. inline void destroy()
  91. {
  92. derived_t& derive = this->derived();
  93. derive.ws_stream_.reset();
  94. super::destroy();
  95. }
  96. /**
  97. * @brief return the websocket stream object reference
  98. */
  99. inline typename args_t::stream_t& stream() noexcept
  100. {
  101. return this->derived().ws_stream();
  102. }
  103. /**
  104. * @brief return the websocket stream object reference
  105. */
  106. inline typename args_t::stream_t const& stream() const noexcept
  107. {
  108. return this->derived().ws_stream();
  109. }
  110. /**
  111. * @brief start the client, blocking connect to server
  112. * @param host - A string identifying a location. May be a descriptive name or
  113. * a numeric address string.
  114. * @param port - A string identifying the requested service. This may be a
  115. * descriptive name or a numeric string corresponding to a port number.
  116. * @param args - The args can be include the upgraged target.
  117. * eg: start("127.0.0.1", 8883); start("127.0.0.1", 8883, "/admin");
  118. * the "/admin" is the websocket upgraged target
  119. */
  120. template<typename String, typename StrOrInt, typename... Args>
  121. inline bool start(String&& host, StrOrInt&& port, Args&&... args)
  122. {
  123. if constexpr (sizeof...(Args) > std::size_t(0))
  124. return this->derived().template _do_connect_with_target<false>(
  125. std::forward<String>(host), std::forward<StrOrInt>(port),
  126. std::forward<Args>(args)...);
  127. else
  128. return this->derived().template _do_connect<false>(
  129. std::forward<String>(host), std::forward<StrOrInt>(port),
  130. ecs_helper::make_ecs('0', std::forward<Args>(args)...));
  131. }
  132. /**
  133. * @brief start the client, asynchronous connect to server
  134. * @param host - A string identifying a location. May be a descriptive name or
  135. * a numeric address string.
  136. * @param port - A string identifying the requested service. This may be a
  137. * descriptive name or a numeric string corresponding to a port number.
  138. * @param args - The args can be include the upgraged target.
  139. * eg: async_start("127.0.0.1", 8883); async_start("127.0.0.1", 8883, "/admin");
  140. * the "/admin" is the websocket upgraged target
  141. */
  142. template<typename String, typename StrOrInt, typename... Args>
  143. inline bool async_start(String&& host, StrOrInt&& port, Args&&... args)
  144. {
  145. if constexpr (sizeof...(Args) > std::size_t(0))
  146. return this->derived().template _do_connect_with_target<true>(
  147. std::forward<String>(host), std::forward<StrOrInt>(port),
  148. std::forward<Args>(args)...);
  149. else
  150. return this->derived().template _do_connect<true>(
  151. std::forward<String>(host), std::forward<StrOrInt>(port),
  152. ecs_helper::make_ecs('0', std::forward<Args>(args)...));
  153. }
  154. /**
  155. * @brief get the websocket upgraged response object
  156. */
  157. inline websocket::response_type& get_upgrade_response() noexcept { return this->upgrade_rep_; }
  158. /**
  159. * @brief get the websocket upgraged response object
  160. */
  161. inline const websocket::response_type& get_upgrade_response() const noexcept { return this->upgrade_rep_; }
  162. /**
  163. * @brief get the websocket upgraged target
  164. */
  165. inline const std::string& get_upgrade_target() const noexcept { return this->upgrade_target_; }
  166. /**
  167. * @brief set the websocket upgraged target
  168. */
  169. inline derived_t & set_upgrade_target(std::string target)
  170. {
  171. this->upgrade_target_ = std::move(target);
  172. return (this->derived());
  173. }
  174. public:
  175. /**
  176. * @brief bind websocket upgrade listener
  177. * @param fun - a user defined callback function.
  178. * Function signature : void()
  179. */
  180. template<class F, class ...C>
  181. inline derived_t & bind_upgrade(F&& fun, C&&... obj)
  182. {
  183. this->listener_.bind(event_type::upgrade,
  184. observer_t<>(std::forward<F>(fun), std::forward<C>(obj)...));
  185. return (this->derived());
  186. }
  187. protected:
  188. template<bool IsAsync, typename String, typename StrOrInt, typename Arg1, typename... Args>
  189. bool _do_connect_with_target(String&& host, StrOrInt&& port, Arg1&& arg1, Args&&... args)
  190. {
  191. if constexpr (detail::can_convert_to_string<detail::remove_cvref_t<Arg1>>::value)
  192. {
  193. this->derived().set_upgrade_target(std::forward<Arg1>(arg1));
  194. return this->derived().template _do_connect<IsAsync>(
  195. std::forward<String>(host), std::forward<StrOrInt>(port),
  196. ecs_helper::make_ecs('0', std::forward<Args>(args)...));
  197. }
  198. else
  199. {
  200. return this->derived().template _do_connect<IsAsync>(
  201. std::forward<String>(host), std::forward<StrOrInt>(port),
  202. ecs_helper::make_ecs('0',
  203. std::forward<Arg1>(arg1), std::forward<Args>(args)...));
  204. }
  205. }
  206. template<typename C>
  207. inline void _do_init(std::shared_ptr<ecs_t<C>>& ecs)
  208. {
  209. super::_do_init(ecs);
  210. this->derived()._ws_init(ecs, this->derived().ssl_stream());
  211. }
  212. template<typename DeferEvent>
  213. inline void _post_shutdown(const error_code& ec, std::shared_ptr<derived_t> this_ptr, DeferEvent chain)
  214. {
  215. ASIO2_LOG_DEBUG("wss_client::_post_shutdown: {} {}", ec.value(), ec.message());
  216. this->derived()._ws_stop(this_ptr, defer_event
  217. {
  218. [this, ec, this_ptr, e = chain.move_event()](event_queue_guard<derived_t> g) mutable
  219. {
  220. super::_post_shutdown(ec, std::move(this_ptr), defer_event(std::move(e), std::move(g)));
  221. }, chain.move_guard()
  222. });
  223. }
  224. template<typename C, typename DeferEvent>
  225. inline void _handle_connect(
  226. const error_code& ec,
  227. std::shared_ptr<derived_t> this_ptr, std::shared_ptr<ecs_t<C>> ecs, DeferEvent chain)
  228. {
  229. set_last_error(ec);
  230. derived_t& derive = this->derived();
  231. if (ec)
  232. {
  233. return derive._done_connect(ec, std::move(this_ptr), std::move(ecs), std::move(chain));
  234. }
  235. derive._ssl_start(this_ptr, ecs, derive.socket(), *this);
  236. derive._ws_start(this_ptr, ecs, derive.ssl_stream());
  237. derive._post_handshake(std::move(this_ptr), std::move(ecs), std::move(chain));
  238. }
  239. template<typename C, typename DeferEvent>
  240. inline void _handle_handshake(
  241. const error_code& ec,
  242. std::shared_ptr<derived_t> this_ptr, std::shared_ptr<ecs_t<C>> ecs, DeferEvent chain)
  243. {
  244. set_last_error(ec);
  245. derived_t& derive = this->derived();
  246. derive._fire_handshake(this_ptr);
  247. if (ec)
  248. {
  249. return derive._done_connect(ec, std::move(this_ptr), std::move(ecs), std::move(chain));
  250. }
  251. derive._post_control_callback(this_ptr, ecs);
  252. derive._post_upgrade(std::move(this_ptr), std::move(ecs), this->upgrade_rep_, std::move(chain));
  253. }
  254. template<class Data, class Callback>
  255. inline bool _do_send(Data& data, Callback&& callback)
  256. {
  257. return this->derived()._ws_send(data, std::forward<Callback>(callback));
  258. }
  259. protected:
  260. template<typename C>
  261. inline void _post_recv(std::shared_ptr<derived_t> this_ptr, std::shared_ptr<ecs_t<C>> ecs)
  262. {
  263. this->derived()._ws_post_recv(std::move(this_ptr), std::move(ecs));
  264. }
  265. template<typename C>
  266. inline void _handle_recv(
  267. const error_code& ec, std::size_t bytes_recvd,
  268. std::shared_ptr<derived_t> this_ptr, std::shared_ptr<ecs_t<C>> ecs)
  269. {
  270. this->derived()._ws_handle_recv(ec, bytes_recvd, std::move(this_ptr), std::move(ecs));
  271. }
  272. inline void _fire_upgrade(std::shared_ptr<derived_t>& this_ptr)
  273. {
  274. // the _fire_upgrade must be executed in the thread 0.
  275. ASIO2_ASSERT(this->derived().io_->running_in_this_thread());
  276. detail::ignore_unused(this_ptr);
  277. this->listener_.notify(event_type::upgrade);
  278. }
  279. protected:
  280. websocket::response_type upgrade_rep_;
  281. std::string upgrade_target_ = "/";
  282. };
  283. }
  284. namespace asio2
  285. {
  286. using wss_client_args = detail::template_args_wss_client;
  287. template<class derived_t, class args_t>
  288. using wss_client_impl_t = detail::wss_client_impl_t<derived_t, args_t>;
  289. template<class derived_t>
  290. class wss_client_t : public detail::wss_client_impl_t<derived_t, detail::template_args_wss_client>
  291. {
  292. public:
  293. using detail::wss_client_impl_t<derived_t, detail::template_args_wss_client>::wss_client_impl_t;
  294. };
  295. class wss_client : public wss_client_t<wss_client>
  296. {
  297. public:
  298. using wss_client_t<wss_client>::wss_client_t;
  299. };
  300. }
  301. #if defined(ASIO2_INCLUDE_RATE_LIMIT)
  302. #include <asio2/tcp/tcp_stream.hpp>
  303. namespace asio2
  304. {
  305. struct wss_rate_client_args : public wss_client_args
  306. {
  307. using socket_t = asio2::tcp_stream<asio2::simple_rate_policy>;
  308. using stream_t = websocket::stream<asio::ssl::stream<socket_t&>&>;
  309. };
  310. template<class derived_t>
  311. class wss_rate_client_t : public asio2::wss_client_impl_t<derived_t, wss_rate_client_args>
  312. {
  313. public:
  314. using asio2::wss_client_impl_t<derived_t, wss_rate_client_args>::wss_client_impl_t;
  315. };
  316. class wss_rate_client : public asio2::wss_rate_client_t<wss_rate_client>
  317. {
  318. public:
  319. using asio2::wss_rate_client_t<wss_rate_client>::wss_rate_client_t;
  320. };
  321. }
  322. #endif
  323. #include <asio2/base/detail/pop_options.hpp>
  324. #endif // !__ASIO2_WSS_CLIENT_HPP__
  325. #endif