45 lines
1.3 KiB
C++

#pragma once
// ConsumerThread.hpp
// SPDX-License-Identifier: GPL-3.0-or-later
// Author: Unai Blazquez <unaibg2000@gmail.com>
#include <QObject>
#include <atomic>
#include <string>
#include <thread>
/// @brief Listens on a UNIX domain socket, receives integers from the
/// producer via IPC, prints them to console, and emits a Qt signal.
class ConsumerThread : public QObject
{
Q_OBJECT
public:
/// @brief Construct the consumer bound to a socket path.
/// @param socket_path UNIX domain socket path to listen on.
/// @param parent Optional QObject parent for memory management.
explicit ConsumerThread(const std::string& socket_path,
QObject* parent = nullptr);
~ConsumerThread() override;
/// @brief Start the listener thread. The server socket is ready
/// when this function returns.
void start();
/// @brief Stop the listener thread gracefully. Safe to call multiple times.
void stop();
signals:
/// @brief Emitted every time an integer is received from the producer.
void valueReceived(int value);
private:
/// @brief Main loop: accept → recv → print → emit. Runs in m_thread.
void run_loop();
std::string m_socket_path;
int m_server_fd = -1;
std::atomic<bool> m_running{false};
std::thread m_thread;
};