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: AuProtocolStack.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 "AuProtocolStack.hpp"
|
|
|
|
#include "AuProtocolPiece.hpp"
|
2022-08-28 19:02:06 +00:00
|
|
|
#include "IProtocolNext.hpp"
|
2022-12-17 20:14:19 +00:00
|
|
|
#include <Source/IO/AuIOPipeProcessor.hpp>
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
namespace Aurora::IO::Protocol
|
|
|
|
{
|
2023-09-21 07:02:02 +00:00
|
|
|
ProtocolStack::ProtocolStack() :
|
2023-09-21 20:18:59 +00:00
|
|
|
readView(this),
|
|
|
|
writeView(this)
|
2023-09-21 07:02:02 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
ProtocolStack::~ProtocolStack()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-11-30 09:39:56 +00:00
|
|
|
AuSPtr<IProtocolPiece> ProtocolStack::AddInterceptor(EProtocolWhere eWhere,
|
|
|
|
const AuSPtr<IProtocolInterceptor> &pInterceptor,
|
|
|
|
AuUInt uOutputBufferSize)
|
2022-09-04 00:16:49 +00:00
|
|
|
{
|
2023-11-30 09:39:56 +00:00
|
|
|
return this->AddInterceptorWhere(eWhere,
|
|
|
|
pInterceptor,
|
|
|
|
uOutputBufferSize);
|
2022-09-04 00:16:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 09:39:56 +00:00
|
|
|
AuSPtr<IProtocolPiece> ProtocolStack::AddInterceptorEx(EProtocolWhere eWhere,
|
|
|
|
const AuSPtr<IProtocolInterceptorEx> &pInterceptorEx,
|
|
|
|
AuUInt uOutputBufferSize)
|
2023-04-28 01:41:45 +00:00
|
|
|
{
|
2023-11-30 09:39:56 +00:00
|
|
|
return this->AddInterceptorWhereEx(eWhere,
|
|
|
|
pInterceptorEx,
|
|
|
|
uOutputBufferSize,
|
|
|
|
false);
|
2023-04-28 01:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 09:39:56 +00:00
|
|
|
AuSPtr<IProtocolPiece> ProtocolStack::AddSingleFrameProcessor(EProtocolWhere eWhere,
|
|
|
|
const AuSPtr<IProtocolInterceptorEx> &pInterceptorEx)
|
2023-04-28 01:41:45 +00:00
|
|
|
{
|
2023-11-30 09:39:56 +00:00
|
|
|
return this->AddInterceptorWhereEx(eWhere,
|
|
|
|
pInterceptorEx,
|
|
|
|
0,
|
|
|
|
true);
|
2023-04-28 01:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 09:39:56 +00:00
|
|
|
AuSPtr<IProtocolPiece> ProtocolStack::AddSingleFrameProcessorEx(EProtocolWhere eWhere,
|
|
|
|
const AuSPtr<IProtocolInterceptorEx> &pInterceptorEx,
|
|
|
|
AuUInt uOutputBufferSize)
|
2023-10-07 19:08:46 +00:00
|
|
|
{
|
2023-11-30 09:39:56 +00:00
|
|
|
return this->AddInterceptorWhereEx(eWhere,
|
|
|
|
pInterceptorEx,
|
|
|
|
uOutputBufferSize,
|
|
|
|
true);
|
2023-10-07 19:08:46 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 09:39:56 +00:00
|
|
|
AuSPtr<IProtocolPiece> ProtocolStack::AddInterceptorDynamicBuffer(EProtocolWhere eWhere,
|
|
|
|
const AuSPtr<IProtocolInterceptorEx> &pInterceptorEx,
|
|
|
|
AuUInt uOutputBufferSize,
|
|
|
|
AuUInt uOutputBufferSizeMax)
|
2023-10-07 19:08:46 +00:00
|
|
|
{
|
2023-11-30 09:39:56 +00:00
|
|
|
return this->AddInterceptorWhereEx(eWhere,
|
|
|
|
pInterceptorEx,
|
|
|
|
uOutputBufferSize,
|
|
|
|
false,
|
|
|
|
uOutputBufferSizeMax);
|
2023-10-07 19:08:46 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 09:39:56 +00:00
|
|
|
AuSPtr<IProtocolPiece> ProtocolStack::AddSingleFrameProcessorDynamicBuffer(EProtocolWhere eWhere,
|
|
|
|
const AuSPtr<IProtocolInterceptorEx> &pInterceptorEx,
|
|
|
|
AuUInt uOutputBufferSize,
|
|
|
|
AuUInt uOutputBufferSizeMax)
|
2023-10-07 19:08:46 +00:00
|
|
|
{
|
2023-11-30 09:39:56 +00:00
|
|
|
return this->AddInterceptorWhereEx(eWhere,
|
|
|
|
pInterceptorEx,
|
|
|
|
uOutputBufferSize,
|
|
|
|
true,
|
|
|
|
uOutputBufferSizeMax);
|
2023-10-07 19:08:46 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 09:39:56 +00:00
|
|
|
AuSPtr<IProtocolPiece> ProtocolStack::AddInterceptorWhere(EProtocolWhere eWhere,
|
2022-09-29 10:45:08 +00:00
|
|
|
const AuSPtr<IProtocolInterceptor> &pInterceptor,
|
|
|
|
AuUInt uOutputBufferSize)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2023-11-30 09:39:56 +00:00
|
|
|
if (!EProtocolWhereIsValid(eWhere))
|
|
|
|
{
|
|
|
|
SysPushErrorArg();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pInterceptor)
|
|
|
|
{
|
|
|
|
SysPushErrorArg();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-09-21 20:18:59 +00:00
|
|
|
AU_LOCK_GUARD(this->mutex);
|
|
|
|
|
2022-09-02 19:53:25 +00:00
|
|
|
if (this->bWrittenEnd)
|
|
|
|
{
|
2022-09-29 10:45:08 +00:00
|
|
|
return {};
|
2022-09-02 19:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
if (!uOutputBufferSize)
|
|
|
|
{
|
2023-10-24 11:02:05 +00:00
|
|
|
uOutputBufferSize = gRuntimeConfig.ioConfig.uProtocolStackDefaultBufferSize;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto pNew = AuMakeShared<ProtocolPiece>();
|
|
|
|
if (!pNew)
|
|
|
|
{
|
|
|
|
SysPushErrorNet("Out of memory");
|
2022-09-29 10:45:08 +00:00
|
|
|
return {};
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 23:39:12 +00:00
|
|
|
pNew->outputBuffer = AuByteBuffer(uOutputBufferSize, true, false);
|
|
|
|
if (!pNew->outputBuffer.IsValid())
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
|
|
|
SysPushErrorNet("Out of memory");
|
2022-09-29 10:45:08 +00:00
|
|
|
return {};
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2023-10-29 20:36:11 +00:00
|
|
|
pNew->outputBuffer.flagNoRealloc = true;
|
2023-10-23 10:30:25 +00:00
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
// Circular ref
|
|
|
|
pNew->pOuputWriter = AuMakeShared<AuIO::Buffered::BlobWriter>(AuSPtr<Memory::ByteBuffer>(pNew, &pNew->outputBuffer));
|
|
|
|
|
|
|
|
struct StreamWrapper : IStreamWriter, IProtocolNext, AuEnableSharedFromThis<StreamWrapper>
|
|
|
|
{
|
2023-04-28 01:41:45 +00:00
|
|
|
AuWPtr<ProtocolStack> pStack;
|
2022-08-28 19:02:06 +00:00
|
|
|
AuWPtr<IProtocolInterceptor> pInterceptor;
|
|
|
|
AuWPtr<ProtocolPiece> pParent;
|
|
|
|
|
|
|
|
EStreamError IsOpen() override
|
|
|
|
{
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
EStreamError Write(const Memory::MemoryViewStreamRead ¶meters) override
|
|
|
|
{
|
2023-12-28 16:49:11 +00:00
|
|
|
parameters.outVariable = 0;
|
|
|
|
|
2023-10-24 10:59:55 +00:00
|
|
|
auto pCopy = AuMakeShared<AuByteBuffer>(parameters);
|
|
|
|
|
|
|
|
if (!pCopy ||
|
|
|
|
!(*pCopy.get()))
|
|
|
|
{
|
|
|
|
return EStreamError::eErrorOutOfMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pStack.lock()->DoTick(pCopy, pParent.lock()) ?
|
2022-08-28 19:02:06 +00:00
|
|
|
EStreamError::eErrorNone :
|
|
|
|
EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Close() override
|
|
|
|
{
|
2023-06-04 18:02:33 +00:00
|
|
|
if (auto pStack = AuTryLockMemoryType(this->pStack))
|
|
|
|
{
|
|
|
|
pStack->Terminate();
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
};
|
2023-06-04 20:18:44 +00:00
|
|
|
|
|
|
|
virtual void Flush() override
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2023-06-04 20:18:44 +00:00
|
|
|
if (auto pStack = AuTryLockMemoryType(this->pStack))
|
|
|
|
{
|
|
|
|
pStack->DoTick();
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
virtual AuSPtr<Memory::ByteBuffer> GetOutputBuffer() override
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual AuSPtr<IStreamWriter> GetStreamWriter() override
|
|
|
|
{
|
|
|
|
return AuSharedFromThis();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto pWrapper = AuMakeShared<StreamWrapper>();
|
|
|
|
if (!pWrapper)
|
|
|
|
{
|
2022-09-29 10:45:08 +00:00
|
|
|
SysPushErrorMemory();
|
|
|
|
return {};
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pWrapper->pInterceptor = pInterceptor;
|
2023-04-28 01:41:45 +00:00
|
|
|
pWrapper->pStack = AuSharedFromThis();
|
2022-08-28 19:02:06 +00:00
|
|
|
pWrapper->pParent = pNew;
|
|
|
|
|
|
|
|
pNew->pWriteInteface = pWrapper;
|
|
|
|
pNew->pParent = this;
|
|
|
|
pNew->pInterceptor = pInterceptor;
|
|
|
|
|
2023-11-30 09:39:56 +00:00
|
|
|
if (eWhere == EProtocolWhere::ePrepend)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2023-12-10 13:56:37 +00:00
|
|
|
if (auto pBottomPiece = this->pBottomPiece)
|
2022-09-04 00:16:49 +00:00
|
|
|
{
|
2023-12-10 13:56:37 +00:00
|
|
|
pBottomPiece->pBefore = pNew;
|
|
|
|
pNew->pNext = pBottomPiece;
|
2022-09-04 00:16:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
this->pBottomPiece = pNew;
|
|
|
|
}
|
2022-09-04 00:16:49 +00:00
|
|
|
else
|
|
|
|
{
|
2023-04-19 22:57:56 +00:00
|
|
|
if (this->pTopPiece)
|
|
|
|
{
|
2023-12-10 13:56:37 +00:00
|
|
|
pNew->pBefore = this->pTopPiece;
|
2023-04-19 22:57:56 +00:00
|
|
|
this->pTopPiece->pNext = pNew;
|
|
|
|
}
|
|
|
|
|
2022-09-04 00:16:49 +00:00
|
|
|
this->pTopPiece = pNew;
|
|
|
|
|
|
|
|
if (!this->pBottomPiece)
|
|
|
|
{
|
|
|
|
this->pBottomPiece = pNew;
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2023-06-11 14:09:07 +00:00
|
|
|
this->DiscardAllocCaches();
|
|
|
|
|
2022-09-29 10:45:08 +00:00
|
|
|
return pNew;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 09:39:56 +00:00
|
|
|
AuSPtr<IProtocolPiece> ProtocolStack::AddInterceptorWhereEx(EProtocolWhere eWhere,
|
2022-09-29 10:45:08 +00:00
|
|
|
const AuSPtr<IProtocolInterceptorEx> &pInterceptor,
|
2022-11-08 19:24:08 +00:00
|
|
|
AuUInt uOutputBufferSize,
|
2023-10-07 19:08:46 +00:00
|
|
|
bool bMultipleTick,
|
|
|
|
AuOptional<AuUInt> uMax)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2023-11-30 09:39:56 +00:00
|
|
|
if (!EProtocolWhereIsValid(eWhere))
|
|
|
|
{
|
|
|
|
SysPushErrorArg();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pInterceptor)
|
|
|
|
{
|
|
|
|
SysPushErrorArg();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-09-21 20:18:59 +00:00
|
|
|
AU_LOCK_GUARD(this->mutex);
|
|
|
|
|
2022-09-02 19:53:25 +00:00
|
|
|
if (this->bWrittenEnd)
|
|
|
|
{
|
2022-09-29 10:45:08 +00:00
|
|
|
return {};
|
2022-09-02 19:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
if (!this->pSourceBufer)
|
|
|
|
{
|
2022-09-29 10:45:08 +00:00
|
|
|
return {};
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!uOutputBufferSize)
|
|
|
|
{
|
2023-10-24 11:02:05 +00:00
|
|
|
uOutputBufferSize = gRuntimeConfig.ioConfig.uProtocolStackDefaultBufferSize;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto pNew = AuMakeShared<ProtocolPiece>();
|
|
|
|
if (!pNew)
|
|
|
|
{
|
|
|
|
SysPushErrorNet("Out of memory");
|
2022-09-29 10:45:08 +00:00
|
|
|
return {};
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 23:39:12 +00:00
|
|
|
pNew->outputBuffer = AuByteBuffer(uOutputBufferSize, true, false);
|
|
|
|
if (!pNew->outputBuffer.IsValid())
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
|
|
|
SysPushErrorNet("Out of memory");
|
2022-09-29 10:45:08 +00:00
|
|
|
return {};
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2023-10-07 19:08:46 +00:00
|
|
|
if (uMax)
|
|
|
|
{
|
|
|
|
pNew->uMax = uMax;
|
|
|
|
pNew->outputBuffer.flagAlwaysExpandable = pNew->outputBuffer.flagExpandable = 1;
|
|
|
|
}
|
2023-10-23 10:30:25 +00:00
|
|
|
else
|
|
|
|
{
|
2023-10-29 20:36:11 +00:00
|
|
|
pNew->outputBuffer.flagNoRealloc = true;
|
2023-10-23 10:30:25 +00:00
|
|
|
}
|
2023-10-07 19:08:46 +00:00
|
|
|
|
|
|
|
pNew->uStartingSize = uOutputBufferSize;
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
// Circular ref
|
|
|
|
pNew->pOuputWriter = AuMakeShared<AuIO::Buffered::BlobWriter>(AuSPtr<Memory::ByteBuffer>(pNew, &pNew->outputBuffer));
|
|
|
|
|
|
|
|
struct StreamWrapper : IStreamWriter, IProtocolNext, AuEnableSharedFromThis<StreamWrapper>
|
|
|
|
{
|
2023-04-28 01:41:45 +00:00
|
|
|
AuWPtr<ProtocolStack> pStack;
|
2022-08-28 19:02:06 +00:00
|
|
|
AuWPtr<IProtocolInterceptorEx> pInterceptor;
|
|
|
|
AuWPtr<ProtocolPiece> pParent;
|
|
|
|
|
|
|
|
EStreamError IsOpen() override
|
|
|
|
{
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
EStreamError Write(const Memory::MemoryViewStreamRead ¶meters) override
|
|
|
|
{
|
2023-12-28 16:49:11 +00:00
|
|
|
parameters.outVariable = 0;
|
|
|
|
|
2023-10-24 10:59:55 +00:00
|
|
|
auto pCopy = AuMakeShared<AuByteBuffer>(parameters);
|
|
|
|
|
|
|
|
if (!pCopy ||
|
|
|
|
!(*pCopy.get()))
|
|
|
|
{
|
|
|
|
return EStreamError::eErrorOutOfMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pStack.lock()->DoTick(pCopy, pParent.lock()) ?
|
2022-08-28 19:02:06 +00:00
|
|
|
EStreamError::eErrorNone :
|
|
|
|
EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Close() override
|
|
|
|
{
|
2023-06-04 18:02:33 +00:00
|
|
|
if (auto pStack = AuTryLockMemoryType(this->pStack))
|
|
|
|
{
|
|
|
|
pStack->Terminate();
|
|
|
|
}
|
2023-06-04 20:18:44 +00:00
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2023-06-04 20:18:44 +00:00
|
|
|
virtual void Flush() override
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2023-06-04 20:18:44 +00:00
|
|
|
if (auto pStack = AuTryLockMemoryType(this->pStack))
|
|
|
|
{
|
|
|
|
pStack->DoTick();
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
virtual AuSPtr<Memory::ByteBuffer> GetOutputBuffer() override
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual AuSPtr<IStreamWriter> GetStreamWriter() override
|
|
|
|
{
|
|
|
|
return AuSharedFromThis();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto pWrapper = AuMakeShared<StreamWrapper>();
|
|
|
|
if (!pWrapper)
|
|
|
|
{
|
2022-09-29 10:45:08 +00:00
|
|
|
SysPushErrorMemory();
|
|
|
|
return {};
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pWrapper->pInterceptor = pInterceptor;
|
2023-04-28 01:41:45 +00:00
|
|
|
pWrapper->pStack = AuSharedFromThis();
|
2022-08-28 19:02:06 +00:00
|
|
|
pWrapper->pParent = pNew;
|
|
|
|
|
|
|
|
pNew->pWriteInteface = pWrapper;
|
|
|
|
pNew->pInterceptorEx = pInterceptor;
|
|
|
|
pNew->pParent = this;
|
2022-11-08 19:24:08 +00:00
|
|
|
pNew->bMultipleTick = bMultipleTick;
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2023-11-30 09:39:56 +00:00
|
|
|
if (eWhere == EProtocolWhere::ePrepend)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2023-12-10 13:56:37 +00:00
|
|
|
if (auto pBottomPiece = this->pBottomPiece)
|
2022-09-04 00:16:49 +00:00
|
|
|
{
|
2023-12-10 13:56:37 +00:00
|
|
|
pBottomPiece->pBefore = pNew;
|
|
|
|
pNew->pNext = pBottomPiece;
|
2022-09-04 00:16:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
this->pBottomPiece = pNew;
|
|
|
|
}
|
2022-09-04 00:16:49 +00:00
|
|
|
else
|
|
|
|
{
|
2023-04-19 22:35:48 +00:00
|
|
|
if (this->pTopPiece)
|
|
|
|
{
|
2023-12-10 13:56:37 +00:00
|
|
|
pNew->pBefore = this->pTopPiece;
|
2023-04-19 22:35:48 +00:00
|
|
|
this->pTopPiece->pNext = pNew;
|
|
|
|
}
|
|
|
|
|
2022-09-04 00:16:49 +00:00
|
|
|
this->pTopPiece = pNew;
|
|
|
|
|
|
|
|
if (!this->pBottomPiece)
|
|
|
|
{
|
|
|
|
this->pBottomPiece = pNew;
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2023-06-11 14:09:07 +00:00
|
|
|
this->DiscardAllocCaches();
|
|
|
|
|
2022-09-29 10:45:08 +00:00
|
|
|
return pNew;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 10:45:08 +00:00
|
|
|
AuSPtr<IProtocolPiece> ProtocolStack::AddEndInterceptor(const AuSPtr<IProtocolInterceptorEx> &pInterceptor)
|
2022-09-02 19:53:25 +00:00
|
|
|
{
|
2023-09-21 20:18:59 +00:00
|
|
|
AU_LOCK_GUARD(this->mutex);
|
|
|
|
|
2022-09-02 19:53:25 +00:00
|
|
|
if (this->bWrittenEnd)
|
|
|
|
{
|
2022-09-29 10:45:08 +00:00
|
|
|
return {};
|
2022-09-02 19:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->pSourceBufer)
|
|
|
|
{
|
2022-09-29 10:45:08 +00:00
|
|
|
return {};
|
2022-09-02 19:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto pNew = AuMakeShared<ProtocolPiece>();
|
|
|
|
if (!pNew)
|
|
|
|
{
|
|
|
|
SysPushErrorNet("Out of memory");
|
2022-09-29 10:45:08 +00:00
|
|
|
return {};
|
2022-09-02 19:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct StreamWrapper : IStreamWriter, IProtocolNext, AuEnableSharedFromThis<StreamWrapper>
|
|
|
|
{
|
2023-04-28 01:41:45 +00:00
|
|
|
AuWPtr<ProtocolStack> pStack;
|
2022-09-02 19:53:25 +00:00
|
|
|
AuWPtr<IProtocolInterceptorEx> pInterceptor;
|
|
|
|
AuWPtr<ProtocolPiece> pParent;
|
|
|
|
|
|
|
|
EStreamError IsOpen() override
|
|
|
|
{
|
|
|
|
return EStreamError::eErrorNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
EStreamError Write(const Memory::MemoryViewStreamRead ¶meters) override
|
|
|
|
{
|
2023-12-28 16:49:11 +00:00
|
|
|
parameters.outVariable = 0;
|
|
|
|
|
2023-10-24 10:59:55 +00:00
|
|
|
auto pCopy = AuMakeShared<AuByteBuffer>(parameters);
|
|
|
|
|
|
|
|
if (!pCopy ||
|
|
|
|
!(*pCopy.get()))
|
|
|
|
{
|
|
|
|
return EStreamError::eErrorOutOfMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pStack.lock()->DoTick(pCopy, pParent.lock()) ?
|
2022-09-02 19:53:25 +00:00
|
|
|
EStreamError::eErrorNone :
|
|
|
|
EStreamError::eErrorStreamInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Close() override
|
|
|
|
{
|
2023-06-04 18:02:33 +00:00
|
|
|
if (auto pStack = AuTryLockMemoryType(this->pStack))
|
|
|
|
{
|
|
|
|
pStack->Terminate();
|
|
|
|
}
|
2023-06-04 20:18:44 +00:00
|
|
|
}
|
2022-09-02 19:53:25 +00:00
|
|
|
|
2023-06-04 20:18:44 +00:00
|
|
|
virtual void Flush() override
|
2022-09-02 19:53:25 +00:00
|
|
|
{
|
2023-06-04 20:18:44 +00:00
|
|
|
if (auto pStack = AuTryLockMemoryType(this->pStack))
|
|
|
|
{
|
|
|
|
pStack->DoTick();
|
|
|
|
}
|
|
|
|
}
|
2022-09-02 19:53:25 +00:00
|
|
|
|
|
|
|
virtual AuSPtr<Memory::ByteBuffer> GetOutputBuffer() override
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual AuSPtr<IStreamWriter> GetStreamWriter() override
|
|
|
|
{
|
|
|
|
return AuSharedFromThis();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto pWrapper = AuMakeShared<StreamWrapper>();
|
|
|
|
if (!pWrapper)
|
|
|
|
{
|
2022-09-29 10:45:08 +00:00
|
|
|
SysPushErrorMemory();
|
|
|
|
return {};
|
2022-09-02 19:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pWrapper->pInterceptor = pInterceptor;
|
2023-04-28 01:41:45 +00:00
|
|
|
pWrapper->pStack = AuSharedFromThis();
|
2022-09-02 19:53:25 +00:00
|
|
|
pWrapper->pParent = pNew;
|
|
|
|
|
|
|
|
pNew->pWriteInteface = pWrapper;
|
|
|
|
pNew->pInterceptorEx = pInterceptor;
|
|
|
|
pNew->pParent = this;
|
|
|
|
|
2023-04-27 09:26:41 +00:00
|
|
|
if (this->pTopPiece)
|
|
|
|
{
|
2023-12-10 13:56:37 +00:00
|
|
|
pNew->pBefore = this->pTopPiece;
|
2023-04-27 09:26:41 +00:00
|
|
|
this->pTopPiece->pNext = pNew;
|
|
|
|
}
|
|
|
|
|
2022-09-02 19:53:25 +00:00
|
|
|
this->pTopPiece = pNew;
|
|
|
|
|
|
|
|
if (!this->pBottomPiece)
|
|
|
|
{
|
|
|
|
this->pBottomPiece = pNew;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->bWrittenEnd = true;
|
2023-06-11 14:09:07 +00:00
|
|
|
this->DiscardAllocCaches();
|
2022-09-02 19:53:25 +00:00
|
|
|
|
2022-09-29 10:45:08 +00:00
|
|
|
return pNew;
|
2022-09-02 19:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
void ProtocolStack::Destroy()
|
|
|
|
{
|
2023-09-21 20:18:59 +00:00
|
|
|
AU_LOCK_GUARD(this->mutex);
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
if (this->bOwnsSource)
|
|
|
|
{
|
|
|
|
if (this->pSourceBufer)
|
|
|
|
{
|
|
|
|
this->pSourceBufer->Reset();
|
|
|
|
this->pSourceBufer.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pItr = this->pBottomPiece;
|
|
|
|
while (pItr)
|
|
|
|
{
|
|
|
|
auto pCur = pItr;
|
|
|
|
pItr = pCur->pNext;
|
|
|
|
pCur->pNext.reset();
|
2023-12-10 13:56:37 +00:00
|
|
|
pCur->pBefore.reset();
|
2022-08-28 19:02:06 +00:00
|
|
|
pCur->pInterceptor.reset();
|
|
|
|
pCur->pInterceptorEx.reset();
|
|
|
|
pCur->pOuputWriter.reset();
|
|
|
|
pCur->pWriteInteface.reset();
|
2023-12-10 13:56:37 +00:00
|
|
|
pCur->PrivateUserDataClear();
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->pBottomPiece.reset();
|
|
|
|
this->pTopPiece.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IStreamWriter> ProtocolStack::AsStreamWriter()
|
|
|
|
{
|
|
|
|
if (!this->bOwnsSource)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-09-21 20:18:59 +00:00
|
|
|
return AuSPtr<IStreamWriter>(this->SharedFromThis(), &this->writeView);
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<Memory::ByteBuffer> ProtocolStack::AsWritableByteBuffer()
|
|
|
|
{
|
|
|
|
if (!this->bOwnsSource)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->pSourceBufer;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IStreamReader> ProtocolStack::AsStreamReader()
|
|
|
|
{
|
2022-09-02 19:53:25 +00:00
|
|
|
auto pBuffer = AsReadableByteBuffer();
|
|
|
|
if (!pBuffer)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-09-21 07:02:02 +00:00
|
|
|
return AuSPtr<IStreamReader>(this->SharedFromThis(), &this->readView);
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<Memory::ByteBuffer> ProtocolStack::AsReadableByteBuffer()
|
|
|
|
{
|
|
|
|
if (!this->pTopPiece)
|
|
|
|
{
|
|
|
|
return this->pSourceBufer;
|
|
|
|
}
|
|
|
|
|
2022-09-02 19:53:25 +00:00
|
|
|
if (this->pTopPiece->outputBuffer.IsEmpty())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
return AuSPtr<AuByteBuffer>(this->pTopPiece, &this->pTopPiece->outputBuffer);
|
|
|
|
}
|
|
|
|
|
2023-06-04 18:02:33 +00:00
|
|
|
void ProtocolStack::Terminate()
|
|
|
|
{
|
2023-09-21 20:18:59 +00:00
|
|
|
AU_LOCK_GUARD(this->mutex);
|
|
|
|
|
2023-06-04 18:02:33 +00:00
|
|
|
if (auto pStack = AuTryLockMemoryType(this->pPipeWork))
|
|
|
|
{
|
|
|
|
(void)pStack->End();
|
|
|
|
}
|
2023-09-21 07:02:02 +00:00
|
|
|
|
|
|
|
this->bDead = true;
|
2023-06-04 18:02:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 11:13:16 +00:00
|
|
|
bool ProtocolStack::DoTick()
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2023-09-21 20:18:59 +00:00
|
|
|
AU_LOCK_GUARD(this->mutex);
|
|
|
|
|
2023-09-21 07:02:02 +00:00
|
|
|
return this->DoTickEx(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtocolStack::DoTickEx(bool bPermitRead)
|
|
|
|
{
|
|
|
|
if (this->bDead)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
if (!this->pSourceBufer)
|
|
|
|
{
|
2023-04-25 11:13:16 +00:00
|
|
|
return false;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->pBottomPiece)
|
|
|
|
{
|
2023-04-25 11:13:16 +00:00
|
|
|
return false;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 07:02:02 +00:00
|
|
|
if (this->eType == EProtocolStackCreateType::eByBufferedAdhocStream)
|
|
|
|
{
|
|
|
|
if (!this->TryReadMore())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 11:13:16 +00:00
|
|
|
if (!this->DoTick(this->pSourceBufer, {}))
|
|
|
|
{
|
2023-04-25 11:57:02 +00:00
|
|
|
if (this->bKillPipeOnFirstRootLevelFalse)
|
2023-04-25 11:13:16 +00:00
|
|
|
{
|
2023-09-21 07:02:02 +00:00
|
|
|
if (this->eType == EProtocolStackCreateType::eByPipe)
|
|
|
|
{
|
|
|
|
if (auto pStack = AuTryLockMemoryType(this->pPipeWork))
|
|
|
|
{
|
|
|
|
pStack->TerminateOnThread(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->bDead = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this->eType == EProtocolStackCreateType::eByBufferedAdhocStream)
|
2023-04-25 11:57:02 +00:00
|
|
|
{
|
2023-09-21 07:02:02 +00:00
|
|
|
if (this->TryReadMore(true))
|
|
|
|
{
|
|
|
|
if (this->DoTick(this->pSourceBufer, {}))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2023-04-25 11:57:02 +00:00
|
|
|
}
|
2023-04-25 11:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:09:02 +00:00
|
|
|
return !this->bDead;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
2023-09-21 07:02:02 +00:00
|
|
|
|
|
|
|
bool ProtocolStack::TryReadMore(bool bForce)
|
|
|
|
{
|
|
|
|
if (!bForce)
|
|
|
|
{
|
|
|
|
if (this->pSourceBufer->readPtr !=
|
|
|
|
this->pSourceBufer->writePtr)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pSource = this->pStreamReaderAsInputSource;
|
|
|
|
if (!pSource)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto uRemBytes = this->pSourceBufer->RemainingWrite();
|
|
|
|
auto uFragment = this->uPreferredFragmentSize ?
|
|
|
|
AuMin<AuUInt>(uRemBytes, this->uPreferredFragmentSize) :
|
|
|
|
uRemBytes;
|
|
|
|
|
|
|
|
AuUInt uBytesRead {};
|
|
|
|
if (AuIO::EStreamError::eErrorNone !=
|
|
|
|
this->pStreamReaderAsInputSource->Read({ AuStaticCast<AuMemoryViewWrite>(*this->pSourceBufer), uBytesRead }))
|
|
|
|
{
|
|
|
|
if (auto pStack = AuTryLockMemoryType(this->pPipeWork))
|
|
|
|
{
|
|
|
|
pStack->TerminateOnThread(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->pSourceBufer->writePtr += uBytesRead;
|
|
|
|
return true;
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
bool ProtocolStack::DoTick(const AuSPtr<AuByteBuffer> &pRead, const AuSPtr<ProtocolPiece> &pPiece)
|
|
|
|
{
|
|
|
|
auto pCurrent = pPiece ? pPiece : this->pBottomPiece;
|
|
|
|
|
|
|
|
if (pCurrent->pInterceptorEx)
|
|
|
|
{
|
2022-11-08 19:24:08 +00:00
|
|
|
bool bTryAgain {};
|
|
|
|
bool bTryAgainAtleastOnce {};
|
|
|
|
do
|
|
|
|
{
|
|
|
|
auto pNextStream = ((pPiece == this->pTopPiece) && (this->pDrainBuffer)) ?
|
|
|
|
(this->pDrainBuffer) :
|
|
|
|
(!pCurrent->outputBuffer.IsEmpty() ? AuSPtr<AuByteBuffer>(pCurrent, &pCurrent->outputBuffer) : AuSPtr<AuByteBuffer> {});
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2022-11-08 19:24:08 +00:00
|
|
|
auto pOldHead = pNextStream ? pNextStream->readPtr : nullptr;
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2023-04-19 22:46:56 +00:00
|
|
|
if (pRead)
|
|
|
|
{
|
|
|
|
if (pRead->flagReadError)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-07 19:08:46 +00:00
|
|
|
bool bStatus = pCurrent->pInterceptorEx->OnDataAvailable(pRead, pNextStream, pCurrent);
|
|
|
|
|
|
|
|
if (!bStatus)
|
2022-11-08 19:24:08 +00:00
|
|
|
{
|
|
|
|
if (pNextStream)
|
|
|
|
{
|
|
|
|
pNextStream->readPtr = pOldHead;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bTryAgainAtleastOnce;
|
|
|
|
}
|
2023-10-07 19:08:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pCurrent->uMax.has_value())
|
|
|
|
{
|
|
|
|
auto bytesRem = pCurrent->outputBuffer.RemainingBytes();
|
|
|
|
auto maxVal = pCurrent->uMax.value();
|
|
|
|
if (bytesRem >= maxVal)
|
|
|
|
{
|
|
|
|
this->Terminate();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto target = AuMin(AuPageRound<AuUInt>(bytesRem + ((bytesRem / 4) * 3), 128), maxVal);
|
2023-10-17 02:08:29 +00:00
|
|
|
target = AuMax<AuUInt>(target, pCurrent->uStartingSize);
|
2023-10-07 19:08:46 +00:00
|
|
|
|
|
|
|
if (target >= pCurrent->outputBuffer.allocSize ||
|
|
|
|
((pCurrent->outputBuffer.length > target) && (bytesRem < pCurrent->uStartingSize)))
|
|
|
|
{
|
|
|
|
AuByteBuffer replacement(target, true, false);
|
2023-10-16 00:37:59 +00:00
|
|
|
if (!replacement)
|
|
|
|
{
|
|
|
|
this->Terminate();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-07 19:08:46 +00:00
|
|
|
if (!replacement.WriteFrom(pCurrent->outputBuffer))
|
|
|
|
{
|
|
|
|
this->Terminate();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pCurrent->outputBuffer = AuMove(replacement);
|
|
|
|
pCurrent->outputBuffer.flagAlwaysExpandable =
|
|
|
|
pCurrent->outputBuffer.flagExpandable = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 19:24:08 +00:00
|
|
|
|
|
|
|
if (!pNextStream)
|
2022-09-02 19:53:25 +00:00
|
|
|
{
|
2022-11-08 19:24:08 +00:00
|
|
|
return true;
|
2022-09-02 19:53:25 +00:00
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2022-11-08 19:24:08 +00:00
|
|
|
if (auto pNext = pCurrent->pNext)
|
|
|
|
{
|
|
|
|
auto pOldHead = pNextStream->readPtr;
|
2022-09-02 19:53:25 +00:00
|
|
|
|
2022-11-08 19:24:08 +00:00
|
|
|
bool bDontCareAboutSubTicks = this->DoTick(pNextStream, pNext);
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2022-11-08 19:24:08 +00:00
|
|
|
if (!bDontCareAboutSubTicks)
|
|
|
|
{
|
|
|
|
pNextStream->readPtr = pOldHead;
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2022-11-08 19:24:08 +00:00
|
|
|
if (pCurrent->bMultipleTick)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2022-11-08 19:24:08 +00:00
|
|
|
bTryAgain = pRead->RemainingBytes();
|
|
|
|
bTryAgainAtleastOnce |= bTryAgain;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
2023-04-19 22:46:56 +00:00
|
|
|
|
|
|
|
if (pCurrent->outputBuffer)
|
|
|
|
{
|
|
|
|
if (pCurrent->outputBuffer.flagWriteError)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
2024-02-25 20:09:02 +00:00
|
|
|
while (bTryAgain && this->IsValid());
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AuUInt8 *pBase {};
|
|
|
|
AuUInt uCount {};
|
|
|
|
|
|
|
|
if (pRead->writePtr >= pRead->readPtr)
|
|
|
|
{
|
|
|
|
uCount = pRead->writePtr - pRead->readPtr;
|
|
|
|
pBase = pRead->readPtr;
|
|
|
|
}
|
|
|
|
else if (pRead->flagCircular)
|
|
|
|
{
|
|
|
|
uCount = (pRead->base + pRead->length) - pRead->readPtr;
|
|
|
|
pBase = pRead->readPtr;
|
|
|
|
}
|
|
|
|
|
2023-12-10 13:56:37 +00:00
|
|
|
auto pNextStream = pCurrent->GetNextWriter();
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
if (!pCurrent->pInterceptor->OnDataAvailable({ pBase, uCount }, pNextStream))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 01:41:45 +00:00
|
|
|
AuList<AuSPtr<IProtocolPiece>> ProtocolStack::GetArrayOfInterceptors()
|
|
|
|
{
|
2023-09-21 20:18:59 +00:00
|
|
|
AU_LOCK_GUARD(this->mutex);
|
2023-04-28 01:41:45 +00:00
|
|
|
AuList<AuSPtr<IProtocolPiece>> list;
|
|
|
|
|
|
|
|
auto pItr = this->pBottomPiece;
|
|
|
|
while (pItr)
|
|
|
|
{
|
|
|
|
auto pCur = pItr;
|
|
|
|
pItr = pCur->pNext;
|
|
|
|
list.push_back(pCur);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IProtocolPiece> ProtocolStack::GetInterceptorAtIndex(AuUInt32 uIndex)
|
|
|
|
{
|
2023-09-21 20:18:59 +00:00
|
|
|
AU_LOCK_GUARD(this->mutex);
|
2023-04-28 01:41:45 +00:00
|
|
|
AuUInt32 uCounter {};
|
|
|
|
|
|
|
|
auto pItr = this->pBottomPiece;
|
|
|
|
while (pItr)
|
|
|
|
{
|
|
|
|
auto pCur = pItr;
|
|
|
|
pItr = pCur->pNext;
|
|
|
|
|
|
|
|
if (uIndex == uCounter++)
|
|
|
|
{
|
|
|
|
return pCur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-10-07 19:08:46 +00:00
|
|
|
bool ProtocolStack::IsValid()
|
|
|
|
{
|
|
|
|
return !this->bDead;
|
|
|
|
}
|
|
|
|
|
2023-06-11 14:09:07 +00:00
|
|
|
void ProtocolStack::DiscardAllocCaches()
|
|
|
|
{
|
|
|
|
AuResetMember(this->pStreamReaderCache);
|
|
|
|
AuResetMember(this->pStreamWriterCache);
|
|
|
|
}
|
|
|
|
|
2023-09-21 07:02:02 +00:00
|
|
|
static AuSPtr<IProtocolStack> _NewBufferedProtocolStack(AuUInt uLength,
|
|
|
|
ProtocolStackExtraData ex)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2022-08-29 11:08:45 +00:00
|
|
|
if (!uLength)
|
|
|
|
{
|
|
|
|
SysPushErrorArg();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
auto pStack = AuMakeShared<ProtocolStack>();
|
|
|
|
if (!pStack)
|
|
|
|
{
|
2022-08-29 11:08:45 +00:00
|
|
|
SysPushErrorMem();
|
2022-08-28 19:02:06 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-09-21 07:02:02 +00:00
|
|
|
pStack->eType = EProtocolStackCreateType::eByBuffered;
|
|
|
|
|
2022-08-28 23:39:12 +00:00
|
|
|
auto pBuffer = AuMakeShared<AuByteBuffer>(uLength, true);
|
2022-08-28 19:02:06 +00:00
|
|
|
if (!pBuffer)
|
|
|
|
{
|
2022-08-29 11:08:45 +00:00
|
|
|
SysPushErrorMem();
|
2022-08-28 19:02:06 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-28 23:39:12 +00:00
|
|
|
if (!pBuffer->IsValid())
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
pStack->bOwnsSource = true;
|
|
|
|
pStack->pSourceBufer = pBuffer;
|
|
|
|
|
2023-09-21 07:02:02 +00:00
|
|
|
pStack->bStreamReaderFlagAdhocPump = ex.bStreamReaderFlagAdhocPump;
|
|
|
|
|
|
|
|
if (ex.bKillPipeOnFirstRootLevelFalse)
|
|
|
|
{
|
|
|
|
pStack->bKillPipeOnFirstRootLevelFalse = ex.bKillPipeOnFirstRootLevelFalse.value();
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
return pStack;
|
|
|
|
}
|
2022-08-29 11:08:45 +00:00
|
|
|
|
2023-09-21 07:02:02 +00:00
|
|
|
AUKN_SYM AuSPtr<IProtocolStack> NewBufferedProtocolStack(AuUInt uLength)
|
|
|
|
{
|
|
|
|
return _NewBufferedProtocolStack(uLength, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
static AuSPtr<IProtocolStack> _NewProtocolStackFromPipe(const AuSPtr<IIOPipeWork> &pWork,
|
|
|
|
bool bAutoTick,
|
|
|
|
bool bKillPipeOnFirstRootLevelFalse,
|
|
|
|
ProtocolStackExtraData ex)
|
2022-08-29 11:08:45 +00:00
|
|
|
{
|
|
|
|
if (!pWork)
|
|
|
|
{
|
|
|
|
SysPushErrorArg();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pStack = AuMakeShared<ProtocolStack>();
|
|
|
|
if (!pStack)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-09-21 07:02:02 +00:00
|
|
|
pStack->eType = EProtocolStackCreateType::eByPipe;
|
|
|
|
|
2022-11-07 13:34:28 +00:00
|
|
|
auto pWorkEx = AuStaticCast<IOPipeWork>(pWork);
|
|
|
|
|
2023-04-25 11:13:16 +00:00
|
|
|
pStack->pPipeWork = pWorkEx;
|
2023-04-25 11:57:02 +00:00
|
|
|
pStack->bKillPipeOnFirstRootLevelFalse = bKillPipeOnFirstRootLevelFalse;
|
2023-04-25 11:13:16 +00:00
|
|
|
|
2023-09-21 07:02:02 +00:00
|
|
|
if (ex.bKillPipeOnFirstRootLevelFalse)
|
|
|
|
{
|
|
|
|
pStack->bKillPipeOnFirstRootLevelFalse = ex.bKillPipeOnFirstRootLevelFalse.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
pStack->bStreamReaderFlagAdhocPump = ex.bStreamReaderFlagAdhocPump;
|
|
|
|
|
2022-11-07 13:34:28 +00:00
|
|
|
if (bAutoTick)
|
|
|
|
{
|
|
|
|
if (pWorkEx->pProtocolStack)
|
|
|
|
{
|
|
|
|
SysPushErrorGeneric("NewProtocolStackFromPipe called twice. wont recv DoTick");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pWorkEx->pProtocolStack = pStack;
|
|
|
|
pStack->pSourceBufer = AuUnsafeRaiiToShared(pWorkEx->GetBuffer());
|
|
|
|
return pStack;
|
|
|
|
}
|
|
|
|
}
|
2022-08-29 11:08:45 +00:00
|
|
|
|
2022-11-07 13:34:28 +00:00
|
|
|
pStack->pSourceBufer = AuSPtr<AuByteBuffer>(pWork, AuStaticCast<IOPipeWork>(pWork)->GetBuffer());
|
2022-08-29 11:08:45 +00:00
|
|
|
return pStack;
|
|
|
|
}
|
2023-09-21 07:02:02 +00:00
|
|
|
|
|
|
|
static AuSPtr<IProtocolStack> _NewProtocolStackFromStream(const ProtocolStackByBufferedAdhoc &data,
|
|
|
|
ProtocolStackExtraData ex)
|
|
|
|
{
|
|
|
|
auto pStack = AuMakeShared<ProtocolStack>();
|
|
|
|
if (!pStack)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data.uBufferedStreamLength)
|
|
|
|
{
|
|
|
|
SysPushErrorArg();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
pStack->eType = EProtocolStackCreateType::eByBufferedAdhocStream;
|
|
|
|
|
|
|
|
auto pBuffer = AuMakeShared<AuByteBuffer>(data.uBufferedStreamLength, true);
|
|
|
|
if (!pBuffer)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pBuffer->IsValid())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ex.bKillPipeOnFirstRootLevelFalse)
|
|
|
|
{
|
|
|
|
pStack->bKillPipeOnFirstRootLevelFalse = ex.bKillPipeOnFirstRootLevelFalse.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
pStack->bOwnsSource = true;
|
|
|
|
pStack->pSourceBufer = pBuffer;
|
|
|
|
pStack->pStreamReaderAsInputSource = data.pStreamReader;
|
|
|
|
pStack->uPreferredFragmentSize = data.uPreferredFragmentSize;
|
|
|
|
|
|
|
|
pStack->bStreamReaderFlagAdhocPump = ex.bStreamReaderFlagAdhocPump;
|
|
|
|
pStack->bStreamReaderFlagAdhocRead = ex.bStreamReaderFlagAdhocReadFromAdhoc;
|
|
|
|
|
|
|
|
return pStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IProtocolStack> NewProtocolStackFromPipe(const AuSPtr<IIOPipeWork> &pWork,
|
|
|
|
bool bAutoTick,
|
|
|
|
bool bKillPipeOnFirstRootLevelFalse)
|
|
|
|
{
|
|
|
|
return _NewProtocolStackFromPipe(pWork, bAutoTick, bKillPipeOnFirstRootLevelFalse, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IProtocolStack> NewProtocolStackFromDescription(const ProtocolStackDescription &description)
|
|
|
|
{
|
|
|
|
if (!EProtocolStackCreateTypeIsValid(description.type))
|
|
|
|
{
|
|
|
|
SysPushErrorArg();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (description.type)
|
|
|
|
{
|
|
|
|
case EProtocolStackCreateType::eByPipe:
|
|
|
|
return _NewProtocolStackFromPipe(description.byPipe.pWork, description.byPipe.bAutoTick, description.byPipe.bKillPipeOnFirstRootLevelFalse, description.extendedFlags);
|
|
|
|
case EProtocolStackCreateType::eByBuffered:
|
|
|
|
return _NewBufferedProtocolStack(description.byBuffered.uBufferedStreamLength, description.extendedFlags);
|
|
|
|
case EProtocolStackCreateType::eByBufferedAdhocStream:
|
|
|
|
return _NewProtocolStackFromStream(description.byBufferedStream, description.extendedFlags);
|
|
|
|
default:
|
|
|
|
SysUnreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IStreamReader> UtilityWrapStreamReaderWithInterceptors(AuSPtr<IStreamReader> pStreamReader,
|
|
|
|
AuList<AuTuple<bool /* bSingleFrame*/, AuUInt /*nextBufferSize*/, AuSPtr<IProtocolInterceptorEx>>> pairs,
|
|
|
|
AuUInt uBaseStreamLength,
|
|
|
|
AuUInt uPreferredFragmentSize,
|
|
|
|
AuSPtr<IProtocolStack> *pOptOutStack)
|
|
|
|
{
|
|
|
|
ProtocolStackDescription desc;
|
|
|
|
desc.type = EProtocolStackCreateType::eByBufferedAdhocStream;
|
|
|
|
desc.byBufferedStream.pStreamReader = pStreamReader;
|
|
|
|
desc.byBufferedStream.uBufferedStreamLength = uBaseStreamLength;
|
|
|
|
desc.byBufferedStream.uPreferredFragmentSize = uPreferredFragmentSize;
|
|
|
|
desc.extendedFlags.bStreamReaderFlagAdhocReadFromAdhoc = true;
|
|
|
|
AuSPtr<IProtocolStack> pStack;
|
|
|
|
|
|
|
|
if (!(pStack = NewProtocolStackFromDescription(desc)))
|
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (AU_ITERATE_N(i, pairs.size()))
|
|
|
|
{
|
|
|
|
const auto &[bSingle, uNextBufferSize, pPointer] = pairs[i];
|
|
|
|
|
|
|
|
if (bSingle)
|
|
|
|
{
|
2023-11-30 09:39:56 +00:00
|
|
|
if (!pStack->AddSingleFrameProcessorEx(EProtocolWhere::eAppend, pPointer, uNextBufferSize))
|
2023-09-21 07:02:02 +00:00
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (i == pairs.size() - 1)
|
|
|
|
{
|
|
|
|
if (!pStack->AddEndInterceptor(pPointer))
|
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-30 09:39:56 +00:00
|
|
|
if (!pStack->AddInterceptorEx(EProtocolWhere::eAppend, pPointer, uNextBufferSize))
|
2023-09-21 07:02:02 +00:00
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pOptOutStack)
|
|
|
|
{
|
|
|
|
*pOptOutStack = pStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pStack->AsStreamReader();
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|