Ferenc Szontágh
2024-06-27 0c428a79ef2379c6c7be29712e83f8c39e43c580
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "IPC.h"
#include <iostream>
 
IPC::IPC()
{
    DLOG(INFO) << "IPC ready.. queue size at init: " << messageQueue.size();
}
 
IPC::~IPC()
{
    while (!messageQueue.empty())
    {
        messageQueue.pop();
    }
}
 
void IPC::registerHandler(std::shared_ptr<IPlugin> handler)
{
    handlers.push_back(handler);
}
 
void IPC::sendMessage(const Command &cmd)
{
    std::lock_guard lock(queueMutex);
    std::string val;
    tser::Serialize(cmd, val);
    messageQueue.push(std::move(val));
    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())
    {
        std::string a = std::move(messageQueue.front());
        messageQueue.pop();
 
        Command c;
        tser::DeSerialize(a, c);
        return c;
    }
    return std::nullopt;
}