From a48d5b48ef9dcb85a0735404fda8ec0a9e664faf Mon Sep 17 00:00:00 2001 From: Koncord Date: Mon, 2 Jul 2018 23:19:39 +0800 Subject: [PATCH] [General] Add maxSize parameter to RW(std::string) Minor type changes --- components/openmw-mp/Packets/BasePacket.cpp | 9 ++------ components/openmw-mp/Packets/BasePacket.hpp | 24 +++++++++++---------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/components/openmw-mp/Packets/BasePacket.cpp b/components/openmw-mp/Packets/BasePacket.cpp index 65b7f5f3c..93de1762c 100644 --- a/components/openmw-mp/Packets/BasePacket.cpp +++ b/components/openmw-mp/Packets/BasePacket.cpp @@ -14,11 +14,6 @@ BasePacket::BasePacket(RakNet::RakPeerInterface *peer) this->peer = peer; } -BasePacket::~BasePacket() -{ - -} - void BasePacket::Packet(RakNet::BitStream *bs, bool send) { this->bs = bs; @@ -42,9 +37,9 @@ void BasePacket::SetSendStream(RakNet::BitStream *bitStream) void BasePacket::SetStreams(RakNet::BitStream *inStream, RakNet::BitStream *outStream) { - if (inStream != 0) + if (inStream != nullptr) bsRead = inStream; - if (outStream != 0) + if (outStream != nullptr) bsSend = outStream; } diff --git a/components/openmw-mp/Packets/BasePacket.hpp b/components/openmw-mp/Packets/BasePacket.hpp index 03b912366..928a7ea0b 100644 --- a/components/openmw-mp/Packets/BasePacket.hpp +++ b/components/openmw-mp/Packets/BasePacket.hpp @@ -12,9 +12,9 @@ namespace mwmp class BasePacket { public: - BasePacket(RakNet::RakPeerInterface *peer); + explicit BasePacket(RakNet::RakPeerInterface *peer); - virtual ~BasePacket(); + virtual ~BasePacket() = default; virtual void Packet(RakNet::BitStream *bs, bool send); virtual uint32_t Send(bool toOtherPlayers = true); @@ -29,19 +29,19 @@ namespace mwmp void SetStreams(RakNet::BitStream *inStream, RakNet::BitStream *outStream); virtual uint32_t RequestData(RakNet::RakNetGUID guid); - static size_t headerSize() + static inline uint32_t headerSize() { - return (size_t)(1 + RakNet::RakNetGUID::size()); // packetID + RakNetGUID (uint64_t) + return static_cast(1 + RakNet::RakNetGUID::size()); // packetID + RakNetGUID (uint64_t) } - unsigned char GetPacketID() + uint8_t GetPacketID() const { return packetID; } protected: template - void RW(templateType &data, unsigned int size, bool write) + void RW(templateType &data, uint32_t size, bool write) { if (write) bs->Write(data, size); @@ -76,16 +76,16 @@ namespace mwmp bs->Read(data); } - void RW(std::string &str, bool write, bool compress = 0) + void RW(std::string &str, bool write, std::string::size_type maxSize = std::string::npos, bool compress = false) { if (write) { if (compress) - RakNet::RakString::SerializeCompressed(str.c_str(), bs); + RakNet::RakString::SerializeCompressed(str.substr(0, maxSize).c_str(), bs); // todo: remove extra copy of string else { RakNet::RakString rstr; - rstr = str.c_str(); + rstr.AppendBytes(str.c_str(), str.size() > maxSize ? maxSize : str.size()); bs->Write(rstr); } } @@ -96,15 +96,17 @@ namespace mwmp rstr.DeserializeCompressed(bs); else bs->Read(rstr); + + rstr.Truncate(rstr.GetLength() > maxSize ? maxSize : rstr.GetLength()); str = rstr.C_String(); } } protected: - unsigned char packetID; + uint8_t packetID; PacketReliability reliability; PacketPriority priority; - int orderChannel; + int8_t orderChannel; RakNet::BitStream *bsRead, *bsSend, *bs; RakNet::RakPeerInterface *peer; RakNet::RakNetGUID guid;