AuroraRuntime/Source/IO/Protocol/AuProtocolStackSpecialReader.cpp
Jamie Reece Wilson 994fcb5010 [*/+] Added/Moved code to AuProtocolStackSpecialWriter[.cpp]
[+] Further mutex guarantees under protocol stacks (hardening)
2023-09-21 21:18:59 +01:00

85 lines
2.2 KiB
C++

/***
Copyright (C) 2022-2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuProtocolStackSpecialReader.cpp
File: AuProtocolStack.cpp
Date: 2022-8-24
Date: 2023-09-21
Author: Reece
***/
#include "Protocol.hpp"
#include "AuProtocolStack.hpp"
#include "AuProtocolStackSpecialReader.hpp"
namespace Aurora::IO::Protocol
{
SpecialReader::SpecialReader(ProtocolStack *pParent) :
pParent(pParent)
{ }
EStreamError SpecialReader::IsOpen()
{
auto pStack = this->pParent;
if (pStack->bDead)
{
return EStreamError::eErrorHandleClosed;
}
auto pStackBuffer = pStack->AsReadableByteBuffer();
if (!pStackBuffer)
{
return EStreamError::eErrorStreamNotOpen;
}
else
{
return EStreamError::eErrorNone;
}
}
EStreamError SpecialReader::Read(const Memory::MemoryViewStreamWrite &parameters)
{
auto pStack = this->pParent;
auto pStackBuffer = pStack->AsReadableByteBuffer();
if (pStack->bDead)
{
return EStreamError::eErrorHandleClosed;
}
AU_LOCK_GUARD(pStack->mutex);
if (!pStackBuffer)
{
return EStreamError::eErrorStreamNotOpen;
}
auto uAvail = pStackBuffer->RemainingBytes();
if (!uAvail)
{
bool bMayAdHocRead = pStack->bStreamReaderFlagAdhocRead &&
pStack->eType == EProtocolStackCreateType::eByBufferedAdhocStream;
bool bMayAdHocPump = pStack->bStreamReaderFlagAdhocPump &&
pStack->pSourceBufer &&
pStack->pSourceBufer->RemainingBytes();
if (bMayAdHocRead || bMayAdHocPump)
{
if (!pStack->DoTickEx(false))
{
return EStreamError::eErrorEndOfStream;
}
}
}
parameters.outVariable = pStackBuffer->Read(parameters.ptr, parameters.length);
return parameters.outVariable == 0 ? EStreamError::eErrorEndOfStream : EStreamError::eErrorNone;
}
void SpecialReader::Close()
{
this->pParent->Terminate();
}
}