1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00
vcmi/lib/serializer/Connection.cpp

184 lines
4.5 KiB
C++
Raw Normal View History

/*
* Connection.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 "Connection.h"
#include "BinaryDeserializer.h"
#include "BinarySerializer.h"
#include "../gameState/CGameState.h"
#include "../networkPacks/NetPacksBase.h"
2024-01-12 01:10:41 +02:00
#include "../network/NetworkInterface.h"
VCMI_LIB_NAMESPACE_BEGIN
class DLL_LINKAGE ConnectionPackWriter final : public IBinaryWriter
{
public:
2024-02-02 01:27:19 +02:00
std::vector<std::byte> buffer;
2024-02-02 02:36:57 +02:00
int write(const std::byte * data, unsigned size) final;
};
class DLL_LINKAGE ConnectionPackReader final : public IBinaryReader
{
public:
2024-02-02 01:27:19 +02:00
const std::vector<std::byte> * buffer;
size_t position;
2024-02-02 02:36:57 +02:00
int read(std::byte * data, unsigned size) final;
};
2024-02-02 02:36:57 +02:00
int ConnectionPackWriter::write(const std::byte * data, unsigned size)
{
2024-02-02 02:36:57 +02:00
buffer.insert(buffer.end(), data, data + size);
return size;
}
2024-02-02 02:36:57 +02:00
int ConnectionPackReader::read(std::byte * data, unsigned size)
{
if (position + size > buffer->size())
throw std::runtime_error("End of file reached when reading received network pack!");
2024-02-02 02:36:57 +02:00
std::copy_n(buffer->begin() + position, size, data);
position += size;
return size;
}
2024-01-12 01:10:41 +02:00
CConnection::CConnection(std::weak_ptr<INetworkConnection> networkConnection)
: networkConnection(networkConnection)
, packReader(std::make_unique<ConnectionPackReader>())
, packWriter(std::make_unique<ConnectionPackWriter>())
, deserializer(std::make_unique<BinaryDeserializer>(packReader.get()))
, serializer(std::make_unique<BinarySerializer>(packWriter.get()))
, connectionID(-1)
{
assert(networkConnection.lock() != nullptr);
enterLobbyConnectionMode();
deserializer->version = ESerializationVersion::CURRENT;
}
CConnection::~CConnection() = default;
void CConnection::sendPack(const CPack * pack)
{
boost::mutex::scoped_lock lock(writeMutex);
auto connectionPtr = networkConnection.lock();
if (!connectionPtr)
throw std::runtime_error("Attempt to send packet on a closed connection!");
packWriter->buffer.clear();
*serializer & pack;
logNetwork->trace("Sending a pack of type %s", typeid(*pack).name());
connectionPtr->sendPacket(packWriter->buffer);
packWriter->buffer.clear();
}
2024-02-02 01:27:19 +02:00
CPack * CConnection::retrievePack(const std::vector<std::byte> & data)
{
CPack * result;
2022-12-26 21:28:36 +02:00
packReader->buffer = &data;
packReader->position = 0;
2022-12-26 21:28:36 +02:00
*deserializer & result;
2022-12-26 21:28:36 +02:00
2024-02-25 20:05:52 +02:00
if (result == nullptr)
throw std::runtime_error("Failed to retrieve pack!");
if (packReader->position != data.size())
throw std::runtime_error("Failed to retrieve pack! Not all data has been read!");
logNetwork->trace("Received CPack of type %s", typeid(*result).name());
return result;
}
2024-01-12 01:10:41 +02:00
bool CConnection::isMyConnection(const std::shared_ptr<INetworkConnection> & otherConnection) const
{
return otherConnection != nullptr && networkConnection.lock() == otherConnection;
}
std::shared_ptr<INetworkConnection> CConnection::getConnection()
{
return networkConnection.lock();
}
void CConnection::disableStackSendingByID()
{
packReader->sendStackInstanceByIds = false;
packWriter->sendStackInstanceByIds = false;
}
void CConnection::enableStackSendingByID()
{
packReader->sendStackInstanceByIds = true;
packWriter->sendStackInstanceByIds = true;
}
void CConnection::enterLobbyConnectionMode()
{
deserializer->loadedPointers.clear();
serializer->savedPointers.clear();
disableSmartVectorMemberSerialization();
disableSmartPointerSerialization();
disableStackSendingByID();
}
void CConnection::setCallback(IGameCallback * cb)
{
deserializer->cb = cb;
}
void CConnection::enterGameplayConnectionMode(CGameState * gs)
{
enableStackSendingByID();
disableSmartPointerSerialization();
setCallback(gs->callback);
enableSmartVectorMemberSerializatoin(gs);
}
void CConnection::disableSmartPointerSerialization()
{
deserializer->smartPointerSerialization = false;
serializer->smartPointerSerialization = false;
}
void CConnection::enableSmartPointerSerialization()
{
deserializer->smartPointerSerialization = true;
serializer->smartPointerSerialization = true;
}
void CConnection::disableSmartVectorMemberSerialization()
{
packReader->smartVectorMembersSerialization = false;
packWriter->smartVectorMembersSerialization = false;
}
void CConnection::enableSmartVectorMemberSerializatoin(CGameState * gs)
2017-08-11 15:50:00 +02:00
{
packWriter->addStdVecItems(gs);
packReader->addStdVecItems(gs);
2017-08-11 15:50:00 +02:00
}
void CConnection::setSerializationVersion(ESerializationVersion version)
{
deserializer->version = version;
serializer->version = version;
}
VCMI_LIB_NAMESPACE_END