-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathzmqclient.cpp
More file actions
157 lines (126 loc) · 4.8 KB
/
Copy pathzmqclient.cpp
File metadata and controls
157 lines (126 loc) · 4.8 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "zmqclient.h"
#ifdef USE_ZMQ
#include <QDebug>
#include <QByteArray>
#include <QThread>
// ZMQ requires these headers for implementation details
#include <zmq.hpp>
// --- Helper Functions ---
/**
* @brief Utility function to safely clean up existing ZMQ resources.
*/
void ZmqClient::cleanupZmq()
{
if (m_socket) {
// Set linger to 0 to prevent blocking during close()
int linger = 0;
m_socket->setsockopt(ZMQ_LINGER, &linger, sizeof(linger));
m_socket.reset();
}
m_context.reset();
m_isConnected = false;
}
// --- Class Implementation ---
ZmqClient::ZmqClient(QObject *parent)
: QObject(parent)
{
// ZMQ context and socket are intentionally not initialized here.
// They must be initialized in the target thread (in setConnection).
qRegisterMetaType<QString>("QString");
}
ZmqClient::~ZmqClient()
{
// Ensure all resources are cleaned up safely
cleanupZmq();
}
/**
* @brief Helper to send the atomic two-part message: [Type] + [Payload]
*/
bool ZmqClient::sendMultipart(const QString& type, const QByteArray& payload)
{
if (!m_isConnected || !m_socket) {
// Drop the message silently if not connected, which is the ZMQ PUB philosophy.
return false;
}
try {
// --- Frame 1: Type Identifier (Text) ---
// Convert QString to UTF-8 QByteArray for ZMQ sending
QByteArray typeData = type.toUtf8();
zmq::message_t type_frame(typeData.constData(), typeData.size());
// Use SNDMORE flag to indicate another frame is coming
m_socket->send(type_frame, zmq::send_flags::sndmore);
// --- Frame 2: Payload (Binary or Text) ---
zmq::message_t payload_frame(payload.constData(), payload.size());
// No SNDMORE flag, this is the last frame
m_socket->send(payload_frame, zmq::send_flags::none);
return true;
} catch (const zmq::error_t& e) {
// If a ZMQ error occurs, emit a signal back to the main thread
emit errorOccurred(QString("ZMQ send failed: %1").arg(e.what()));
return false;
}
}
// --- Public Slots (Called from GUI/Other Threads via signal/slot) ---
void ZmqClient::setConnection(const QString& host, int port)
{
// CRITICAL: This method MUST run on the thread the ZmqClient object is in!
if (QThread::currentThread() != this->thread()) {
qCritical() << "ZmqClient::setConnection called from the wrong thread! Use QMetaObject::invokeMethod.";
return;
}
if (m_isConnected) {
cleanupZmq();
}
m_host = host;
m_port = port;
QString endpoint = QString("tcp://%1:%2").arg(m_host).arg(m_port);
try {
// 1. Initialize Context and PUB Socket
m_context = std::unique_ptr<zmq::context_t>(new zmq::context_t(1));
m_socket = std::unique_ptr<zmq::socket_t>(new zmq::socket_t(*m_context, zmq::socket_type::pub));
//m_context = std::make_unique<zmq::context_t>(1);
//m_socket = std::make_unique<zmq::socket_t>(*m_context, zmq::socket_type::pub);
// 2. Set Linger to 0: Ensures closeConnection() won't block
int linger = 0;
m_socket->setsockopt(ZMQ_LINGER, &linger, sizeof(linger));
// 3. Connect to the server
m_socket->connect(endpoint.toStdString());
m_isConnected = true;
emit connectionStatus(true, QString("Successfully connected ZMQ PUB to %1").arg(endpoint));
} catch (const zmq::error_t& e) {
cleanupZmq();
emit connectionStatus(false, QString("Failed to connect ZMQ PUB to %1: %2").arg(endpoint).arg(e.what()));
emit errorOccurred(QString("ZMQ connection error: %1").arg(e.what()));
}
}
void ZmqClient::sendText(const QString& type, const QString& payload)
{
// Convert QString to QByteArray (UTF-8)
QByteArray payloadData = payload.toUtf8();
if (sendMultipart(type, payloadData)) {
// Optional: Debugging output within the worker thread
// qDebug() << QThread::currentThreadId() << "Sent TEXT:" << type;
}
}
void ZmqClient::sendBinary(const QString& type, const char* data, int size)
{
// Create QByteArray from the raw pointer and size
QByteArray payloadData(data, size);
if (sendMultipart(type, payloadData)) {
// Optional: Debugging output within the worker thread
// qDebug() << QThread::currentThreadId() << "Sent BINARY:" << type << "Size:" << size;
}
}
void ZmqClient::closeConnection()
{
// CRITICAL: This method MUST run on the thread the ZmqClient object is in!
if (QThread::currentThread() != this->thread()) {
qCritical() << "ZmqClient::closeConnection called from the wrong thread! Use QMetaObject::invokeMethod.";
return;
}
if (m_isConnected) {
cleanupZmq();
emit connectionStatus(false, "Connection explicitly closed.");
}
}
#endif // USE_ZMQ