From 8c0c1d0e2de3ba0652e8f088a3614f7fa2a184d1 Mon Sep 17 00:00:00 2001 From: matthiakl Date: Fri, 21 Mar 2025 19:08:48 +0100 Subject: [PATCH 1/2] Update asio interfaces --- src/network/bufferedconnection.cc | 14 +++++++------- src/network/bufferedconnection.h | 4 ++-- src/network/nethost.cc | 12 ++++++------ src/network/nethost.h | 4 ++-- src/network/network.cc | 13 ++++++------- src/network/network_lan_promotion.cc | 4 ++-- src/network/network_lan_promotion.h | 2 +- 7 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/network/bufferedconnection.cc b/src/network/bufferedconnection.cc index 715c2baf5c..24ea15f956 100644 --- a/src/network/bufferedconnection.cc +++ b/src/network/bufferedconnection.cc @@ -141,12 +141,12 @@ void BufferedConnection::close() { verb_log_info("[BufferedConnection] Closing network socket."); } // Stop the thread - io_service_.stop(); + io_context_.stop(); // Not sure if that is required, wait up to one second for the io_service to stop - for (int i = 0; i < 1000 && !io_service_.stopped(); i++) { + for (int i = 0; i < 1000 && !io_context_.stopped(); i++) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } - assert(io_service_.stopped()); + assert(io_context_.stopped()); if (asio_thread_.joinable()) { try { asio_thread_.join(); @@ -334,7 +334,7 @@ void BufferedConnection::reduce_send_buffer(asio::ip::tcp::socket& socket) { } BufferedConnection::BufferedConnection(const NetAddress& host) - : socket_(io_service_), currently_sending_(false) { + : socket_(io_context_), currently_sending_(false) { const asio::ip::tcp::endpoint destination(host.ip, host.port); @@ -353,7 +353,7 @@ BufferedConnection::BufferedConnection(const NetAddress& host) asio_thread_ = std::thread([this]() { // The output might actually be messed up if it collides with the main thread... verb_log_info("[BufferedConnection] Starting networking thread\n"); - io_service_.run(); + io_context_.run(); verb_log_info("[BufferedConnection] Stopping networking thread\n"); }); } else { @@ -364,7 +364,7 @@ BufferedConnection::BufferedConnection(const NetAddress& host) } } -BufferedConnection::BufferedConnection() : socket_(io_service_) { +BufferedConnection::BufferedConnection() : socket_(io_context_) { } void BufferedConnection::notify_connected() { @@ -379,7 +379,7 @@ void BufferedConnection::notify_connected() { asio_thread_ = std::thread([this]() { // The output might actually be messed up if it collides with the main thread... verb_log_info("[BufferedConnection] Starting networking thread"); - io_service_.run(); + io_context_.run(); verb_log_info("[BufferedConnection] Stopping networking thread"); }); } diff --git a/src/network/bufferedconnection.h b/src/network/bufferedconnection.h index 71f9bdfd6a..c47b994500 100644 --- a/src/network/bufferedconnection.h +++ b/src/network/bufferedconnection.h @@ -314,8 +314,8 @@ private: /// Each packet in the queue is a vector of uint8_t. std::map>> buffers_to_send_; - /// An io_service needed by asio. Primarily needed for asynchronous operations. - asio::io_service io_service_; + /// An io_context needed by asio. Primarily needed for asynchronous operations. + asio::io_context io_context_; /// The socket that connects us to the host. asio::ip::tcp::socket socket_; diff --git a/src/network/nethost.cc b/src/network/nethost.cc index 16b0845492..25e369ef3c 100644 --- a/src/network/nethost.cc +++ b/src/network/nethost.cc @@ -67,11 +67,11 @@ bool NetHost::is_connected(const ConnectionId id) const { void NetHost::stop_listening() { // Stop the thread - io_service_.stop(); - for (int i = 0; i < 1000 && !io_service_.stopped(); i++) { + io_context_.stop(); + for (int i = 0; i < 1000 && !io_context_.stopped(); i++) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } - assert(io_service_.stopped()); + assert(io_context_.stopped()); if (asio_thread_.joinable()) { try { asio_thread_.join(); @@ -183,7 +183,7 @@ void NetHost::start_accepting( }); } -NetHost::NetHost(const uint16_t port) : acceptor_v4_(io_service_), acceptor_v6_(io_service_) { +NetHost::NetHost(const uint16_t port) : acceptor_v4_(io_context_), acceptor_v6_(io_context_) { if (open_acceptor(&acceptor_v4_, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port))) { verb_log_info("[NetHost] Opening a listening IPv4 socket on TCP port %u", port); @@ -198,7 +198,7 @@ NetHost::NetHost(const uint16_t port) : acceptor_v4_(io_service_), acceptor_v6_( asio_thread_ = std::thread([this]() { verb_log_info("[NetHost] Starting networking thread"); - io_service_.run(); + io_context_.run(); verb_log_info("[NetHost] Stopping networking thread"); }); } @@ -214,7 +214,7 @@ bool NetHost::open_acceptor(asio::ip::tcp::acceptor* acceptor, acceptor->set_option(option_v6only); } acceptor->bind(endpoint); - acceptor->listen(asio::socket_base::max_connections); + acceptor->listen(asio::socket_base::max_listen_connections); return true; } catch (const std::system_error&) { return false; diff --git a/src/network/nethost.h b/src/network/nethost.h index 86fe4f920d..7d17502b9d 100644 --- a/src/network/nethost.h +++ b/src/network/nethost.h @@ -101,8 +101,8 @@ private: std::map> clients_; /// The next client id that will be used NetHostInterface::ConnectionId next_id_{1}; - /// An io_service needed by asio. Primary needed for async operations. - asio::io_service io_service_; + /// An io_context needed by asio. Primary needed for async operations. + asio::io_context io_context_; /// The acceptor we get IPv4 connection requests to. asio::ip::tcp::acceptor acceptor_v4_; /// The acceptor we get IPv6 connection requests to. diff --git a/src/network/network.cc b/src/network/network.cc index f945b28b66..4ce15fdd64 100644 --- a/src/network/network.cc +++ b/src/network/network.cc @@ -30,17 +30,16 @@ bool do_resolve(const asio::ip::tcp& protocol, uint16_t port) { assert(addr != nullptr); try { - asio::io_service io_service; - asio::ip::tcp::resolver resolver(io_service); - asio::ip::tcp::resolver::query query(protocol, hostname, as_string(port)); - asio::ip::tcp::resolver::iterator iter = resolver.resolve(query); - if (iter == asio::ip::tcp::resolver::iterator()) { + asio::io_context io_context; + asio::ip::tcp::resolver resolver(io_context); + asio::ip::tcp::resolver::results_type iter = resolver.resolve(protocol, hostname, as_string(port)); + if (iter.empty()) { // Resolution failed log_err("Could not resolve network name '%s:%u' to %s-address\n", hostname.c_str(), port, ((protocol == asio::ip::tcp::v4()) ? "IPv4" : "IPv6")); return false; } - addr->ip = iter->endpoint().address(); + addr->ip = iter.begin()->endpoint().address(); addr->port = port; verb_log_info("Resolved network name '%s:%u' to %s", hostname.c_str(), port, addr->ip.to_string().c_str()); @@ -64,7 +63,7 @@ bool NetAddress::resolve_to_v6(NetAddress* addr, const std::string& hostname, ui bool NetAddress::parse_ip(NetAddress* addr, const std::string& ip, uint16_t port) { std::error_code ec; - asio::ip::address new_addr = asio::ip::address::from_string(ip, ec); + asio::ip::address new_addr = asio::ip::make_address(ip, ec); if (ec) { return false; } diff --git a/src/network/network_lan_promotion.cc b/src/network/network_lan_promotion.cc index 5e8e8f6b91..7cdbf1962e 100644 --- a/src/network/network_lan_promotion.cc +++ b/src/network/network_lan_promotion.cc @@ -75,7 +75,7 @@ int get_ip_version(const asio::ip::udp& version) { * On Apple we have to specify the interface, forcing us to send our message over all interfaces we * can find. */ -LanBase::LanBase(uint16_t port) : socket_v4(io_service), socket_v6(io_service) { +LanBase::LanBase(uint16_t port) : socket_v4(io_context), socket_v6(io_context) { #ifndef _WIN32 // Iterate over all interfaces. If they support IPv4, store the broadcast-address @@ -252,7 +252,7 @@ bool LanBase::broadcast(void const* const buf, size_t const len, uint16_t const asio::ip::udp::socket& socket, const std::string& address) -> bool { if (socket.is_open()) { std::error_code ec; - asio::ip::udp::endpoint destination(asio::ip::address::from_string(address), port); + asio::ip::udp::endpoint destination(asio::ip::make_address(address), port); socket.send_to(asio::buffer(buf, len), destination, 0, ec); if (!ec) { return true; diff --git a/src/network/network_lan_promotion.h b/src/network/network_lan_promotion.h index dc1ccbadbc..a227043ccf 100644 --- a/src/network/network_lan_promotion.h +++ b/src/network/network_lan_promotion.h @@ -135,7 +135,7 @@ private: void close_socket(asio::ip::udp::socket* socket); /// No idea what this does. I think it is only really used when asynchronous operations are done. - asio::io_service io_service; + asio::io_context io_context; /// The socket for IPv4. asio::ip::udp::socket socket_v4; /// The socket for IPv6. -- 2.45.2 From c6aff96817daa6ebc464339974cb20fced8826f9 Mon Sep 17 00:00:00 2001 From: Widelands Bunnybot Date: Sat, 22 Mar 2025 15:41:03 +0100 Subject: [PATCH 2/2] 'src/network/network.cc' was automatically formatted. --- src/network/network.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/network/network.cc b/src/network/network.cc index 4ce15fdd64..1c142f1f84 100644 --- a/src/network/network.cc +++ b/src/network/network.cc @@ -32,7 +32,8 @@ bool do_resolve(const asio::ip::tcp& protocol, try { asio::io_context io_context; asio::ip::tcp::resolver resolver(io_context); - asio::ip::tcp::resolver::results_type iter = resolver.resolve(protocol, hostname, as_string(port)); + asio::ip::tcp::resolver::results_type iter = + resolver.resolve(protocol, hostname, as_string(port)); if (iter.empty()) { // Resolution failed log_err("Could not resolve network name '%s:%u' to %s-address\n", hostname.c_str(), port, -- 2.45.2