From a4f6db7ec9199f6313299277655caf6197e4de9b Mon Sep 17 00:00:00 2001 From: J Reece Wilson Date: Sat, 16 Sep 2023 23:30:10 +0100 Subject: [PATCH] [*] Linux IPC reliability: finally got around to closing #48 --- Source/IO/IPC/AuIPCPipe.Unix.cpp | 45 +++++++++++++++++++++++++++++--- Source/IO/IPC/AuIPCPipe.Unix.hpp | 2 ++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Source/IO/IPC/AuIPCPipe.Unix.cpp b/Source/IO/IPC/AuIPCPipe.Unix.cpp index 5fc00c31..181b3a00 100644 --- a/Source/IO/IPC/AuIPCPipe.Unix.cpp +++ b/Source/IO/IPC/AuIPCPipe.Unix.cpp @@ -60,7 +60,40 @@ namespace Aurora::IO::IPC void IPCPipeImpl::DrainOtherFd() { - // TODO (Reece): urgent + #if defined(FIONREAD) + + int available {}; + if (::ioctl(fds[1], FIONREAD, &available) < 0) + { + return; + } + + if (!available) + { + return; + } + + AuByteBuffer temp(available); + SysAssert(temp); + + AuUInt uCounter {}; + while (this->ReadEx({temp, uCounter}, true, false)) + { + temp.writePtr = temp.readPtr = temp.base; + } + + #else + + AuByteBuffer temp(4096 * 10); + SysAssert(temp); + + AuUInt uCounter {}; + while (this->ReadEx({temp, uCounter}, true, false)) + { + temp.writePtr = temp.readPtr = temp.base; + } + + #endif } AuUInt IPCPipeImpl::GetPreemptFd() @@ -77,8 +110,7 @@ namespace Aurora::IO::IPC #if defined(FIONREAD) int available {}; - int res = ::ioctl(fds[0], FIONREAD, &available) ; - if (res< 0) + if (::ioctl(fds[0], FIONREAD, &available) < 0) { return false; } @@ -162,7 +194,12 @@ namespace Aurora::IO::IPC bool IPCPipeImpl::Read(const Memory::MemoryViewStreamWrite &write, bool nonblock) { - auto handle = fds[0]; + return this->ReadEx(write, nonblock, true); + } + + bool IPCPipeImpl::ReadEx(const Memory::MemoryViewStreamWrite &write, bool nonblock, bool bSide) + { + auto handle = bSide ? fds[0] : fds[1]; auto control = ::fcntl(handle, F_GETFL); auto ref = control; diff --git a/Source/IO/IPC/AuIPCPipe.Unix.hpp b/Source/IO/IPC/AuIPCPipe.Unix.hpp index b60fe671..0290fcb5 100644 --- a/Source/IO/IPC/AuIPCPipe.Unix.hpp +++ b/Source/IO/IPC/AuIPCPipe.Unix.hpp @@ -62,6 +62,8 @@ namespace Aurora::IO::IPC virtual bool Write(const Memory::MemoryViewStreamRead &read) override; virtual AuString ExportToString() override; + bool ReadEx(const Memory::MemoryViewStreamWrite &write, bool nonblock, bool bSide); + bool IsSignaled() override; bool WaitOn(AuUInt32 timeout) override; Loop::ELoopSource GetType() override;