Fix crash on moving empty NetPacket

This commit is contained in:
Jérôme Leclercq 2018-09-25 14:53:49 +02:00
parent 602bdbe292
commit b6a33c76ca
3 changed files with 28 additions and 13 deletions

View File

@ -27,6 +27,8 @@ namespace Nz
ByteStream(ByteStream&& stream) noexcept = default; ByteStream(ByteStream&& stream) noexcept = default;
virtual ~ByteStream(); virtual ~ByteStream();
inline void ClearStream();
inline Endianness GetDataEndianness() const; inline Endianness GetDataEndianness() const;
inline Nz::UInt64 GetSize() const; inline Nz::UInt64 GetSize() const;
inline Stream* GetStream() const; inline Stream* GetStream() const;

View File

@ -30,6 +30,18 @@ namespace Nz
NazaraWarning("Failed to flush bits at serializer destruction"); NazaraWarning("Failed to flush bits at serializer destruction");
} }
/*!
* \brief Reset stream
*/
inline void ByteStream::ClearStream()
{
// We don't want to lose some bits..
FlushBits();
m_context.stream = nullptr;
m_ownedStream.reset();
}
/*! /*!
* \brief Gets the stream endianness * \brief Gets the stream endianness
* \return Type of the endianness * \return Type of the endianness
@ -113,22 +125,15 @@ namespace Nz
} }
/*! /*!
* \brief Sets this with a stream * \brief Changes stream
* *
* \param stream Stream existing * \param stream Stream existing
*
* \remark Produces a NazaraAssert if stream is invalid
*/ */
inline void ByteStream::SetStream(Stream* stream) inline void ByteStream::SetStream(Stream* stream)
{ {
NazaraAssert(stream, "Invalid stream"); ClearStream();
// We don't want to lose some bits..
FlushBits();
m_context.stream = stream; m_context.stream = stream;
m_ownedStream.reset();
} }
/*! /*!

View File

@ -56,8 +56,11 @@ namespace Nz
m_netCode(packet.m_netCode) m_netCode(packet.m_netCode)
{ {
///< Redirect memory stream to the moved buffer ///< Redirect memory stream to the moved buffer
m_memoryStream.SetBuffer(m_buffer.get(), m_memoryStream.GetOpenMode()); if (m_buffer)
SetStream(&m_memoryStream); {
m_memoryStream.SetBuffer(m_buffer.get(), m_memoryStream.GetOpenMode());
SetStream(&m_memoryStream);
}
} }
/*! /*!
@ -206,8 +209,13 @@ namespace Nz
m_netCode = packet.m_netCode; m_netCode = packet.m_netCode;
///< Redirect memory stream to the moved buffer ///< Redirect memory stream to the moved buffer
m_memoryStream.SetBuffer(m_buffer.get(), m_memoryStream.GetOpenMode()); if (m_buffer)
SetStream(&m_memoryStream); {
m_memoryStream.SetBuffer(m_buffer.get(), m_memoryStream.GetOpenMode());
SetStream(&m_memoryStream);
}
else
SetStream(static_cast<Stream*>(nullptr));
return *this; return *this;
} }