Kylin/Universal/SingletonProcess.h
2023-07-25 10:40:14 +08:00

40 lines
1.0 KiB
C++

#ifndef SINGLETONPROCESS_H
#define SINGLETONPROCESS_H
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/signals2/signal.hpp>
#include <condition_variable>
#include <string_view>
#include <thread>
class SingletonProcess
{
public:
using MessageHandler = boost::signals2::signal<void(const char *, size_t)>;
using MessageHandlerSlot = MessageHandler::slot_type;
SingletonProcess(const std::string_view &name);
bool alreadyRun() const;
MessageHandler messageHandler;
~SingletonProcess();
bool sendMessage(const char *data, size_t size);
protected:
void run();
private:
#if WIN32
void *m_handle{nullptr}; // windows HANDLE
#else
int m_handle{-1};
#endif
bool m_alreadyRun{false};
std::unique_ptr<boost::interprocess::message_queue> m_messageQueue;
std::unique_ptr<std::thread> m_thread;
std::condition_variable m_conditionVariable;
std::mutex m_mutex;
bool m_exit{false};
};
#endif // SINGLETONPROCESS_H