1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Revert "Simple implementation of heartbeat package"

This reverts commit 5f344e410a.
This commit is contained in:
Ivan Savenko 2024-05-07 08:31:08 +00:00
parent 4949b98ed3
commit ede92c1a9c
4 changed files with 7 additions and 30 deletions

View File

@ -12,12 +12,12 @@
VCMI_LIB_NAMESPACE_BEGIN
NetworkConnection::NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket, const std::shared_ptr<NetworkContext> & context)
NetworkConnection::NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket)
: 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,32 +43,12 @@ 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)
@ -91,8 +71,7 @@ void NetworkConnection::onHeaderReceived(const boost::system::error_code & ecHea
if (messageSize == 0)
{
//heartbeat package with no payload - wait for next packet
start();
listener.onDisconnected(shared_from_this(), "Zero-sized packet!");
return;
}

View File

@ -19,17 +19,15 @@ class NetworkConnection : public INetworkConnection, public std::enable_shared_f
static const int messageMaxSize = 64 * 1024 * 1024; // arbitrary size to prevent potential massive allocation if we receive garbage input
std::shared_ptr<NetworkSocket> socket;
std::shared_ptr<NetworkContext> context;
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);
public:
NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket, const std::shared_ptr<NetworkContext> & context);
NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket);
void start();
void close() override;

View File

@ -34,14 +34,14 @@ void NetworkHandler::connectToRemote(INetworkClientListener & listener, const st
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, [this, socket, &listener](const boost::system::error_code& error, const boost::asio::ip::tcp::endpoint& endpoint)
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, io);
auto connection = std::make_shared<NetworkConnection>(listener, socket);
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, io);
auto connection = std::make_shared<NetworkConnection>(*this, upcomingConnection);
connections.insert(connection);
connection->start();
listener.onNewConnection(connection);