1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

Finalized new TCP networking API

This commit is contained in:
Ivan Savenko
2024-01-12 01:10:41 +02:00
parent 476a05fed3
commit 80e960bc8e
23 changed files with 306 additions and 241 deletions

View File

@@ -13,16 +13,15 @@
VCMI_LIB_NAMESPACE_BEGIN
NetworkServer::NetworkServer(INetworkServerListener & listener)
:listener(listener)
NetworkServer::NetworkServer(INetworkServerListener & listener, const std::shared_ptr<NetworkContext> & context)
: listener(listener)
, io(context)
{
}
void NetworkServer::start(uint16_t port)
{
io = std::make_shared<boost::asio::io_service>();
acceptor = std::make_shared<NetworkAcceptor>(*io, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
startAsyncAccept();
}
@@ -32,16 +31,6 @@ void NetworkServer::startAsyncAccept()
acceptor->async_accept(*upcomingConnection, std::bind(&NetworkServer::connectionAccepted, this, upcomingConnection, _1));
}
void NetworkServer::run()
{
io->run();
}
void NetworkServer::run(std::chrono::milliseconds duration)
{
io->run_for(duration);
}
void NetworkServer::connectionAccepted(std::shared_ptr<NetworkSocket> upcomingConnection, const boost::system::error_code & ec)
{
if(ec)
@@ -50,44 +39,34 @@ void NetworkServer::connectionAccepted(std::shared_ptr<NetworkSocket> upcomingCo
}
logNetwork->info("We got a new connection! :)");
auto connection = std::make_shared<NetworkConnection>(upcomingConnection, *this);
auto connection = std::make_shared<NetworkConnection>(*this, upcomingConnection);
connections.insert(connection);
connection->start();
listener.onNewConnection(connection);
startAsyncAccept();
}
void NetworkServer::sendPacket(const std::shared_ptr<NetworkConnection> & connection, const std::vector<uint8_t> & message)
void NetworkServer::sendPacket(const std::shared_ptr<INetworkConnection> & connection, const std::vector<uint8_t> & message)
{
connection->sendPacket(message);
}
void NetworkServer::closeConnection(const std::shared_ptr<NetworkConnection> & connection)
void NetworkServer::closeConnection(const std::shared_ptr<INetworkConnection> & connection)
{
assert(connections.count(connection));
connections.erase(connection);
}
void NetworkServer::onDisconnected(const std::shared_ptr<NetworkConnection> & connection)
void NetworkServer::onDisconnected(const std::shared_ptr<INetworkConnection> & connection)
{
assert(connections.count(connection));
connections.erase(connection);
listener.onDisconnected(connection);
}
void NetworkServer::onPacketReceived(const std::shared_ptr<NetworkConnection> & connection, const std::vector<uint8_t> & message)
void NetworkServer::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<uint8_t> & message)
{
listener.onPacketReceived(connection, message);
}
void NetworkServer::setTimer(std::chrono::milliseconds duration)
{
auto timer = std::make_shared<NetworkTimer>(*io, duration);
timer->async_wait([this, timer](const boost::system::error_code& error){
if (!error)
listener.onTimer();
});
}
VCMI_LIB_NAMESPACE_END