Skip to content

Commit

Permalink
fix: gcc compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
shuai132 committed Sep 13, 2023
1 parent 1c097e3 commit 6951cfb
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 95 deletions.
121 changes: 69 additions & 52 deletions detail/tcp_client_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,58 +115,6 @@ class tcp_client_t : public tcp_channel_t<T> {
template <socket_type>
void async_connect_handler(const std::error_code& ec);

template <>
void async_connect_handler<socket_type::normal>(const std::error_code& ec) {
if (!ec) {
this->init_socket();
tcp_channel_t<T>::on_close = [this] {
tcp_client_t::on_close();
check_reconnect();
};
if (on_open) on_open();
if (reconnect_timer_) {
reconnect_timer_->cancel();
}
this->do_read_start();
} else {
if (on_open_failed) on_open_failed(ec);
check_reconnect();
}
}

template <>
inline void async_connect_handler<socket_type::domain>(const std::error_code& ec) {
async_connect_handler<socket_type::normal>(ec);
}

#ifdef ASIO_NET_ENABLE_SSL
template <>
void async_connect_handler<socket_type::ssl>(const std::error_code& ec) {
if (!ec) {
this->init_socket();
socket_.async_handshake(asio::ssl::stream_base::client, [this](const std::error_code& error) {
if (!error) {
tcp_channel_t<T>::on_close = [this] {
tcp_client_t::on_close();
check_reconnect();
};
if (on_open) on_open();
if (reconnect_timer_) {
reconnect_timer_->cancel();
}
this->do_read_start();
} else {
if (on_open_failed) on_open_failed(error);
check_reconnect();
}
});
} else {
if (on_open_failed) on_open_failed(ec);
check_reconnect();
}
}
#endif

public:
std::function<void()> on_open;
std::function<void(std::error_code)> on_open_failed;
Expand All @@ -184,5 +132,74 @@ class tcp_client_t : public tcp_channel_t<T> {
std::function<void()> open_;
};

template <>
template <>
void tcp_client_t<socket_type::normal>::async_connect_handler<socket_type::normal>(const std::error_code& ec) {
if (!ec) {
this->init_socket();
tcp_channel_t<socket_type::normal>::on_close = [this] {
tcp_client_t::on_close();
check_reconnect();
};
if (on_open) on_open();
if (reconnect_timer_) {
reconnect_timer_->cancel();
}
this->do_read_start();
} else {
if (on_open_failed) on_open_failed(ec);
check_reconnect();
}
}

template <>
template <>
void tcp_client_t<socket_type::domain>::async_connect_handler<socket_type::domain>(const std::error_code& ec) {
if (!ec) {
this->init_socket();
tcp_channel_t<socket_type::domain>::on_close = [this] {
tcp_client_t::on_close();
check_reconnect();
};
if (on_open) on_open();
if (reconnect_timer_) {
reconnect_timer_->cancel();
}
this->do_read_start();
} else {
if (on_open_failed) on_open_failed(ec);
check_reconnect();
}
}

#ifdef ASIO_NET_ENABLE_SSL
template <>
template <>
void tcp_client_t<socket_type::ssl>::async_connect_handler<socket_type::ssl>(const std::error_code& ec) {
if (!ec) {
this->init_socket();
socket_.async_handshake(asio::ssl::stream_base::client, [this](const std::error_code& error) {
if (!error) {
tcp_channel_t<socket_type::ssl>::on_close = [this] {
tcp_client_t::on_close();
check_reconnect();
};
if (on_open) on_open();
if (reconnect_timer_) {
reconnect_timer_->cancel();
}
this->do_read_start();
} else {
if (on_open_failed) on_open_failed(error);
check_reconnect();
}
});
} else {
if (on_open_failed) on_open_failed(ec);
check_reconnect();
}
}
#endif

} // namespace detail
} // namespace asio_net
93 changes: 53 additions & 40 deletions detail/tcp_server_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,46 +85,6 @@ class tcp_server_t {
template <socket_type>
void do_accept();

template <>
void do_accept<socket_type::normal>() {
acceptor_.async_accept([this](const std::error_code& ec, socket socket) {
if (!ec) {
auto session = std::make_shared<tcp_session_t<T>>(std::move(socket), config_);
session->start();
if (on_session) on_session(session);
do_accept<T>();
} else {
ASIO_NET_LOGE("do_accept: %s", ec.message().c_str());
}
});
}

template <>
inline void do_accept<socket_type::domain>() {
do_accept<socket_type::normal>();
}

#ifdef ASIO_NET_ENABLE_SSL
template <>
void do_accept<socket_type::ssl>() {
acceptor_.async_accept([this](const std::error_code& ec, asio::ip::tcp::socket socket) {
if (!ec) {
auto session = std::make_shared<tcp_session_t<T>>(typename socket_impl<T>::socket(std::move(socket), ssl_context_), config_);
session->async_handshake([this, session](const std::error_code& error) {
if (!error) {
if (on_session) on_session(session);
} else {
if (on_handshake_error) on_handshake_error(error);
}
});
do_accept<T>();
} else {
ASIO_NET_LOGE("do_accept: %s", ec.message().c_str());
}
});
}
#endif

private:
asio::io_context& io_context_;
#ifdef ASIO_NET_ENABLE_SSL
Expand All @@ -134,5 +94,58 @@ class tcp_server_t {
config config_;
};

template <>
template <>
void tcp_server_t<socket_type::normal>::do_accept<socket_type::normal>() {
acceptor_.async_accept([this](const std::error_code& ec, socket socket) {
if (!ec) {
auto session = std::make_shared<tcp_session_t<socket_type::normal>>(std::move(socket), config_);
session->start();
if (on_session) on_session(session);
tcp_server_t<socket_type::normal>::do_accept<socket_type::normal>();
} else {
ASIO_NET_LOGE("do_accept: %s", ec.message().c_str());
}
});
}

template <>
template <>
void tcp_server_t<socket_type::domain>::do_accept<socket_type::domain>() {
acceptor_.async_accept([this](const std::error_code& ec, socket socket) {
if (!ec) {
auto session = std::make_shared<tcp_session_t<socket_type::domain>>(std::move(socket), config_);
session->start();
if (on_session) on_session(session);
tcp_server_t<socket_type::domain>::do_accept<socket_type::domain>();
} else {
ASIO_NET_LOGE("do_accept: %s", ec.message().c_str());
}
});
}

#ifdef ASIO_NET_ENABLE_SSL
template <>
template <>
void tcp_server_t<socket_type::ssl>::do_accept<socket_type::ssl>() {
acceptor_.async_accept([this](const std::error_code& ec, asio::ip::tcp::socket socket) {
if (!ec) {
using ssl_stream = typename socket_impl<socket_type::ssl>::socket;
auto session = std::make_shared<tcp_session_t<socket_type::ssl>>(ssl_stream(std::move(socket), ssl_context_), config_);
session->async_handshake([this, session](const std::error_code& error) {
if (!error) {
if (on_session) on_session(session);
} else {
if (on_handshake_error) on_handshake_error(error);
}
});
do_accept<socket_type::ssl>();
} else {
ASIO_NET_LOGE("do_accept: %s", ec.message().c_str());
}
});
}
#endif

} // namespace detail
} // namespace asio_net
6 changes: 4 additions & 2 deletions rpc/detail/rpc_client_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class rpc_client_t : noncopyable {
public:
explicit rpc_client_t(asio::io_context& io_context, uint32_t max_body_size = UINT32_MAX)
: io_context_(io_context),
client_(std::make_shared<detail::tcp_client_t<T>>(io_context, config{.auto_pack = true, .max_body_size = max_body_size})) {
client_(
std::make_shared<detail::tcp_client_t<T>>(io_context, config{.auto_pack = true, .enable_ipv6 = true, .max_body_size = max_body_size})) {
client_->on_open = [this]() {
auto session = std::make_shared<rpc_session_t<T>>(io_context_);
session->init(client_);
Expand All @@ -37,7 +38,8 @@ class rpc_client_t : noncopyable {
#ifdef ASIO_NET_ENABLE_SSL
explicit rpc_client_t(asio::io_context& io_context, asio::ssl::context& ssl_context, uint32_t max_body_size = UINT32_MAX)
: io_context_(io_context),
client_(std::make_shared<detail::tcp_client_t<T>>(io_context, ssl_context, config{.auto_pack = true, .max_body_size = max_body_size})) {
client_(std::make_shared<detail::tcp_client_t<T>>(io_context, ssl_context,
config{.auto_pack = true, .enable_ipv6 = true, .max_body_size = max_body_size})) {
client_->on_open = [this]() {
auto session = std::make_shared<rpc_session_t<T>>(io_context_);
session->init(client_);
Expand Down
2 changes: 1 addition & 1 deletion test/tcp_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main(int argc, char** argv) {
client.on_close = [] {
LOG("client on_close:");
};
client.open("localhost", PORT);
client.open("::1", PORT);
client.run();
return 0;
}

0 comments on commit 6951cfb

Please sign in to comment.