From b424a3b434c000b4235706b1165d54966c9d44b8 Mon Sep 17 00:00:00 2001 From: Reece Date: Sun, 17 Apr 2022 14:21:21 +0100 Subject: [PATCH] [+] NT IPC memory --- Source/IPC/IPCMemory.NT.cpp | 144 +++++++++++++++++++++++++++++++++++- Source/IPC/IPCPipe.NT.cpp | 1 - 2 files changed, 140 insertions(+), 5 deletions(-) diff --git a/Source/IPC/IPCMemory.NT.cpp b/Source/IPC/IPCMemory.NT.cpp index 900afb0d..b9f858c4 100644 --- a/Source/IPC/IPCMemory.NT.cpp +++ b/Source/IPC/IPCMemory.NT.cpp @@ -10,17 +10,153 @@ #include "IPCHandle.hpp" #include "IPCMemory.NT.hpp" +#include +#include + namespace Aurora::IPC { + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // Shared memory + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + struct IPCSharedMemoryImpl : IPCSharedMemory + { + IPCSharedMemoryImpl(HANDLE handle, void *ptr, const IPC::IPCHandle &ipcHandle); + ~IPCSharedMemoryImpl(); + + virtual Memory::MemoryViewWrite GetMemory() override; + virtual AuUInt GetLength() override; + + virtual AuString ExportToString() override; + + private: + IPC::IPCHandle ipcHandle_; + bool owns_; + HANDLE handle_{}; + void *base_{}; + AuUInt len_ {}; + }; + + IPCSharedMemoryImpl::IPCSharedMemoryImpl(HANDLE handle, void *ptr, const IPC::IPCHandle &ipcHandle) : + base_(ptr), len_(ipcHandle.word), handle_(handle), ipcHandle_(ipcHandle) + { + + } + + IPCSharedMemoryImpl::~IPCSharedMemoryImpl() + { + if (this->base_) + { + ::UnmapViewOfFile(this->base_); + } + + AuWin32CloseHandle(this->handle_); + } + + Memory::MemoryViewWrite IPCSharedMemoryImpl::GetMemory() + { + return AuMemoryViewWrite(this->base_, this->len_); + } + + AuUInt IPCSharedMemoryImpl::GetLength() + { + return this->len_; + } + + AuString IPCSharedMemoryImpl::ExportToString() + { + return this->ipcHandle_.ToString(); + } + AUKN_SYM AuSPtr NewSharedMemory(AuUInt length) { - SysPushErrorUnimplemented(); - return {}; + IPC::IPCHandle handle; + handle.NewId(length); + + auto path = handle.ToNTPath(); + auto file = CreateFileMappingA(INVALID_HANDLE_VALUE, + nullptr, + PAGE_READWRITE, + #if defined(AURORA_IS_64BIT) + AuBitsToHigher(length), + AuBitsToLower(length), + #else + 0, + length, + #endif + path.c_str()); + + if (file == INVALID_HANDLE_VALUE) + { + return {}; + } + + auto map = MapViewOfFile(file, + FILE_MAP_ALL_ACCESS, + 0, + 0, + length); + if (!map) + { + SysPushErrorIO(); + AuWin32CloseHandle(file); + return {}; + } + + auto object = AuMakeShared(file, map, handle); + if (!object) + { + SysPushErrorMem(); + ::UnmapViewOfFile(map); + AuWin32CloseHandle(file); + return {}; + } + + return object; } AUKN_SYM AuSPtr ImportSharedMemory(const AuString &handleString) { - SysPushErrorUnimplemented(); - return {}; + IPC::IPCHandle handle; + + if (!handle.FromString(handleString)) + { + SysPushErrorParseError(); + return {}; + } + + auto length = handle.word; + auto path = handle.ToNTPath(); + auto file = OpenFileMappingA(FILE_MAP_ALL_ACCESS, + FALSE, + path.c_str()); + + if (file == INVALID_HANDLE_VALUE) + { + return {}; + } + + auto map = MapViewOfFile(file, + FILE_MAP_ALL_ACCESS, + 0, + 0, + length); + if (!map) + { + SysPushErrorIO(); + AuWin32CloseHandle(file); + return {}; + } + + auto object = AuMakeShared(file, map, handle); + if (!object) + { + SysPushErrorMem(); + ::UnmapViewOfFile(map); + AuWin32CloseHandle(file); + return {}; + } + + return object; } } \ No newline at end of file diff --git a/Source/IPC/IPCPipe.NT.cpp b/Source/IPC/IPCPipe.NT.cpp index 2d703745..8f834a0d 100644 --- a/Source/IPC/IPCPipe.NT.cpp +++ b/Source/IPC/IPCPipe.NT.cpp @@ -70,7 +70,6 @@ namespace Aurora::IPC bool bFirstTime {true}; }; - IPCHasConnectionEvent::IPCHasConnectionEvent(AuSPtr parent) : parent_(parent), LSEvent(false, false, true) {