2023-11-11 16:43:58 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2024-05-14 21:40:20 +02:00
|
|
|
class NetworkConnection final : public INetworkConnection, public std::enable_shared_from_this<NetworkConnection>
|
2023-11-11 16:43:58 +02:00
|
|
|
{
|
|
|
|
static const int messageHeaderSize = sizeof(uint32_t);
|
2023-12-25 21:23:27 +02:00
|
|
|
static const int messageMaxSize = 64 * 1024 * 1024; // arbitrary size to prevent potential massive allocation if we receive garbage input
|
2023-11-11 16:43:58 +02:00
|
|
|
|
2024-05-11 10:46:00 +02:00
|
|
|
std::list<std::vector<std::byte>> dataToSend;
|
2023-11-11 16:43:58 +02:00
|
|
|
std::shared_ptr<NetworkSocket> socket;
|
2024-05-11 17:12:49 +02:00
|
|
|
std::shared_ptr<NetworkTimer> timer;
|
2024-05-07 10:34:47 +02:00
|
|
|
std::mutex writeMutex;
|
2023-11-11 16:43:58 +02:00
|
|
|
|
|
|
|
NetworkBuffer readBuffer;
|
2023-11-12 13:27:22 +02:00
|
|
|
INetworkConnectionListener & listener;
|
2024-05-14 21:40:20 +02:00
|
|
|
bool asyncWritesEnabled = false;
|
2023-11-11 16:43:58 +02:00
|
|
|
|
2024-05-11 16:32:43 +02:00
|
|
|
void heartbeat();
|
2024-05-12 21:13:17 +02:00
|
|
|
void onError(const std::string & message);
|
2024-05-11 17:12:49 +02:00
|
|
|
|
|
|
|
void startReceiving();
|
2023-11-11 16:43:58 +02:00
|
|
|
void onHeaderReceived(const boost::system::error_code & ec);
|
|
|
|
void onPacketReceived(const boost::system::error_code & ec, uint32_t expectedPacketSize);
|
|
|
|
|
2024-05-11 10:46:00 +02:00
|
|
|
void doSendData();
|
|
|
|
void onDataSent(const boost::system::error_code & ec);
|
|
|
|
|
2023-11-11 16:43:58 +02:00
|
|
|
public:
|
2024-05-11 16:32:43 +02:00
|
|
|
NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket, const std::shared_ptr<NetworkContext> & context);
|
2023-11-11 16:43:58 +02:00
|
|
|
|
|
|
|
void start();
|
2024-02-02 15:32:06 +02:00
|
|
|
void close() override;
|
2024-02-02 01:27:19 +02:00
|
|
|
void sendPacket(const std::vector<std::byte> & message) override;
|
2024-05-14 21:40:20 +02:00
|
|
|
void setAsyncWritesEnabled(bool on) override;
|
2023-11-11 16:43:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|