2023-11-11 16:43:58 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2024-01-12 01:10:41 +02:00
|
|
|
NetworkConnection::NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket)
|
2023-11-11 16:43:58 +02:00
|
|
|
: socket(socket)
|
2023-11-12 13:27:22 +02:00
|
|
|
, listener(listener)
|
2023-11-11 16:43:58 +02:00
|
|
|
{
|
2023-12-25 21:23:27 +02:00
|
|
|
socket->set_option(boost::asio::ip::tcp::no_delay(true));
|
|
|
|
socket->set_option(boost::asio::socket_base::send_buffer_size(4194304));
|
|
|
|
socket->set_option(boost::asio::socket_base::receive_buffer_size(4194304));
|
2023-11-11 16:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkConnection::start()
|
|
|
|
{
|
|
|
|
boost::asio::async_read(*socket,
|
|
|
|
readBuffer,
|
|
|
|
boost::asio::transfer_exactly(messageHeaderSize),
|
2024-02-02 15:32:06 +02:00
|
|
|
[self = shared_from_this()](const auto & ec, const auto & endpoint) { self->onHeaderReceived(ec); });
|
2023-11-11 16:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkConnection::onHeaderReceived(const boost::system::error_code & ec)
|
|
|
|
{
|
2023-11-12 13:27:22 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t messageSize = readPacketSize();
|
2023-11-11 16:43:58 +02:00
|
|
|
|
|
|
|
boost::asio::async_read(*socket,
|
|
|
|
readBuffer,
|
|
|
|
boost::asio::transfer_exactly(messageSize),
|
2024-02-02 15:32:06 +02:00
|
|
|
[self = shared_from_this(), messageSize](const auto & ec, const auto & endpoint) { self->onPacketReceived(ec, messageSize); });
|
2023-11-11 16:43:58 +02:00
|
|
|
}
|
|
|
|
|
2023-11-12 13:27:22 +02:00
|
|
|
uint32_t NetworkConnection::readPacketSize()
|
2023-11-11 16:43:58 +02:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
|
2023-11-11 16:43:58 +02:00
|
|
|
if (messageSize > messageMaxSize)
|
2024-02-02 15:32:06 +02:00
|
|
|
listener.onDisconnected(shared_from_this(), "Invalid packet size!");
|
2023-11-11 16:43:58 +02:00
|
|
|
|
|
|
|
return 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;
|
2023-11-11 16:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (readBuffer.size() < expectedPacketSize)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to read header!");
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2023-11-11 16:43:58 +02:00
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2024-02-02 01:27:19 +02:00
|
|
|
void NetworkConnection::sendPacket(const std::vector<std::byte> & message)
|
2023-11-11 16:43:58 +02:00
|
|
|
{
|
2024-02-02 01:27:19 +02:00
|
|
|
boost::system::error_code ec;
|
2023-11-11 16:43:58 +02:00
|
|
|
|
2024-02-02 01:27:19 +02:00
|
|
|
std::array<uint32_t, 1> messageSize{static_cast<uint32_t>(message.size())};
|
2023-11-11 16:43:58 +02:00
|
|
|
|
2024-02-02 01:27:19 +02:00
|
|
|
boost::asio::write(*socket, boost::asio::buffer(messageSize), ec );
|
|
|
|
boost::asio::write(*socket, boost::asio::buffer(message), ec );
|
2024-01-21 16:48:36 +02:00
|
|
|
|
2024-02-02 15:32:06 +02:00
|
|
|
if (ec)
|
|
|
|
listener.onDisconnected(shared_from_this(), ec.message());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkConnection::close()
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
socket->close(ec);
|
|
|
|
|
|
|
|
//NOTE: ignoring error code
|
2023-11-11 16:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|