Skip to content

Commit

Permalink
Update async connect:
Browse files Browse the repository at this point in the history
1. Drop c++03 on async_connect.cpp
2. Use tcp::socket instead net::basic_stream_socket<tcp, net::io_context::executor_type> avoid compile error
3. Fix check_condition, use auto& stream
  • Loading branch information
Jackarain committed Oct 26, 2023
1 parent c1e306e commit 51b3259
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 75 deletions.
160 changes: 95 additions & 65 deletions proxy/include/proxy/async_connect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <boost/asio/dispatch.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/async_result.hpp>

#include <boost/asio/cancellation_signal.hpp>
#include <boost/asio/associated_cancellation_slot.hpp>
Expand Down Expand Up @@ -358,14 +359,18 @@ namespace asio_util {
};
}

template <typename Stream,
template <typename Protocol, typename Executor,
typename Iterator, typename ConnectHandler>
inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ConnectHandler,
void(boost::system::error_code, Iterator))
async_connect(Stream& s, Iterator begin,
BOOST_ASIO_MOVE_ARG(ConnectHandler) handler,
inline auto async_connect(net::basic_stream_socket<Protocol, Executor>& s,
Iterator begin,
ConnectHandler handler = net::default_completion_token_t<Executor>(),
typename net::enable_if<
!net::is_endpoint_sequence<Iterator>::value>::type* = 0)
-> decltype(net::async_initiate<ConnectHandler,
void(boost::system::error_code, Iterator)>
(detail::initiate_do_connect{}, handler, &s,
begin, Iterator(),
detail::default_connect_condition{}))
{
return net::async_initiate<ConnectHandler,
void(boost::system::error_code, Iterator)>
Expand All @@ -374,81 +379,106 @@ namespace asio_util {
detail::default_connect_condition{});
}

template <typename Stream,
typename Iterator, typename ConnectHandler>
inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ConnectHandler,
void(boost::system::error_code, Iterator))
async_connect(Stream& s, Iterator begin, Iterator end,
BOOST_ASIO_MOVE_ARG(ConnectHandler) handler)
{
template <typename Protocol, typename Executor, typename Iterator,
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(boost::system::error_code,
Iterator))
ConnectHandler = net::default_completion_token_t<Executor>>
auto async_connect(
net::basic_stream_socket<Protocol, Executor>& s, Iterator begin,
Iterator end,
ConnectHandler&& handler = net::default_completion_token_t<Executor>())
-> decltype(net::async_initiate<ConnectHandler,
void(boost::system::error_code, Iterator)>(
detail::initiate_do_connect{}, handler, &s, begin, end,
detail::default_connect_condition{})) {
return net::async_initiate<ConnectHandler,
void(boost::system::error_code, Iterator)>
(detail::initiate_do_connect{}, handler, &s,
begin, end,
detail::default_connect_condition{});
void(boost::system::error_code, Iterator)>(
detail::initiate_do_connect{}, handler, &s, begin, end,
detail::default_connect_condition{});
}

template <typename Stream,
typename EndpointSequence, typename ConnectHandler>
inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ConnectHandler,
void(boost::system::error_code, typename Stream::endpoint_type))
async_connect(Stream& s, const EndpointSequence& endpoints,
BOOST_ASIO_MOVE_ARG(ConnectHandler) handler,
template <typename Protocol, typename Executor, typename EndpointSequence,
typename ConnectCondition,
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(boost::system::error_code,
typename Protocol::endpoint))
ConnectHandler = net::default_completion_token_t<Executor>>
auto async_connect(
net::basic_stream_socket<Protocol, Executor>& s,
const EndpointSequence& endpoints,
ConnectHandler&& handler = net::default_completion_token_t<Executor>(),
typename net::enable_if<
net::is_endpoint_sequence<EndpointSequence>::value>::type* = 0)
{
net::is_endpoint_sequence<EndpointSequence>::value>::type* = 0) {
return net::async_initiate<ConnectHandler,
void(boost::system::error_code, typename Stream::endpoint_type)>
(detail::initiate_do_connect{}, handler, &s, endpoints,
detail::default_connect_condition{});
void(boost::system::error_code,
typename net::basic_stream_socket<
Protocol, Executor>::endpoint_type)>(
detail::initiate_do_connect{}, handler, &s, endpoints,
detail::default_connect_condition{});
}

template <typename Stream,
typename Iterator, typename ConnectHandler, typename ConnectCondition>
inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ConnectHandler,
void(boost::system::error_code, Iterator))
async_connect(Stream& s, Iterator begin,
ConnectCondition connect_condition,
BOOST_ASIO_MOVE_ARG(ConnectHandler) handler,
typename net::enable_if<
!net::is_endpoint_sequence<Iterator>::value>::type* = 0)
{
template <typename Protocol, typename Executor, typename Iterator,
typename ConnectCondition,
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(boost::system::error_code,
Iterator))
ConnectHandler = net::default_completion_token_t<Executor>>
auto async_connect(
net::basic_stream_socket<Protocol, Executor>& s, Iterator begin,
ConnectCondition connect_condition,
ConnectHandler&& handler = net::default_completion_token_t<Executor>(),
typename net::enable_if<
!net::is_endpoint_sequence<Iterator>::value>::type* = 0)
-> decltype(net::async_initiate<ConnectHandler,
void(boost::system::error_code, Iterator)>(
detail::initiate_do_connect{}, handler, &s, begin, Iterator(),
connect_condition)) {
return net::async_initiate<ConnectHandler,
void(boost::system::error_code, Iterator)>
(detail::initiate_do_connect{}, handler, &s,
begin, Iterator(),
connect_condition);
void(boost::system::error_code, Iterator)>(
detail::initiate_do_connect{}, handler, &s, begin, Iterator(),
connect_condition);
}

