2022-06-11 23:01:27 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: IOAdapterAsyncStream.cpp
|
|
|
|
Date: 2022-6-6
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include <Aurora/IO/IOExperimental.hpp>
|
|
|
|
#include "IOAdapterAsyncStream.hpp"
|
|
|
|
#include "IOWaitableIOLoopSource.hpp"
|
|
|
|
|
|
|
|
namespace Aurora::IO
|
|
|
|
{
|
2022-06-29 13:56:59 +00:00
|
|
|
struct AsyncStreamAdapter;
|
2022-06-11 23:01:27 +00:00
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
struct AsyncStreamReader : IAsyncStreamReader
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-06-29 13:56:59 +00:00
|
|
|
AsyncStreamAdapter *parent;
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
EStreamError IsOpen() override;
|
|
|
|
EStreamError Read(const Memory::MemoryViewStreamWrite ¶meters) override;
|
|
|
|
void Close() override;
|
2022-06-21 04:49:36 +00:00
|
|
|
|
|
|
|
EStreamError BeginRead(const AuSPtr<Memory::MemoryViewWrite> &internalView) override;
|
|
|
|
EStreamError Dequeue(AuUInt reqLength, Memory::MemoryViewWrite &out) override;
|
2022-06-11 23:01:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AsyncStreamMemory : AuMemoryViewWrite
|
|
|
|
{
|
2022-06-21 04:49:36 +00:00
|
|
|
AsyncStreamMemory(const AuMemoryViewWrite &ref);
|
|
|
|
AsyncStreamMemory(const AuSPtr<AuMemoryViewWrite> &ref);
|
2022-06-11 23:01:27 +00:00
|
|
|
AsyncStreamMemory(AuUInt length);
|
|
|
|
~AsyncStreamMemory();
|
|
|
|
|
|
|
|
bool IsValid();
|
2022-07-04 22:41:29 +00:00
|
|
|
bool owned {};
|
2022-06-21 04:49:36 +00:00
|
|
|
AuSPtr<AuMemoryViewWrite> pin;
|
2022-06-11 23:01:27 +00:00
|
|
|
AuUInt streamIndex {};
|
2022-06-21 04:49:36 +00:00
|
|
|
// todo shared
|
2022-06-11 23:01:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AsyncStreamWriter : IStreamWriter
|
|
|
|
{
|
|
|
|
~AsyncStreamWriter();
|
2022-06-29 13:56:59 +00:00
|
|
|
AsyncStreamAdapter *parent;
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
EStreamError IsOpen() override;
|
|
|
|
EStreamError Write(const Memory::MemoryViewStreamRead ¶meters) override;
|
|
|
|
void Close() override;
|
|
|
|
void Flush() override;
|
|
|
|
|
|
|
|
void Preframe();
|
|
|
|
void Frame();
|
|
|
|
|
|
|
|
AuList<AuSPtr<AsyncStreamMemory>> writesPending;
|
|
|
|
bool HasWorkItems();
|
2022-06-21 04:49:36 +00:00
|
|
|
bool used {};
|
2022-06-11 23:01:27 +00:00
|
|
|
};
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
struct AsyncStreamAdapter : IAsyncStreamAdapter, AuEnableSharedFromThis<AsyncStreamAdapter>
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-06-29 13:56:59 +00:00
|
|
|
AsyncStreamAdapter();
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
AuSPtr<AsyncStreamMemory> AllocateNextPageCached(AuUInt length);
|
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
virtual AuSPtr<IAsyncStreamReader> ToStreamReader() override;
|
2022-06-11 23:01:27 +00:00
|
|
|
virtual AuSPtr<IStreamWriter> ToStreamWriter() override;
|
|
|
|
|
|
|
|
virtual AuSPtr<IIOWaitableItem> ToWaitable() override;
|
|
|
|
|
|
|
|
virtual bool Reset() override;
|
|
|
|
|
|
|
|
bool Init(const AuSPtr<IAsyncTransaction> &transaction, bool isStream);
|
|
|
|
|
|
|
|
AuSPtr<AsyncStreamMemory> lastAllocation;
|
|
|
|
AuSPtr<IAsyncTransaction> transaction;
|
|
|
|
|
|
|
|
bool SetFlushOnWrite(bool value) override;
|
|
|
|
|
|
|
|
void ReserveBuffer(AuUInt length) override;
|
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
AuUInt GetReadOffset() override;
|
|
|
|
AuUInt SetReadOffset(AuUInt offset) override;
|
2022-06-11 23:01:27 +00:00
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
AuUInt GetWriteOffset() override;
|
|
|
|
AuUInt SetWriteOffset(AuUInt offset) override;
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
bool asyncActive {};
|
|
|
|
AuUInt readOffset {};
|
|
|
|
AuUInt writeOffset {};
|
|
|
|
bool isStream {};
|
|
|
|
|
|
|
|
bool flushOnWrite {true};
|
|
|
|
|
|
|
|
AuOptionalEx<EStreamError> errorCode;
|
|
|
|
int locked {};
|
|
|
|
|
|
|
|
AsyncStreamReader reader;
|
|
|
|
AsyncStreamWriter writer;
|
|
|
|
IOWatachableIOLoopSource source;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
AsyncStreamMemory::AsyncStreamMemory(const AuMemoryViewWrite &ref) :
|
|
|
|
AuMemoryViewWrite(ref.ptr, ref.length)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncStreamMemory::AsyncStreamMemory(const AuSPtr<AuMemoryViewWrite> &ref) :
|
|
|
|
AuMemoryViewWrite(ref->ptr, ref->length),
|
|
|
|
pin(ref)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-21 02:53:50 +00:00
|
|
|
AsyncStreamMemory::AsyncStreamMemory(AuUInt length) : AuMemoryViewWrite(AuMemory::ZAlloc<AuUInt8*>(length, AuHwInfo::GetPageSize()), length)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-07-04 22:41:29 +00:00
|
|
|
this->owned = true;
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AsyncStreamMemory::~AsyncStreamMemory()
|
|
|
|
{
|
2022-07-04 22:41:29 +00:00
|
|
|
if (this->ptr && this->owned)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
AuMemory::Free(this->ptr);
|
|
|
|
this->ptr = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AsyncStreamMemory::IsValid()
|
|
|
|
{
|
|
|
|
return bool(this->ptr);
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
bool AsyncStreamAdapter::SetFlushOnWrite(bool value)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
return AuExchange(this->flushOnWrite, value);
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
AuUInt AsyncStreamAdapter::GetReadOffset()
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
return this->readOffset;
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
AuUInt AsyncStreamAdapter::SetReadOffset(AuUInt offset)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (this->locked == 1)
|
|
|
|
{
|
|
|
|
this->writer.Preframe();
|
|
|
|
}
|
|
|
|
return AuExchange(this->readOffset, offset);
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
AuUInt AsyncStreamAdapter::GetWriteOffset()
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (this->locked == 1)
|
|
|
|
{
|
|
|
|
this->writer.Preframe();
|
|
|
|
}
|
|
|
|
return this->writeOffset;
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
AuUInt AsyncStreamAdapter::SetWriteOffset(AuUInt offset)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
return AuExchange(this->writeOffset, offset);
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
bool AsyncStreamAdapter::Init(const AuSPtr<IAsyncTransaction> &transaction, bool isStream)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
this->transaction = transaction;
|
|
|
|
this->lastAllocation.reset();
|
|
|
|
this->asyncActive = false;
|
|
|
|
this->reader.parent = this;
|
|
|
|
this->writer.parent = this;
|
2022-07-05 19:35:40 +00:00
|
|
|
this->isStream = isStream;
|
2022-06-11 23:01:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
AsyncStreamAdapter::AsyncStreamAdapter() : source({})
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
AuSPtr<AsyncStreamMemory> AsyncStreamAdapter::AllocateNextPageCached(AuUInt length)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (this->lastAllocation)
|
|
|
|
{
|
|
|
|
if (this->lastAllocation->length >= length)
|
|
|
|
{
|
|
|
|
return this->lastAllocation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto newMem = AuMakeShared<AsyncStreamMemory>(length);
|
|
|
|
if (!newMem)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newMem->IsValid())
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->lastAllocation = newMem;
|
|
|
|
}
|
|
|
|
|
|
|
|
EStreamError AsyncStreamReader::IsOpen()
|
|
|
|
{
|
|
|
|
return this->parent->errorCode.HasValue() ?
|
|
|
|
this->parent->errorCode.value() :
|
|
|
|
EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
EStreamError AsyncStreamReader::BeginRead(const AuSPtr<Memory::MemoryViewWrite> &internalView)
|
|
|
|
{
|
|
|
|
if (parent->lastAllocation)
|
|
|
|
{
|
|
|
|
auto length = parent->transaction->GetLastPacketLength();
|
|
|
|
if (length &&
|
|
|
|
parent->lastAllocation->streamIndex != length)
|
|
|
|
{
|
|
|
|
AuDebugBreak();
|
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent->lastAllocation.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent->asyncActive && !parent->transaction->Complete())
|
|
|
|
{
|
|
|
|
AuDebugBreak();
|
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async success or blank state
|
|
|
|
parent->transaction->Reset();
|
|
|
|
parent->asyncActive = true;
|
|
|
|
|
|
|
|
parent->lastAllocation = AuMakeShared<AsyncStreamMemory>(internalView);
|
|
|
|
if (!parent->lastAllocation)
|
|
|
|
{
|
|
|
|
AuDebugBreak();
|
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent->lastAllocation->streamIndex = 0;
|
|
|
|
|
|
|
|
if (!parent->transaction->StartRead(parent->isStream ? 0 : parent->readOffset, parent->lastAllocation))
|
|
|
|
{
|
|
|
|
parent->asyncActive = false;
|
|
|
|
SysPushErrorNested("Couldn't start async aio read");
|
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
EStreamError AsyncStreamReader::Dequeue(AuUInt reqLength, Memory::MemoryViewWrite &out)
|
|
|
|
{
|
|
|
|
out = {};
|
|
|
|
|
2022-06-29 18:47:32 +00:00
|
|
|
if (!parent->transaction->Complete())
|
|
|
|
{
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
2022-06-22 20:24:04 +00:00
|
|
|
// Transaction error
|
|
|
|
if (parent->transaction->Failed())
|
|
|
|
{
|
|
|
|
parent->asyncActive = false;
|
|
|
|
parent->transaction->Reset();
|
2022-08-21 02:53:50 +00:00
|
|
|
SysPushErrorIO("IO: {}", parent->transaction->GetOSErrorCode());
|
2022-06-22 20:24:04 +00:00
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
// Async error
|
|
|
|
if (parent->errorCode.HasValue())
|
|
|
|
{
|
|
|
|
auto code = parent->isStream ?
|
|
|
|
parent->errorCode.Value() :
|
|
|
|
AuExchange(parent->errorCode, {}).Value();
|
|
|
|
|
|
|
|
if (code != EStreamError::eErrorNone)
|
|
|
|
{
|
2022-06-22 20:24:04 +00:00
|
|
|
parent->asyncActive = false;
|
2022-06-22 13:42:17 +00:00
|
|
|
parent->transaction->Reset();
|
2022-06-21 04:49:36 +00:00
|
|
|
return code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto length = parent->transaction->GetLastPacketLength();
|
|
|
|
if (!length)
|
|
|
|
{
|
2022-06-22 20:24:04 +00:00
|
|
|
parent->asyncActive = false;
|
|
|
|
parent->transaction->Reset();
|
2022-06-21 04:49:36 +00:00
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto streamIndex = parent->lastAllocation->streamIndex;
|
|
|
|
if (streamIndex == length)
|
|
|
|
{
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto toRead = AuMin<AuUInt>(parent->lastAllocation->length, length - streamIndex);
|
|
|
|
auto bRequestedLen = bool(reqLength);
|
|
|
|
|
|
|
|
out.ptr = this->parent->lastAllocation->ToPointer() + streamIndex;
|
|
|
|
out.length = reqLength ? AuMin(toRead, reqLength) : toRead;
|
|
|
|
|
|
|
|
if (bRequestedLen)
|
|
|
|
{
|
|
|
|
streamIndex += out.length;
|
|
|
|
parent->lastAllocation->streamIndex = streamIndex;
|
|
|
|
|
|
|
|
if (!parent->isStream)
|
|
|
|
{
|
|
|
|
parent->readOffset += out.length;
|
|
|
|
}
|
2022-06-22 13:42:17 +00:00
|
|
|
|
|
|
|
if (parent->lastAllocation->streamIndex == length)
|
|
|
|
{
|
2022-06-22 20:24:04 +00:00
|
|
|
parent->asyncActive = false;
|
2022-06-22 13:42:17 +00:00
|
|
|
parent->transaction->Reset();
|
|
|
|
}
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
2022-06-11 23:01:27 +00:00
|
|
|
EStreamError AsyncStreamReader::Read(const Memory::MemoryViewStreamWrite ¶meters)
|
|
|
|
{
|
|
|
|
if (!parameters.length)
|
|
|
|
{
|
|
|
|
SysPushErrorArg();
|
|
|
|
return EStreamError::eErrorEndOfStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read from the last tranaction, if not fully consumed
|
|
|
|
if (parent->lastAllocation)
|
|
|
|
{
|
|
|
|
auto length = parent->transaction->GetLastPacketLength();
|
|
|
|
if (length &&
|
|
|
|
parent->lastAllocation->streamIndex != length)
|
|
|
|
{
|
|
|
|
auto toRead = AuMin<AuUInt>(parameters.length, length - parent->lastAllocation->streamIndex);
|
|
|
|
if (toRead)
|
|
|
|
{
|
|
|
|
if (parameters.ptr)
|
|
|
|
{
|
|
|
|
AuMemcpy(parameters.ptr, parent->lastAllocation->Begin<AuUInt8>() + parent->lastAllocation->streamIndex, toRead);
|
|
|
|
|
|
|
|
if (parent->isStream)
|
|
|
|
{
|
|
|
|
parent->lastAllocation->streamIndex += toRead;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parent->lastAllocation->streamIndex += length;
|
2022-06-21 04:49:36 +00:00
|
|
|
parent->readOffset += length;
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent->isStream)
|
|
|
|
{
|
|
|
|
parent->lastAllocation->streamIndex += length;
|
|
|
|
}
|
|
|
|
|
|
|
|
parameters.outVariable = toRead;
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent->transaction && parent->transaction->Failed())
|
|
|
|
{
|
|
|
|
SysPushErrorIO("AIO transaction read failed: {}", parent->transaction->GetOSErrorCode());
|
|
|
|
parent->errorCode = EStreamError::eErrorStreamInterrupted;
|
|
|
|
parent->lastAllocation.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async error
|
|
|
|
if (parent->errorCode.HasValue())
|
|
|
|
{
|
|
|
|
auto code = parent->isStream ?
|
|
|
|
parent->errorCode.Value() :
|
|
|
|
AuExchange(parent->errorCode, {}).Value();
|
|
|
|
|
|
|
|
if (code != EStreamError::eErrorNone)
|
|
|
|
{
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async awaiting response
|
|
|
|
if (parent->asyncActive && !parent->transaction->Complete())
|
|
|
|
{
|
|
|
|
parameters.outVariable = 0;
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async success or blank state
|
|
|
|
parent->transaction->Reset();
|
|
|
|
parent->asyncActive = true;
|
|
|
|
|
|
|
|
parent->lastAllocation = parent->AllocateNextPageCached(parameters.length);
|
|
|
|
parent->lastAllocation->streamIndex = 0;
|
|
|
|
|
|
|
|
if (!parent->transaction->StartRead(parent->isStream ? 0 : parent->readOffset, parent->lastAllocation))
|
|
|
|
{
|
|
|
|
parent->asyncActive = false;
|
|
|
|
SysPushErrorNested("Couldn't start async aio read");
|
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncStreamReader::Close()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncStreamWriter::~AsyncStreamWriter()
|
|
|
|
{
|
2022-06-21 04:49:36 +00:00
|
|
|
if (used)
|
|
|
|
{
|
|
|
|
Flush();
|
|
|
|
}
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EStreamError AsyncStreamWriter::IsOpen()
|
|
|
|
{
|
|
|
|
return this->parent->errorCode.HasValue() ?
|
|
|
|
this->parent->errorCode.value() :
|
|
|
|
EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
EStreamError AsyncStreamWriter::Write(const Memory::MemoryViewStreamRead ¶meters)
|
|
|
|
{
|
2022-06-21 04:49:36 +00:00
|
|
|
used = true;
|
|
|
|
|
2022-06-11 23:01:27 +00:00
|
|
|
if (!parameters.ptr)
|
|
|
|
{
|
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
Preframe();
|
|
|
|
|
|
|
|
if (parent->errorCode.HasValue())
|
|
|
|
{
|
|
|
|
auto code = parent->isStream ?
|
|
|
|
parent->errorCode.Value() :
|
|
|
|
AuExchange(parent->errorCode, {}).Value();
|
|
|
|
|
|
|
|
if (code != EStreamError::eErrorNone)
|
|
|
|
{
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto newMem = AuMakeShared<AsyncStreamMemory>(parameters.length);
|
|
|
|
if (!newMem)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newMem->IsValid())
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuMemcpy(newMem->ptr, parameters.ptr, parameters.length);
|
|
|
|
parameters.outVariable = parameters.length;
|
|
|
|
|
|
|
|
if (!AuTryInsert(this->writesPending, newMem))
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->parent->flushOnWrite)
|
|
|
|
{
|
|
|
|
Frame();
|
|
|
|
}
|
|
|
|
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncStreamWriter::Flush()
|
|
|
|
{
|
|
|
|
Preframe();
|
|
|
|
Frame();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncStreamWriter::Close()
|
|
|
|
{
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncStreamWriter::Preframe()
|
|
|
|
{
|
|
|
|
if (parent->transaction->Complete())
|
|
|
|
{
|
|
|
|
if (parent->transaction->Failed())
|
|
|
|
{
|
|
|
|
SysPushErrorIO("AIO transaction write failed: {}", parent->transaction->GetOSErrorCode());
|
|
|
|
parent->errorCode = EStreamError::eErrorStreamInterrupted;
|
|
|
|
parent->lastAllocation.reset();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parent->transaction->GetLastPacketLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent->transaction->Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncStreamWriter::Frame()
|
|
|
|
{
|
|
|
|
AuSPtr<AsyncStreamMemory> buffer;
|
|
|
|
|
|
|
|
if (this->writesPending.size() == 1)
|
|
|
|
{
|
|
|
|
buffer = AuMove(this->writesPending[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AuUInt length {};
|
|
|
|
|
|
|
|
for (auto &a : this->writesPending)
|
|
|
|
{
|
|
|
|
length += a->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = this->parent->AllocateNextPageCached(length);
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt index {};
|
|
|
|
|
|
|
|
for (auto &a : this->writesPending)
|
|
|
|
{
|
|
|
|
if (a->length + index > buffer->length)
|
|
|
|
{
|
|
|
|
SysPanic("");
|
|
|
|
}
|
|
|
|
|
|
|
|
AuMemcpy(buffer->Begin<AuUInt8>() + index, a->ptr, a->length);
|
|
|
|
index += a->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
//....
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async success or blank state
|
|
|
|
parent->transaction->Reset();
|
|
|
|
parent->asyncActive = true;
|
|
|
|
|
|
|
|
struct WriteMem : AuMemoryViewRead
|
|
|
|
{
|
|
|
|
AuSPtr<AsyncStreamMemory> write;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto annoying = AuMakeShared<WriteMem>();
|
|
|
|
if (!annoying)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
annoying->write = buffer;
|
|
|
|
annoying->ptr = buffer->ptr;
|
|
|
|
annoying->length = buffer->length;
|
|
|
|
|
|
|
|
parent->lastAllocation = buffer;
|
|
|
|
parent->lastAllocation->streamIndex = 0;
|
|
|
|
|
|
|
|
if (!parent->transaction->StartWrite(parent->isStream ? 0 : parent->writeOffset, annoying))
|
|
|
|
{
|
|
|
|
parent->asyncActive = false;
|
|
|
|
SysPushErrorNested("Couldn't start async aio write");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->writesPending.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AsyncStreamWriter::HasWorkItems()
|
|
|
|
{
|
|
|
|
return this->writesPending.size();
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
AuSPtr<IAsyncStreamReader> AsyncStreamAdapter::ToStreamReader()
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (this->locked != 0 && this->locked != 2)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
this->locked = 2;
|
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
return AuSPtr<IAsyncStreamReader>(AuSharedFromThis(), &this->reader);
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
AuSPtr<IStreamWriter> AsyncStreamAdapter::ToStreamWriter()
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (this->locked != 0 && this->locked != 1)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
this->locked = 1;
|
|
|
|
|
|
|
|
return AuSPtr<IStreamWriter>(AuSharedFromThis(), &this->writer);
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
AuSPtr<IIOWaitableItem> AsyncStreamAdapter::ToWaitable()
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-08-28 19:02:06 +00:00
|
|
|
auto pLoopSource = this->transaction->NewLoopSource();
|
|
|
|
if (!pLoopSource)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
this->source.SetLoopSource(pLoopSource);
|
2022-06-11 23:01:27 +00:00
|
|
|
return AuSPtr<IIOWaitableItem>(AuSharedFromThis(), &this->source);
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
void AsyncStreamAdapter::ReserveBuffer(AuUInt length)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (!this->lastAllocation || !this->asyncActive)
|
|
|
|
{
|
|
|
|
this->lastAllocation = this->AllocateNextPageCached(length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
bool AsyncStreamAdapter::Reset()
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (this->locked == 1)
|
|
|
|
{
|
|
|
|
if (this->writer.HasWorkItems())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->writer.Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->asyncActive)
|
|
|
|
{
|
|
|
|
if (!this->transaction->Complete())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->locked = 0;
|
|
|
|
this->transaction->Reset();
|
|
|
|
this->writeOffset = 0;
|
|
|
|
this->readOffset = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
AUKN_SYM AuSPtr<IAsyncStreamAdapter> NewAsyncStreamAdapter(const AuSPtr<IAsyncTransaction> &transaction, bool isStream)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (!transaction)
|
|
|
|
{
|
|
|
|
SysPushErrorArg();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:59 +00:00
|
|
|
auto adapter = AuMakeShared<AsyncStreamAdapter>();
|
2022-06-11 23:01:27 +00:00
|
|
|
if (!adapter)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!adapter->Init(transaction, isStream))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return adapter;
|
|
|
|
}
|
|
|
|
}
|