2022-06-11 23:01:27 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
File: AuIOAdapterAsyncStream.cpp
|
2022-06-11 23:01:27 +00:00
|
|
|
Date: 2022-6-6
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include <Aurora/IO/IOExperimental.hpp>
|
2022-12-17 20:14:19 +00:00
|
|
|
#include "AuIOAdapterAsyncStream.hpp"
|
2023-09-23 15:40:13 +00:00
|
|
|
#include "../AuIOWaitableIOLoopSource.hpp"
|
2022-06-11 23:01:27 +00:00
|
|
|
|
2023-09-23 15:40:13 +00:00
|
|
|
namespace Aurora::IO::Adapters
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
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;
|
2022-12-17 20:14:19 +00:00
|
|
|
EStreamError Dequeue(AuUInt uReqLength, 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-12-17 20:14:19 +00:00
|
|
|
AsyncStreamMemory(AuUInt uLength);
|
2022-06-11 23:01:27 +00:00
|
|
|
~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
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
AuSPtr<AsyncStreamMemory> AllocateNextPageCached(AuUInt64 uLength);
|
2022-06-11 23:01:27 +00:00
|
|
|
|
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;
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
bool Init(const AuSPtr<IAsyncTransaction> &transaction, bool bIsStream);
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
AuSPtr<AsyncStreamMemory> lastAllocation;
|
|
|
|
AuSPtr<IAsyncTransaction> transaction;
|
|
|
|
|
|
|
|
bool SetFlushOnWrite(bool value) override;
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
void ReserveBuffer(AuUInt64 uLength) override;
|
2022-06-11 23:01:27 +00:00
|
|
|
|
2022-11-08 21:25:43 +00:00
|
|
|
AuUInt64 GetReadOffset() override;
|
|
|
|
AuUInt64 SetReadOffset(AuUInt64 offset) override;
|
2022-06-11 23:01:27 +00:00
|
|
|
|
2022-11-08 21:25:43 +00:00
|
|
|
AuUInt64 GetWriteOffset() override;
|
|
|
|
AuUInt64 SetWriteOffset(AuUInt64 offset) override;
|
2022-06-11 23:01:27 +00:00
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
bool bAsyncActive {};
|
|
|
|
bool bIsStream {};
|
|
|
|
bool bFlushOnWrite { true };
|
|
|
|
|
2022-11-08 21:25:43 +00:00
|
|
|
AuUInt64 readOffset {};
|
|
|
|
AuUInt64 writeOffset {};
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
AuOptionalEx<EStreamError> errorCode;
|
|
|
|
int locked {};
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
IOWatachableIOLoopSource source;
|
|
|
|
|
|
|
|
// impl
|
2022-06-11 23:01:27 +00:00
|
|
|
AsyncStreamReader reader;
|
|
|
|
AsyncStreamWriter writer;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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-12-17 20:14:19 +00:00
|
|
|
AsyncStreamMemory::AsyncStreamMemory(AuUInt uLength) : AuMemoryViewWrite(AuMemory::ZAlloc<AuUInt8*>(uLength, AuHwInfo::GetPageSize()), uLength)
|
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
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
return AuExchange(this->bFlushOnWrite, value);
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 21:25:43 +00:00
|
|
|
AuUInt64 AsyncStreamAdapter::GetReadOffset()
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
return this->readOffset;
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:25:43 +00:00
|
|
|
AuUInt64 AsyncStreamAdapter::SetReadOffset(AuUInt64 offset)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (this->locked == 1)
|
|
|
|
{
|
|
|
|
this->writer.Preframe();
|
|
|
|
}
|
|
|
|
return AuExchange(this->readOffset, offset);
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:25:43 +00:00
|
|
|
AuUInt64 AsyncStreamAdapter::GetWriteOffset()
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (this->locked == 1)
|
|
|
|
{
|
|
|
|
this->writer.Preframe();
|
|
|
|
}
|
|
|
|
return this->writeOffset;
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:25:43 +00:00
|
|
|
AuUInt64 AsyncStreamAdapter::SetWriteOffset(AuUInt64 offset)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
return AuExchange(this->writeOffset, offset);
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
bool AsyncStreamAdapter::Init(const AuSPtr<IAsyncTransaction> &transaction, bool bIsStream)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
this->transaction = transaction;
|
|
|
|
this->lastAllocation.reset();
|
2022-12-17 20:14:19 +00:00
|
|
|
this->bAsyncActive = false;
|
2022-06-11 23:01:27 +00:00
|
|
|
this->reader.parent = this;
|
|
|
|
this->writer.parent = this;
|
2022-12-17 20:14:19 +00:00
|
|
|
this->bIsStream = bIsStream;
|
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-12-17 20:14:19 +00:00
|
|
|
AuSPtr<AsyncStreamMemory> AsyncStreamAdapter::AllocateNextPageCached(AuUInt64 uLength)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (this->lastAllocation)
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
if (this->lastAllocation->length >= uLength)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
return this->lastAllocation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
auto newMem = AuMakeShared<AsyncStreamMemory>(uLength);
|
2023-10-24 16:43:00 +00:00
|
|
|
SysCheckNotNull(newMem, {});
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
auto uLength = parent->transaction->GetLastPacketLength();
|
|
|
|
if (uLength &&
|
|
|
|
parent->lastAllocation->streamIndex != uLength)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
|
|
|
AuDebugBreak();
|
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent->lastAllocation.reset();
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
if (parent->bAsyncActive && !parent->transaction->Complete())
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
|
|
|
AuDebugBreak();
|
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async success or blank state
|
|
|
|
parent->transaction->Reset();
|
2022-12-17 20:14:19 +00:00
|
|
|
parent->bAsyncActive = true;
|
2022-06-21 04:49:36 +00:00
|
|
|
|
|
|
|
parent->lastAllocation = AuMakeShared<AsyncStreamMemory>(internalView);
|
|
|
|
if (!parent->lastAllocation)
|
|
|
|
{
|
|
|
|
AuDebugBreak();
|
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent->lastAllocation->streamIndex = 0;
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
if (!parent->transaction->StartRead(parent->bIsStream ? 0 : parent->readOffset, parent->lastAllocation))
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
parent->bAsyncActive = false;
|
2022-06-21 04:49:36 +00:00
|
|
|
SysPushErrorNested("Couldn't start async aio read");
|
|
|
|
return EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
EStreamError AsyncStreamReader::Dequeue(AuUInt uReqLength, Memory::MemoryViewWrite &out)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
|
|
|
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())
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
parent->bAsyncActive = false;
|
2022-06-22 20:24:04 +00:00
|
|
|
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())
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
auto code = parent->bIsStream ?
|
2022-06-21 04:49:36 +00:00
|
|
|
parent->errorCode.Value() :
|
|
|
|
AuExchange(parent->errorCode, {}).Value();
|
|
|
|
|
|
|
|
if (code != EStreamError::eErrorNone)
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
parent->bAsyncActive = false;
|
2022-06-22 13:42:17 +00:00
|
|
|
parent->transaction->Reset();
|
2022-06-21 04:49:36 +00:00
|
|
|
return code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
auto uLength = parent->transaction->GetLastPacketLength();
|
|
|
|
if (!uLength)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
parent->bAsyncActive = false;
|
2022-06-22 20:24:04 +00:00
|
|
|
parent->transaction->Reset();
|
2022-06-21 04:49:36 +00:00
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto streamIndex = parent->lastAllocation->streamIndex;
|
2022-12-17 20:14:19 +00:00
|
|
|
if (streamIndex == uLength)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
auto toRead = AuMin<AuUInt>(parent->lastAllocation->length, uLength - streamIndex);
|
|
|
|
auto bRequestedLen = bool(uReqLength);
|
2022-06-21 04:49:36 +00:00
|
|
|
|
|
|
|
out.ptr = this->parent->lastAllocation->ToPointer() + streamIndex;
|
2022-12-17 20:14:19 +00:00
|
|
|
out.length = uReqLength ? AuMin(toRead, uReqLength) : toRead;
|
2022-06-21 04:49:36 +00:00
|
|
|
|
|
|
|
if (bRequestedLen)
|
|
|
|
{
|
|
|
|
streamIndex += out.length;
|
|
|
|
parent->lastAllocation->streamIndex = streamIndex;
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
if (!parent->bIsStream)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
|
|
|
parent->readOffset += out.length;
|
|
|
|
}
|
2022-06-22 13:42:17 +00:00
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
if (parent->lastAllocation->streamIndex == uLength)
|
2022-06-22 13:42:17 +00:00
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
parent->bAsyncActive = 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)
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
auto uLength = parent->transaction->GetLastPacketLength();
|
|
|
|
if (uLength &&
|
|
|
|
parent->lastAllocation->streamIndex != uLength)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
auto toRead = AuMin<AuUInt>(parameters.length, uLength - parent->lastAllocation->streamIndex);
|
2022-06-11 23:01:27 +00:00
|
|
|
if (toRead)
|
|
|
|
{
|
|
|
|
if (parameters.ptr)
|
|
|
|
{
|
|
|
|
AuMemcpy(parameters.ptr, parent->lastAllocation->Begin<AuUInt8>() + parent->lastAllocation->streamIndex, toRead);
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
if (parent->bIsStream)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
parent->lastAllocation->streamIndex += toRead;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
parent->lastAllocation->streamIndex += uLength;
|
|
|
|
parent->readOffset += uLength;
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
if (parent->bIsStream)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
parent->lastAllocation->streamIndex += uLength;
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
auto code = parent->bIsStream ?
|
2022-06-11 23:01:27 +00:00
|
|
|
parent->errorCode.Value() :
|
|
|
|
AuExchange(parent->errorCode, {}).Value();
|
|
|
|
|
|
|
|
if (code != EStreamError::eErrorNone)
|
|
|
|
{
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async awaiting response
|
2022-12-17 20:14:19 +00:00
|
|
|
if (parent->bAsyncActive && !parent->transaction->Complete())
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
parameters.outVariable = 0;
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async success or blank state
|
|
|
|
parent->transaction->Reset();
|
2022-12-17 20:14:19 +00:00
|
|
|
parent->bAsyncActive = true;
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
parent->lastAllocation = parent->AllocateNextPageCached(parameters.length);
|
|
|
|
parent->lastAllocation->streamIndex = 0;
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
if (!parent->transaction->StartRead(parent->bIsStream ? 0 : parent->readOffset, parent->lastAllocation))
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
parent->bAsyncActive = false;
|
2022-06-11 23:01:27 +00:00
|
|
|
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())
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
auto code = parent->bIsStream ?
|
2022-06-11 23:01:27 +00:00
|
|
|
parent->errorCode.Value() :
|
|
|
|
AuExchange(parent->errorCode, {}).Value();
|
|
|
|
|
|
|
|
if (code != EStreamError::eErrorNone)
|
|
|
|
{
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto newMem = AuMakeShared<AsyncStreamMemory>(parameters.length);
|
2023-10-24 16:43:00 +00:00
|
|
|
SysCheckNotNull(newMem, EStreamError::eErrorOutOfMemory);
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
if (this->parent->bFlushOnWrite)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
AuUInt uLength {};
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
for (auto &a : this->writesPending)
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
uLength += a->length;
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
buffer = this->parent->AllocateNextPageCached(uLength);
|
2022-06-11 23:01:27 +00:00
|
|
|
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();
|
2022-12-17 20:14:19 +00:00
|
|
|
parent->bAsyncActive = true;
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
if (!parent->transaction->StartWrite(parent->bIsStream ? 0 : parent->writeOffset, annoying))
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
parent->bAsyncActive = false;
|
2022-06-11 23:01:27 +00:00
|
|
|
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-12-17 20:14:19 +00:00
|
|
|
void AsyncStreamAdapter::ReserveBuffer(AuUInt64 uLength)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
if (!this->lastAllocation || !this->bAsyncActive)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
this->lastAllocation = this->AllocateNextPageCached(uLength);
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
if (this->bAsyncActive)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
if (!this->transaction->Complete())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->locked = 0;
|
|
|
|
this->transaction->Reset();
|
|
|
|
this->writeOffset = 0;
|
|
|
|
this->readOffset = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
AUKN_SYM AuSPtr<IAsyncStreamAdapter> NewAsyncStreamAdapter(const AuSPtr<IAsyncTransaction> &pTransaction, bool bIsStream)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
if (!pTransaction)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
SysPushErrorArg();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
auto pAdapter = AuMakeShared<AsyncStreamAdapter>();
|
|
|
|
if (!pAdapter)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
if (!pAdapter->Init(pTransaction, bIsStream))
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
return pAdapter;
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
}
|