From c1c552d39477f8453b2e1938c0085574aed61f22 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Fri, 4 Oct 2024 13:35:31 +0000 Subject: [PATCH] Replaced some of pointers to CPack's with references --- client/CServerHandler.cpp | 4 ++-- client/Client.cpp | 4 +--- lib/serializer/Connection.cpp | 12 ++++++------ lib/serializer/Connection.h | 4 ++-- server/CGameHandler.cpp | 4 ++-- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index 373bbec3e..6214e6380 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -853,7 +853,7 @@ void CServerHandler::onPacketReceived(const std::shared_ptr if(getState() == EClientState::DISCONNECTING) return; - CPack * pack = logicConnection->retrievePack(message); + auto pack = logicConnection->retrievePack(message); ServerHandlerCPackVisitor visitor(*this); pack->visit(visitor); } @@ -945,7 +945,7 @@ void CServerHandler::visitForClient(CPackForClient & clientPack) void CServerHandler::sendLobbyPack(const CPackForLobby & pack) const { if(getState() != EClientState::STARTING) - logicConnection->sendPack(&pack); + logicConnection->sendPack(pack); } bool CServerHandler::inLobbyRoom() const diff --git a/client/Client.cpp b/client/Client.cpp index 2d57375ca..c6b024198 100644 --- a/client/Client.cpp +++ b/client/Client.cpp @@ -362,8 +362,6 @@ void CClient::handlePack(CPackForClient * pack) logNetwork->trace("\tApplied on gs: %s", typeid(*pack).name()); pack->visit(afterVisitor); logNetwork->trace("\tMade second apply on cl: %s", typeid(*pack).name()); - - delete pack; } int CClient::sendRequest(const CPackForServer * request, PlayerColor player) @@ -376,7 +374,7 @@ int CClient::sendRequest(const CPackForServer * request, PlayerColor player) waitingRequest.pushBack(requestID); request->requestID = requestID; request->player = player; - CSH->logicConnection->sendPack(request); + CSH->logicConnection->sendPack(*request); if(vstd::contains(playerint, player)) playerint[player]->requestSent(request, requestID); diff --git a/lib/serializer/Connection.cpp b/lib/serializer/Connection.cpp index 3560dff95..823fe721c 100644 --- a/lib/serializer/Connection.cpp +++ b/lib/serializer/Connection.cpp @@ -68,7 +68,7 @@ CConnection::CConnection(std::weak_ptr networkConnection) CConnection::~CConnection() = default; -void CConnection::sendPack(const CPack * pack) +void CConnection::sendPack(const CPack & pack) { boost::mutex::scoped_lock lock(writeMutex); @@ -78,18 +78,18 @@ void CConnection::sendPack(const CPack * pack) throw std::runtime_error("Attempt to send packet on a closed connection!"); packWriter->buffer.clear(); - *serializer & pack; + (*serializer) & (&pack); - logNetwork->trace("Sending a pack of type %s", typeid(*pack).name()); + logNetwork->trace("Sending a pack of type %s", typeid(pack).name()); connectionPtr->sendPacket(packWriter->buffer); packWriter->buffer.clear(); serializer->savedPointers.clear(); } -CPack * CConnection::retrievePack(const std::vector & data) +std::unique_ptr CConnection::retrievePack(const std::vector & data) { - CPack * result; + std::unique_ptr result; packReader->buffer = &data; packReader->position = 0; @@ -102,7 +102,7 @@ CPack * CConnection::retrievePack(const std::vector & data) 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()); + logNetwork->trace("Received CPack of type %s", typeid(result.get()).name()); deserializer->loadedPointers.clear(); deserializer->loadedSharedPointers.clear(); return result; diff --git a/lib/serializer/Connection.h b/lib/serializer/Connection.h index 838bcba53..b62849894 100644 --- a/lib/serializer/Connection.h +++ b/lib/serializer/Connection.h @@ -51,8 +51,8 @@ public: explicit CConnection(std::weak_ptr networkConnection); ~CConnection(); - void sendPack(const CPack * pack); - CPack * retrievePack(const std::vector & data); + void sendPack(const CPack & pack); + std::unique_ptr retrievePack(const std::vector & data); void enterLobbyConnectionMode(); void setCallback(IGameCallback * cb); diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 25b7c1c81..c859d81b3 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -467,7 +467,7 @@ void CGameHandler::handleReceivedPack(CPackForServer * pack) applied.result = successfullyApplied; applied.packType = CTypeList::getInstance().getTypeID(pack); applied.requestID = pack->requestID; - pack->c->sendPack(&applied); + pack->c->sendPack(applied); }; if(isBlockedByQueries(pack, pack->player)) @@ -1438,7 +1438,7 @@ void CGameHandler::sendToAllClients(CPackForClient * pack) { logNetwork->trace("\tSending to all clients: %s", typeid(*pack).name()); for (auto c : lobby->activeConnections) - c->sendPack(pack); + c->sendPack(*pack); } void CGameHandler::sendAndApply(CPackForClient * pack)