Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
tcpsocket.cpp
Go to the documentation of this file.
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#include "tcpsocket.h"
6
8
9namespace Tasking {
10
12{
13 if (m_socket) {
14 qWarning("The TcpSocket is already running. Ignoring the call to start().");
15 return;
16 }
17 if (m_address.isNull()) {
18 qWarning("Can't start the TcpSocket with invalid address. "
19 "Stopping with an error.");
20 m_error = QAbstractSocket::HostNotFoundError;
21 emit done(DoneResult::Error);
22 return;
23 }
24
25 m_socket.reset(new QTcpSocket);
26 connect(m_socket.get(), &QAbstractSocket::errorOccurred, this,
27 [this](QAbstractSocket::SocketError error) {
28 m_error = error;
29 m_socket->disconnect();
30 emit done(DoneResult::Error);
31 m_socket.release()->deleteLater();
32 });
33 connect(m_socket.get(), &QAbstractSocket::connected, this, [this] {
34 if (!m_writeData.isEmpty())
35 m_socket->write(m_writeData);
36 emit started();
37 });
38 connect(m_socket.get(), &QAbstractSocket::disconnected, this, [this] {
39 m_socket->disconnect();
40 emit done(DoneResult::Success);
41 m_socket.release()->deleteLater();
42 });
43
44 m_socket->connectToHost(m_address, m_port);
45}
46
47TcpSocket::~TcpSocket()
48{
49 if (m_socket) {
50 m_socket->disconnect();
51 m_socket->abort();
52 }
53}
54
55} // namespace Tasking
56
57QT_END_NAMESPACE
\inmodule TaskingSolution