123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- /*
- * Copyright (c) 2017-2023 zhllxt
- *
- * author : zhllxt
- * email : 37792738@qq.com
- *
- * Distributed under the Boost Software License, Version 1.0. (See accompanying
- * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- */
- #if defined(ASIO2_ENABLE_SSL) || defined(ASIO2_USE_SSL)
- #ifndef __ASIO2_WSS_CLIENT_HPP__
- #define __ASIO2_WSS_CLIENT_HPP__
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- #pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include <asio2/base/detail/push_options.hpp>
- #include <asio2/tcp/tcps_client.hpp>
- #include <asio2/http/ws_client.hpp>
- namespace asio2::detail
- {
- struct template_args_wss_client : public template_args_ws_client
- {
- using stream_t = websocket::stream<asio::ssl::stream<typename template_args_ws_client::socket_t&>&>;
- };
- ASIO2_CLASS_FORWARD_DECLARE_BASE;
- ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE;
- ASIO2_CLASS_FORWARD_DECLARE_TCP_CLIENT;
- template<class derived_t, class args_t = template_args_wss_client>
- class wss_client_impl_t
- : public tcps_client_impl_t<derived_t, args_t>
- , public ws_stream_cp <derived_t, args_t>
- , public ws_send_op <derived_t, args_t>
- {
- ASIO2_CLASS_FRIEND_DECLARE_BASE;
- ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
- ASIO2_CLASS_FRIEND_DECLARE_TCP_CLIENT;
- public:
- using super = tcps_client_impl_t<derived_t, args_t>;
- using self = wss_client_impl_t <derived_t, args_t>;
- using args_type = args_t;
- using body_type = typename args_t::body_t;
- using buffer_type = typename args_t::buffer_t;
- using ws_stream_comp = ws_stream_cp<derived_t, args_t>;
- using super::send;
- using super::async_send;
- public:
- /**
- * @brief constructor
- */
- template<class... Args>
- explicit wss_client_impl_t(
- asio::ssl::context::method method,
- Args&&... args
- )
- : super(method, std::forward<Args>(args)...)
- , ws_stream_cp<derived_t, args_t>()
- , ws_send_op <derived_t, args_t>()
- {
- }
- /**
- * @brief constructor
- */
- template<class... Args>
- explicit wss_client_impl_t(Args&&... args)
- : super(ASIO2_DEFAULT_SSL_METHOD, std::forward<Args>(args)...)
- , ws_stream_cp<derived_t, args_t>()
- , ws_send_op <derived_t, args_t>()
- {
- }
- /**
- * @brief constructor
- */
- explicit wss_client_impl_t()
- : super(ASIO2_DEFAULT_SSL_METHOD)
- , ws_stream_cp<derived_t, args_t>()
- , ws_send_op <derived_t, args_t>()
- {
- }
- /**
- * @brief destructor
- */
- ~wss_client_impl_t()
- {
- this->stop();
- }
- /**
- * @brief destroy the content of all member variables, this is used for solve the memory leaks.
- * After this function is called, this class object cannot be used again.
- */
- inline void destroy()
- {
- derived_t& derive = this->derived();
- derive.ws_stream_.reset();
- super::destroy();
- }
- /**
- * @brief return the websocket stream object reference
- */
- inline typename args_t::stream_t& stream() noexcept
- {
- return this->derived().ws_stream();
- }
- /**
- * @brief return the websocket stream object reference
- */
- inline typename args_t::stream_t const& stream() const noexcept
- {
- return this->derived().ws_stream();
- }
- /**
- * @brief start the client, blocking connect to server
- * @param host - A string identifying a location. May be a descriptive name or
- * a numeric address string.
- * @param port - A string identifying the requested service. This may be a
- * descriptive name or a numeric string corresponding to a port number.
- * @param args - The args can be include the upgraged target.
- * eg: start("127.0.0.1", 8883); start("127.0.0.1", 8883, "/admin");
- * the "/admin" is the websocket upgraged target
- */
- template<typename String, typename StrOrInt, typename... Args>
- inline bool start(String&& host, StrOrInt&& port, Args&&... args)
- {
- if constexpr (sizeof...(Args) > std::size_t(0))
- return this->derived().template _do_connect_with_target<false>(
- std::forward<String>(host), std::forward<StrOrInt>(port),
- std::forward<Args>(args)...);
- else
- return this->derived().template _do_connect<false>(
- std::forward<String>(host), std::forward<StrOrInt>(port),
- ecs_helper::make_ecs('0', std::forward<Args>(args)...));
- }
- /**
- * @brief start the client, asynchronous connect to server
- * @param host - A string identifying a location. May be a descriptive name or
- * a numeric address string.
- * @param port - A string identifying the requested service. This may be a
- * descriptive name or a numeric string corresponding to a port number.
- * @param args - The args can be include the upgraged target.
- * eg: async_start("127.0.0.1", 8883); async_start("127.0.0.1", 8883, "/admin");
- * the "/admin" is the websocket upgraged target
- */
- template<typename String, typename StrOrInt, typename... Args>
- inline bool async_start(String&& host, StrOrInt&& port, Args&&... args)
- {
- if constexpr (sizeof...(Args) > std::size_t(0))
- return this->derived().template _do_connect_with_target<true>(
- std::forward<String>(host), std::forward<StrOrInt>(port),
- std::forward<Args>(args)...);
- else
- return this->derived().template _do_connect<true>(
- std::forward<String>(host), std::forward<StrOrInt>(port),
- ecs_helper::make_ecs('0', std::forward<Args>(args)...));
- }
- /**
- * @brief get the websocket upgraged response object
- */
- inline websocket::response_type& get_upgrade_response() noexcept { return this->upgrade_rep_; }
- /**
- * @brief get the websocket upgraged response object
- */
- inline const websocket::response_type& get_upgrade_response() const noexcept { return this->upgrade_rep_; }
- /**
- * @brief get the websocket upgraged target
- */
- inline const std::string& get_upgrade_target() const noexcept { return this->upgrade_target_; }
- /**
- * @brief set the websocket upgraged target
- */
- inline derived_t & set_upgrade_target(std::string target)
- {
- this->upgrade_target_ = std::move(target);
- return (this->derived());
- }
- public:
- /**
- * @brief bind websocket upgrade listener
- * @param fun - a user defined callback function.
- * Function signature : void()
- */
- template<class F, class ...C>
- inline derived_t & bind_upgrade(F&& fun, C&&... obj)
- {
- this->listener_.bind(event_type::upgrade,
- observer_t<>(std::forward<F>(fun), std::forward<C>(obj)...));
- return (this->derived());
- }
- protected:
- template<bool IsAsync, typename String, typename StrOrInt, typename Arg1, typename... Args>
- bool _do_connect_with_target(String&& host, StrOrInt&& port, Arg1&& arg1, Args&&... args)
- {
- if constexpr (detail::can_convert_to_string<detail::remove_cvref_t<Arg1>>::value)
- {
- this->derived().set_upgrade_target(std::forward<Arg1>(arg1));
- return this->derived().template _do_connect<IsAsync>(
- std::forward<String>(host), std::forward<StrOrInt>(port),
- ecs_helper::make_ecs('0', std::forward<Args>(args)...));
- }
- else
- {
- return this->derived().template _do_connect<IsAsync>(
- std::forward<String>(host), std::forward<StrOrInt>(port),
- ecs_helper::make_ecs('0',
- std::forward<Arg1>(arg1), std::forward<Args>(args)...));
- }
- }
- template<typename C>
- inline void _do_init(std::shared_ptr<ecs_t<C>>& ecs)
- {
- super::_do_init(ecs);
- this->derived()._ws_init(ecs, this->derived().ssl_stream());
- }
- template<typename DeferEvent>
- inline void _post_shutdown(const error_code& ec, std::shared_ptr<derived_t> this_ptr, DeferEvent chain)
- {
- ASIO2_LOG_DEBUG("wss_client::_post_shutdown: {} {}", ec.value(), ec.message());
- this->derived()._ws_stop(this_ptr, defer_event
- {
- [this, ec, this_ptr, e = chain.move_event()](event_queue_guard<derived_t> g) mutable
- {
- super::_post_shutdown(ec, std::move(this_ptr), defer_event(std::move(e), std::move(g)));
- }, chain.move_guard()
- });
- }
- template<typename C, typename DeferEvent>
- inline void _handle_connect(
- const error_code& ec,
- std::shared_ptr<derived_t> this_ptr, std::shared_ptr<ecs_t<C>> ecs, DeferEvent chain)
- {
- set_last_error(ec);
- derived_t& derive = this->derived();
- if (ec)
- {
- return derive._done_connect(ec, std::move(this_ptr), std::move(ecs), std::move(chain));
- }
- derive._ssl_start(this_ptr, ecs, derive.socket(), *this);
- derive._ws_start(this_ptr, ecs, derive.ssl_stream());
- derive._post_handshake(std::move(this_ptr), std::move(ecs), std::move(chain));
- }
- template<typename C, typename DeferEvent>
- inline void _handle_handshake(
- const error_code& ec,
- std::shared_ptr<derived_t> this_ptr, std::shared_ptr<ecs_t<C>> ecs, DeferEvent chain)
- {
- set_last_error(ec);
- derived_t& derive = this->derived();
- derive._fire_handshake(this_ptr);
- if (ec)
- {
- return derive._done_connect(ec, std::move(this_ptr), std::move(ecs), std::move(chain));
- }
- derive._post_control_callback(this_ptr, ecs);
- derive._post_upgrade(std::move(this_ptr), std::move(ecs), this->upgrade_rep_, std::move(chain));
- }
- template<class Data, class Callback>
- inline bool _do_send(Data& data, Callback&& callback)
- {
- return this->derived()._ws_send(data, std::forward<Callback>(callback));
- }
- protected:
- template<typename C>
- inline void _post_recv(std::shared_ptr<derived_t> this_ptr, std::shared_ptr<ecs_t<C>> ecs)
- {
- this->derived()._ws_post_recv(std::move(this_ptr), std::move(ecs));
- }
- template<typename C>
- inline void _handle_recv(
- const error_code& ec, std::size_t bytes_recvd,
- std::shared_ptr<derived_t> this_ptr, std::shared_ptr<ecs_t<C>> ecs)
- {
- this->derived()._ws_handle_recv(ec, bytes_recvd, std::move(this_ptr), std::move(ecs));
- }
- inline void _fire_upgrade(std::shared_ptr<derived_t>& this_ptr)
- {
- // the _fire_upgrade must be executed in the thread 0.
- ASIO2_ASSERT(this->derived().io_->running_in_this_thread());
- detail::ignore_unused(this_ptr);
- this->listener_.notify(event_type::upgrade);
- }
- protected:
- websocket::response_type upgrade_rep_;
- std::string upgrade_target_ = "/";
- };
- }
- namespace asio2
- {
- using wss_client_args = detail::template_args_wss_client;
- template<class derived_t, class args_t>
- using wss_client_impl_t = detail::wss_client_impl_t<derived_t, args_t>;
- template<class derived_t>
- class wss_client_t : public detail::wss_client_impl_t<derived_t, detail::template_args_wss_client>
- {
- public:
- using detail::wss_client_impl_t<derived_t, detail::template_args_wss_client>::wss_client_impl_t;
- };
- class wss_client : public wss_client_t<wss_client>
- {
- public:
- using wss_client_t<wss_client>::wss_client_t;
- };
- }
- #if defined(ASIO2_INCLUDE_RATE_LIMIT)
- #include <asio2/tcp/tcp_stream.hpp>
- namespace asio2
- {
- struct wss_rate_client_args : public wss_client_args
- {
- using socket_t = asio2::tcp_stream<asio2::simple_rate_policy>;
- using stream_t = websocket::stream<asio::ssl::stream<socket_t&>&>;
- };
- template<class derived_t>
- class wss_rate_client_t : public asio2::wss_client_impl_t<derived_t, wss_rate_client_args>
- {
- public:
- using asio2::wss_client_impl_t<derived_t, wss_rate_client_args>::wss_client_impl_t;
- };
- class wss_rate_client : public asio2::wss_rate_client_t<wss_rate_client>
- {
- public:
- using asio2::wss_rate_client_t<wss_rate_client>::wss_rate_client_t;
- };
- }
- #endif
- #include <asio2/base/detail/pop_options.hpp>
- #endif // !__ASIO2_WSS_CLIENT_HPP__
- #endif
|