Question

Is there some way to make a function pause it's execution until the socket receives a specific message? Using Signals + QEventLoop to wait doesn't work because while it can wait for signals, there isn't any way to get the data the signal emitted (or is there?).

Was it helpful?

Solution

You could connect to the following signal:

void QIODevice::readyRead() [signal]

Then, you would basically read the data and if it is the one you are looking for, you could set a boolean variable to true that is initially false. Your function would continue the execution only when the variable is true.

Make sure that the function paused is not sleeping in a sync manner too much, etc, without having a dedicated thread.

So, this would be one way of solving your task:

MySocketManager::MySocketManager(QObject *parent): QObject(parent)
{
    ...
    connect(m_mySocket, SIGNAL(readyRead()), SLOT(handleReadyRead()));
    ...
}

void MySocketManager::handleReadyRead()
{
    if (m_mySocket.readAll() == "myMessage")
        continue = true;
}

...

void myFunction()
{
    ...
    continue = false;
    qDebug() << "Pause";
    while (!continue) { ... }
    qDebug() << "Continue";
    ...
}

This is a tad simplication of the issue, but since you have not shown much effort other than asking for solution, this should get you started.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top