带有 Qt 5.12 QSocketNotifier 的 ZeroMQ 只触发一次
Posted
技术标签:
【中文标题】带有 Qt 5.12 QSocketNotifier 的 ZeroMQ 只触发一次【英文标题】:ZeroMQ with Qt 5.12 QSocketNotifier Only Fires Once 【发布时间】:2018-12-31 01:36:47 【问题描述】:在 Ubuntu 16.04 机器上,我使用 Python3 和 pyzmq 通过 ZeroMQ PUB/SUB 套接字将 base64 结尾的 JPEG 发送到 Qt5 应用程序。 Qt5 应用程序使用 QSocketNotifier 来监视 ZMQ 套接字,并且应该在套接字变为“激活”并读取更多数据时触发。在我的示例中,数据完全在 Qt5 端接收,并且 JPEG 看起来没问题 - 但是,即使 Python Publisher 不断发送数据,套接字通知程序也只会触发一次。如果我使用 ctrl-C 退出 Python 应用程序,则会在 Qt5 App 端触发另一个套接字通知激活插槽的实例。我希望每次从 Python 发送 ZMQ 消息时都会触发该插槽。我在下面附上了 Python 和 Qt5 整个应用程序。我怎样才能使这项工作?谢谢。
-------- Header File ------
#ifndef SOCKETRECEIVER_H
#define SOCKETRECEIVER_H
#include <QObject>
// Debug
#include <QDebug>
#include <QDataStream>
#include <QFile>
#include <unistd.h>
// User ZeroMQ Sockets
#include <QSocketNotifier>
// ZeroMQ Includes
#include <zmq.h>
// Debug prints
static void AppDebug(QString message)
qDebug() << Q_FUNC_INFO << message;
class SocketReceiver : public QObject
Q_OBJECT
public:
explicit SocketReceiver(QObject *parent = nullptr);
~SocketReceiver();
private slots:
void readZMQData();
private:
QSocketNotifier *m_SocketNotifier;
void *m_Context;
void *m_Subscriber;
long long int m_RxFrameCounter;
bool m_DidRXFrame;
signals:
public slots:
;
#endif // SOCKETRECEIVER_H
-------- CCP File ------
#include "socketreceiver.h"
SocketReceiver::SocketReceiver(QObject *parent) : QObject(parent) , m_SocketNotifier(nullptr) , m_Context(nullptr) , m_Subscriber(nullptr) , m_RxFrameCounter(0) , m_DidRXFrame(false)
/***** ZMQ *****/
int major, minor, patch;
zmq_version (&major, &minor, &patch);
m_Context = zmq_ctx_new();
m_Subscriber = zmq_socket (m_Context, ZMQ_SUB);
int rc = -1;
unsigned int fd = 0;
do
const char *filter = std::string("").c_str();
rc = zmq_connect (m_Subscriber, "tcp://localhost:5556");
rc = zmq_setsockopt (m_Subscriber, ZMQ_SUBSCRIBE,filter, strlen (filter));
size_t fd_size = sizeof(fd);
rc = zmq_getsockopt(m_Subscriber,ZMQ_FD,&fd,&fd_size);
m_SocketNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
connect(m_SocketNotifier, SIGNAL(activated(int)), this, SLOT(readZMQData()), Qt::DirectConnection);
while ( rc < 0 );
AppDebug("Done setting up");
SocketReceiver::~SocketReceiver()
zmq_close (this->m_Subscriber);
zmq_ctx_destroy (this->m_Context);
void SocketReceiver::readZMQData()
m_SocketNotifier->setEnabled(false);
AppDebug("Waiting for next frame...");
QByteArray newFrame;
int events = 0;
std::size_t eventsSize = sizeof(events);
zmq_getsockopt(m_Subscriber,ZMQ_EVENTS, &events, &eventsSize);
if(events & ZMQ_POLLIN)
AppDebug("Read Data...");
// Receive data from socket
zmq_msg_t message;
zmq_msg_init(&message);
zmq_recvmsg(m_Subscriber, &message, 0);
size_t size = zmq_msg_size (&message);
AppDebug(QString("Message Size: %1").arg(size));
char *string = static_cast<char*>(malloc(size + 1));
memcpy (string, zmq_msg_data(&message), size);
zmq_msg_close (&message);
string [size] = 0;
if ( string != nullptr )
QByteArray newDecodedData = QByteArray::fromBase64(QByteArray(string));
newFrame.append(newDecodedData);
free(string);
if ( !m_DidRXFrame )
m_DidRXFrame = true;
if ( m_RxFrameCounter == 0 && m_DidRXFrame )
AppDebug(QString("Debug RX Frame Size: %1").arg(newFrame.size()));
QFile output("/tmp/abcd.jpeg");
if ( output.open(QIODevice::WriteOnly) )
output.write(newFrame);
output.close();
//sleep(86400);
m_RxFrameCounter++;
AppDebug("Setting enabled true...");
m_SocketNotifier->setEnabled(true);
------- Python 脚本 ---------
#!/usr/bin/python3
import zmq
import random
import sys
import time
import base64
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5556")
time.sleep(1)
while True:
f = open("test.jpg",'rb')
bytes = bytearray(f.read())
print("Encoded Data Length: %s"%(len(bytes)))
strng = base64.b64encode(bytes)
print("Encoded Data Length: %s"%(len(strng)))
socket.send(strng)
f.close()
time.sleep(1)
【问题讨论】:
【参考方案1】:您必须使用一段时间,因为在读取数据时您可以获取另一个数据:
int SocketReceiver::events()
int events = 0;
std::size_t eventsSize = sizeof(events);
zmq_getsockopt(m_Subscriber,ZMQ_EVENTS, &events, &eventsSize);
return events;
void SocketReceiver::readZMQData()
m_SocketNotifier->setEnabled(false);
while (events() & ZMQ_POLLIN)
zmq_msg_t message;
zmq_msg_init(&message);
zmq_recvmsg(m_Subscriber, &message, 0);
size_t size = zmq_msg_size (&message);
AppDebug(QString("Message Size: %1").arg(size));
char *string = static_cast<char*>(malloc(size + 1));
memcpy (string, zmq_msg_data(&message), size);
zmq_msg_close (&message);
string [size] = 0;
if (string)
QByteArray frame = QByteArray::fromBase64(QByteArray(string));
free(string);
AppDebug(QString("Debug RX Frame Size: %1").arg(frame.size()));
QFile output("/tmp/abcd.jpeg");
if ( output.open(QIODevice::WriteOnly) )
output.write(frame);
output.close();
m_SocketNotifier->setEnabled(true);
【讨论】:
以上是关于带有 Qt 5.12 QSocketNotifier 的 ZeroMQ 只触发一次的主要内容,如果未能解决你的问题,请参考以下文章