diff --git a/Include/Aurora/IO/Net/INetSrvSockets.hpp b/Include/Aurora/IO/Net/INetSrvSockets.hpp index dd5726bf..31d320aa 100644 --- a/Include/Aurora/IO/Net/INetSrvSockets.hpp +++ b/Include/Aurora/IO/Net/INetSrvSockets.hpp @@ -15,6 +15,7 @@ namespace Aurora::IO::Net { NetEndpoint endpoint; AuSPtr pDriver; + AuUInt32 uMaxConnectTimeMs {}; }; struct NetSocketConnectMany diff --git a/Include/Aurora/IO/Protocol/IProtocolStack.hpp b/Include/Aurora/IO/Protocol/IProtocolStack.hpp index bb3538a4..8b5ba267 100644 --- a/Include/Aurora/IO/Protocol/IProtocolStack.hpp +++ b/Include/Aurora/IO/Protocol/IProtocolStack.hpp @@ -11,11 +11,39 @@ namespace Aurora::IO::Protocol { struct IProtocolStack : IProtocolBaseReader, IProtocolBaseWriter { + /** + * @brief + * @param pInterceptor + * @param uOutputBufferSize When 0, a hopefully not stupid default is used. + * @return + */ virtual bool AddInterceptor(const AuSPtr &pInterceptor, AuUInt uOutputBufferSize) = 0; + + /** + * @brief + * @param pInterceptor + * @param uOutputBufferSize When 0, a hopefully not stupid default is used. + * @return + */ virtual bool AddInterceptorEx(const AuSPtr &pInterceptor, AuUInt uOutputBufferSize) = 0; + /** + * @brief + * @param pInterceptor + * @return + */ + virtual bool AddEndInterceptor(const AuSPtr &pInterceptor) = 0; + + /** + * @brief Sends one down the protocol stack, regardless of how much data is written into the + * next piece/interceptor, and regardless of if another read tick is required. + * Latterly, you are responsible for consuming all available bytes in your interceptor. + */ virtual void DoTick() = 0; + /** + * @brief + */ virtual void Destroy() = 0; }; diff --git a/Include/Aurora/IO/TLS/ITLSContext.hpp b/Include/Aurora/IO/TLS/ITLSContext.hpp index 68cc03a4..47426022 100644 --- a/Include/Aurora/IO/TLS/ITLSContext.hpp +++ b/Include/Aurora/IO/TLS/ITLSContext.hpp @@ -38,7 +38,7 @@ namespace Aurora::IO::TLS /** * @brief */ - int iServerBacMacLimit { 0 }; + int iServerBadMacLimit { 0 }; /** * @brief diff --git a/Source/IO/Protocol/ProtocolStack.cpp b/Source/IO/Protocol/ProtocolStack.cpp index 259cc29e..6262f4df 100644 --- a/Source/IO/Protocol/ProtocolStack.cpp +++ b/Source/IO/Protocol/ProtocolStack.cpp @@ -20,6 +20,11 @@ namespace Aurora::IO::Protocol bool ProtocolStack::AddInterceptor(const AuSPtr &pInterceptor, AuUInt uOutputBufferSize) { + if (this->bWrittenEnd) + { + return false; + } + if (!uOutputBufferSize) { uOutputBufferSize = 64 * 1024; @@ -107,6 +112,11 @@ namespace Aurora::IO::Protocol bool ProtocolStack::AddInterceptorEx(const AuSPtr &pInterceptor, AuUInt uOutputBufferSize) { + if (this->bWrittenEnd) + { + return false; + } + if (!this->pSourceBufer) { return false; @@ -197,6 +207,93 @@ namespace Aurora::IO::Protocol return true; } + bool ProtocolStack::AddEndInterceptor(const AuSPtr &pInterceptor) + { + if (this->bWrittenEnd) + { + return false; + } + + if (!this->pSourceBufer) + { + return false; + } + + auto pNew = AuMakeShared(); + if (!pNew) + { + SysPushErrorNet("Out of memory"); + return false; + } + + pNew->outputBuffer = {}; + pNew->pOuputWriter = {}; + + struct StreamWrapper : IStreamWriter, IProtocolNext, AuEnableSharedFromThis + { + ProtocolStack *pStack; + AuWPtr pInterceptor; + AuWPtr pParent; + + EStreamError IsOpen() override + { + return EStreamError::eErrorNone; + } + + EStreamError Write(const Memory::MemoryViewStreamRead ¶meters) override + { + return pStack->DoTick(AuMakeShared(parameters), pParent.lock()) ? + EStreamError::eErrorNone : + EStreamError::eErrorStreamInterrupted; + } + + void Close() override + { + + }; + + void Flush() override + { + + }; + + virtual AuSPtr GetOutputBuffer() override + { + return {}; + } + + virtual AuSPtr GetStreamWriter() override + { + return AuSharedFromThis(); + } + }; + + auto pWrapper = AuMakeShared(); + if (!pWrapper) + { + return false; + } + + pWrapper->pInterceptor = pInterceptor; + pWrapper->pStack = this; + pWrapper->pParent = pNew; + + pNew->pWriteInteface = pWrapper; + pNew->pInterceptorEx = pInterceptor; + pNew->pParent = this; + + this->pTopPiece = pNew; + + if (!this->pBottomPiece) + { + this->pBottomPiece = pNew; + } + + this->bWrittenEnd = true; + + return true; + } + void ProtocolStack::Destroy() { if (this->bOwnsSource) @@ -246,7 +343,13 @@ namespace Aurora::IO::Protocol AuSPtr ProtocolStack::AsStreamReader() { - return AuMakeShared(AsReadableByteBuffer()); + auto pBuffer = AsReadableByteBuffer(); + if (!pBuffer) + { + return {}; + } + + return AuMakeShared(pBuffer); } AuSPtr ProtocolStack::AsReadableByteBuffer() @@ -256,6 +359,11 @@ namespace Aurora::IO::Protocol return this->pSourceBufer; } + if (this->pTopPiece->outputBuffer.IsEmpty()) + { + return {}; + } + return AuSPtr(this->pTopPiece, &this->pTopPiece->outputBuffer); } @@ -280,20 +388,26 @@ namespace Aurora::IO::Protocol if (pCurrent->pInterceptorEx) { - auto pNextStream = ((pPiece == this->pTopPiece) && - (this->pDrainBuffer)) ? - this->pDrainBuffer : - AuSPtr(pCurrent, &pCurrent->outputBuffer); + auto pNextStream = ((pPiece == this->pTopPiece) && (this->pDrainBuffer)) ? + (this->pDrainBuffer) : + (!pCurrent->outputBuffer.IsEmpty() ? AuSPtr(pCurrent, &pCurrent->outputBuffer) : AuSPtr {}); - - auto pOldHead = pNextStream->readPtr; + auto pOldHead = pNextStream ? pNextStream->readPtr : nullptr; if (!pCurrent->pInterceptorEx->OnDataAvailable(pRead, pNextStream)) { - pNextStream->readPtr = pOldHead; + if (pNextStream) + { + pNextStream->readPtr = pOldHead; + } return false; } + if (!pNextStream) + { + return true; + } + if (auto pNext = pCurrent->pNext) { auto pOldHead = pNextStream->readPtr; diff --git a/Source/IO/Protocol/ProtocolStack.hpp b/Source/IO/Protocol/ProtocolStack.hpp index 099975de..5637fc3e 100644 --- a/Source/IO/Protocol/ProtocolStack.hpp +++ b/Source/IO/Protocol/ProtocolStack.hpp @@ -20,6 +20,7 @@ namespace Aurora::IO::Protocol bool AddInterceptor(const AuSPtr &pInterceptor, AuUInt uOutputBufferSize) override; bool AddInterceptorEx(const AuSPtr &pInterceptor, AuUInt uOutputBufferSize) override; + bool AddEndInterceptor(const AuSPtr &pInterceptor) override; void Destroy() override; @@ -36,5 +37,7 @@ namespace Aurora::IO::Protocol AuSPtr pBottomPiece; AuSPtr pTopPiece; + + bool bWrittenEnd {}; }; } \ No newline at end of file diff --git a/Source/IO/TLS/TLSContext.cpp b/Source/IO/TLS/TLSContext.cpp index bfc8e7fa..35d11459 100644 --- a/Source/IO/TLS/TLSContext.cpp +++ b/Source/IO/TLS/TLSContext.cpp @@ -284,7 +284,7 @@ namespace Aurora::IO::TLS #endif ::mbedtls_ssl_conf_dtls_badmac_limit(&this->conf, - this->meta_.dtls.iServerBacMacLimit); + this->meta_.dtls.iServerBadMacLimit); }