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:
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();

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)
@ -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;
}

View File

@ -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<ByteArray>();
if (m_buffer->GetSize() < minSize)
m_buffer->Resize(minSize);
m_buffer->Resize(static_cast<std::size_t>(cursorPos));
m_memoryStream.SetBuffer(m_buffer.get(), openMode);
m_memoryStream.SetCursorPos(cursorPos);