IPCHelper.cpp
1 #include "pch.h" 2 #include "IPCHelper.h" 3 4 #include <common/logger/logger.h> 5 6 IPCHelper::IPCHelper(const std::wstring& currentPipeName, const std::wstring receiverPipeName, std::function<void(const std::wstring&)> messageCallback) : 7 callback(messageCallback) 8 { 9 HANDLE hToken = nullptr; 10 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) 11 { 12 Logger::error("Failed to get process token"); 13 return; 14 } 15 16 std::unique_lock lock{ ipcMutex }; 17 ipc = make_unique<TwoWayPipeMessageIPC>(currentPipeName, receiverPipeName, std::bind(&IPCHelper::receive, this, std::placeholders::_1)); 18 ipc->start(hToken); 19 } 20 21 IPCHelper::~IPCHelper() 22 { 23 std::unique_lock lock{ ipcMutex }; 24 if (ipc) 25 { 26 ipc->end(); 27 ipc = nullptr; 28 } 29 } 30 31 void IPCHelper::send(const std::wstring& message) const 32 { 33 ipc->send(message); 34 } 35 36 void IPCHelper::receive(const std::wstring& msg) 37 { 38 if (callback) 39 { 40 callback(msg); 41 } 42 }