Ferenc Szontágh
2024-06-25 e5f77bce96abea49d65ff25e41c9ce2dae01c8dc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "IPC.h"
 
IPC::IPC() {}
 
void IPC::registerHandler(std::shared_ptr<IPlugin> handler) {
    handlers.push_back(handler);
}
 
void IPC::sendMessage(const Command& cmd) {
    {
        std::lock_guard<std::mutex> lock(queueMutex);
        messageQueue.push(cmd);
    }
    queueCondVar.notify_one();
}
 
std::optional<Command> IPC::receiveMessage() {
    std::unique_lock<std::mutex> lock(queueMutex);
    queueCondVar.wait(lock, [this] { return !messageQueue.empty(); });
 
    if (!messageQueue.empty()) {
        Command cmd = messageQueue.front();
        messageQueue.pop();
        return cmd;
    }
    return std::nullopt;
}