#include "ipc_interface.h" #include #include #include #include #include #include "sharedipc.h" #include #include "QDebug" class IPCControllerImpl : public IPCController { public: IPCControllerImpl() : hMapFile(nullptr), hEvent(nullptr), isRunning(false) { // Инициализация shared memory и событий hEvent = CreateEventA(nullptr, FALSE, FALSE, "Local\\IPCEvent"); if (hEvent == NULL) { DWORD err = GetLastError(); std::cerr << "hEvent failed: " << err << std::endl; // return; } hMapFile = CreateFileMappingA(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, 1024, "Local\\IPCMemory"); if (hMapFile == NULL) { DWORD err = GetLastError(); std::cerr << "hMapFile failed: " << err << std::endl; // return; } } ~IPCControllerImpl() { stopListening(); CloseHandle(hEvent); CloseHandle(hMapFile); } void sendMessage(const std::string& msg) override { // std::cout << "IPC_Controller::sendMessage: " << msg << std::endl << std::flush; LPVOID pBuf = MapViewOfFile(hMapFile, FILE_MAP_WRITE, 0, 0, 1024); if (pBuf) { memcpy(pBuf, msg.c_str(), msg.size() + 1); SetEvent(hEvent); // Уведомляем получателей UnmapViewOfFile(pBuf); } else { std::cerr << "IPC_Controller::sendMessage: pBuf is empty" << std::endl << std::flush; DWORD err = GetLastError(); std::cerr << "pBuf failed: " << err << std::endl; } } void setMessageCallback(std::function callback) override { messageCallback = callback; } void startListening() override { if (isRunning) return; isRunning = true; listenerThread = std::thread(&IPCControllerImpl::listenLoop, this); } void stopListening() override { isRunning = false; SetEvent(hEvent); // Прерываем ожидание if (listenerThread.joinable()) listenerThread.join(); } private: void listenLoop() { while (isRunning) { WaitForSingleObject(hEvent, INFINITE); // Ожидаем уведомление if (!isRunning) break; LPVOID pBuf = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 1024); if (pBuf && messageCallback) { messageCallback(static_cast(pBuf)); UnmapViewOfFile(pBuf); } } } HANDLE hMapFile; HANDLE hEvent; std::atomic isRunning; std::thread listenerThread; std::function messageCallback; }; class IPCControllerFactoryImpl : public IPCControllerFactory { public: IPCControllerFactoryImpl() { qDebug() << "creating IPCControllerFactoryImpl"; } ~IPCControllerFactoryImpl() { std::lock_guard lock(mutex_); for (auto& pair : controllers_) { delete pair.second; } } IPCController* getController(const std::string& memoryKey) override { qDebug() << "locking"; std::lock_guard lock(mutex_); qDebug() << "Creating controller"; // Если контроллер уже существует auto it = controllers_.find(memoryKey); if (it != controllers_.end()) { qDebug() << "controllers by key" << memoryKey << " already exsist"; return it->second; } // Создаём новый контроллер qDebug() << "Creating new controller"; auto* controller = new IPCControllerImpl(); controllers_[memoryKey] = controller; return controller; } IPCController* getExistController(const std::string& memoryKey) override { std::lock_guard lock(mutex_); // Если контроллер уже существует auto it = controllers_.find(memoryKey); if (it != controllers_.end()) { return it->second; } return 0; } void releaseController(const std::string& memoryKey) override { std::lock_guard lock(mutex_); auto it = controllers_.find(memoryKey); if (it != controllers_.end()) { delete it->second; controllers_.erase(it); } } private: std::unordered_map controllers_; std::mutex mutex_; }; extern "C" __declspec(dllexport) IPCControllerFactory* createIPCControllerFactory() { qDebug() << "Heta"; return new IPCControllerFactoryImpl(); }