QSocketNotifier vs nanomsg

QSocketNotifier vs nanomsg

本文关键字:nanomsg vs QSocketNotifier      更新时间:2023-10-16

如果接收到任何数据,是否可以为nanomsg套接字使用QSocketNotifier来执行某些操作?我尝试使用此代码,但运行nanocat --req --connect ipc:///tmp/node0.ipc --data pong --format ascii时没有发生任何事情。我甚至不知道如何检查问题发生在哪一步,因为没有错误。

Wrapper::Wrapper(QObject *parent) : QObject(parent) {
...
createNode();
int fd;
size_t sz = sizeof(fd);
nn_getsockopt(sock, NN_SOL_SOCKET, NN_RCVFD, &fd, &sz);
QSocketNotifier m_notifier(fd, QSocketNotifier::Read);
QObject::connect(&m_notifier, SIGNAL(activated(int)), this,   SLOT(nmsgRecieve()));
m_notifier.setEnabled(true);
...
}
void Wrapper::createNode() {
const char* url = "ipc:///tmp/node0.ipc";
if ((sock = nn_socket(AF_SP, NN_REP)) < 0) {
qDebug() << "nn_socket" << nn_strerror(nn_errno());
exit(1);
}
if ((rv = nn_bind(sock, url)) < 0) {
qDebug() << "nn_bind" << nn_strerror(nn_errno());
exit(1);
}
}
void Wrapper::nmsgRecieve() {
qDebug() << "Some msg";
char *buf = NULL;
int bytes;
if ((bytes = nn_recv(sock, &buf, NN_MSG, 0)) < 0) {
qDebug() << "nn_recv" << nn_strerror(nn_errno());
exit(1);
}
qDebug() << buf;
nn_freemsg(buf);
}

这是一个非常愚蠢的问题,与nanomsg或QSocketNotifier无关。我创建了我的QSocketNotifer,这样构造函数块一结束它就被销毁了。