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: AuIOPipeProcessor.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 "AuIOProcessor.hpp"
|
|
|
|
#include "AuIOPipeProcessor.hpp"
|
2022-11-07 13:34:28 +00:00
|
|
|
#include <Aurora/IO/Protocol/Protocol.hpp>
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
namespace Aurora::IO
|
|
|
|
{
|
2022-06-21 04:49:36 +00:00
|
|
|
IOPipeWork::IOPipeWork(const AuSPtr<IOPipeProcessor> &parent, const IOPipeRequestAIO &request) :
|
|
|
|
parent_(parent),
|
|
|
|
request(request),
|
|
|
|
startCallback(this),
|
|
|
|
endCallback(this),
|
|
|
|
output(request.output)
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
this->uFrameCap_ = request.uPageLengthOrZero ? request.uPageLengthOrZero : request.kFallbackPageSize;
|
|
|
|
this->uBufferSize_ = request.uBufferLengthOrZero ? request.uBufferLengthOrZero : request.kFallbackBufferSize;
|
|
|
|
this->uBytesWrittenLimit_ = request.uLengthOrZero;
|
2022-11-20 10:31:13 +00:00
|
|
|
this->uBytesWrittenTarget_ = request.uMinBytesToRead ? request.uMinBytesToRead : request.uLengthOrZero;
|
2022-08-29 15:46:46 +00:00
|
|
|
this->pAsyncTransaction_ = request.pAsyncTransaction;
|
|
|
|
this->pAsyncAdapter_ = NewAsyncStreamAdapter(request.pAsyncTransaction, request.bIsStream);
|
|
|
|
SysAssert(this->pAsyncAdapter_);
|
2022-11-07 13:34:28 +00:00
|
|
|
this->pAsyncAdapter_->SetReadOffset(request.uStartOffset);
|
|
|
|
this->pAsyncAdapter_->SetWriteOffset(request.uStartOffset);
|
2022-08-29 15:46:46 +00:00
|
|
|
this->pAsyncStreamReader_ = this->pAsyncAdapter_->ToStreamReader();
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IOPipeWork::IOPipeWork(const AuSPtr<IOPipeProcessor> &parent, const IOPipeRequestBasic &request) :
|
|
|
|
parent_(parent),
|
|
|
|
request(request),
|
|
|
|
startCallback(this),
|
|
|
|
endCallback(this),
|
|
|
|
output(request.output)
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
this->uBufferSize_ = request.uBufferLengthOrZero ? request.uBufferLengthOrZero : request.kFallbackBufferSize;
|
|
|
|
this->uFrameCap_ = request.uPageLengthOrZero ? request.uPageLengthOrZero : request.kFallbackPageSize;
|
|
|
|
this->uBytesWrittenLimit_ = request.uLengthOrZero;
|
2022-11-20 10:31:13 +00:00
|
|
|
this->uBytesWrittenTarget_ = request.uMinBytesToRead ? request.uMinBytesToRead : request.uLengthOrZero;
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IOPipeWork::Tick_FrameEpilogue()
|
|
|
|
{
|
2023-05-11 15:05:00 +00:00
|
|
|
if (this->bWritingAheadLowLatency)
|
|
|
|
{
|
|
|
|
this->TryPump();
|
|
|
|
}
|
|
|
|
|
2022-06-22 20:24:04 +00:00
|
|
|
if (AuExchange(this->bShouldReadNext, false))
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
|
|
|
this->ReadNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOPipeWork::Tick_Any()
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
if (this->pAsyncTransaction_)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
|
|
|
this->AsyncPump();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->StreamPump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOPipeWork::OnFailureCompletion()
|
|
|
|
{
|
|
|
|
this->TerminateOnThread(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOPipeWork::OnNominalCompletion()
|
|
|
|
{
|
|
|
|
this->TerminateOnThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
IOWorkStart::IOWorkStart(IOPipeWork *parent) : parent(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOWorkStart::OnRun()
|
|
|
|
{
|
|
|
|
parent->RunOnThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOWorkStart::OnCanceled()
|
|
|
|
{
|
|
|
|
parent->TerminateOnThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
IOWorkEnd::IOWorkEnd(IOPipeWork *parent) : parent(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOWorkEnd::OnRun()
|
|
|
|
{
|
|
|
|
parent->TerminateOnThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOWorkEnd::OnCanceled()
|
|
|
|
{
|
|
|
|
parent->TerminateOnThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool IOPipeWork::Start()
|
|
|
|
{
|
|
|
|
AuSPtr<IIOProcessorItem> ret;
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (this->pAsyncTransaction_)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
auto pWaitable = this->pAsyncAdapter_->ToWaitable();
|
2022-08-28 19:02:06 +00:00
|
|
|
if (pWaitable)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
2022-08-28 19:02:06 +00:00
|
|
|
ret = this->parent_->parent_->StartIOWatch(pWaitable, AuSharedFromThis());
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
ret = this->parent_->parent_->StartIOWatch(this->input_.pWatchItem, AuSharedFromThis());
|
2022-06-21 04:49:36 +00:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
this->pWatch = ret;
|
2022-06-21 04:49:36 +00:00
|
|
|
|
|
|
|
if (this->parent_->parent_->CheckThread())
|
|
|
|
{
|
|
|
|
RunOnThread();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->parent_->
|
|
|
|
parent_->SubmitIOWorkItem(AuSPtr<IIOProcessorWorkUnit>(this->SharedFromThis(),
|
|
|
|
&this->startCallback));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IOPipeWork::End()
|
|
|
|
{
|
|
|
|
if (this->parent_->parent_->CheckThread())
|
|
|
|
{
|
|
|
|
TerminateOnThread();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->parent_->
|
|
|
|
parent_->SubmitIOWorkItem(AuSPtr<IIOProcessorWorkUnit>(this->SharedFromThis(),
|
|
|
|
&this->endCallback));
|
|
|
|
}
|
|
|
|
|
2022-12-06 22:53:37 +00:00
|
|
|
AuInt64 IOPipeWork::GetLastTickMS()
|
|
|
|
{
|
|
|
|
return this->throughput_.GetLastFrameTimeWall();
|
|
|
|
}
|
|
|
|
|
|
|
|
AuInt64 IOPipeWork::GetStartTickMS()
|
|
|
|
{
|
|
|
|
return this->iStartTickMS_;
|
|
|
|
}
|
|
|
|
|
|
|
|
double IOPipeWork::GetPredictedThroughput()
|
|
|
|
{
|
|
|
|
return this->throughput_.GetEstimatedHertz();
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt64 IOPipeWork::GetBytesProcessed()
|
|
|
|
{
|
|
|
|
return this->uBytesWritten_;
|
|
|
|
}
|
|
|
|
|
2023-04-25 05:36:31 +00:00
|
|
|
AuUInt64 IOPipeWork::GetBytesProcessedInterframe()
|
|
|
|
{
|
|
|
|
return this->uBytesWritten_ + this->bytesProcessedInterframe_;
|
|
|
|
}
|
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
void IOPipeWork::PrepareStream()
|
|
|
|
{
|
2022-08-21 02:53:50 +00:00
|
|
|
if (!this->buffer_.IsEmpty())
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
2022-08-21 02:53:50 +00:00
|
|
|
return;
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
2022-08-21 02:53:50 +00:00
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (!this->buffer_.Allocate(this->uBufferSize_, AuHwInfo::GetPageSize(), true))
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
TerminateOnThread(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOPipeWork::PrepareAsync()
|
|
|
|
{
|
|
|
|
PrepareStream();
|
|
|
|
ReadNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOPipeWork::AsyncPump()
|
|
|
|
{
|
|
|
|
AuMemoryViewWrite internalBuffer;
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
auto err = this->pAsyncStreamReader_->Dequeue(0, internalBuffer);
|
2022-06-21 04:49:36 +00:00
|
|
|
if (err != EStreamError::eErrorNone)
|
|
|
|
{
|
|
|
|
SysPushErrorIO("Async Stream Error: {}", err);
|
|
|
|
TerminateOnThread(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (internalBuffer.length == 0)
|
|
|
|
{
|
|
|
|
// end of stream
|
|
|
|
TerminateOnThread(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
err = this->pAsyncStreamReader_->Dequeue(internalBuffer.length, internalBuffer);
|
2022-06-21 04:49:36 +00:00
|
|
|
if (err != EStreamError::eErrorNone)
|
|
|
|
{
|
|
|
|
SysPushErrorIO("Async Stream Error: {}", err);
|
|
|
|
TerminateOnThread(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->buffer_.writePtr += internalBuffer.length;
|
|
|
|
|
2023-05-11 15:05:00 +00:00
|
|
|
// end of low-latency read-ahead tick
|
|
|
|
if (this->bWritingAheadLowLatency)
|
|
|
|
{
|
|
|
|
// shift if running out of linear space
|
|
|
|
auto readHead = this->buffer_.readPtr - this->buffer_.base;
|
|
|
|
if (readHead > (this->buffer_.length / 4) * 3)
|
|
|
|
{
|
|
|
|
auto readPtr = this->buffer_.base + readHead;
|
|
|
|
auto len = this->buffer_.writePtr - readPtr;
|
|
|
|
AuMemmove(this->buffer_.base, readPtr, len);
|
|
|
|
this->buffer_.writePtr = this->buffer_.base + len;
|
|
|
|
this->buffer_.readPtr = this->buffer_.base;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->bWritingAheadLowLatency = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// attempt low-latency read-ahead
|
|
|
|
if (!this->bWritingAheadLowLatency &&
|
2023-05-11 17:31:47 +00:00
|
|
|
this->buffer_.CanWrite(this->uFrameCap_) /*ensure we can run ahead*/ &&
|
|
|
|
!this->IsAtRequestedEnd() /*do not preemptively terminate before the last callback is fired*/)
|
2023-05-11 15:05:00 +00:00
|
|
|
{
|
|
|
|
this->bWritingAheadLowLatency = true;
|
|
|
|
this->ReadNext();
|
|
|
|
|
|
|
|
// TryPump is delegated to the frame epilogue so that we can do a batched send of the next frames reads
|
|
|
|
// followed by a tick of frame[-1]
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->TryPump();
|
|
|
|
}
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IOPipeWork::StreamPump()
|
|
|
|
{
|
|
|
|
AuUInt canBuffer = this->buffer_.RemainingWrite();
|
|
|
|
canBuffer = AuMin(canBuffer, AuUInt((this->buffer_.length + this->buffer_.base) - this->buffer_.writePtr));
|
|
|
|
|
|
|
|
AuUInt read {};
|
|
|
|
try
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
if (this->input_.pReader->Read(AuMemoryViewStreamWrite(AuMemoryViewWrite(this->buffer_.writePtr, canBuffer), read)) !=
|
2022-06-21 04:49:36 +00:00
|
|
|
AuIO::EStreamError::eErrorNone)
|
|
|
|
{
|
|
|
|
TerminateOnThread();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
SysPushErrorCatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->buffer_.writePtr += read;
|
|
|
|
if (this->buffer_.writePtr == this->buffer_.base + this->buffer_.length)
|
|
|
|
{
|
|
|
|
this->buffer_.writePtr = this->buffer_.base;
|
|
|
|
}
|
|
|
|
|
|
|
|
TryPump();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOPipeWork::ReadNextAsync()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
AuUInt canBuffer = this->buffer_.RemainingWrite();
|
2022-07-04 22:41:29 +00:00
|
|
|
canBuffer = AuMin(canBuffer, AuUInt((this->buffer_.length + this->buffer_.base) - this->buffer_.writePtr));
|
2022-08-29 15:46:46 +00:00
|
|
|
canBuffer = AuMin(AuUInt(this->uFrameCap_), canBuffer);
|
2022-06-21 04:49:36 +00:00
|
|
|
|
|
|
|
this->nextWriteAsync_ = AuMemoryViewWrite(this->buffer_.writePtr, canBuffer);
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (this->pAsyncStreamReader_->BeginRead(AuSPtr<AuMemoryViewWrite>(this->SharedFromThis(), &this->nextWriteAsync_)) !=
|
2022-06-21 04:49:36 +00:00
|
|
|
AuIO::EStreamError::eErrorNone)
|
2022-07-20 14:10:07 +00:00
|
|
|
{
|
2023-05-11 15:05:00 +00:00
|
|
|
if (this->bWritingAheadLowLatency)
|
|
|
|
{
|
|
|
|
this->bWritingAheadIOUOneTerminate = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TerminateOnThread(true);
|
|
|
|
}
|
2022-07-20 14:10:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->nextWriteAsync_)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
|
|
|
TerminateOnThread();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
SysPushErrorCatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOPipeWork::ReadNext()
|
|
|
|
{
|
|
|
|
if (!this->bActive)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-20 14:10:07 +00:00
|
|
|
if (IsAtRequestedEnd())
|
|
|
|
{
|
|
|
|
TerminateOnThread(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (this->pAsyncTransaction_)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
|
|
|
ReadNextAsync();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
if (this->input_.pBackend)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
this->input_.pBackend->OnEndPump();
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt32 IOPipeWork::TryPump()
|
|
|
|
{
|
2023-04-25 05:36:31 +00:00
|
|
|
AuUInt &bytesProcessedTotal = this->bytesProcessedInterframe_;
|
2022-06-21 04:49:36 +00:00
|
|
|
AuUInt bytesProcessed {};
|
2023-04-25 05:36:31 +00:00
|
|
|
bool bIsCullingLastFrame {};
|
|
|
|
|
|
|
|
bytesProcessedTotal = 0;
|
2022-06-21 04:49:36 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2023-04-25 05:36:31 +00:00
|
|
|
AuUInt canRead2 = this->buffer_.RemainingBytes();
|
|
|
|
AuUInt canRead = canRead2;
|
2022-07-04 22:41:29 +00:00
|
|
|
if (!canRead)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
canRead = AuMin<AuUInt>(canRead, (this->buffer_.length + this->buffer_.base) - this->buffer_.readPtr);
|
2022-07-04 22:41:29 +00:00
|
|
|
if (!canRead)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-21 04:49:36 +00:00
|
|
|
|
2022-07-20 14:10:07 +00:00
|
|
|
auto oldReadHeadPtr = this->buffer_.readPtr;
|
|
|
|
auto readHead = oldReadHeadPtr - this->buffer_.base;
|
|
|
|
|
2023-04-25 05:36:31 +00:00
|
|
|
auto oldWriteHeadPtr = this->buffer_.writePtr;
|
|
|
|
auto writeHead = oldWriteHeadPtr - this->buffer_.base;
|
|
|
|
|
|
|
|
auto uInterframeProgress = this->GetBytesProcessedInterframe();
|
|
|
|
|
2023-08-18 22:30:38 +00:00
|
|
|
if ((bIsCullingLastFrame = ((this->uBytesWrittenLimit_ && canRead2 + uInterframeProgress > this->uBytesWrittenLimit_))))
|
2023-04-25 05:36:31 +00:00
|
|
|
{
|
|
|
|
auto uLastFrameBytes = this->uBytesWrittenLimit_ - uInterframeProgress;
|
|
|
|
auto uAbsDataToRead = AuMin<AuUInt>(canRead, uLastFrameBytes);
|
|
|
|
this->buffer_.writePtr = this->buffer_.readPtr + uAbsDataToRead;
|
|
|
|
}
|
|
|
|
|
2023-04-27 05:09:19 +00:00
|
|
|
if (this->pProtocolStack)
|
2022-11-07 13:34:28 +00:00
|
|
|
{
|
2023-04-27 05:09:19 +00:00
|
|
|
this->pProtocolStack->DoTick();
|
2022-11-07 13:34:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (this->output.type == EPipeCallbackType::eTryHandleBufferedPart)
|
|
|
|
{
|
2022-09-09 21:54:24 +00:00
|
|
|
if (this->output.handleBufferedStream.pOnData)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
2022-09-09 21:54:24 +00:00
|
|
|
if (!this->output.handleBufferedStream.pOnData->OnDataAvailable(this->buffer_))
|
|
|
|
{
|
|
|
|
bytesProcessed = 0;
|
|
|
|
this->buffer_.readPtr = this->buffer_.base + readHead;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytesProcessed = this->buffer_.readPtr - oldReadHeadPtr;
|
|
|
|
}
|
2022-07-20 14:10:07 +00:00
|
|
|
}
|
2022-11-07 13:34:28 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bytesProcessed = this->buffer_.readPtr - oldReadHeadPtr;
|
|
|
|
}
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
2022-09-09 21:54:24 +00:00
|
|
|
else if (this->output.type == EPipeCallbackType::eWriteToWriter)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
2023-06-04 19:49:13 +00:00
|
|
|
if (this->output.forwardStream.pIntercepter &&
|
|
|
|
this->output.forwardStream.pWriter)
|
2022-07-20 14:10:07 +00:00
|
|
|
{
|
2022-09-09 21:54:24 +00:00
|
|
|
if (!this->output.forwardStream.pIntercepter->OnDataAvailable(AuMemoryViewStreamRead(AuMemoryViewRead(this->buffer_.readPtr, canRead), bytesProcessed), this->output.forwardStream.pWriter))
|
|
|
|
{
|
|
|
|
bytesProcessed = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->buffer_.readPtr += bytesProcessed;
|
|
|
|
}
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
2022-11-07 13:34:28 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bytesProcessed = this->buffer_.readPtr - oldReadHeadPtr;
|
|
|
|
}
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
SysPushErrorCatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesProcessedTotal += bytesProcessed;
|
|
|
|
|
2023-05-11 15:05:00 +00:00
|
|
|
if (!this->bWritingAheadLowLatency)
|
2023-04-25 05:36:31 +00:00
|
|
|
{
|
2023-05-11 15:05:00 +00:00
|
|
|
if (oldWriteHeadPtr != this->buffer_.writePtr)
|
|
|
|
{
|
|
|
|
this->buffer_.writePtr = this->buffer_.base + writeHead;
|
|
|
|
}
|
2023-04-25 05:36:31 +00:00
|
|
|
|
2023-05-11 15:05:00 +00:00
|
|
|
if (bIsCullingLastFrame)
|
|
|
|
{
|
|
|
|
this->bShouldReadNext = false;
|
|
|
|
break;
|
|
|
|
}
|
2022-07-20 14:10:07 +00:00
|
|
|
|
2023-05-11 15:05:00 +00:00
|
|
|
if (this->buffer_.readPtr == this->buffer_.writePtr)
|
|
|
|
{
|
|
|
|
this->buffer_.readPtr = this->buffer_.base;
|
|
|
|
this->buffer_.writePtr = this->buffer_.base;
|
2022-07-20 14:10:07 +00:00
|
|
|
|
2023-05-11 15:05:00 +00:00
|
|
|
this->bShouldReadNext = true;
|
|
|
|
}
|
|
|
|
else if (!bytesProcessed)
|
|
|
|
{
|
|
|
|
this->bShouldReadNext = true;
|
|
|
|
}
|
2022-07-20 14:10:07 +00:00
|
|
|
|
2023-05-11 15:05:00 +00:00
|
|
|
// Prevent fucky end of allocation issues by moving the tail end of a partially buffered stream back to the start
|
|
|
|
|
|
|
|
// Should help with packing massive files, where faster disks can spin through smaller frames, leaving
|
|
|
|
// the CPU to catch up towards the end of the buffer, at which point the linearity breaks.
|
|
|
|
// We must instead force linearity, and with the assumption we can move peekable memory around, we must eventually
|
|
|
|
// move the tail end of the buffer back to the start, just so we can continue that stream view linearity.
|
|
|
|
|
|
|
|
// I really don't know how ReadNextAsync can be expected to wrap around a ring buffer.
|
|
|
|
// We'd need to know if this pass failed, and if the read head is near the end, it'd know to wrap back around to zero.
|
|
|
|
// An overengineered pain and liability.
|
|
|
|
|
|
|
|
// This should allow us to continue working in linear space without resorting to circular ring buffers
|
|
|
|
|
|
|
|
{
|
|
|
|
auto readHead = this->buffer_.readPtr - this->buffer_.base;
|
|
|
|
if (readHead > (this->buffer_.length / 4) * 3)
|
|
|
|
{
|
|
|
|
auto readPtr = this->buffer_.base + readHead;
|
|
|
|
auto len = this->buffer_.writePtr - readPtr;
|
|
|
|
AuMemmove(this->buffer_.base, readPtr, len);
|
|
|
|
this->buffer_.writePtr = this->buffer_.base + len;
|
|
|
|
this->buffer_.readPtr = this->buffer_.base;
|
|
|
|
}
|
|
|
|
}
|
2022-07-20 14:10:07 +00:00
|
|
|
}
|
2022-06-21 04:49:36 +00:00
|
|
|
|
2022-06-22 13:42:17 +00:00
|
|
|
if (this->output.type == EPipeCallbackType::eWriteToWriter)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
2022-06-22 13:42:17 +00:00
|
|
|
if (this->output.forwardStream.bFlushWriter)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
2022-09-09 21:54:24 +00:00
|
|
|
if (this->output.forwardStream.pWriter)
|
|
|
|
{
|
|
|
|
this->output.forwardStream.pWriter->Flush();
|
|
|
|
}
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (AuExchange(bytesProcessed, 0));
|
|
|
|
|
2023-05-11 15:05:00 +00:00
|
|
|
|
|
|
|
if (!this->bWritingAheadLowLatency)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
2023-05-11 15:05:00 +00:00
|
|
|
if (!bIsCullingLastFrame)
|
2023-04-25 05:36:31 +00:00
|
|
|
{
|
2023-05-11 15:05:00 +00:00
|
|
|
if (this->buffer_.readPtr == this->buffer_.base)
|
|
|
|
{
|
|
|
|
this->bShouldReadNext = true;
|
|
|
|
}
|
2023-04-25 05:36:31 +00:00
|
|
|
}
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
this->uBytesWritten_ += bytesProcessedTotal;
|
2022-08-09 00:55:18 +00:00
|
|
|
|
2022-12-06 22:53:37 +00:00
|
|
|
this->throughput_.OnUpdate(bytesProcessedTotal);
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (this->request.pListener)
|
2022-08-09 00:55:18 +00:00
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
this->request.pListener->OnPipePartialEvent(bytesProcessedTotal);
|
2022-08-09 00:55:18 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 05:36:31 +00:00
|
|
|
if (bIsCullingLastFrame)
|
|
|
|
{
|
2023-05-11 15:05:00 +00:00
|
|
|
this->TerminateOnThread(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->bWritingAheadIOUOneTerminate)
|
|
|
|
{
|
|
|
|
this->TerminateOnThread(true);
|
2023-04-25 05:36:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
return bytesProcessedTotal;
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
bool IOPipeWork::IsAtRequestedEnd()
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
return this->uBytesWrittenLimit_ && (this->uBytesWrittenLimit_ <= this->uBytesWritten_);
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuByteBuffer *IOPipeWork::GetBuffer()
|
|
|
|
{
|
|
|
|
return &this->buffer_;
|
|
|
|
}
|
|
|
|
|
2022-06-21 04:49:36 +00:00
|
|
|
void IOPipeWork::RunOnThread()
|
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
if (this->input_.pBackend)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
this->input_.pBackend->OnStart();
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (this->pAsyncTransaction_)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
|
|
|
PrepareAsync();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PrepareStream();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOPipeWork::TerminateOnThread(bool error)
|
|
|
|
{
|
|
|
|
if (!this->bActive)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->bActive = false;
|
|
|
|
|
2023-06-04 19:49:13 +00:00
|
|
|
if (this->output.type == EPipeCallbackType::eWriteToWriter)
|
|
|
|
{
|
|
|
|
if (this->output.forwardStream.bCloseWriter)
|
|
|
|
{
|
|
|
|
if (auto pWriter = this->output.forwardStream.pWriter)
|
|
|
|
{
|
|
|
|
pWriter->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (this->pWatch)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
this->pWatch->StopWatch();
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (this->request.pListener)
|
2022-06-21 04:49:36 +00:00
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
{
|
2022-08-06 19:56:21 +00:00
|
|
|
// We explicitly failed...
|
2022-08-29 15:46:46 +00:00
|
|
|
this->request.pListener->OnPipeFailureEvent();
|
2022-08-06 19:56:21 +00:00
|
|
|
}
|
2022-11-20 10:31:13 +00:00
|
|
|
else if (this->uBytesWrittenTarget_ && (this->uBytesWrittenTarget_ > this->uBytesWritten_))
|
2022-08-06 19:56:21 +00:00
|
|
|
{
|
|
|
|
// Finished without error early
|
2022-08-29 15:46:46 +00:00
|
|
|
this->request.pListener->OnPipeFailureEvent();
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-06 19:56:21 +00:00
|
|
|
// We finished...
|
2022-08-29 15:46:46 +00:00
|
|
|
this->request.pListener->OnPipeSuccessEvent();
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
this->request.pListener.reset();
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
this->output.handleBufferedStream.pOnData.reset();
|
|
|
|
this->output.forwardStream.pIntercepter.reset();
|
|
|
|
this->output.forwardStream.pWriter.reset();
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (this->input_.pBackend)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2022-08-29 15:46:46 +00:00
|
|
|
this->input_.pBackend->OnEnd();
|
|
|
|
this->input_.pBackend.reset();
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
if (auto transaction = this->pAsyncTransaction_)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
|
|
|
transaction->Reset();
|
2022-08-29 15:46:46 +00:00
|
|
|
this->pAsyncTransaction_.reset();
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 15:46:46 +00:00
|
|
|
this->pAsyncAdapter_.reset();
|
|
|
|
this->pAsyncStreamReader_.reset();
|
2023-04-27 05:09:19 +00:00
|
|
|
|
|
|
|
if (this->pProtocolStack)
|
|
|
|
{
|
|
|
|
this->pProtocolStack->Destroy();
|
|
|
|
this->pProtocolStack.reset();
|
|
|
|
}
|
2023-06-04 16:28:29 +00:00
|
|
|
|
|
|
|
this->PrivateUserDataClear();
|
2022-06-21 04:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IOPipeProcessor::IOPipeProcessor(IOProcessor *parent) :
|
|
|
|
parent_(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IIOPipeWork> IOPipeProcessor::NewBasicPipe(const IOPipeRequestBasic &request)
|
|
|
|
{
|
|
|
|
// TODO: Incomplete
|
|
|
|
return AuMakeShared<IOPipeWork>(AuStaticCast<IOPipeProcessor>(this->parent_->ToPipeProcessor()), request);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IIOPipeWork> IOPipeProcessor::NewAIOPipe(const IOPipeRequestAIO &request)
|
|
|
|
{
|
|
|
|
// TODO: Incomplete
|
|
|
|
return AuMakeShared<IOPipeWork>(AuStaticCast<IOPipeProcessor>(this->parent_->ToPipeProcessor()), request);
|
|
|
|
}
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|