[+] IProtocolPiece::GetPreviousPiece()

[+] IProtocolPiece::ReallocateSourceBuffer()
[*] IProtocolPiece::GetInputWriter()
[*] IProtocolPiece::GetNextWriter()
This commit is contained in:
Reece Wilson 2023-12-10 13:56:37 +00:00
parent 9ba725973f
commit 76d3759295
5 changed files with 94 additions and 14 deletions

View File

@ -25,6 +25,12 @@ namespace Aurora::IO::Protocol
*/ */
virtual AuSPtr<IProtocolPiece> GetNextPiece() = 0; virtual AuSPtr<IProtocolPiece> GetNextPiece() = 0;
/**
* @brief
* @return
*/
virtual AuSPtr<IProtocolPiece> GetPreviousPiece() = 0;
/** /**
* @brief * @brief
* @param uOutputLength * @param uOutputLength
@ -32,6 +38,13 @@ namespace Aurora::IO::Protocol
*/ */
virtual bool ReallocateDrainBuffer(AuUInt32 uOutputLength) = 0; virtual bool ReallocateDrainBuffer(AuUInt32 uOutputLength) = 0;
/**
* @brief
* @param uInputLength
* @return
*/
virtual bool ReallocateSourceBuffer(AuUInt32 uInputLength) = 0;
/** /**
* @brief Removes this piece from the stack * @brief Removes this piece from the stack
*/ */
@ -41,13 +54,19 @@ namespace Aurora::IO::Protocol
* @brief * @brief
* @return * @return
*/ */
virtual AuSPtr<IStreamWriter> ToInputWriter() = 0; virtual AuSPtr<IStreamWriter> GetInputWriter() = 0;
/**
* @brief
* @return
*/
virtual AuSPtr<IStreamReader> GetOutputReader() = 0;
/** /**
* @brief Fetches an IStreamWriter representation of the next piece in the stack * @brief Fetches an IStreamWriter representation of the next piece in the stack
* @return * @return
*/ */
virtual AuSPtr<IStreamWriter> ToNextWriter() = 0; virtual AuSPtr<IStreamWriter> GetNextWriter() = 0;
/** /**
* @brief * @brief

View File

@ -28,7 +28,7 @@ namespace Aurora::IO::Protocol
} }
} }
AuSPtr<IStreamWriter> ProtocolPiece::ToNextWriter() AuSPtr<IStreamWriter> ProtocolPiece::GetNextWriter()
{ {
if (!this->pParent) if (!this->pParent)
{ {
@ -50,7 +50,7 @@ namespace Aurora::IO::Protocol
this->pOuputWriter; this->pOuputWriter;
} }
AuSPtr<IStreamWriter> ProtocolPiece::ToInputWriter() AuSPtr<IStreamWriter> ProtocolPiece::GetInputWriter()
{ {
return this->pWriteInteface ? this->pWriteInteface->GetStreamWriter() : AuSPtr<IStreamWriter> {}; return this->pWriteInteface ? this->pWriteInteface->GetStreamWriter() : AuSPtr<IStreamWriter> {};
} }
@ -60,6 +60,17 @@ namespace Aurora::IO::Protocol
return this->pNext; return this->pNext;
} }
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));
}
bool ProtocolPiece::ReallocateDrainBuffer(AuUInt32 uOutputLength) bool ProtocolPiece::ReallocateDrainBuffer(AuUInt32 uOutputLength)
{ {
if (!this->pParent) if (!this->pParent)
@ -86,6 +97,40 @@ namespace Aurora::IO::Protocol
return bRet; return bRet;
} }
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;
}
void ProtocolPiece::Remove() void ProtocolPiece::Remove()
{ {
this->PrivateUserDataClear(); this->PrivateUserDataClear();
@ -99,7 +144,7 @@ namespace Aurora::IO::Protocol
auto &pBottomPiece = this->pParent->pBottomPiece; auto &pBottomPiece = this->pParent->pBottomPiece;
// fix chain // fix chain
AuSPtr<IProtocolPiece> pLast; AuSPtr<ProtocolPiece> pLast;
auto pCurrent = pBottomPiece; auto pCurrent = pBottomPiece;
while (true) while (true)
{ {
@ -110,7 +155,11 @@ namespace Aurora::IO::Protocol
if (pCurrent.get() == this) if (pCurrent.get() == this)
{ {
AuReinterpretCast<ProtocolPiece>(pLast)->pNext = pCurrent->pNext; if (auto pNext = pCurrent->pNext)
{
pNext->pBefore = pLast;
}
pLast->pNext = pCurrent->pNext;
break; break;
} }

View File

@ -21,18 +21,23 @@ namespace Aurora::IO::Protocol
AuSPtr<IProtocolInterceptorEx> pInterceptorEx; AuSPtr<IProtocolInterceptorEx> pInterceptorEx;
AuSPtr<IProtocolNext> pWriteInteface; AuSPtr<IProtocolNext> pWriteInteface;
AuSPtr<ProtocolPiece> pNext; AuSPtr<ProtocolPiece> pNext;
AuSPtr<ProtocolPiece> pBefore;
AuByteBuffer outputBuffer; AuByteBuffer outputBuffer;
AuSPtr<IStreamWriter> pOuputWriter; AuSPtr<IStreamWriter> pOuputWriter;
bool bMultipleTick {}; bool bMultipleTick {};
AuOptional<AuUInt> uMax; AuOptional<AuUInt> uMax;
AuUInt64 uStartingSize {}; AuUInt64 uStartingSize {};
friend struct ProtocolStack; // IProtocolPiece::PrivateUserDataClear()
AuSPtr<ProtocolStack> GetParent() override; AuSPtr<ProtocolStack> GetParent() override;
AuSPtr<IProtocolPiece> GetNextPiece() override; AuSPtr<IProtocolPiece> GetNextPiece() override;
AuSPtr<IProtocolPiece> GetPreviousPiece() override;
bool ReallocateDrainBuffer(AuUInt32 uOutputLength) override; bool ReallocateDrainBuffer(AuUInt32 uOutputLength) override;
bool ReallocateSourceBuffer(AuUInt32 uInputLength) override;
void Remove() override; void Remove() override;
AuSPtr<IStreamWriter> ToNextWriter() override; AuSPtr<IStreamWriter> GetNextWriter() override;
AuSPtr<IStreamWriter> ToInputWriter() override; AuSPtr<IStreamWriter> GetInputWriter() override;
AuSPtr<IStreamReader> GetOutputReader() override;
AuSPtr<Memory::ByteBuffer> GetNextPieceBuffer() override; AuSPtr<Memory::ByteBuffer> GetNextPieceBuffer() override;
AuSPtr<IProtocolInterceptorEx> GetExtendedInterceptor() override; AuSPtr<IProtocolInterceptorEx> GetExtendedInterceptor() override;
AuSPtr<IProtocolInterceptor> GetShortPipeInterceptor() override; AuSPtr<IProtocolInterceptor> GetShortPipeInterceptor() override;

View File

@ -203,9 +203,10 @@ namespace Aurora::IO::Protocol
if (eWhere == EProtocolWhere::ePrepend) if (eWhere == EProtocolWhere::ePrepend)
{ {
if (this->pBottomPiece) if (auto pBottomPiece = this->pBottomPiece)
{ {
pNew->pNext = this->pBottomPiece; pBottomPiece->pBefore = pNew;
pNew->pNext = pBottomPiece;
} }
this->pBottomPiece = pNew; this->pBottomPiece = pNew;
@ -214,6 +215,7 @@ namespace Aurora::IO::Protocol
{ {
if (this->pTopPiece) if (this->pTopPiece)
{ {
pNew->pBefore = this->pTopPiece;
this->pTopPiece->pNext = pNew; this->pTopPiece->pNext = pNew;
} }
@ -365,9 +367,10 @@ namespace Aurora::IO::Protocol
if (eWhere == EProtocolWhere::ePrepend) if (eWhere == EProtocolWhere::ePrepend)
{ {
if (this->pBottomPiece) if (auto pBottomPiece = this->pBottomPiece)
{ {
pNew->pNext = this->pBottomPiece; pBottomPiece->pBefore = pNew;
pNew->pNext = pBottomPiece;
} }
this->pBottomPiece = pNew; this->pBottomPiece = pNew;
@ -376,6 +379,7 @@ namespace Aurora::IO::Protocol
{ {
if (this->pTopPiece) if (this->pTopPiece)
{ {
pNew->pBefore = this->pTopPiece;
this->pTopPiece->pNext = pNew; this->pTopPiece->pNext = pNew;
} }
@ -483,6 +487,7 @@ namespace Aurora::IO::Protocol
if (this->pTopPiece) if (this->pTopPiece)
{ {
pNew->pBefore = this->pTopPiece;
this->pTopPiece->pNext = pNew; this->pTopPiece->pNext = pNew;
} }
@ -518,10 +523,12 @@ namespace Aurora::IO::Protocol
auto pCur = pItr; auto pCur = pItr;
pItr = pCur->pNext; pItr = pCur->pNext;
pCur->pNext.reset(); pCur->pNext.reset();
pCur->pBefore.reset();
pCur->pInterceptor.reset(); pCur->pInterceptor.reset();
pCur->pInterceptorEx.reset(); pCur->pInterceptorEx.reset();
pCur->pOuputWriter.reset(); pCur->pOuputWriter.reset();
pCur->pWriteInteface.reset(); pCur->pWriteInteface.reset();
pCur->PrivateUserDataClear();
} }
this->pBottomPiece.reset(); this->pBottomPiece.reset();
@ -815,7 +822,7 @@ namespace Aurora::IO::Protocol
pBase = pRead->readPtr; pBase = pRead->readPtr;
} }
auto pNextStream = pCurrent->ToNextWriter(); auto pNextStream = pCurrent->GetNextWriter();
if (!pCurrent->pInterceptor->OnDataAvailable({ pBase, uCount }, pNextStream)) if (!pCurrent->pInterceptor->OnDataAvailable({ pBase, uCount }, pNextStream))
{ {

View File

@ -97,7 +97,7 @@ namespace Aurora::IO::TLS
{ {
AuUInt count {}; AuUInt count {};
if (Aurora::IO::EStreamError::eErrorNone != if (Aurora::IO::EStreamError::eErrorNone !=
pPiece->ToNextWriter()->Write(AuMemoryViewStreamRead { AuMemoryViewRead { pIn, length }, count })) pPiece->GetNextWriter()->Write(AuMemoryViewStreamRead { AuMemoryViewRead { pIn, length }, count }))
{ {
SysPushErrorIO("TLS couldn't flush write into next protocol layer or drain"); SysPushErrorIO("TLS couldn't flush write into next protocol layer or drain");
return -1; return -1;