AuroraRuntime/Source/Compression/AuBaseStream.cpp
Reece Wilson 267c2216b0 [+] UDP over socket API via existing INetSrvDatagram layer
(...missing send)
[+] AuIO::Buffer::ViewReader
[+] AuIO::Buffer::ViewSeekableReadable
[+] AuIO::Buffer::ViewWriter
[*] Clean up AuCompression
[*[ AuLog messages must always crunch for memory
[*] Various bug fixes
[*] Refactor+clean up
2022-12-12 23:50:05 +00:00

136 lines
3.6 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuBaseStream.cpp
Date: 2022-2-14
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuCompression.hpp"
#include "AuIngestableReadBase.hpp"
#include "AuBaseStream.hpp"
namespace Aurora::Compression
{
AuStreamReadWrittenPair_t BaseStream::ReadEx(const AuMemoryViewWrite &/*optional/nullable*/destination,
bool bIngestUntilEOS)
{
AU_LOCK_GUARD(this->_spinlock);
AuUInt32 dwRead {}, dwBytesWritten {};
if (!destination.length && !destination.ptr)
{
return {0, this->pOutputBuffer_->RemainingBytes()};
}
if (bIngestUntilEOS)
{
while (this->pOutputBuffer_->RemainingBytes() < destination.length)
{
auto toRead = destination.length ? AuUInt32(destination.length - this->pOutputBuffer_->RemainingBytes()) : 10 * 1024;
if (Ingest_s(toRead).second == 0)
{
if (!this->pOutputBuffer_->RemainingBytes(true))
{
return {};
}
break;
}
dwRead += toRead;
}
}
dwBytesWritten = this->pOutputBuffer_->Read(destination.ptr, destination.length, destination.ptr == nullptr);
return {dwRead, dwBytesWritten};
}
AuUInt32 BaseStream::GetAvailableProcessedBytes()
{
AU_LOCK_GUARD(this->_spinlock);
return this->pOutputBuffer_->RemainingBytes(true);
}
AuUInt32 BaseStream::Read(const AuMemoryViewWrite & /*opt*/ destination)
{
AU_LOCK_GUARD(this->_spinlock);
if (!destination.length && !destination.ptr)
{
return this->pOutputBuffer_->RemainingBytes();
}
return this->pOutputBuffer_->Read(destination.ptr, destination.length, destination.ptr == nullptr);
}
bool BaseStream::GoBackByProcessedN(AuUInt32 dwOffset)
{
AU_LOCK_GUARD(this->_spinlock);
return this->pOutputBuffer_->ReaderTryGoBack(dwOffset);
}
bool BaseStream::GoForwardByProcessedN(AuUInt32 dwOffset)
{
AU_LOCK_GUARD(this->_spinlock);
if (!dwOffset)
{
return true;
}
return this->pOutputBuffer_->ReaderTryGoForward(dwOffset);
}
AuStreamReadWrittenPair_t BaseStream::Ingest(AuUInt32 dwBytesFromUnprocessedInputSource)
{
AU_LOCK_GUARD(this->_spinlock);
if (!dwBytesFromUnprocessedInputSource)
{
return {};
}
return Ingest_s(dwBytesFromUnprocessedInputSource);
}
bool BaseStream::IsValid()
{
return this->uBufferSize_ ? (this->pOutputBuffer_ && this->pOutputBuffer_->IsValid()) : true;
}
AuSPtr<Memory::ByteBuffer> BaseStream::GetBuffer()
{
return this->pOutputBuffer_;
}
void BaseStream::SetBuffer(const AuSPtr<Memory::ByteBuffer> &pBuffer)
{
if (!pBuffer)
{
this->pOutputBuffer_ = AuUnsafeRaiiToShared(&this->_outbufferOwned);
}
else
{
this->pOutputBuffer_ = pBuffer;
}
}
bool BaseStream::Write(const void *pDest, AuUInt32 dwLength)
{
return this->pOutputBuffer_->Write(AuReinterpretCast<const AuUInt8 *>(pDest), dwLength) == dwLength;
}
AuUInt32 BaseStream::GetInternalBufferSize()
{
return this->pOutputBuffer_->allocSize;
}
bool BaseStream::Flush()
{
return false;
}
bool BaseStream::Finish()
{
return false;
}
}