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