2023-11-11 16:43:58 +02:00
|
|
|
/*
|
|
|
|
* NetworkServer.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 "NetworkServer.h"
|
|
|
|
#include "NetworkConnection.h"
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2024-01-12 01:10:41 +02:00
|
|
|
NetworkServer::NetworkServer(INetworkServerListener & listener, const std::shared_ptr<NetworkContext> & context)
|
2024-01-20 00:26:25 +02:00
|
|
|
: io(context)
|
|
|
|
, listener(listener)
|
2023-11-18 16:34:18 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-07-11 23:39:36 +02:00
|
|
|
uint16_t NetworkServer::start(uint16_t port)
|
2023-11-11 16:43:58 +02:00
|
|
|
{
|
2024-07-12 13:54:28 +02:00
|
|
|
acceptor = std::make_shared<NetworkAcceptor>(*io, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
|
2024-07-11 23:39:36 +02:00
|
|
|
return startAsyncAccept();
|
2023-11-11 16:43:58 +02:00
|
|
|
}
|
|
|
|
|
2024-07-11 23:39:36 +02:00
|
|
|
uint16_t NetworkServer::startAsyncAccept()
|
2023-11-11 16:43:58 +02:00
|
|
|
{
|
2024-01-26 17:40:31 +02:00
|
|
|
auto upcomingConnection = std::make_shared<NetworkSocket>(*io);
|
2024-02-02 15:32:06 +02:00
|
|
|
acceptor->async_accept(*upcomingConnection, [this, upcomingConnection](const auto & ec) { connectionAccepted(upcomingConnection, ec); });
|
2024-07-11 23:39:36 +02:00
|
|
|
return acceptor->local_endpoint().port();
|
2023-11-11 16:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkServer::connectionAccepted(std::shared_ptr<NetworkSocket> upcomingConnection, const boost::system::error_code & ec)
|
|
|
|
{
|
|
|
|
if(ec)
|
|
|
|
{
|
2023-11-12 13:27:22 +02:00
|
|
|
throw std::runtime_error("Something wrong during accepting: " + ec.message());
|
2023-11-11 16:43:58 +02:00
|
|
|
}
|
|
|
|
|
2023-11-12 13:27:22 +02:00
|
|
|
logNetwork->info("We got a new connection! :)");
|
2024-05-11 16:32:43 +02:00
|
|
|
auto connection = std::make_shared<NetworkConnection>(*this, upcomingConnection, io);
|
2023-11-12 13:27:22 +02:00
|
|
|
connections.insert(connection);
|
|
|
|
connection->start();
|
2023-11-18 16:34:18 +02:00
|
|
|
listener.onNewConnection(connection);
|
2023-11-11 16:43:58 +02:00
|
|
|
startAsyncAccept();
|
|
|
|
}
|
|
|
|
|
2024-02-02 01:27:19 +02:00
|
|
|
void NetworkServer::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
|
2023-11-12 13:27:22 +02:00
|
|
|
{
|
2024-02-02 01:27:19 +02:00
|
|
|
logNetwork->info("Connection lost! Reason: %s", errorMessage);
|
2024-05-13 16:09:59 +02:00
|
|
|
if (connections.count(connection))
|
|
|
|
{
|
|
|
|
connections.erase(connection);
|
|
|
|
listener.onDisconnected(connection, errorMessage);
|
|
|
|
}
|
2023-11-18 16:34:18 +02:00
|
|
|
}
|
|
|
|
|
2024-02-02 01:27:19 +02:00
|
|
|
void NetworkServer::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message)
|
2023-11-18 16:34:18 +02:00
|
|
|
{
|
|
|
|
listener.onPacketReceived(connection, message);
|
2023-11-12 13:27:22 +02:00
|
|
|
}
|
|
|
|
|
2023-11-11 16:43:58 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|