[*] idiot-proofing memory ownership

This commit is contained in:
Reece Wilson 2023-06-11 15:39:36 +01:00
parent 1f505fcf02
commit 573031cd96
2 changed files with 102 additions and 13 deletions

View File

@ -51,8 +51,7 @@ namespace Aurora::IO::Buffered
this->buffer_->clear();
}
private:
protected:
AuSPtr<Memory::ByteBuffer> buffer_;
AuUInt32 offset_ {};
};
}

View File

@ -446,19 +446,62 @@ namespace Aurora::IO::Protocol
{
return this->pStreamWriterCache;
}
struct A : AuIO::Buffered::BlobWriter
struct A : AuIO::IStreamWriter
{
AuWPtr<ProtocolStack> wpStack;
A(AuSPtr<AuByteBuffer> pBuffer,
AuWPtr<ProtocolStack> wpStack) :
AuIO::Buffered::BlobWriter(pBuffer),
A(AuWPtr<ProtocolStack> wpStack) :
wpStack(wpStack)
{
}
inline virtual EStreamError IsOpen() override
{
if (auto pStack = AuTryLockMemoryType(this->wpStack))
{
auto pStackBuffer = pStack->pSourceBufer;//pStack->AsWritableByteBuffer();
if (!pStackBuffer)
{
return EStreamError::eErrorStreamNotOpen;
}
else
{
return EStreamError::eErrorNone;
}
}
else
{
return EStreamError::eErrorHandleClosed;
}
}
inline virtual EStreamError Write(const Memory::MemoryViewStreamRead &parameters) override
{
if (auto pStack = AuTryLockMemoryType(this->wpStack))
{
auto pStackBuffer = pStack->pSourceBufer; //pStack->AsWritableByteBuffer();
if (!pStackBuffer)
{
return EStreamError::eErrorStreamNotOpen;
}
else
{
parameters.outVariable = pStackBuffer->Write(parameters.ptr, parameters.length);
return EStreamError::eErrorNone;
}
}
else
{
return EStreamError::eErrorHandleClosed;
}
}
virtual void Close() override
{
if (auto pStack = AuTryLockMemoryType(wpStack))
@ -476,7 +519,7 @@ namespace Aurora::IO::Protocol
}
};
return this->pStreamWriterCache = AuMakeShared<A>(this->pSourceBufer, this->SharedFromThis());
return this->pStreamWriterCache = AuMakeShared<A>(this->SharedFromThis());
}
AuSPtr<Memory::ByteBuffer> ProtocolStack::AsWritableByteBuffer()
@ -502,18 +545,65 @@ namespace Aurora::IO::Protocol
return this->pStreamReaderCache;
}
struct A : AuIO::Buffered::BlobReader
struct A : AuIO::IStreamReader
{
AuWPtr<ProtocolStack> wpStack;
A(AuSPtr<AuByteBuffer> pBuffer,
AuWPtr<ProtocolStack> wpStack) :
AuIO::Buffered::BlobReader(pBuffer),
A(AuWPtr<ProtocolStack> wpStack) :
wpStack(wpStack)
{
}
inline virtual EStreamError IsOpen() override
{
if (auto pStack = AuTryLockMemoryType(this->wpStack))
{
auto pStackBuffer = pStack->AsReadableByteBuffer();
if (!pStackBuffer)
{
return EStreamError::eErrorStreamNotOpen;
}
else
{
return EStreamError::eErrorNone;
}
}
else
{
return EStreamError::eErrorHandleClosed;
}
}
// let's be really kind...
// let's permit the protocol-stack to be updated after our creation.
// ordinarily, one would assume this action would invalidate previous readers.
// we could support this by: reading the old buffer (preferred)
// or: reading the top buffer no matter what (pragmatic - we are a reader for this ProtocolStack stack)
// this routine implements the latter - always use the current state of the protocol stack to read.
inline virtual EStreamError Read(const Memory::MemoryViewStreamWrite &parameters) override
{
if (auto pStack = AuTryLockMemoryType(this->wpStack))
{
auto pStackBuffer = pStack->AsReadableByteBuffer();
if (!pStackBuffer)
{
return EStreamError::eErrorStreamNotOpen;
}
else
{
parameters.outVariable = pStackBuffer->Read(parameters.ptr, parameters.length);
return parameters.outVariable == 0 ? EStreamError::eErrorEndOfStream : EStreamError::eErrorNone;
}
}
else
{
return EStreamError::eErrorHandleClosed;
}
}
virtual void Close() override
{
if (auto pStack = AuTryLockMemoryType(wpStack))
@ -523,7 +613,7 @@ namespace Aurora::IO::Protocol
}
};
return this->pStreamReaderCache = AuMakeShared<A>(pBuffer, this->SharedFromThis());
return this->pStreamReaderCache = AuMakeShared<A>(this->SharedFromThis());
}
AuSPtr<Memory::ByteBuffer> ProtocolStack::AsReadableByteBuffer()