|
| 1 | +#ifndef ZMQ_REP_H |
| 2 | +#define ZMQ_REP_H |
| 3 | + |
| 4 | +#include <functional> |
| 5 | +#include <pch.h> |
| 6 | +#include <string> |
| 7 | +#include <thread> |
| 8 | +#include <zmq.hpp> |
| 9 | + |
| 10 | +namespace WSS { |
| 11 | + |
| 12 | +class ZMQRep { |
| 13 | + zmq::context_t m_Context; |
| 14 | + zmq::socket_t m_Socket; |
| 15 | + |
| 16 | + std::atomic_bool m_Running{false}; |
| 17 | + std::thread m_Thread; |
| 18 | + |
| 19 | + std::unordered_map<std::string, std::function<void(const json&)>> m_Listeners; |
| 20 | + std::mutex m_ListenersMutex; |
| 21 | + |
| 22 | + public: |
| 23 | + ZMQRep() = default; |
| 24 | + ~ZMQRep(); |
| 25 | + |
| 26 | + void RunAsync(); |
| 27 | + void Listen(const std::string& type, std::function<void(const json&)> listener); |
| 28 | +}; |
| 29 | + |
| 30 | +inline ZMQRep::~ZMQRep() { |
| 31 | + if (m_Running) { |
| 32 | + m_Running = false; |
| 33 | + if (m_Thread.joinable()) { |
| 34 | + m_Thread.join(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if (m_Socket.handle() != nullptr) |
| 39 | + m_Socket.close(); |
| 40 | + if (m_Context.handle() != nullptr) |
| 41 | + m_Context.close(); |
| 42 | +} |
| 43 | + |
| 44 | +inline void ZMQRep::RunAsync() { |
| 45 | + m_Socket = zmq::socket_t(m_Context, zmq::socket_type::rep); |
| 46 | + m_Socket.set(zmq::sockopt::linger, 0); // Set linger to 0 to avoid blocking on close |
| 47 | + m_Thread = std::thread([this]() { |
| 48 | + try { |
| 49 | + m_Running = true; |
| 50 | + m_Socket.bind("ipc:///tmp/wss_ipc"); |
| 51 | + |
| 52 | + while (m_Running) { |
| 53 | + zmq::message_t request; |
| 54 | + auto result = m_Socket.recv(request, zmq::recv_flags::none); |
| 55 | + if (!result) { |
| 56 | + fprintf(stderr, "WSS-ZMQRep recv failed: %s\n", zmq_strerror(zmq_errno())); |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + std::string message(static_cast<char*>(request.data()), request.size()); |
| 61 | + WSS_DEBUG("[WSS-ZMQ] Received message: {}", message); |
| 62 | + json response; |
| 63 | + { |
| 64 | + json msg = json::parse(message, nullptr, false); |
| 65 | + |
| 66 | + std::lock_guard lock(m_ListenersMutex); |
| 67 | + auto it = m_Listeners.find(msg["type"]); |
| 68 | + if (it != m_Listeners.end()) { |
| 69 | + try { |
| 70 | + it->second(msg["payload"]); |
| 71 | + response = {{"status", "success"}, {"message", "Listener executed successfully"}}; |
| 72 | + } catch (const std::exception& e) { |
| 73 | + response = {{"error", "Listener execution failed"}, {"details", e.what()}}; |
| 74 | + } |
| 75 | + } else { |
| 76 | + response = {{"error", "No listener for this message type"}}; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + std::string responseStr = response.dump(); |
| 81 | + zmq::message_t zmqResponse(responseStr.size()); |
| 82 | + memcpy(zmqResponse.data(), responseStr.data(), responseStr.size()); |
| 83 | + if (!m_Socket.send(zmqResponse, zmq::send_flags::none)) |
| 84 | + fprintf(stderr, "WSS-ZMQRep send failed: %s\n", zmq_strerror(zmq_errno())); |
| 85 | + WSS_DEBUG("[WSS-ZMQ] Sent response: {}", responseStr); |
| 86 | + } |
| 87 | + |
| 88 | + } catch (const std::exception& e) { |
| 89 | + WSS_ERROR("[WSS-ZMQ] Exception in ZMQRep thread: {}", e.what()); |
| 90 | + } catch (...) { |
| 91 | + WSS_ERROR("[WSS-ZMQ] Unknown exception in ZMQRep thread."); |
| 92 | + } |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +inline void ZMQRep::Listen(const std::string& type, std::function<void(const json&)> listener) { |
| 97 | + std::lock_guard lock(m_ListenersMutex); |
| 98 | + if (m_Listeners.find(type) != m_Listeners.end()) { |
| 99 | + WSS_WARN("[WSS-ZMQ] Listener for type '{}' already exists, replacing it.", type); |
| 100 | + } |
| 101 | + m_Listeners[type] = std::move(listener); |
| 102 | + WSS_DEBUG("[WSS-ZMQ] Registered listener for type '{}'", type); |
| 103 | +} |
| 104 | +} // namespace WSS |
| 105 | + |
| 106 | +#endif // ZMQ_REP_H |
0 commit comments