1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/lib/network/NetworkConnection.cpp

104 lines
2.8 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
2024-01-12 01:10:41 +02:00
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::send_buffer_size(4194304));
socket->set_option(boost::asio::socket_base::receive_buffer_size(4194304));
}
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); });
}
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();
boost::asio::async_read(*socket,
readBuffer,
boost::asio::transfer_exactly(messageSize),
[self = shared_from_this(), messageSize](const auto & ec, const auto & endpoint) { self->onPacketReceived(ec, messageSize); });
}
2023-11-12 13:27:22 +02:00
uint32_t NetworkConnection::readPacketSize()
{
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)
listener.onDisconnected(shared_from_this(), "Invalid packet size!");
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;
}
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);
start();
}
2024-02-02 01:27:19 +02:00
void NetworkConnection::sendPacket(const std::vector<std::byte> & message)
{
2024-02-02 01:27:19 +02:00
boost::system::error_code ec;
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 );
boost::asio::write(*socket, boost::asio::buffer(message), ec );
if (ec)
listener.onDisconnected(shared_from_this(), ec.message());
}
void NetworkConnection::close()
{
boost::system::error_code ec;
socket->close(ec);
//NOTE: ignoring error code
}
VCMI_LIB_NAMESPACE_END