2024-01-12 01:10:41 +02:00
|
|
|
/*
|
|
|
|
* NetworkHandler.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "NetworkHandler.h"
|
|
|
|
|
|
|
|
#include "NetworkServer.h"
|
2024-01-12 16:55:36 +02:00
|
|
|
#include "NetworkConnection.h"
|
2024-01-12 01:10:41 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
std::unique_ptr<INetworkHandler> INetworkHandler::createHandler()
|
|
|
|
{
|
|
|
|
return std::make_unique<NetworkHandler>();
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkHandler::NetworkHandler()
|
|
|
|
: io(std::make_shared<NetworkContext>())
|
|
|
|
{}
|
|
|
|
|
|
|
|
std::unique_ptr<INetworkServer> NetworkHandler::createServerTCP(INetworkServerListener & listener)
|
|
|
|
{
|
|
|
|
return std::make_unique<NetworkServer>(listener, io);
|
|
|
|
}
|
|
|
|
|
2024-01-12 16:55:36 +02:00
|
|
|
void NetworkHandler::connectToRemote(INetworkClientListener & listener, const std::string & host, uint16_t port)
|
2024-01-12 01:10:41 +02:00
|
|
|
{
|
2024-01-12 16:55:36 +02:00
|
|
|
auto socket = std::make_shared<NetworkSocket>(*io);
|
2024-05-11 10:46:41 +02:00
|
|
|
auto resolver = std::make_shared<boost::asio::ip::tcp::resolver>(*io);
|
|
|
|
|
|
|
|
resolver->async_resolve(host, std::to_string(port),
|
2024-05-11 18:18:21 +02:00
|
|
|
[this, &listener, resolver, socket](const boost::system::error_code& error, const boost::asio::ip::tcp::resolver::results_type & endpoints)
|
2024-01-12 16:55:36 +02:00
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
listener.onConnectionFailed(error.message());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-11 18:18:21 +02:00
|
|
|
boost::asio::async_connect(*socket, endpoints, [this, socket, &listener](const boost::system::error_code& error, const boost::asio::ip::tcp::endpoint& endpoint)
|
2024-05-11 10:46:41 +02:00
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
listener.onConnectionFailed(error.message());
|
|
|
|
return;
|
|
|
|
}
|
2024-05-11 16:32:43 +02:00
|
|
|
auto connection = std::make_shared<NetworkConnection>(listener, socket, io);
|
2024-05-11 10:46:41 +02:00
|
|
|
connection->start();
|
|
|
|
|
|
|
|
listener.onConnectionEstablished(connection);
|
|
|
|
});
|
2024-01-12 16:55:36 +02:00
|
|
|
});
|
2024-01-12 01:10:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkHandler::run()
|
|
|
|
{
|
|
|
|
boost::asio::executor_work_guard<decltype(io->get_executor())> work{io->get_executor()};
|
|
|
|
io->run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkHandler::createTimer(INetworkTimerListener & listener, std::chrono::milliseconds duration)
|
|
|
|
{
|
|
|
|
auto timer = std::make_shared<NetworkTimer>(*io, duration);
|
|
|
|
timer->async_wait([&listener, timer](const boost::system::error_code& error){
|
|
|
|
if (!error)
|
|
|
|
listener.onTimer();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkHandler::stop()
|
|
|
|
{
|
|
|
|
io->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|