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

154 lines
4.2 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.
File: IPCMemory.NT.cpp
Date: 2022-4-15
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "IPC.hpp"
#include "IPCHandle.hpp"
#include "IPCMemory.NT.hpp"
namespace Aurora::IO::IPC
2022-04-16 15:42:48 +00:00
{
2022-04-17 13:21:21 +00:00
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Shared memory
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IPCSharedMemoryImpl::IPCSharedMemoryImpl(HANDLE handle, void *ptr, const IPC::IPCHandle &ipcHandle) :
2022-07-21 06:18:53 +00:00
base_(ptr), len_(ipcHandle.values[0].token.word), handle_(handle), ipcHandle_(ipcHandle)
2022-04-17 13:21:21 +00:00
{
}
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();
}
2022-04-16 17:40:36 +00:00
AUKN_SYM AuSPtr<IPCSharedMemory> NewSharedMemory(AuUInt length)
{
2022-04-17 13:21:21 +00:00
IPC::IPCHandle handle;
2022-07-21 06:18:53 +00:00
IPC::IPCToken token;
token.NewId();
token.word = length;
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)
AuBitsToHigher(length),
AuBitsToLower(length),
#else
0,
length,
#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,
length);
2022-04-17 13:21:21 +00:00
if (!map)
{
SysPushErrorIO();
AuWin32CloseHandle(file);
return {};
}
auto object = AuMakeShared<IPCSharedMemoryImpl>(file, map, handle);
if (!object)
{
SysPushErrorMem();
::UnmapViewOfFile(map);
AuWin32CloseHandle(file);
return {};
}
return object;
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 {};
}
auto length = token->token.word;
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,
length);
2022-04-17 13:21:21 +00:00
if (!map)
{
SysPushErrorIO();
AuWin32CloseHandle(file);
return {};
}
auto object = AuMakeShared<IPCSharedMemoryImpl>(file, map, handle);
if (!object)
{
SysPushErrorMem();
::UnmapViewOfFile(map);
AuWin32CloseHandle(file);
return {};
}
return object;
2022-04-16 17:40:36 +00:00
}
2022-04-16 15:42:48 +00:00
}