[*/+] Added/Moved code to AuProtocolStackSpecialWriter[.cpp]

[+] Further mutex guarantees under protocol stacks (hardening)
This commit is contained in:
Reece Wilson 2023-09-21 21:18:59 +01:00
parent 7da39d2022
commit 994fcb5010
7 changed files with 126 additions and 96 deletions

View File

@ -35,6 +35,8 @@ namespace Aurora::IO::Protocol
return {}; return {};
} }
AU_LOCK_GUARD(this->pParent->mutex);
if (this->pParent->pTopPiece.get() == this) if (this->pParent->pTopPiece.get() == this)
{ {
if (this->pParent->pDrainBuffer) if (this->pParent->pDrainBuffer)
@ -65,6 +67,8 @@ namespace Aurora::IO::Protocol
return false; return false;
} }
AU_LOCK_GUARD(this->pParent->mutex);
if (!this->pNext) if (!this->pNext)
{ {
if (this->pParent->pDrainBuffer) if (this->pParent->pDrainBuffer)
@ -84,6 +88,8 @@ namespace Aurora::IO::Protocol
return; return;
} }
AU_LOCK_GUARD(this->pParent->mutex);
auto &pBottomPiece = this->pParent->pBottomPiece; auto &pBottomPiece = this->pParent->pBottomPiece;
// fix chain // fix chain

View File

