[General] Add maxSize parameter to RW(std::string)

Minor type changes
0.6.3
Koncord 7 years ago
parent 62877f38b7
commit a48d5b48ef

@ -14,11 +14,6 @@ BasePacket::BasePacket(RakNet::RakPeerInterface *peer)
this->peer = peer; this->peer = peer;
} }
BasePacket::~BasePacket()
{
}
void BasePacket::Packet(RakNet::BitStream *bs, bool send) void BasePacket::Packet(RakNet::BitStream *bs, bool send)
{ {
this->bs = bs; this->bs = bs;
@ -42,9 +37,9 @@ void BasePacket::SetSendStream(RakNet::BitStream *bitStream)
void BasePacket::SetStreams(RakNet::BitStream *inStream, RakNet::BitStream *outStream) void BasePacket::SetStreams(RakNet::BitStream *inStream, RakNet::BitStream *outStream)
{ {
if (inStream != 0) if (inStream != nullptr)
bsRead = inStream; bsRead = inStream;
if (outStream != 0) if (outStream != nullptr)
bsSend = outStream; bsSend = outStream;
} }

@ -12,9 +12,9 @@ namespace mwmp
class BasePacket class BasePacket
{ {
public: public:
BasePacket(RakNet::RakPeerInterface *peer); explicit BasePacket(RakNet::RakPeerInterface *peer);
virtual ~BasePacket(); virtual ~BasePacket() = default;
virtual void Packet(RakNet::BitStream *bs, bool send); virtual void Packet(RakNet::BitStream *bs, bool send);
virtual uint32_t Send(bool toOtherPlayers = true); virtual uint32_t Send(bool toOtherPlayers = true);
@ -29,19 +29,19 @@ namespace mwmp
void SetStreams(RakNet::BitStream *inStream, RakNet::BitStream *outStream); void SetStreams(RakNet::BitStream *inStream, RakNet::BitStream *outStream);
virtual uint32_t RequestData(RakNet::RakNetGUID guid); 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<uint32_t>(1 + RakNet::RakNetGUID::size()); // packetID + RakNetGUID (uint64_t)
} }
unsigned char GetPacketID() uint8_t GetPacketID() const
{ {
return packetID; return packetID;
} }
protected: protected:
template<class templateType> template<class templateType>
void RW(templateType &data, unsigned int size, bool write) void RW(templateType &data, uint32_t size, bool write)
{ {
if (write) if (write)
bs->Write(data, size); bs->Write(data, size);
@ -76,16 +76,16 @@ namespace mwmp
bs->Read(data); 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 (write)
{ {
if (compress) 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 else
{ {
RakNet::RakString rstr; RakNet::RakString rstr;
rstr = str.c_str(); rstr.AppendBytes(str.c_str(), str.size() > maxSize ? maxSize : str.size());
bs->Write(rstr); bs->Write(rstr);
} }
} }
@ -96,15 +96,17 @@ namespace mwmp
rstr.DeserializeCompressed(bs); rstr.DeserializeCompressed(bs);
else else
bs->Read(rstr); bs->Read(rstr);
rstr.Truncate(rstr.GetLength() > maxSize ? maxSize : rstr.GetLength());
str = rstr.C_String(); str = rstr.C_String();
} }
} }
protected: protected:
unsigned char packetID; uint8_t packetID;
PacketReliability reliability; PacketReliability reliability;
PacketPriority priority; PacketPriority priority;
int orderChannel; int8_t orderChannel;
RakNet::BitStream *bsRead, *bsSend, *bs; RakNet::BitStream *bsRead, *bsSend, *bs;
RakNet::RakPeerInterface *peer; RakNet::RakPeerInterface *peer;
RakNet::RakNetGUID guid; RakNet::RakNetGUID guid;

Loading…
Cancel
Save