AuroraRuntime/Source/IO/IPC/AuIPCMemory.NT.cpp

160 lines
4.4 KiB
C++
Raw Normal View History

2022-04-16 15:42:48 +00:00
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
2022-12-14 05:03:37 +00:00
File: AuIPCMemory.NT.cpp
2022-04-16 15:42:48 +00:00
Date: 2022-4-15
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "IPC.hpp"
2022-12-14 05:03:37 +00:00
#include "AuIPCHandle.hpp"
#include "AuIPCMemory.NT.hpp"
2022-04-16 15:42:48 +00:00
namespace Aurora::IO::IPC
2022-04-16 15:42:48 +00:00
{
2022-04-17 13:21:21 +00:00
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Shared memory
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2022-12-14 05:03:37 +00:00
IPCSharedMemoryImpl::IPCSharedMemoryImpl(HANDLE handle,
void *pBase,
const IPC::IPCHandle &ipcHandle) :
pBase_(pBase),
uLen_(ipcHandle.values[0].token.word),
handle_(handle),
ipcHandle_(ipcHandle)
2022-04-17 13:21:21 +00:00
{
}
IPCSharedMemoryImpl::~IPCSharedMemoryImpl()
{
2022-12-14 05:03:37 +00:00
if (this->pBase_)
2022-04-17 13:21:21 +00:00
{
2022-12-14 05:03:37 +00:00
::UnmapViewOfFile(this->pBase_);
this->pBase_ = nullptr;
2022-04-17 13:21:21 +00:00
}
AuWin32CloseHandle(this->handle_);
}
Memory::MemoryViewWrite IPCSharedMemoryImpl::GetMemory()
{
2022-12-14 05:03:37 +00:00
return AuMemoryViewWrite(this->pBase_, this->uLen_);
2022-04-17 13:21:21 +00:00
}
AuUInt IPCSharedMemoryImpl::GetLength()
{
2022-12-14 05:03:37 +00:00
return this->uLen_;
2022-04-17 13:21:21 +00:00
}
AuString IPCSharedMemoryImpl::ExportToString()
{
return this->ipcHandle_.ToString();
}
2022-12-14 05:03:37 +00:00
AUKN_SYM AuSPtr<IPCSharedMemory> NewSharedMemory(AuUInt uLength)
2022-04-16 17:40:36 +00:00
{
2022-04-17 13:21:21 +00:00
IPC::IPCHandle handle;
2022-07-21 06:18:53 +00:00
IPC::IPCToken token;
token.NewId();
2022-12-14 05:03:37 +00:00
token.word = uLength;
2022-04-17 13:21:21 +00:00
2022-07-21 06:18:53 +00:00
handle.PushId(EIPCHandleType::eIPCMemory, token);
auto path = token.ToNTPath();
2022-04-17 13:21:21 +00:00
auto file = CreateFileMappingA(INVALID_HANDLE_VALUE,
nullptr,
PAGE_READWRITE,
#if defined(AURORA_IS_64BIT)
2022-12-14 05:03:37 +00:00
AuBitsToHigher(uLength),
AuBitsToLower(uLength),
2022-04-17 13:21:21 +00:00
#else
0,
2022-12-14 05:03:37 +00:00
uLength,
2022-04-17 13:21:21 +00:00
#endif
path.c_str());
2022-04-17 14:18:00 +00:00
if ((file == INVALID_HANDLE_VALUE) ||
(!file))
2022-04-17 13:21:21 +00:00
{
return {};
}
auto map = ::MapViewOfFile(file,
FILE_MAP_ALL_ACCESS,
0,
0,
2022-12-14 05:03:37 +00:00
uLength);
2022-04-17 13:21:21 +00:00
if (!map)
{
SysPushErrorIO();
AuWin32CloseHandle(file);
return {};
}
2022-12-14 05:03:37 +00:00
auto pObject = AuMakeShared<IPCSharedMemoryImpl>(file, map, handle);
if (!pObject)
2022-04-17 13:21:21 +00:00
{
SysPushErrorMem();
::UnmapViewOfFile(map);
AuWin32CloseHandle(file);
return {};
}
2022-12-14 05:03:37 +00:00
return pObject;
2022-04-16 17:40:36 +00:00
}
2022-04-16 15:42:48 +00:00
2022-04-16 17:40:36 +00:00
AUKN_SYM AuSPtr<IPCSharedMemory> ImportSharedMemory(const AuString &handleString)
{
2022-04-17 13:21:21 +00:00
IPC::IPCHandle handle;
if (!handle.FromString(handleString))
{
SysPushErrorParseError();
return {};
}
2022-07-21 06:18:53 +00:00
auto token = handle.GetToken(EIPCHandleType::eIPCMemory, 0);
if (!token)
{
SysPushErrorParseError();
return {};
}
2022-12-14 05:03:37 +00:00
auto uLength = token->token.word;
2022-07-21 06:18:53 +00:00
auto path = token->token.ToNTPath();
auto file = ::OpenFileMappingA(FILE_MAP_ALL_ACCESS,
FALSE,
path.c_str());
2022-04-17 13:21:21 +00:00
2022-04-17 14:18:00 +00:00
if ((file == INVALID_HANDLE_VALUE) ||
(!file))
2022-04-17 13:21:21 +00:00
{
return {};
}
auto map = ::MapViewOfFile(file,
FILE_MAP_ALL_ACCESS,
0,
0,
2022-12-14 05:03:37 +00:00
uLength);
2022-04-17 13:21:21 +00:00
if (!map)
{
SysPushErrorIO();
AuWin32CloseHandle(file);
return {};
}
2022-12-14 05:03:37 +00:00
auto pObject = AuMakeShared<IPCSharedMemoryImpl>(file, map, handle);
if (!pObject)
2022-04-17 13:21:21 +00:00
{
SysPushErrorMem();
::UnmapViewOfFile(map);
AuWin32CloseHandle(file);
return {};
}
2022-12-14 05:03:37 +00:00
return pObject;
2022-04-16 17:40:36 +00:00
}
2022-04-16 15:42:48 +00:00
}