c++qtipcnanomsg

QSocketNotifier vs nanomsg


Is it possible to use QSocketNotifier for nanomsg socket in order to do something if any data is received? I tried to use this code, but nothing happens when I run nanocat --req --connect ipc:///tmp/node0.ipc --data pong --format ascii . I don't even know how to check at which step the problem occurs, because there are no errors.

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);
 }

Solution

  • Well, it was a really stupid question which had nothing to do with nanomsg or QSocketNotifier. I created my QSocketNotifer so that it got destroyed as soon as constructor block ends.