123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #ifndef __ASIO2_SOCKS5_CLIENT_HPP__
- #define __ASIO2_SOCKS5_CLIENT_HPP__
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- #pragma once
- #endif
- #include <asio2/base/detail/push_options.hpp>
- #include <asio2/tcp/tcp_client.hpp>
- namespace asio2::detail
- {
- ASIO2_CLASS_FORWARD_DECLARE_BASE;
- ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE;
- ASIO2_CLASS_FORWARD_DECLARE_UDP_BASE;
- ASIO2_CLASS_FORWARD_DECLARE_TCP_CLIENT;
- ASIO2_CLASS_FORWARD_DECLARE_UDP_CLIENT;
- template<class, class> class socks5_session_impl_t;
- template<class derived_t, class executor_t>
- class socks5_client_impl_t
- : public executor_t
- {
- ASIO2_CLASS_FRIEND_DECLARE_BASE;
- ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
- ASIO2_CLASS_FRIEND_DECLARE_UDP_BASE;
- ASIO2_CLASS_FRIEND_DECLARE_TCP_CLIENT;
- ASIO2_CLASS_FRIEND_DECLARE_UDP_CLIENT;
- template<class, class> friend class socks5_session_impl_t;
- public:
- using super = executor_t;
- using self = socks5_client_impl_t<derived_t, executor_t>;
- using executor_type = executor_t;
- using args_type = typename executor_t::args_type;
- using super::async_start;
- public:
-
- template<class... Args>
- explicit socks5_client_impl_t(Args&&... args)
- : super(std::forward<Args>(args)...)
- {
- this->connect_finish_timer_ = std::make_shared<asio::steady_timer>(this->io_->context());
- }
-
- ~socks5_client_impl_t()
- {
- this->stop();
- }
-
- inline void destroy()
- {
- derived_t& derive = this->derived();
- derive.connect_finish_timer_.reset();
- super::destroy();
- }
-
- template<typename String, typename StrOrInt, typename CompletionToken, typename... Args>
- auto async_start(String&& host, StrOrInt&& port, CompletionToken&& token, Args&&... args)
- {
- derived_t& derive = this->derived();
- bool f = executor_t::template async_start(
- std::forward<String>(host), std::forward<StrOrInt>(port), std::forward<Args>(args)...);
- derive.connect_finish_timer_->expires_after(f ?
- (std::chrono::steady_clock::duration::max)() :
- (std::chrono::steady_clock::duration::zero)());
- return asio::async_compose<CompletionToken, void(asio::error_code)>(
- detail::wait_timer_op{*(derive.connect_finish_timer_)},
- token, derive.socket());
- }
- protected:
- 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)
- {
- detail::cancel_timer(*(this->derived().connect_finish_timer_));
- super::_handle_connect(ec, std::move(this_ptr), std::move(ecs), std::move(chain));
- }
- protected:
- std::shared_ptr<asio::steady_timer> connect_finish_timer_;
- };
- }
- namespace asio2
- {
- template<class derived_t, class executor_t>
- using socks5_client_impl_t = detail::socks5_client_impl_t<derived_t, executor_t>;
-
- template<class derived_t>
- class socks5_tcp_client_t : public detail::socks5_client_impl_t<derived_t, tcp_client_t<derived_t>>
- {
- public:
- using detail::socks5_client_impl_t<derived_t, tcp_client_t<derived_t>>::socks5_client_impl_t;
- };
-
- class socks5_tcp_client : public socks5_tcp_client_t<socks5_tcp_client>
- {
- public:
- using socks5_tcp_client_t<socks5_tcp_client>::socks5_tcp_client_t;
- };
- }
- #include <asio2/base/detail/pop_options.hpp>
- #endif
|