Last active
July 12, 2020 10:35
-
-
Save yuyoyuppe/3748b8504f16388245047c3ce3ad39a3 to your computer and use it in GitHub Desktop.
A simple wrapper class for working with a spin-lock protected shared memory on Windows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SerializedSharedMemory.h" | |
class HandleTransferChannel | |
{ | |
std::optional<HANDLE> _handle; | |
const DWORD _destination_pid; | |
public: | |
HandleTransferChannel(const DWORD destination_pid) noexcept | |
:_destination_pid{destination_pid} | |
{ | |
} | |
std::optional<wil::unique_handle> try_extract() noexcept | |
{ | |
std::optional<HANDLE> extracted; | |
_handle.swap(extracted); | |
if(!extracted.has_value()) | |
{ | |
return std::nullopt; | |
} | |
return wil::unique_handle {*extracted}; | |
} | |
bool try_send(const HANDLE handle) noexcept | |
{ | |
HANDLE destination_process_handle = OpenProcess(PROCESS_DUP_HANDLE, false, _destination_pid); | |
if(!destination_process_handle) | |
{ | |
return false; | |
} | |
HANDLE target_handle = INVALID_HANDLE_VALUE; | |
if(!DuplicateHandle( | |
GetCurrentProcess(), | |
handle, | |
&destination_process_handle, | |
&target_handle, | |
0, | |
true, | |
DUPLICATE_SAME_ACCESS)) | |
{ | |
return false; | |
} | |
_handle.emplace(target_handle); | |
return true; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SerializedSharedMemory.h" | |
#include <iostream> | |
#include <atomic> | |
std::atomic_bool ShouldExit = false; | |
BOOL WINAPI consoleHandler(DWORD signal) | |
{ | |
if(signal == CTRL_C_EVENT) | |
ShouldExit = true; | |
return TRUE; | |
} | |
struct Settings | |
{ | |
std::optional<size_t> _pending_overlay_image_size; | |
}; | |
int main(int argc, char **) | |
{ | |
const bool iAmServer = argc == 1; | |
SetConsoleCtrlHandler(consoleHandler, TRUE); | |
const wchar_t shMemSettingsName[] = L"SettingsSharedMemory"; | |
const wchar_t shMemOverlayImageName[] = L"OverlayImageMemory"; | |
if(iAmServer) | |
{ | |
std::cout << "server mode!\n"; | |
auto settings = SerializedSharedMemory::create(shMemSettingsName, sizeof(Settings), false); | |
if(!settings) | |
{ | |
std::cout << "couldn't create shared memory!\n"; | |
return 1; | |
} | |
settings->access([](auto memory) { new(memory.data()) Settings{}; }); | |
std::optional<SerializedSharedMemory> image; | |
do | |
{ | |
settings->access([&](auto memory) mutable { | |
auto settings = reinterpret_cast<Settings *>(memory.data()); | |
if(!settings->_pending_overlay_image_size.has_value()) | |
{ | |
return; | |
} | |
image.reset(); | |
image = SerializedSharedMemory::open(shMemOverlayImageName, *settings->_pending_overlay_image_size, true); | |
settings->_pending_overlay_image_size.reset(); | |
if(!image.has_value()) | |
{ | |
std::cout << "couldn't open pending image: " << std::system_category().message(GetLastError()) << '\n'; | |
} | |
}); | |
if(!image.has_value()) | |
{ | |
Sleep(50); | |
continue; | |
} | |
std::cout << "reading file contents:\n"; | |
image->access([](auto memory) { | |
for(const auto byte : memory) | |
{ | |
std::cout << byte; | |
} | |
}); | |
std::cout << std::endl; | |
image.reset(); | |
Sleep(1000); | |
} while(!ShouldExit); | |
} | |
else | |
{ | |
std::cout << "client mode!\n"; | |
auto settings = SerializedSharedMemory::open(shMemSettingsName, sizeof(Settings), false); | |
if(!settings) | |
{ | |
std::cout << "couldn't open shared memory!\n"; | |
return 1; | |
} | |
std::optional<SerializedSharedMemory> image; | |
do | |
{ | |
std::wstring filePath; | |
std::cout << "\nfilename: "; | |
std::wcin >> filePath; | |
image.reset(); | |
image = SerializedSharedMemory::create_readonly(shMemOverlayImageName, filePath); | |
if(!image) | |
{ | |
std::cout << "couldn't open the image filemapping!\n"; | |
continue; | |
} | |
settings->access([&](auto memory) { | |
auto transfer = reinterpret_cast<Settings *>(memory.data()); | |
transfer->_pending_overlay_image_size = image->size(); | |
std::cout << "file size has been posted!\n"; | |
}); | |
} while(!ShouldExit); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment