AuroraRuntime/Source/IO/IPC/AuIPCPipe.Unix.hpp
2023-10-21 10:43:28 +01:00

121 lines
3.5 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuIPCPipe.Unix.hpp
Date: 2022-4-14
Author: Reece
***/
#pragma once
#include <Source/IO/Loop/ILoopSourceEx.hpp>
#include <Source/IO/Loop/LSHandle.hpp>
#include <Source/IO/Loop/LSEvent.hpp>
#if defined(AURORA_IS_LINUX_DERIVED)
#include <Source/IO/UNIX/IOSubmit.Linux.hpp>
#include <Source/IO/FS/Async.Linux.hpp>
#include "AuIPCPrimitives.Linux.hpp"
#include "AuIPCMutexFutex.Linux.hpp"
#endif
#include "AuIPCMemory.Unix.hpp" // required for handle
#include "AuIPCPipeWriter.hpp"
#include "AuIPCPipeReader.hpp"
namespace Aurora::IO::IPC
{
struct IPCPipeImpl : IPCPipe, Loop::LSHandle, AuEnableSharedFromThis<IPCPipeImpl>
#if defined(AURORA_IS_LINUX_DERIVED)
, IMutexClosedHook
#endif
{
IPCPipeImpl(int (fds)[2], int (secondary)[2], IPCToken readEnd, IPCToken writeEnd, AuSPtr<IPCEvent> event, AuSPtr<IPCMutex> mutex);
~IPCPipeImpl();
#if defined(AURORA_IS_LINUX_DERIVED)
inline virtual void OnPresleep() override
{
Loop::LSHandle::OnPresleep();
};
inline virtual void OnFinishSleep() override
{
Loop::LSHandle:: OnFinishSleep();
}
inline virtual AuUInt GetHandle() override
{
return Loop::LSHandle:: GetHandle();
}
inline bool HasValidHandle()
{
return Loop::LSHandle:: HasValidHandle();
}
#else
PROXY_INTERNAL_INTERFACE_(LSHandle::)
#endif
virtual AuSPtr<IO::IAsyncTransaction> NewAsyncTransaction() override;
virtual AuSPtr<IO::IIOHandle> GetCurrentSharedDuplexHandles() override;
virtual AuSPtr<Loop::ILoopSource> AsReadChannelIsOpen() override;
virtual AuSPtr<Loop::ILoopSource> AsReadChannelHasData() override;
virtual bool Read(const Memory::MemoryViewStreamWrite &write, bool nonblock) override;
virtual bool Write(const Memory::MemoryViewStreamRead &read) override;
virtual AuString ExportToString() override;
virtual AuSPtr<IO::FS::IFileStream> ToFileStream() override;
virtual AuSPtr<IStreamReader> ToStreamReader() override;
virtual AuSPtr<IStreamWriter> ToStreamWriter() override;
bool ReadEx(const Memory::MemoryViewStreamWrite &write, bool nonblock, bool bSide);
bool IsSignaled() override;
bool WaitOn(AuUInt32 timeout) override;
Loop::ELoopSource GetType() override;
#if defined(AURORA_IS_LINUX_DERIVED)
bool OnClosed() override;
virtual const AuList<AuUInt> &GetHandles() override;
virtual bool Singular() override;
virtual bool OnTrigger(AuUInt handle) override;
#endif
bool LIOS_PopOne();
void DrainOtherFd();
AuUInt GetPreemptFd();
bool bIsSendingZero {};
private:
int fds[2] {-1, -1};
int secondary[2] {-1, -1};
AuSPtr<IO::IIOHandle> fsHandle_;
AuSPtr<IO::FS::LinuxAsyncFileStream> fsStream_;
void SendTerminateSignal();
AuList<AuUInt> handles;
IPCToken readEnd_;
IPCToken writeEnd_;
AuSPtr<IPCEvent> event_;
AuSPtr<IPCMutex> mutex_;
AuLoop::LSEvent eventPreempt_;
bool bHasDied {};
AuSPtr<IO::FS::IFileStream> fsBlockingStream_;
IPCPipeReader pipeReader_;
IPCPipeWriter pipeWriter_;
void FinishFinalize();
};
}