diff --git a/include/Nazara/Network/NetPacket.hpp b/include/Nazara/Network/NetPacket.hpp index 83815b0fe..bbd5a750b 100644 --- a/include/Nazara/Network/NetPacket.hpp +++ b/include/Nazara/Network/NetPacket.hpp @@ -22,7 +22,7 @@ namespace Nz public: inline NetPacket(); - inline NetPacket(UInt16 netCode, std::size_t minSize = 0); + inline NetPacket(UInt16 netCode, std::size_t minCapacity = 0); inline NetPacket(UInt16 netCode, const void* ptr, std::size_t size); NetPacket(const NetPacket&) = delete; NetPacket(NetPacket&&) = default; @@ -30,13 +30,14 @@ namespace Nz inline const UInt8* GetConstData() const; inline UInt8* GetData() const; + inline size_t GetDataSize() const; inline UInt16 GetNetCode() const; virtual void OnReceive(UInt16 netCode, const void* data, std::size_t size); virtual const void* OnSend(std::size_t* newSize) const; inline void Reset(); - inline void Reset(UInt16 netCode, std::size_t minSize = 0); + inline void Reset(UInt16 netCode, std::size_t minCapacity = 0); inline void Reset(UInt16 netCode, const void* ptr, std::size_t size); inline void Resize(std::size_t newSize); @@ -55,7 +56,7 @@ namespace Nz void OnEmptyStream() override; void FreeStream(); - void InitStream(std::size_t minSize, UInt64 cursorPos, UInt32 openMode); + void InitStream(std::size_t minCapacity, UInt64 cursorPos, UInt32 openMode); static bool Initialize(); static void Uninitialize(); diff --git a/include/Nazara/Network/NetPacket.inl b/include/Nazara/Network/NetPacket.inl index 1fdf6c6ee..d21f9a3db 100644 --- a/include/Nazara/Network/NetPacket.inl +++ b/include/Nazara/Network/NetPacket.inl @@ -14,9 +14,9 @@ namespace Nz { } - inline NetPacket::NetPacket(UInt16 netCode, std::size_t minSize) + inline NetPacket::NetPacket(UInt16 netCode, std::size_t minCapacity) { - Reset(netCode, minSize); + Reset(netCode, minCapacity); } inline NetPacket::NetPacket(UInt16 netCode, const void* ptr, std::size_t size) @@ -44,6 +44,14 @@ namespace Nz return m_buffer->GetBuffer(); } + inline size_t NetPacket::GetDataSize() const + { + if (m_buffer) + return m_buffer->GetSize() - HeaderSize; + else + return 0; + } + inline UInt16 NetPacket::GetNetCode() const { return m_netCode; @@ -54,9 +62,9 @@ namespace Nz FreeStream(); } - inline void NetPacket::Reset(UInt16 netCode, std::size_t minSize) + inline void NetPacket::Reset(UInt16 netCode, std::size_t minCapacity) { - InitStream(HeaderSize + minSize, HeaderSize, OpenMode_ReadWrite); + InitStream(HeaderSize + minCapacity, HeaderSize, OpenMode_ReadWrite); m_netCode = netCode; } diff --git a/src/Nazara/Network/NetPacket.cpp b/src/Nazara/Network/NetPacket.cpp index e0c5b3df8..7a1c0d7f5 100644 --- a/src/Nazara/Network/NetPacket.cpp +++ b/src/Nazara/Network/NetPacket.cpp @@ -66,9 +66,9 @@ namespace Nz s_availableBuffers.emplace_back(std::make_pair(size, std::move(m_buffer))); } - void NetPacket::InitStream(std::size_t minSize, UInt64 cursorPos, UInt32 openMode) + void NetPacket::InitStream(std::size_t minCapacity, UInt64 cursorPos, UInt32 openMode) { - NazaraAssert(minSize >= cursorPos, "Cannot init stream with a smaller size than wanted cursor pos"); + NazaraAssert(minCapacity >= cursorPos, "Cannot init stream with a smaller capacity than wanted cursor pos"); { Nz::LockGuard lock(*s_availableBuffersMutex); @@ -85,8 +85,7 @@ namespace Nz if (!m_buffer) m_buffer = std::make_unique(); - if (m_buffer->GetSize() < minSize) - m_buffer->Resize(minSize); + m_buffer->Resize(static_cast(cursorPos)); m_memoryStream.SetBuffer(m_buffer.get(), openMode); m_memoryStream.SetCursorPos(cursorPos);