| 1 | // Copyright (C) 2024 Jarek Kobus |
|---|---|
| 2 | // Copyright (C) 2024 The Qt Company Ltd. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | |
| 5 | #ifndef TASKING_TCPSOCKET_H |
| 6 | #define TASKING_TCPSOCKET_H |
| 7 | |
| 8 | #include "tasking_global.h" |
| 9 | |
| 10 | // |
| 11 | // W A R N I N G |
| 12 | // ------------- |
| 13 | // |
| 14 | // This file is not part of the Qt API. It exists purely as an |
| 15 | // implementation detail. This header file may change from version to |
| 16 | // version without notice, or even be removed. |
| 17 | // |
| 18 | // We mean it. |
| 19 | // |
| 20 | |
| 21 | #include "tasktree.h" |
| 22 | |
| 23 | #include <QtNetwork/QTcpSocket> |
| 24 | |
| 25 | #include <memory> |
| 26 | |
| 27 | QT_BEGIN_NAMESPACE |
| 28 | |
| 29 | namespace Tasking { |
| 30 | |
| 31 | // This class introduces the dependency to Qt::Network, otherwise Tasking namespace |
| 32 | // is independent on Qt::Network. |
| 33 | // Possibly, it could be placed inside Qt::Network library, as a wrapper around QTcpSocket. |
| 34 | |
| 35 | class TASKING_EXPORT TcpSocket final : public QObject |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | |
| 39 | public: |
| 40 | ~TcpSocket(); |
| 41 | void setAddress(const QHostAddress &address) { m_address = address; } |
| 42 | void setPort(quint16 port) { m_port = port; } |
| 43 | void setWriteData(const QByteArray &data) { m_writeData = data; } |
| 44 | QTcpSocket *socket() const { return m_socket.get(); } |
| 45 | void start(); |
| 46 | |
| 47 | Q_SIGNALS: |
| 48 | void started(); |
| 49 | void done(DoneResult result); |
| 50 | |
| 51 | private: |
| 52 | QHostAddress m_address; |
| 53 | quint16 m_port = 0; |
| 54 | QByteArray m_writeData; |
| 55 | std::unique_ptr<QTcpSocket> m_socket; |
| 56 | QAbstractSocket::SocketError m_error = QAbstractSocket::UnknownSocketError; |
| 57 | }; |
| 58 | |
| 59 | class TASKING_EXPORT TcpSocketTaskAdapter final : public TaskAdapter<TcpSocket> |
| 60 | { |
| 61 | public: |
| 62 | TcpSocketTaskAdapter(); |
| 63 | void start() final; |
| 64 | }; |
| 65 | |
| 66 | using TcpSocketTask = CustomTask<TcpSocketTaskAdapter>; |
| 67 | |
| 68 | } // namespace Tasking |
| 69 | |
| 70 | QT_END_NAMESPACE |
| 71 | |
| 72 | #endif // TASKING_TCPSOCKET_H |
| 73 |
