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
|
|
|
|
{
|
2023-06-04 18:07:26 +00:00
|
|
|
AuSPtr<ProtocolStack> ProtocolPiece::GetParent()
|
|
|
|
|
|
|
|
{
|
|
|
|
if (this->pParent)
|
|
|
|
{
|
|
|
|
return this->pParent->SharedFromThis();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
AuSPtr<IStreamWriter> ProtocolPiece::ToNextWriter()
|
|
|
|
{
|
2023-04-29 06:00:29 +00:00
|
|
|
if (!this->pParent)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
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-04-28 01:41:45 +00:00
|
|
|
AuSPtr<IStreamWriter> ProtocolPiece::ToInputWriter()
|
|
|
|
{
|
|
|
|
return this->pWriteInteface ? this->pWriteInteface->GetStreamWriter() : AuSPtr<IStreamWriter> {};
|
|
|
|
}
|
|
|
|
|
2023-04-29 06:00:29 +00:00
|
|
|
AuSPtr<IProtocolPiece> ProtocolPiece::GetNextPiece()
|
|
|
|
{
|
|
|
|
return this->pNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtocolPiece::ReallocateDrainBuffer(AuUInt32 uOutputLength)
|
|
|
|
{
|
|
|
|
if (!this->pParent)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->pNext)
|
|
|
|
{
|
|
|
|
if (this->pParent->pDrainBuffer)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->outputBuffer.Resize(uOutputLength);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-29 10:45:08 +00:00
|
|
|
auto &pBottomPiece = this->pParent->pBottomPiece;
|
|
|
|
|
|
|
|
// fix chain
|
|
|
|
AuSPtr<IProtocolPiece> pLast;
|
|
|
|
auto pCurrent = pBottomPiece;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (!pCurrent)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pCurrent.get() == this)
|
|
|
|
{
|
|
|
|
AuReinterpretCast<ProtocolPiece>(pLast)->pNext = pCurrent->pNext;
|
|
|
|
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;
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|