2022-08-28 19:02:06 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-12-12 23:50:05 +00:00
|
|
|
File: AuProtocolPiece.cpp
|
2022-08-28 19:02:06 +00:00
|
|
|
Date: 2022-8-24
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include "Protocol.hpp"
|
2022-12-12 23:50:05 +00:00
|
|
|
#include "AuProtocolPiece.hpp"
|
|
|
|
#include "AuProtocolStack.hpp"
|
2022-08-28 19:02:06 +00:00
|
|
|
#include "IProtocolNext.hpp"
|
|
|
|
|
2023-06-04 18:07:26 +00:00
|
|
|
// TODO: check correct thread or throw api
|
|
|
|
// restrict this to one thread
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
namespace Aurora::IO::Protocol
|
|
|
|
{
|
2024-01-12 14:14:17 +00:00
|
|
|
AuSPtr<IProtocolStack> ProtocolPiece::GetParent()
|
2023-06-04 18:07:26 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
if (this->pParent)
|
|
|
|
{
|
|
|
|
return this->pParent->SharedFromThis();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-10 13:56:37 +00:00
|
|
|
AuSPtr<IStreamWriter> ProtocolPiece::GetNextWriter()
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2023-04-29 06:00:29 +00:00
|
|
|
if (!this->pParent)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-09-21 20:18:59 +00:00
|
|
|
AU_LOCK_GUARD(this->pParent->mutex);
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
if (this->pParent->pTopPiece.get() == this)
|
|
|
|
{
|
|
|
|
if (this->pParent->pDrainBuffer)
|
|
|
|
{
|
|
|
|
return AuMakeShared<Buffered::BlobWriter>(this->pParent->pDrainBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->pNext ?
|
|
|
|
this->pNext->pWriteInteface->GetStreamWriter() :
|
|
|
|
this->pOuputWriter;
|
|
|
|
}
|
2022-09-29 10:45:08 +00:00
|
|
|
|
2023-12-10 13:56:37 +00:00
|
|
|
AuSPtr<IStreamWriter> ProtocolPiece::GetInputWriter()
|
2023-04-28 01:41:45 +00:00
|
|
|
{
|
|
|
|
return this->pWriteInteface ? this->pWriteInteface->GetStreamWriter() : AuSPtr<IStreamWriter> {};
|
|
|
|
}
|
|
|
|
|
2023-04-29 06:00:29 +00:00
|
|
|
AuSPtr<IProtocolPiece> ProtocolPiece::GetNextPiece()
|
|
|
|
{
|
|
|
|
return this->pNext;
|
|
|
|
}
|
|
|
|
|
2023-12-10 13:56:37 +00:00
|
|
|
AuSPtr<IProtocolPiece> ProtocolPiece::GetPreviousPiece()
|
|
|
|
{
|
|
|
|
return this->pBefore;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IStreamReader> ProtocolPiece::GetOutputReader()
|
|
|
|
{
|
|
|
|
// TODO: cache if required
|
|
|
|
return IO::Adapters::NewByteBufferReadAdapter(AuSPtr<AuByteBuffer>(this->SharedFromThis(), &this->outputBuffer));
|
|
|
|
}
|
|
|
|
|
2023-04-29 06:00:29 +00:00
|
|
|
bool ProtocolPiece::ReallocateDrainBuffer(AuUInt32 uOutputLength)
|
|
|
|
{
|
|
|
|
if (!this->pParent)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-21 20:18:59 +00:00
|
|
|
AU_LOCK_GUARD(this->pParent->mutex);
|
|
|
|
|
2023-04-29 06:00:29 +00:00
|
|
|
if (!this->pNext)
|
|
|
|
{
|
|
|
|
if (this->pParent->pDrainBuffer)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 20:36:11 +00:00
|
|
|
this->outputBuffer.flagNoRealloc = false;
|
2023-10-23 10:30:25 +00:00
|
|
|
auto bRet = this->outputBuffer.Resize(uOutputLength);
|
|
|
|
if (!this->uMax.has_value())
|
|
|
|
{
|
2023-10-29 20:36:11 +00:00
|
|
|
this->outputBuffer.flagNoRealloc = true;
|
2023-10-23 10:30:25 +00:00
|
|
|
}
|
|
|
|
return bRet;
|
2023-04-29 06:00:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-10 13:56:37 +00:00
|
|
|
bool ProtocolPiece::ReallocateSourceBuffer(AuUInt32 uInputLength)
|
|
|
|
{
|
|
|
|
if (!this->pParent)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AU_LOCK_GUARD(this->pParent->mutex);
|
|
|
|
|
|
|
|
if (auto pBefore = this->pBefore)
|
|
|
|
{
|
|
|
|
if (pBefore->outputBuffer)
|
|
|
|
{
|
|
|
|
pBefore->outputBuffer.flagNoRealloc = false;
|
|
|
|
auto bRet = pBefore->outputBuffer.Resize(uInputLength);
|
|
|
|
if (!pBefore->uMax.has_value())
|
|
|
|
{
|
|
|
|
pBefore->outputBuffer.flagNoRealloc = true;
|
|
|
|
}
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this->pParent->bOwnsSource &&
|
|
|
|
this->pParent->pSourceBufer)
|
|
|
|
{
|
|
|
|
return this->pParent->pSourceBufer->Resize(uInputLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-29 10:45:08 +00:00
|
|
|
void ProtocolPiece::Remove()
|
|
|
|
{
|
2023-06-04 18:07:26 +00:00
|
|
|
this->PrivateUserDataClear();
|
2023-04-29 06:00:29 +00:00
|
|
|
if (!this->pParent)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-21 20:18:59 +00:00
|
|
|
AU_LOCK_GUARD(this->pParent->mutex);
|
|
|
|
|
2022-09-29 10:45:08 +00:00
|
|
|
auto &pBottomPiece = this->pParent->pBottomPiece;
|
|
|
|
|
|
|
|
// fix chain
|
2023-12-10 13:56:37 +00:00
|
|
|
AuSPtr<ProtocolPiece> pLast;
|
2022-09-29 10:45:08 +00:00
|
|
|
auto pCurrent = pBottomPiece;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (!pCurrent)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pCurrent.get() == this)
|
|
|
|
{
|
2023-12-10 13:56:37 +00:00
|
|
|
if (auto pNext = pCurrent->pNext)
|
|
|
|
{
|
|
|
|
pNext->pBefore = pLast;
|
|
|
|
}
|
|
|
|
pLast->pNext = pCurrent->pNext;
|
2022-09-29 10:45:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pLast = pCurrent;
|
|
|
|
pCurrent = pCurrent->pNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fix head pointers
|
|
|
|
if (this->pParent->pTopPiece.get() == this)
|
|
|
|
{
|
|
|
|
this->pParent->pTopPiece = AuReinterpretCast<ProtocolPiece>(pLast);
|
2023-06-11 14:09:07 +00:00
|
|
|
|
|
|
|
this->pParent->bWrittenEnd = false;
|
2022-09-29 10:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this->pParent->pBottomPiece.get() == this)
|
|
|
|
{
|
|
|
|
this->pParent->pBottomPiece = this->pNext;
|
|
|
|
}
|
2023-04-29 06:00:29 +00:00
|
|
|
|
2023-06-04 18:07:26 +00:00
|
|
|
this->PrivateUserDataClear();
|
2023-06-11 14:09:07 +00:00
|
|
|
this->pParent->DiscardAllocCaches();
|
2023-06-04 18:07:26 +00:00
|
|
|
|
2023-04-29 06:00:29 +00:00
|
|
|
this->pParent = nullptr;
|
2022-09-29 10:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<Memory::ByteBuffer> ProtocolPiece::GetNextPieceBuffer()
|
|
|
|
{
|
|
|
|
return AuSPtr<AuByteBuffer>(AuSharedFromThis(), &this->outputBuffer);
|
|
|
|
}
|
2023-04-28 01:41:45 +00:00
|
|
|
|
|
|
|
AuSPtr<IProtocolInterceptorEx> ProtocolPiece::GetExtendedInterceptor()
|
|
|
|
{
|
|
|
|
return this->pInterceptorEx;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IProtocolInterceptor> ProtocolPiece::GetShortPipeInterceptor()
|
|
|
|
{
|
|
|
|
return this->pInterceptor;
|
|
|
|
}
|
2023-10-07 19:08:46 +00:00
|
|
|
|
|
|
|
AuOptional<AuUInt> ProtocolPiece::ExchangeMaximumBufferLength(AuOptional<AuUInt> optMax)
|
|
|
|
{
|
|
|
|
if (!optMax)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->uMax)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return AuExchange(this->uMax, optMax);
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|