1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Use async_resolve to avoid crash on resolve error

This commit is contained in:
Ivan Savenko 2024-05-11 08:46:41 +00:00
parent a74d9791a1
commit 16f43254b5

View File

@ -32,19 +32,29 @@ std::unique_ptr<INetworkServer> NetworkHandler::createServerTCP(INetworkServerLi
void NetworkHandler::connectToRemote(INetworkClientListener & listener, const std::string & host, uint16_t port)
{
auto socket = std::make_shared<NetworkSocket>(*io);
boost::asio::ip::tcp::resolver resolver(*io);
auto endpoints = resolver.resolve(host, std::to_string(port));
boost::asio::async_connect(*socket, endpoints, [socket, &listener](const boost::system::error_code& error, const boost::asio::ip::tcp::endpoint& endpoint)
auto resolver = std::make_shared<boost::asio::ip::tcp::resolver>(*io);
resolver->async_resolve(host, std::to_string(port),
[&listener, resolver, socket](const boost::system::error_code& error, const boost::asio::ip::tcp::resolver::results_type & endpoints)
{
if (error)
{
listener.onConnectionFailed(error.message());
return;
}
auto connection = std::make_shared<NetworkConnection>(listener, socket);
connection->start();
listener.onConnectionEstablished(connection);
boost::asio::async_connect(*socket, endpoints, [socket, &listener](const boost::system::error_code& error, const boost::asio::ip::tcp::endpoint& endpoint)
{
if (error)
{
listener.onConnectionFailed(error.message());
return;
}
auto connection = std::make_shared<NetworkConnection>(listener, socket);
connection->start();
listener.onConnectionEstablished(connection);
});
});
}