@ -14,7 +14,8 @@
namespace Aurora::IO::Protocol namespace Aurora::IO::Protocol
{ {
ProtocolStack::ProtocolStack() : ProtocolStack::ProtocolStack() :
readView(this) readView(this),
writeView(this)
{ {
} }
@ -73,6 +74,8 @@ namespace Aurora::IO::Protocol
const AuSPtr<IProtocolInterceptor> &pInterceptor, const AuSPtr<IProtocolInterceptor> &pInterceptor,
AuUInt uOutputBufferSize) AuUInt uOutputBufferSize)
{ {
AU_LOCK_GUARD(this->mutex);
if (this->bWrittenEnd) if (this->bWrittenEnd)
{ {
return {}; return {};
@ -194,6 +197,8 @@ namespace Aurora::IO::Protocol
AuUInt uOutputBufferSize, AuUInt uOutputBufferSize,
bool bMultipleTick) bool bMultipleTick)
{ {
AU_LOCK_GUARD(this->mutex);
if (this->bWrittenEnd) if (this->bWrittenEnd)
{ {
return {}; return {};
@ -318,6 +323,8 @@ namespace Aurora::IO::Protocol
AuSPtr<IProtocolPiece> ProtocolStack::AddEndInterceptor(const AuSPtr<IProtocolInterceptorEx> &pInterceptor) AuSPtr<IProtocolPiece> ProtocolStack::AddEndInterceptor(const AuSPtr<IProtocolInterceptorEx> &pInterceptor)
{ {
AU_LOCK_GUARD(this->mutex);
if (this->bWrittenEnd) if (this->bWrittenEnd)
{ {
return {}; return {};
@ -415,6 +422,8 @@ namespace Aurora::IO::Protocol
void ProtocolStack::Destroy() void ProtocolStack::Destroy()
{ {
AU_LOCK_GUARD(this->mutex);
if (this->bOwnsSource) if (this->bOwnsSource)
{ {
if (this->pSourceBufer) if (this->pSourceBufer)
@ -447,94 +456,7 @@ namespace Aurora::IO::Protocol
return {}; return {};
} }
if (this->pStreamWriterCache) return AuSPtr<IStreamWriter>(this->SharedFromThis(), &this->writeView);
{
return this->pStreamWriterCache;
}
struct A : AuIO::IStreamWriter
{
AuWPtr<ProtocolStack> wpStack;
A(AuWPtr<ProtocolStack> wpStack) :
wpStack(wpStack)
{
}
inline virtual EStreamError IsOpen() override
{
if (auto pStack = AuTryLockMemoryType(this->wpStack))
{
auto pStackBuffer = pStack->pSourceBufer;//pStack->AsWritableByteBuffer();
if (pStack->bDead)
{
return EStreamError::eErrorHandleClosed;
}
if (!pStackBuffer)
{
return EStreamError::eErrorStreamNotOpen;
}
else
{
return EStreamError::eErrorNone;
}
}
else
{
return EStreamError::eErrorHandleClosed;
}
}
inline virtual EStreamError Write(const Memory::MemoryViewStreamRead &parameters) override
{
if (auto pStack = AuTryLockMemoryType(this->wpStack))
{
auto pStackBuffer = pStack->pSourceBufer; //pStack->AsWritableByteBuffer();
if (pStack->bDead)
{
return EStreamError::eErrorHandleClosed;
}
if (!pStackBuffer)
{
return EStreamError::eErrorStreamNotOpen;
}
else
{
parameters.outVariable = pStackBuffer->Write(parameters.ptr, parameters.length);
return EStreamError::eErrorNone;
}
}
else
{
return EStreamError::eErrorHandleClosed;
}
}
virtual void Close() override
{
if (auto pStack = AuTryLockMemoryType(wpStack))
{
pStack->Terminate();
}
}
virtual void Flush() override
{
if (auto pStack = AuTryLockMemoryType(wpStack))
{
pStack->DoTick();
}
}
};
return this->pStreamWriterCache = AuMakeShared<A>(this->SharedFromThis());
} }
AuSPtr<Memory::ByteBuffer> ProtocolStack::AsWritableByteBuffer() AuSPtr<Memory::ByteBuffer> ProtocolStack::AsWritableByteBuffer()
@ -575,6 +497,8 @@ namespace Aurora::IO::Protocol
void ProtocolStack::Terminate() void ProtocolStack::Terminate()
{ {
AU_LOCK_GUARD(this->mutex);
if (auto pStack = AuTryLockMemoryType(this->pPipeWork)) if (auto pStack = AuTryLockMemoryType(this->pPipeWork))
{ {
(void)pStack->End(); (void)pStack->End();
@ -585,6 +509,8 @@ namespace Aurora::IO::Protocol
bool ProtocolStack::DoTick() bool ProtocolStack::DoTick()
{ {
AU_LOCK_GUARD(this->mutex);
return this->DoTickEx(true); return this->DoTickEx(true);
} }
@ -783,6 +709,7 @@ namespace Aurora::IO::Protocol
AuList<AuSPtr<IProtocolPiece>> ProtocolStack::GetArrayOfInterceptors() AuList<AuSPtr<IProtocolPiece>> ProtocolStack::GetArrayOfInterceptors()
{ {
AU_LOCK_GUARD(this->mutex);
AuList<AuSPtr<IProtocolPiece>> list; AuList<AuSPtr<IProtocolPiece>> list;
auto pItr = this->pBottomPiece; auto pItr = this->pBottomPiece;
@ -798,6 +725,7 @@ namespace Aurora::IO::Protocol
AuSPtr<IProtocolPiece> ProtocolStack::GetInterceptorAtIndex(AuUInt32 uIndex) AuSPtr<IProtocolPiece> ProtocolStack::GetInterceptorAtIndex(AuUInt32 uIndex)
{ {
AU_LOCK_GUARD(this->mutex);
AuUInt32 uCounter {}; AuUInt32 uCounter {};
auto pItr = this->pBottomPiece; auto pItr = this->pBottomPiece;

View File

@ -9,6 +9,7 @@
#include "../AuIOPipeProcessor.hpp" #include "../AuIOPipeProcessor.hpp"
#include "AuProtocolStackSpecialReader.hpp" #include "AuProtocolStackSpecialReader.hpp"
#include "AuProtocolStackSpecialWriter.hpp"
namespace Aurora::IO::Protocol namespace Aurora::IO::Protocol
{ {
@ -80,6 +81,9 @@ namespace Aurora::IO::Protocol
AuUInt uPreferredFragmentSize {}; AuUInt uPreferredFragmentSize {};
SpecialReader readView; SpecialReader readView;
SpecialWriter writeView;
AuRenterableMutex mutex;
void DiscardAllocCaches(); void DiscardAllocCaches();
}; };

View File

@ -9,11 +9,14 @@
***/ ***/
#include "Protocol.hpp" #include "Protocol.hpp"
#include "AuProtocolStack.hpp" #include "AuProtocolStack.hpp"
#include "AuProtocolPiece.hpp"
#include "AuProtocolStackSpecialReader.hpp" #include "AuProtocolStackSpecialReader.hpp"
namespace Aurora::IO::Protocol namespace Aurora::IO::Protocol
{ {
SpecialReader::SpecialReader(ProtocolStack *pParent) :
pParent(pParent)
{ }
EStreamError SpecialReader::IsOpen() EStreamError SpecialReader::IsOpen()
{ {
auto pStack = this->pParent; auto pStack = this->pParent;
@ -45,6 +48,8 @@ namespace Aurora::IO::Protocol
return EStreamError::eErrorHandleClosed; return EStreamError::eErrorHandleClosed;
} }
AU_LOCK_GUARD(pStack->mutex);
if (!pStackBuffer) if (!pStackBuffer)
{ {
return EStreamError::eErrorStreamNotOpen; return EStreamError::eErrorStreamNotOpen;

View File

@ -15,12 +15,7 @@ namespace Aurora::IO::Protocol
{ {
ProtocolStack *pParent {}; ProtocolStack *pParent {};
inline SpecialReader() SpecialReader(ProtocolStack *pParent);
{ }
inline SpecialReader(ProtocolStack *pParent) : pParent(pParent)
{ }
inline ~SpecialReader()
{ }
EStreamError IsOpen() override; EStreamError IsOpen() override;

View File

@ -0,0 +1,64 @@
/***
Copyright (C) 2022-2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuProtocolStackSpecialWriter.cpp
File: AuProtocolStack.cpp
Date: 2022-8-24
Date: 2023-09-21
Author: Reece
***/
#include "Protocol.hpp"
#include "AuProtocolStack.hpp"
#include "AuProtocolStackSpecialWriter.hpp"
namespace Aurora::IO::Protocol
{
SpecialWriter::SpecialWriter(ProtocolStack *pParent) :
pParent(pParent)
{ }
EStreamError SpecialWriter::IsOpen()
{
auto pStack = this->pParent;
if (pStack->bDead)
{
return EStreamError::eErrorHandleClosed;
}
if (!pStack->pSourceBufer)
{
return EStreamError::eErrorStreamNotOpen;
}
return EStreamError::eErrorNone;
}
EStreamError SpecialWriter::Write(const Memory::MemoryViewStreamRead &parameters)
{
auto pStack = this->pParent;
if (pStack->bDead)
{
return EStreamError::eErrorHandleClosed;
}
auto pStackBuffer = pStack->pSourceBufer; //pStack->AsWritableByteBuffer();
if (!pStackBuffer)
{
return EStreamError::eErrorStreamNotOpen;
}
parameters.outVariable = pStackBuffer->Write(parameters.ptr, parameters.length);
return EStreamError::eErrorNone;
}
void SpecialWriter::Close()
{
this->pParent->Terminate();
}
void SpecialWriter::Flush()
{
this->pParent->DoTick();
}
}

View File

@ -0,0 +1,28 @@
/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuProtocolStackSpecialWriter.hpp
Date: 2023-09-21
Author: Reece
***/
#pragma once
namespace Aurora::IO::Protocol
{
struct ProtocolStack;
struct SpecialWriter : IStreamWriter
{
ProtocolStack *pParent {};
SpecialWriter(ProtocolStack *pParent);
EStreamError IsOpen() override;
EStreamError Write(const Memory::MemoryViewStreamRead &parameters) override;
void Flush() override;
void Close() override;
};
}