QAbstractSocket
class QCoroAbstractSocket : public QCoroIODevice;
QAbstractSocket
is a base class for QTcpSocket
and QUdpSocket
and has some potentially asynchronous operations.
In addition to reading and writing, which are provided by QIODevice
baseclass and can be used with coroutines thanks to QCoro's QCoroIODevice
.
Those operations are connecting to and disconnecting from the server.
Since QAbstractSocket
doesn't provide the ability to co_await
those operations, QCoro provides
a wrapper calss QCoroAbstractSocket
. To wrap a QAbstractSocket
object into the QCoroAbstractSocket
wrapper, use qCoro()
:
QCoroAbstractSocket qCoro(QAbstractSocket &);
QCoroAbstractSocket qCoro(QAbstractSocket *);
Same as QAbstractSocket
is a subclass of QIODevice
, QCoroAbstractSocket
subclasses
QCoroIODevice
, so it also provides the awaitable interface for selected
QIODevice
functions. See QCoroIODevice
documentation for details.
waitForConnected()
Waits for the socket to connect or until it times out. Returns bool
indicating whether
connection has been established or whether the operation has timed out. The coroutine
is not suspended if the socket is already connected.
See documentation for QAbstractSocket::waitForConnected()
for details.
Awaitable auto QCoroAbstractSocket::waitForConnected(int timeout_msecs = 30'000);
Awaitable auto QCoroAbstractSocket::waitForConnected(std::chrono::milliseconds timeout);
waitForDisconnected()
Waits for the socket to disconnect from the server or until the operation times out. The coroutine is not suspended if the socket is already disconnected.
See documentation for QAbstractSocket::waitForDisconnected()
for details.
Awaitable auto QCoroAbstractSocket::waitForDisconnected(timeout_msecs = 30'000);
Awaitable auto QCoroAbstractSocket::waitForDisconnected(std::chrono::milliseconds timeout);
connectToHost()
QCoroAbstractSocket
provides an additional method called connectToHost()
which is equivalent
to calling QAbstractSocket::connectToHost()
followed by QAbstractSocket::waitForConnected()
. This
operation is co_awaitable as well.
See the documentation for [QAbstractSocket::connectToHost()
][qtdoc-qabstractsocket-connectToHost] and
QAbstractSocket::waitForConnected()
for details.
Awaitable auto QCoroAbstractSocket::connectToHost(const QHostAddress &address, quint16 port,
QIODevice::OpenMode openMode = QIODevice::ReadOnly);
Awaitable auto QCoroAbstractSocket::connectToHost(const QString &hostName, quint16 port,
QIODevice::OpenMode openMode = QIODevice::ReadOnly,
QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::AnyIPProtocol);
Examples
QCoro::Task<QByteArray> requestDataFromServer(const QString &hostName) {
QTcpSocket socket;
if (!co_await qCoro(socket).connectToHost(hostName)) {
qWarning() << "Failed to connect to the server";
co_return QByteArray{};
}
socket.write("SEND ME DATA!");
QByteArray data;
while (!data.endsWith("\r\n.\r\n")) {
data += co_await qCoro(socket).readAll();
}
co_return data;
}