template <typename Stream,
typename Iterator, typename ConnectHandler, typename ConnectCondition>
inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ConnectHandler,
void(boost::system::error_code, Iterator))
async_connect(Stream& s, Iterator begin, Iterator end,
ConnectCondition connect_condition,
BOOST_ASIO_MOVE_ARG(ConnectHandler) handler)
{
template <typename Protocol, typename Executor, typename EndpointSequence,
typename Iterator, typename ConnectCondition,
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(boost::system::error_code,
typename Protocol::endpoint))
ConnectHandler = net::default_completion_token_t<Executor>>
auto async_connect(
net::basic_stream_socket<Protocol, Executor>& s, Iterator begin,
Iterator end, ConnectCondition connect_condition,
ConnectHandler&& handler = net::default_completion_token_t<Executor>())
-> decltype(net::async_initiate<ConnectHandler,
void(boost::system::error_code, Iterator)>(
detail::initiate_do_connect{}, handler, &s, begin, end,
connect_condition)) {
return net::async_initiate<ConnectHandler,
void(boost::system::error_code, Iterator)>
(detail::initiate_do_connect{}, handler, &s,
begin, end,
connect_condition);
void(boost::system::error_code, Iterator)>(
detail::initiate_do_connect{}, handler, &s, begin, end,
connect_condition);
}

template <typename Stream, typename EndpointSequence,
typename ConnectHandler, typename ConnectCondition>
inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ConnectHandler,
void(boost::system::error_code, typename Stream::endpoint_type))
async_connect(Stream& s, const EndpointSequence& endpoints,
ConnectCondition connect_condition,
BOOST_ASIO_MOVE_ARG(ConnectHandler) handler,
template <typename Protocol, typename Executor, typename EndpointSequence,
typename ConnectCondition,
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(boost::system::error_code,
typename Protocol::endpoint))
ConnectHandler = net::default_completion_token_t<Executor>>
auto async_connect(
net::basic_stream_socket<Protocol, Executor>& s,
const EndpointSequence& endpoints, ConnectCondition connect_condition,
ConnectHandler&& handler = net::default_completion_token_t<Executor>(),
typename net::enable_if<
net::is_endpoint_sequence<EndpointSequence>::value>::type* = 0)
{
net::is_endpoint_sequence<EndpointSequence>::value>::type* = 0)
-> decltype(net::async_initiate<
ConnectHandler, void(boost::system::error_code,
typename net::basic_stream_socket<
Protocol, Executor>::endpoint_type)>(
detail::initiate_do_connect{}, handler, & s, endpoints,
connect_condition)) {
return net::async_initiate<ConnectHandler,
void(boost::system::error_code, typename Stream::endpoint_type)>
(detail::initiate_do_connect{}, handler, &s,
endpoints, connect_condition);
void(boost::system::error_code,
typename net::basic_stream_socket<
Protocol, Executor>::endpoint_type)>(
detail::initiate_do_connect{}, handler, &s, endpoints,
connect_condition);
}
}

Expand Down
11 changes: 4 additions & 7 deletions proxy/include/proxy/base_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ namespace util {
using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
using udp = net::ip::udp; // from <boost/asio/ip/udp.hpp>

using tcp_acceptor = net::basic_socket_acceptor<
tcp, net::io_context::executor_type>;

using tcp_socket = net::basic_stream_socket<
tcp, net::io_context::executor_type>;
using tcp_acceptor = tcp::acceptor;
using tcp_socket = tcp::socket;

template<typename... T>
class base_stream : public boost::variant2::variant<T...>
Expand All @@ -49,7 +46,7 @@ namespace util {
base_stream& operator=(base_stream&&) = default;
base_stream(base_stream&&) = default;

using executor_type = net::io_context::executor_type;
using executor_type = net::any_io_executor;

executor_type get_executor()
{
Expand Down Expand Up @@ -140,7 +137,7 @@ namespace util {
}

inline proxy_stream_type instantiate_proxy_stream(
net::io_context::executor_type executor)
net::any_io_executor executor)
{
return proxy_stream_type(tcp_socket(executor));
}
Expand Down
3 changes: 1 addition & 2 deletions proxy/include/proxy/http_proxy_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ namespace proxy {
using http_request = http::request<http::string_body>;
using http_response = http::response<http::dynamic_body>;

using tcp_socket = net::basic_stream_socket<
tcp, net::io_context::executor_type>;
using tcp_socket = tcp::socket;

using ssl_stream = net::ssl::stream<tcp_socket>;

Expand Down
2 changes: 1 addition & 1 deletion proxy/include/proxy/proxy_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,7 @@ namespace proxy {

auto check_condition = [this, bind_interface](
const boost::system::error_code&,
tcp_socket& stream, auto&) mutable
auto& stream, auto&) mutable
{
if (m_option.local_ip_.empty())
return true;
Expand Down

0 comments on commit 51b3259

Please sign in to comment.