Is it possible to use `waitForReadyRead ()` instead of creating a slot for the signal `readyRead ()`?

Writing a cross-platform application using Qt (including Windows with MinGW). To read data from an SSL socket, I create a separate stream. This thread exists for a historical reason, since the application was previously written using the C socket / ssl / crypto libraries. Now all this is being replaced by the Qt Network library.

To block the flow waitForReadyRead(milliseconds)seems to be the best choice. Now according to the Qt hierarchy:

QIODevice
   |
QAbstractSocket
   |
QTcpSocket
   |
QSslSocket

The documentation QAbscractSocket::waitForReadyRead()offers:

Note. This feature may accidentally happen on Windows. Consider using the event loop and the readyRead () signal if your software runs on Windows.

QIODevice::waitForReadyRead().

: QSslSocket::waitForReadyRead() ?


readyRead()?
- , - readyRead(), . , QSslSocket:: write() , . - .

+4
4

: , QSslSocket::waitForReadyRead(), , . , , , . , QAbstractSocket::bytesAvailable() > 0, .

, -, (, ). - , /.

Qt.

+4

. QIODevice , false. . QAbstractSocket -, "nativeSelect", , , . Windows select false. , waitForReadyRead(). QSslSocket waitForReadyRead() internaly QAbstactSocket SSL.

. , , Qt, , , MainLoop, QApplication:: exec() - . .

, .

+1

.

waitForReady*, ( ). , ? , . , , IMO - , ​​.

:

readyRead()? - , - readyRead(), . , QSslSocket:: write() , . - .

It is suspicious.
I have never seen anyone have a similar problem. Maybe some parts of your code block the event loop?

+1
source

Although this is not an exact answer from Qn, I am posting a possible implementation waitForReadyRead()that can be used in a local event loop.

class MySslSocket : QSslSocket
{
  Q_OBJECT 
public:
  virtual
  bool
  waitForReadyRead (int milliseconds) override final
  {
    QEventLoop eventLoop;
    QTimer timer;
    connect(this, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
    connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
    timer.setSingleShot(true);
    timer.start(milliseconds);

    eventLoop.exec();
    return timer.isActive();
  }
};

This can be used either exclusively for Windows, or it can be used for all platforms in general.

0
source

Source: https://habr.com/ru/post/1679258/


All Articles