Fix buffering issues

This commit is contained in:
SirLynix
2022-04-19 13:38:44 +02:00
parent d9ab6720d8
commit 138f2b88b3
14 changed files with 99 additions and 112 deletions

View File

@@ -23,15 +23,6 @@ namespace Nz
m_size = 0;
}
/*!
* \brief Checks whether the stream reached the end of the stream
* \return Always false
*/
bool EmptyStream::EndOfStream() const
{
return false;
}
/*!
* \brief Gets the size of the raw memory (how many bytes would have been written on a regular stream)
* \return Size occupied until now
@@ -83,6 +74,15 @@ namespace Nz
return 0;
}
/*!
* \brief Checks whether the stream reached the end of the stream
* \return Always false
*/
bool EmptyStream::TestStreamEnd() const
{
return false;
}
/*!
* \brief Writes data
* \return size

View File

@@ -107,40 +107,6 @@ namespace Nz
return std::filesystem::remove(m_filePath);
}
/*!
* \brief Checks whether the file has reached the end
* \return true if cursor is at the end of the file
*
* \remark Produces a NazaraError if file is not open with NAZARA_CORE_SAFE defined
*/
bool File::EndOfFile() const
{
#if NAZARA_CORE_SAFE
if (!IsOpen())
{
NazaraError("File not open");
return false;
}
#endif
return m_impl->EndOfFile();
}
/*!
* \brief Checks whether the file has reached the end of the stream
* \return true if cursor is at the end of the file
*
* \remark Produces a NazaraError if file is not open with NAZARA_CORE_SAFE defined
*
* \see EndOfFile
*/
bool File::EndOfStream() const
{
return EndOfFile();
}
/*!
* \brief Checks whether the file exists
* \return true if file exists
@@ -373,6 +339,21 @@ namespace Nz
return m_impl->GetCursorPos();
}
/*!
* \brief Checks whether the file has reached the end of the stream
* \return true if cursor is at the end of the file
*
* \remark Produces a NazaraError if file is not open with NAZARA_CORE_SAFE defined
*
* \see EndOfFile
*/
bool File::TestStreamEnd() const
{
NazaraAssert(IsOpen(), "File is not open");
return m_impl->EndOfFile();
}
/*!
* \brief Writes blocks
* \return Number of blocks written
@@ -383,7 +364,6 @@ namespace Nz
* \remark Produces a NazaraAssert if file is not open
* \remark Produces a NazaraAssert if buffer is nullptr
*/
std::size_t File::WriteBlock(const void* buffer, std::size_t size)
{
NazaraAssert(IsOpen(), "File is not open");

View File

@@ -26,16 +26,6 @@ namespace Nz
m_pos = 0;
}
/*!
* \brief Checks whether the stream reached the end of the stream
* \return true if cursor is at the end of the stream
*/
bool MemoryStream::EndOfStream() const
{
return m_pos >= m_buffer->size();
}
/*!
* \brief Gets the size of the raw memory
* \return Size of the memory
@@ -82,7 +72,7 @@ namespace Nz
std::size_t MemoryStream::ReadBlock(void* buffer, std::size_t size)
{
if (EndOfStream())
if (TestStreamEnd())
return 0;
std::size_t readSize = std::min<std::size_t>(size, static_cast<std::size_t>(m_buffer->GetSize() - m_pos));
@@ -116,6 +106,15 @@ namespace Nz
return m_pos;
}
/*!
* \brief Checks whether the stream reached the end of the stream
* \return true if cursor is at the end of the stream
*/
bool MemoryStream::TestStreamEnd() const
{
return m_pos >= m_buffer->size();
}
/*!
* \brief Writes blocks
* \return Number of blocks written

View File

@@ -49,16 +49,6 @@ namespace Nz
{
}
/*!
* \brief Checks whether the stream reached the end of the stream
* \return true if cursor is at the end of the stream
*/
bool MemoryView::EndOfStream() const
{
return m_pos >= m_size;
}
/*!
* \brief Gets the size of the raw memory
* \return Size of the memory
@@ -119,6 +109,15 @@ namespace Nz
return m_pos;
}
/*!
* \brief Checks whether the stream reached the end of the stream
* \return true if cursor is at the end of the stream
*/
bool MemoryView::TestStreamEnd() const
{
return m_pos >= m_size;
}
/*!
* \brief Writes blocks
* \return Number of blocks written

View File

@@ -23,6 +23,17 @@ namespace Nz
Stream::~Stream() = default;
bool Stream::EndOfStream() const
{
if (m_bufferCapacity > 0)
{
if (m_bufferOffset < m_bufferSize)
return false;
}
return TestStreamEnd();
}
/*!
* \brief Gets the directory of the stream
* \return Empty string (meant to be virtual)
@@ -91,6 +102,7 @@ namespace Nz
if (size > m_bufferCapacity)
{
// Unbuffered read
m_bufferOffset = 0;
m_bufferSize = 0;
std::size_t blockSize = ReadBlock(ptr, size);
m_bufferCursor += blockSize;
@@ -201,6 +213,30 @@ namespace Nz
return line;
}
bool Stream::SetCursorPos(UInt64 offset)
{
if (m_bufferCapacity == 0)
return SeekStreamCursor(offset);
else
{
assert(m_bufferCursor >= m_bufferSize);
if (offset < m_bufferCursor && offset >= m_bufferCursor - m_bufferSize)
m_bufferOffset = offset - (m_bufferCursor - m_bufferSize);
else
{
// Out of buffer
if (!SeekStreamCursor(offset))
return false;
m_bufferCursor = offset;
m_bufferOffset = 0;
m_bufferSize = 0;
}
return true;
}
}
/*!
* \brief Writes a ByteArray into the stream
* \return true if successful

View File

@@ -137,16 +137,6 @@ namespace Nz
}
}
/*!
* \brief Checks whether the stream reached the end of the stream
* \return true if there is no more available bytes
*/
bool TcpClient::EndOfStream() const
{
return QueryAvailableBytes() == 0;
}
/*!
* \brief Gets the size of the raw memory available
* \return Size of the memory available
@@ -603,6 +593,15 @@ namespace Nz
return 0;
}
/*!
* \brief Checks whether the stream reached the end of the stream
* \return true if there is no more available bytes
*/
bool TcpClient::TestStreamEnd() const
{
return QueryAvailableBytes() == 0;
}
/*!
* \brief Writes blocks
* \return Number of blocks written
@@ -613,7 +612,6 @@ namespace Nz
* \remark Produces a NazaraAssert if buffer is nullptr
* \remark Produces a NazaraAssert if socket is invalid
*/
std::size_t TcpClient::WriteBlock(const void* buffer, std::size_t size)
{
NazaraAssert(buffer, "Invalid buffer");