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

Reapply "Simple implementation of heartbeat package"

This reverts commit ede92c1a9c.
This commit is contained in:
Ivan Savenko 2024-05-11 14:32:43 +00:00
parent 4c3aac8f19
commit 67604e1e01
4 changed files with 28 additions and 6 deletions

View File

@ -12,12 +12,12 @@
VCMI_LIB_NAMESPACE_BEGIN
NetworkConnection::NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket)
NetworkConnection::NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket, const std::shared_ptr<NetworkContext> & context)
: socket(socket)
, context(context)
, listener(listener)
{
socket->set_option(boost::asio::ip::tcp::no_delay(true));
socket->set_option(boost::asio::socket_base::keep_alive(true));
// iOS throws exception on attempt to set buffer size
constexpr auto bufferSize = 4 * 1024 * 1024;
@ -43,12 +43,32 @@ NetworkConnection::NetworkConnection(INetworkConnectionListener & listener, cons
void NetworkConnection::start()
{
heartbeat();
boost::asio::async_read(*socket,
readBuffer,
boost::asio::transfer_exactly(messageHeaderSize),
[self = shared_from_this()](const auto & ec, const auto & endpoint) { self->onHeaderReceived(ec); });
}
void NetworkConnection::heartbeat()
{
constexpr auto heartbeatInterval = std::chrono::seconds(10);
auto timer = std::make_shared<NetworkTimer>(*context, heartbeatInterval);
timer->async_wait( [self = shared_from_this(), timer](const auto & ec)
{
if (ec)
return;
if (!self->socket->is_open())
return;
self->sendPacket({});
self->heartbeat();
});
}
void NetworkConnection::onHeaderReceived(const boost::system::error_code & ecHeader)
{
if (ecHeader)
@ -71,7 +91,7 @@ void NetworkConnection::onHeaderReceived(const boost::system::error_code & ecHea
if (messageSize == 0)
{
// Zero-sized packet. Strange, but safe to ignore. Start reading next packet
//heartbeat package with no payload - wait for next packet
start();
return;
}

View File

@ -20,11 +20,13 @@ class NetworkConnection : public INetworkConnection, public std::enable_shared_f
std::list<std::vector<std::byte>> dataToSend;
std::shared_ptr<NetworkSocket> socket;
std::shared_ptr<NetworkContext> context;
std::mutex writeMutex;
NetworkBuffer readBuffer;
INetworkConnectionListener & listener;
void heartbeat();
void onHeaderReceived(const boost::system::error_code & ec);
void onPacketReceived(const boost::system::error_code & ec, uint32_t expectedPacketSize);
@ -32,7 +34,7 @@ class NetworkConnection : public INetworkConnection, public std::enable_shared_f
void onDataSent(const boost::system::error_code & ec);
public:
NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket);
NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket, const std::shared_ptr<NetworkContext> & context);
void start();
void close() override;

View File

@ -50,7 +50,7 @@ void NetworkHandler::connectToRemote(INetworkClientListener & listener, const st
listener.onConnectionFailed(error.message());
return;
}
auto connection = std::make_shared<NetworkConnection>(listener, socket);
auto connection = std::make_shared<NetworkConnection>(listener, socket, io);
connection->start();
listener.onConnectionEstablished(connection);

View File

@ -39,7 +39,7 @@ void NetworkServer::connectionAccepted(std::shared_ptr<NetworkSocket> upcomingCo
}
logNetwork->info("We got a new connection! :)");
auto connection = std::make_shared<NetworkConnection>(*this, upcomingConnection);
auto connection = std::make_shared<NetworkConnection>(*this, upcomingConnection, io);
connections.insert(connection);
connection->start();
listener.onNewConnection(connection);