1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-24 03:47:18 +02:00
vcmi/lib/network/NetworkConnection.cpp

130 lines
3.5 KiB
C++
Raw Normal View History

/*
* NetworkConnection.cpp, 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
*
*/
#include "StdInc.h"
#include "NetworkConnection.h"
VCMI_LIB_NAMESPACE_BEGIN
NetworkConnection::NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket)
: socket(socket)
2023-11-12 13:27:22 +02:00
, 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;
try
{
socket->set_option(boost::asio::socket_base::send_buffer_size{bufferSize});
}
catch(const boost::system::system_error & e)
{
logNetwork->error("error setting 'send buffer size' socket option: %s", e.what());
}
try
{
socket->set_option(boost::asio::socket_base::receive_buffer_size{bufferSize});
}
catch(const boost::system::system_error & e)
{
logNetwork->error("error setting 'receive buffer size' socket option: %s", e.what());
}
}
void NetworkConnection::start()
{
boost::asio::async_read(*socket,
readBuffer,
boost::asio::transfer_exactly(messageHeaderSize),
[self = shared_from_this()](const auto & ec, const auto & endpoint) { self->onHeaderReceived(ec); });
}
2024-02-03 22:59:56 +02:00
void NetworkConnection::onHeaderReceived(const boost::system::error_code & ecHeader)
{
2024-02-03 22:59:56 +02:00
if (ecHeader)
2023-11-12 13:27:22 +02:00
{
2024-02-03 22:59:56 +02:00
listener.onDisconnected(shared_from_this(), ecHeader.message());
2023-11-12 13:27:22 +02:00
return;
}
if (readBuffer.size() < messageHeaderSize)
throw std::runtime_error("Failed to read header!");
uint32_t messageSize;
2024-02-02 01:27:19 +02:00
readBuffer.sgetn(reinterpret_cast<char *>(&messageSize), sizeof(messageSize));
if (messageSize > messageMaxSize)
2024-02-03 22:59:56 +02:00
{
listener.onDisconnected(shared_from_this(), "Invalid packet size!");
2024-02-03 22:59:56 +02:00
return;
}
2024-02-19 13:46:07 +02:00
if (messageSize == 0)
{
// Zero-sized packet. Strange, but safe to ignore. Start reading next packet
start();
2024-02-19 13:46:07 +02:00
return;
}
2024-02-03 22:59:56 +02:00
boost::asio::async_read(*socket,
readBuffer,
boost::asio::transfer_exactly(messageSize),
[self = shared_from_this(), messageSize](const auto & ecPayload, const auto & endpoint) { self->onPacketReceived(ecPayload, messageSize); });
}
void NetworkConnection::onPacketReceived(const boost::system::error_code & ec, uint32_t expectedPacketSize)
{
if (ec)
{
2024-02-02 01:27:19 +02:00
listener.onDisconnected(shared_from_this(), ec.message());
2023-11-12 13:27:22 +02:00
return;
}
if (readBuffer.size() < expectedPacketSize)
{
2024-02-03 22:59:56 +02:00
throw std::runtime_error("Failed to read packet!");
}
2024-02-02 01:27:19 +02:00
std::vector<std::byte> message(expectedPacketSize);
readBuffer.sgetn(reinterpret_cast<char *>(message.data()), expectedPacketSize);
2023-11-12 13:27:22 +02:00
listener.onPacketReceived(shared_from_this(), message);
start();
}
2024-02-02 01:27:19 +02:00
void NetworkConnection::sendPacket(const std::vector<std::byte> & message)
{
std::lock_guard<std::mutex> lock(writeMutex);
2024-02-02 01:27:19 +02:00
boost::system::error_code ec;
2024-02-04 19:56:04 +02:00
// create array with single element - boost::asio::buffer can be constructed from containers, but not from plain integer
2024-02-02 01:27:19 +02:00
std::array<uint32_t, 1> messageSize{static_cast<uint32_t>(message.size())};
2024-02-02 01:27:19 +02:00
boost::asio::write(*socket, boost::asio::buffer(messageSize), ec );
if (message.size() > 0)
boost::asio::write(*socket, boost::asio::buffer(message), ec );
2024-02-04 19:56:04 +02:00
//Note: ignoring error code, intended
}
void NetworkConnection::close()
{
boost::system::error_code ec;
socket->close(ec);
2024-02-04 19:56:04 +02:00
//NOTE: ignoring error code, intended
}
VCMI_LIB_NAMESPACE_END