mirror of
https://github.com/vcmi/vcmi.git
synced 2024-11-24 08:32:34 +02:00
Fix excessive calls of steady_timer::async_wait
This commit is contained in:
parent
67604e1e01
commit
0403626c93
@ -14,7 +14,7 @@ VCMI_LIB_NAMESPACE_BEGIN
|
||||
|
||||
NetworkConnection::NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket, const std::shared_ptr<NetworkContext> & context)
|
||||
: socket(socket)
|
||||
, context(context)
|
||||
, timer(std::make_shared<NetworkTimer>(*context))
|
||||
, listener(listener)
|
||||
{
|
||||
socket->set_option(boost::asio::ip::tcp::no_delay(true));
|
||||
@ -44,7 +44,11 @@ NetworkConnection::NetworkConnection(INetworkConnectionListener & listener, cons
|
||||
void NetworkConnection::start()
|
||||
{
|
||||
heartbeat();
|
||||
startReceiving();
|
||||
}
|
||||
|
||||
void NetworkConnection::startReceiving()
|
||||
{
|
||||
boost::asio::async_read(*socket,
|
||||
readBuffer,
|
||||
boost::asio::transfer_exactly(messageHeaderSize),
|
||||
@ -55,8 +59,8 @@ 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)
|
||||
timer->expires_after(heartbeatInterval);
|
||||
timer->async_wait( [self = shared_from_this()](const auto & ec)
|
||||
{
|
||||
if (ec)
|
||||
return;
|
||||
@ -92,7 +96,7 @@ void NetworkConnection::onHeaderReceived(const boost::system::error_code & ecHea
|
||||
if (messageSize == 0)
|
||||
{
|
||||
//heartbeat package with no payload - wait for next packet
|
||||
start();
|
||||
startReceiving();
|
||||
return;
|
||||
}
|
||||
|
||||
@ -119,7 +123,7 @@ void NetworkConnection::onPacketReceived(const boost::system::error_code & ec, u
|
||||
readBuffer.sgetn(reinterpret_cast<char *>(message.data()), expectedPacketSize);
|
||||
listener.onPacketReceived(shared_from_this(), message);
|
||||
|
||||
start();
|
||||
startReceiving();
|
||||
}
|
||||
|
||||
void NetworkConnection::sendPacket(const std::vector<std::byte> & message)
|
||||
@ -157,7 +161,7 @@ void NetworkConnection::onDataSent(const boost::system::error_code & ec)
|
||||
if (ec)
|
||||
{
|
||||
logNetwork->error("Failed to send package: %s", ec.message());
|
||||
// TODO: Throw?
|
||||
listener.onDisconnected(shared_from_this(), ec.message());
|
||||
return;
|
||||
}
|
||||
|
||||
@ -169,6 +173,7 @@ void NetworkConnection::close()
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
socket->close(ec);
|
||||
timer->cancel(ec);
|
||||
|
||||
//NOTE: ignoring error code, intended
|
||||
}
|
||||
|
@ -20,13 +20,15 @@ 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::shared_ptr<NetworkTimer> timer;
|
||||
std::mutex writeMutex;
|
||||
|
||||
NetworkBuffer readBuffer;
|
||||
INetworkConnectionListener & listener;
|
||||
|
||||
void heartbeat();
|
||||
|
||||
void startReceiving();
|
||||
void onHeaderReceived(const boost::system::error_code & ec);
|
||||
void onPacketReceived(const boost::system::error_code & ec, uint32_t expectedPacketSize);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user