mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-16 10:19:47 +02:00
35954dc41b
At the moment, vcmilobby *requires* async writes in order to handle multiple connections with different speeds and at optimal performance, without hanging if one player is too slow and can't eat all data server sent to him at once. However server (and potentially - client) can not handle this mode and may shutdown either socket or entire asio service too early, before all writes are performed, leading to weird freeze on ending scenario where client would not receive notifications about end of game.
50 lines
1.5 KiB
C++
50 lines
1.5 KiB
C++
/*
|
|
* NetworkConnection.h, 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
|
|
*
|
|
*/
|
|
#pragma once
|
|
|
|
#include "NetworkDefines.h"
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
class NetworkConnection final : public INetworkConnection, public std::enable_shared_from_this<NetworkConnection>
|
|
{
|
|
static const int messageHeaderSize = sizeof(uint32_t);
|
|
static const int messageMaxSize = 64 * 1024 * 1024; // arbitrary size to prevent potential massive allocation if we receive garbage input
|
|
|
|
std::list<std::vector<std::byte>> dataToSend;
|
|
std::shared_ptr<NetworkSocket> socket;
|
|
std::shared_ptr<NetworkTimer> timer;
|
|
std::mutex writeMutex;
|
|
|
|
NetworkBuffer readBuffer;
|
|
INetworkConnectionListener & listener;
|
|
bool asyncWritesEnabled = false;
|
|
|
|
void heartbeat();
|
|
void onError(const std::string & message);
|
|
|
|
void startReceiving();
|
|
void onHeaderReceived(const boost::system::error_code & ec);
|
|
void onPacketReceived(const boost::system::error_code & ec, uint32_t expectedPacketSize);
|
|
|
|
void doSendData();
|
|
void onDataSent(const boost::system::error_code & ec);
|
|
|
|
public:
|
|
NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket, const std::shared_ptr<NetworkContext> & context);
|
|
|
|
void start();
|
|
void close() override;
|
|
void sendPacket(const std::vector<std::byte> & message) override;
|
|
void setAsyncWritesEnabled(bool on) override;
|
|
};
|
|
|
|
VCMI_LIB_NAMESPACE_END
|