2024-02-26 16:48:10 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AuIOAdapterAsyncDelegators.cpp
|
|
|
|
Date: 2024-2-24
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "AuIOAdapterAsyncDelegators.hpp"
|
|
|
|
#include <Source/IO/Async/AuIOThreadPool.hpp>
|
|
|
|
#include <Source/IO/Async/AuIOAPCLessWaitable.hpp>
|
|
|
|
|
|
|
|
namespace Aurora::IO::Adapters
|
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
void AsyncReaderWriter::DispatchFrame(ProcessInfo &info)
|
|
|
|
{
|
|
|
|
try
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
if (this->iBaseOffset + AuInt64(this->uLastOffset) < 0)
|
|
|
|
{
|
|
|
|
this->eStreamError = EStreamError::eErrorOutOfBounds;
|
|
|
|
}
|
|
|
|
else if (this->pReadView)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
if (this->pStreamWriter)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-07-12 19:22:48 +00:00
|
|
|
this->eStreamError = this->pStreamWriter->Write(AuMemoryViewStreamRead(this->pReadView, this->uLastLength));
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
2024-03-07 06:49:23 +00:00
|
|
|
else if (this->pStreamWriterEx)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-07-12 19:22:48 +00:00
|
|
|
this->eStreamError = this->pStreamWriterEx->ArbitraryWrite(this->iBaseOffset + this->uLastOffset, AuMemoryViewStreamRead(this->pReadView, this->uLastLength));
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-07 06:49:23 +00:00
|
|
|
else if (this->pWriteView)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
if (this->pStreamReader)
|
|
|
|
{
|
2024-07-12 19:22:48 +00:00
|
|
|
this->eStreamError = this->pStreamReader->Read(AuMemoryViewStreamWrite(this->pWriteView, this->uLastLength));
|
2024-03-07 06:49:23 +00:00
|
|
|
}
|
|
|
|
else if (this->pStreamReaderEx)
|
|
|
|
{
|
2024-07-12 19:22:48 +00:00
|
|
|
this->eStreamError = this->pStreamReaderEx->ArbitraryRead(this->iBaseOffset + this->uLastOffset, AuMemoryViewStreamWrite(this->pWriteView, this->uLastLength));
|
2024-03-07 06:49:23 +00:00
|
|
|
}
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-07 06:49:23 +00:00
|
|
|
catch (...)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
this->eStreamError = EStreamError::eErrorGenericFault;
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
this->bInProgress = false;
|
|
|
|
this->SignalComplete();
|
|
|
|
}
|
2024-02-26 16:48:10 +00:00
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
void AsyncReaderWriter::OnFailure()
|
|
|
|
{
|
|
|
|
this->bInProgress = false;
|
|
|
|
this->Reset();
|
|
|
|
}
|
2024-02-26 16:48:10 +00:00
|
|
|
|
2024-07-12 19:22:48 +00:00
|
|
|
bool AsyncReaderWriter::StartRead(AuUInt64 uOffset, const Memory::MemoryViewWrite &memoryView)
|
2024-03-07 06:49:23 +00:00
|
|
|
{
|
|
|
|
if (!memoryView)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
SysPushErrorArg();
|
|
|
|
return false;
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
if (this->bInProgress)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
return false;
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
if (!this->pStreamReaderEx && !this->pStreamReader)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
return false;
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
AuResetMember(this->pReadView);
|
|
|
|
AuResetMember(this->pWriteView);
|
|
|
|
this->bInProgress = true;
|
|
|
|
this->uLastOffset = uOffset;
|
|
|
|
this->pWriteView = memoryView;
|
2024-02-26 16:48:10 +00:00
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
auto pThat = AuAsync::NewWorkItem(this->workers, AuStaticCast<AsyncReaderWriter>(this->SharedFromThis()));
|
|
|
|
if (!pThat)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
return false;
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
return pThat->Dispatch();
|
|
|
|
}
|
2024-02-26 16:48:10 +00:00
|
|
|
|
2024-07-12 19:22:48 +00:00
|
|
|
bool AsyncReaderWriter::StartWrite(AuUInt64 uOffset, const Memory::MemoryViewRead &memoryView)
|
2024-03-07 06:49:23 +00:00
|
|
|
{
|
|
|
|
if (!memoryView)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
SysPushErrorArg();
|
|
|
|
return false;
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
if (this->bInProgress)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
return false;
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
2024-03-07 06:49:23 +00:00
|
|
|
|
|
|
|
if (!this->pStreamWriterEx && !this->pStreamWriter)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
return false;
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
AuResetMember(this->pReadView);
|
|
|
|
AuResetMember(this->pWriteView);
|
|
|
|
this->bInProgress = true;
|
|
|
|
this->uLastOffset = uOffset;
|
|
|
|
this->pReadView = memoryView;
|
|
|
|
|
|
|
|
auto pThat = AuAsync::NewWorkItem(this->workers, AuStaticCast<AsyncReaderWriter>(this->SharedFromThis()));
|
|
|
|
if (!pThat)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
return false;
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
return pThat->Dispatch();
|
|
|
|
}
|
2024-02-26 16:48:10 +00:00
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
AuUInt32 AsyncReaderWriter::GetLastPacketLength()
|
|
|
|
{
|
|
|
|
return this->uLastLength;
|
|
|
|
}
|
2024-02-26 16:48:10 +00:00
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
void AsyncReaderWriter::OnOriginThreadComplete()
|
|
|
|
{
|
|
|
|
if (this->pSubscriber)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
this->pSubscriber->OnAsyncFileOpFinished(this->uLastOffset, this->GetLastPacketLength());
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
AuResetMember(this->pReadView);
|
|
|
|
AuResetMember(this->pWriteView);
|
|
|
|
}
|
2024-02-26 16:48:10 +00:00
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
bool AsyncReaderWriter::Complete()
|
|
|
|
{
|
|
|
|
Async::APCLessWaitable::CheckLocal();
|
|
|
|
return Async::APCLessWaitable::HasBeenSignaled();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AsyncReaderWriter::HasFailed()
|
|
|
|
{
|
|
|
|
return Async::APCLessWaitable::HasBeenSignaled() && this->eStreamError != EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AsyncReaderWriter::HasCompleted()
|
|
|
|
{
|
|
|
|
return Async::APCLessWaitable::HasBeenSignaled() && this->eStreamError == EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt AsyncReaderWriter::GetOSErrorCode()
|
|
|
|
{
|
|
|
|
return this->HasFailed() ? AuUInt(this->eStreamError) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncReaderWriter::SetCallback(const AuSPtr<IAsyncFinishedSubscriber> &pSubscriber)
|
|
|
|
{
|
|
|
|
this->pSubscriber = pSubscriber;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AsyncReaderWriter::Wait(AuUInt32 uTimeout)
|
|
|
|
{
|
|
|
|
return NewLoopSource()->WaitOn(uTimeout);
|
|
|
|
}
|
2024-02-26 16:48:10 +00:00
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
AuSPtr<IO::Loop::ILoopSource> AsyncReaderWriter::NewLoopSource()
|
|
|
|
{
|
|
|
|
return Async::APCLessWaitable::GetLoopSource();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncReaderWriter::Reset()
|
|
|
|
{
|
|
|
|
if (this->bInProgress)
|
2024-02-26 16:48:10 +00:00
|
|
|
{
|
2024-03-07 06:49:23 +00:00
|
|
|
return;
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 06:49:23 +00:00
|
|
|
AuResetMember(this->pReadView);
|
|
|
|
AuResetMember(this->pWriteView);
|
|
|
|
Async::APCLessWaitable::Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncReaderWriter::SetBaseOffset(AuUInt64 uBaseOffset)
|
|
|
|
{
|
|
|
|
this->iBaseOffset = uBaseOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AsyncReaderWriter::TryAttachToCompletionGroup(const AuSPtr<CompletionGroup::ICompletionGroup> &pCompletionGroup)
|
|
|
|
{
|
|
|
|
return Async::APCLessWaitable::TryAttachToCompletionGroup(pCompletionGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
CompletionGroup::ICompletionGroupWorkHandle *AsyncReaderWriter::ToCompletionGroupHandle()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<CompletionGroup::ICompletionGroup> AsyncReaderWriter::GetCompletionGroup()
|
|
|
|
{
|
|
|
|
return Async::APCLessWaitable::GetCompletionGroup();
|
|
|
|
}
|
|
|
|
|
2024-02-26 16:48:10 +00:00
|
|
|
|
|
|
|
static AuWorkerPId GetAuxWorkerPool()
|
|
|
|
{
|
|
|
|
return Async::GetAuxWorkerPoolAndRegister();
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IAsyncTransaction> NewAsyncTransactionFromStreamReader(const AuSPtr<IStreamReader> &pStreamReader, AuOptional<AuWorkerPId> workers)
|
|
|
|
{
|
|
|
|
SysCheckArgNotNull(pStreamReader, {});
|
|
|
|
auto pObject = AuMakeShared<AsyncReaderWriter>();
|
|
|
|
SysCheckNotNullMemory(pObject, {});
|
|
|
|
pObject->pStreamReader = pStreamReader;
|
|
|
|
pObject->workers = workers.ValueOr(GetAuxWorkerPool());
|
|
|
|
return pObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IAsyncTransaction> NewAsyncTransactionFromStreamSeekingReader(const AuSPtr<ISeekingReader> &pStreamReader, AuOptional<AuWorkerPId> workers)
|
|
|
|
{
|
|
|
|
SysCheckArgNotNull(pStreamReader, {});
|
|
|
|
auto pObject = AuMakeShared<AsyncReaderWriter>();
|
|
|
|
SysCheckNotNullMemory(pObject, {});
|
|
|
|
pObject->pStreamReaderEx = pStreamReader;
|
|
|
|
pObject->workers = workers.ValueOr(GetAuxWorkerPool());
|
|
|
|
return pObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IAsyncTransaction> NewAsyncTransactionFromStreamWriter(const AuSPtr<IStreamWriter> &pStreamWriter, AuOptional<AuWorkerPId> workers)
|
|
|
|
{
|
|
|
|
SysCheckArgNotNull(pStreamWriter, {});
|
|
|
|
auto pObject = AuMakeShared<AsyncReaderWriter>();
|
|
|
|
SysCheckNotNullMemory(pObject, {});
|
|
|
|
pObject->pStreamWriter = pStreamWriter;
|
|
|
|
pObject->workers = workers.ValueOr(GetAuxWorkerPool());
|
|
|
|
return pObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IAsyncTransaction> NewAsyncTransactionFromStreamSeekingWriter(const AuSPtr<ISeekingWriter> &pStreamWriter, AuOptional<Aurora::Async::WorkerPId_t> workers)
|
|
|
|
{
|
|
|
|
SysCheckArgNotNull(pStreamWriter, {});
|
|
|
|
auto pObject = AuMakeShared<AsyncReaderWriter>();
|
|
|
|
SysCheckNotNullMemory(pObject, {});
|
|
|
|
pObject->pStreamWriterEx = pStreamWriter;
|
|
|
|
pObject->workers = workers.ValueOr(GetAuxWorkerPool());
|
|
|
|
return pObject;
|
|
|
|
}
|
2024-03-09 00:20:26 +00:00
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IAsyncTransaction> NewAsyncTransactionFromStreamSeekingPair(const AuSPtr<ISeekingReader> &pStreamReader,
|
|
|
|
const AuSPtr<ISeekingWriter> &pStreamWriter,
|
|
|
|
AuOptional<Aurora::Async::WorkerPId_t> workers)
|
|
|
|
{
|
|
|
|
SysCheckArgNotNull(pStreamWriter, {});
|
|
|
|
SysCheckArgNotNull(pStreamReader, {});
|
|
|
|
auto pObject = AuMakeShared<AsyncReaderWriter>();
|
|
|
|
SysCheckNotNullMemory(pObject, {});
|
|
|
|
pObject->pStreamReaderEx = pStreamReader;
|
|
|
|
pObject->pStreamWriterEx = pStreamWriter;
|
|
|
|
pObject->workers = workers.ValueOr(GetAuxWorkerPool());
|
|
|
|
return pObject;
|
|
|
|
}
|
2024-02-26 16:48:10 +00:00
|
|
|
}
|