Network/Packet: Fixes buffer size

Former-commit-id: 51fd56f76af8abe8feb1ed88802ef7ec0bd4a175
This commit is contained in:
Lynix 2016-02-24 14:17:28 +01:00
parent d6279914b4
commit a851056c0a
3 changed files with 19 additions and 11 deletions

View File

@ -22,7 +22,7 @@ namespace Nz
public: public:
inline NetPacket(); 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); inline NetPacket(UInt16 netCode, const void* ptr, std::size_t size);
NetPacket(const NetPacket&) = delete; NetPacket(const NetPacket&) = delete;
NetPacket(NetPacket&&) = default; NetPacket(NetPacket&&) = default;
@ -30,13 +30,14 @@ namespace Nz
inline const UInt8* GetConstData() const; inline const UInt8* GetConstData() const;
inline UInt8* GetData() const; inline UInt8* GetData() const;
inline size_t GetDataSize() const;
inline UInt16 GetNetCode() const; inline UInt16 GetNetCode() const;
virtual void OnReceive(UInt16 netCode, const void* data, std::size_t size); virtual void OnReceive(UInt16 netCode, const void* data, std::size_t size);
virtual const void* OnSend(std::size_t* newSize) const; virtual const void* OnSend(std::size_t* newSize) const;
inline void Reset(); 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 Reset(UInt16 netCode, const void* ptr, std::size_t size);
inline void Resize(std::size_t newSize); inline void Resize(std::size_t newSize);
@ -55,7 +56,7 @@ namespace Nz
void OnEmptyStream() override; void OnEmptyStream() override;
void FreeStream(); 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 bool Initialize();
static void Uninitialize(); static void Uninitialize();

View File

@ -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) inline NetPacket::NetPacket(UInt16 netCode, const void* ptr, std::size_t size)
@ -44,6 +44,14 @@ namespace Nz
return m_buffer->GetBuffer(); 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 inline UInt16 NetPacket::GetNetCode() const
{ {
return m_netCode; return m_netCode;
@ -54,9 +62,9 @@ namespace Nz
FreeStream(); 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; m_netCode = netCode;
} }

View File

@ -66,9 +66,9 @@ namespace Nz
s_availableBuffers.emplace_back(std::make_pair(size, std::move(m_buffer))); 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); Nz::LockGuard lock(*s_availableBuffersMutex);
@ -85,8 +85,7 @@ namespace Nz
if (!m_buffer) if (!m_buffer)
m_buffer = std::make_unique<ByteArray>(); m_buffer = std::make_unique<ByteArray>();
if (m_buffer->GetSize() < minSize) m_buffer->Resize(static_cast<std::size_t>(cursorPos));
m_buffer->Resize(minSize);
m_memoryStream.SetBuffer(m_buffer.get(), openMode); m_memoryStream.SetBuffer(m_buffer.get(), openMode);
m_memoryStream.SetCursorPos(cursorPos); m_memoryStream.SetCursorPos(cursorPos);