1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtNetwork module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QABSTRACTSOCKET_H
41#define QABSTRACTSOCKET_H
42
43#include <QtNetwork/qtnetworkglobal.h>
44#include <QtCore/qiodevice.h>
45#include <QtCore/qobject.h>
46#ifndef QT_NO_DEBUG_STREAM
47#include <QtCore/qdebug.h>
48#endif
49
50QT_BEGIN_NAMESPACE
51
52
53class QHostAddress;
54#ifndef QT_NO_NETWORKPROXY
55class QNetworkProxy;
56#endif
57class QAbstractSocketPrivate;
58class QAuthenticator;
59
60class Q_NETWORK_EXPORT QAbstractSocket : public QIODevice
61{
62 Q_OBJECT
63public:
64 enum SocketType {
65 TcpSocket,
66 UdpSocket,
67 SctpSocket,
68 UnknownSocketType = -1
69 };
70 Q_ENUM(SocketType)
71 enum NetworkLayerProtocol {
72 IPv4Protocol,
73 IPv6Protocol,
74 AnyIPProtocol,
75 UnknownNetworkLayerProtocol = -1
76 };
77 Q_ENUM(NetworkLayerProtocol)
78 enum SocketError {
79 ConnectionRefusedError,
80 RemoteHostClosedError,
81 HostNotFoundError,
82 SocketAccessError,
83 SocketResourceError,
84 SocketTimeoutError, /* 5 */
85 DatagramTooLargeError,
86 NetworkError,
87 AddressInUseError,
88 SocketAddressNotAvailableError,
89 UnsupportedSocketOperationError, /* 10 */
90 UnfinishedSocketOperationError,
91 ProxyAuthenticationRequiredError,
92 SslHandshakeFailedError,
93 ProxyConnectionRefusedError,
94 ProxyConnectionClosedError, /* 15 */
95 ProxyConnectionTimeoutError,
96 ProxyNotFoundError,
97 ProxyProtocolError,
98 OperationError,
99 SslInternalError, /* 20 */
100 SslInvalidUserDataError,
101 TemporaryError,
102
103 UnknownSocketError = -1
104 };
105 Q_ENUM(SocketError)
106 enum SocketState {
107 UnconnectedState,
108 HostLookupState,
109 ConnectingState,
110 ConnectedState,
111 BoundState,
112 ListeningState,
113 ClosingState
114 };
115 Q_ENUM(SocketState)
116 enum SocketOption {
117 LowDelayOption, // TCP_NODELAY
118 KeepAliveOption, // SO_KEEPALIVE
119 MulticastTtlOption, // IP_MULTICAST_TTL
120 MulticastLoopbackOption, // IP_MULTICAST_LOOPBACK
121 TypeOfServiceOption, //IP_TOS
122 SendBufferSizeSocketOption, //SO_SNDBUF
123 ReceiveBufferSizeSocketOption, //SO_RCVBUF
124 PathMtuSocketOption // IP_MTU
125 };
126 Q_ENUM(SocketOption)
127 enum BindFlag {
128 DefaultForPlatform = 0x0,
129 ShareAddress = 0x1,
130 DontShareAddress = 0x2,
131 ReuseAddressHint = 0x4
132 };
133 Q_DECLARE_FLAGS(BindMode, BindFlag)
134 enum PauseMode {
135 PauseNever = 0x0,
136 PauseOnSslErrors = 0x1
137 };
138 Q_DECLARE_FLAGS(PauseModes, PauseMode)
139
140 QAbstractSocket(SocketType socketType, QObject *parent);
141 virtual ~QAbstractSocket();
142
143 virtual void resume(); // to continue after proxy authentication required, SSL errors etc.
144 PauseModes pauseMode() const;
145 void setPauseMode(PauseModes pauseMode);
146
147 // ### Qt6: make the first one virtual
148 bool bind(const QHostAddress &address, quint16 port = 0, BindMode mode = DefaultForPlatform);
149 bool bind(quint16 port = 0, BindMode mode = DefaultForPlatform);
150
151 // ### Qt6: de-virtualize connectToHost(QHostAddress) overload
152 virtual void connectToHost(const QString &hostName, quint16 port, OpenMode mode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol);
153 virtual void connectToHost(const QHostAddress &address, quint16 port, OpenMode mode = ReadWrite);
154 virtual void disconnectFromHost();
155
156 bool isValid() const;
157
158 qint64 bytesAvailable() const override;
159 qint64 bytesToWrite() const override;
160
161 bool canReadLine() const override; // ### Qt6: remove me
162
163 quint16 localPort() const;
164 QHostAddress localAddress() const;
165 quint16 peerPort() const;
166 QHostAddress peerAddress() const;
167 QString peerName() const;
168
169 qint64 readBufferSize() const;
170 virtual void setReadBufferSize(qint64 size);
171
172 void abort();
173
174 virtual qintptr socketDescriptor() const;
175 virtual bool setSocketDescriptor(qintptr socketDescriptor, SocketState state = ConnectedState,
176 OpenMode openMode = ReadWrite);
177
178 virtual void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
179 virtual QVariant socketOption(QAbstractSocket::SocketOption option);
180
181 SocketType socketType() const;
182 SocketState state() const;
183 SocketError error() const;
184
185 // from QIODevice
186 void close() override;
187 bool isSequential() const override;
188 bool atEnd() const override; // ### Qt6: remove me
189 bool flush();
190
191 // for synchronous access
192 virtual bool waitForConnected(int msecs = 30000);
193 bool waitForReadyRead(int msecs = 30000) override;
194 bool waitForBytesWritten(int msecs = 30000) override;
195 virtual bool waitForDisconnected(int msecs = 30000);
196
197#ifndef QT_NO_NETWORKPROXY
198 void setProxy(const QNetworkProxy &networkProxy);
199 QNetworkProxy proxy() const;
200 QString protocolTag() const;
201 void setProtocolTag(const QString &tag);
202#endif
203
204Q_SIGNALS:
205 void hostFound();
206 void connected();
207 void disconnected();
208 void stateChanged(QAbstractSocket::SocketState);
209#if QT_DEPRECATED_SINCE(5,15)
210 QT_DEPRECATED_NETWORK_API_5_15_X("Use QAbstractSocket::errorOccurred(QAbstractSocket::SocketError) instead")
211 void error(QAbstractSocket::SocketError);
212#endif
213 void errorOccurred(QAbstractSocket::SocketError);
214#ifndef QT_NO_NETWORKPROXY
215 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
216#endif
217
218protected:
219 qint64 readData(char *data, qint64 maxlen) override;
220 qint64 readLineData(char *data, qint64 maxlen) override;
221 qint64 writeData(const char *data, qint64 len) override;
222
223 void setSocketState(SocketState state);
224 void setSocketError(SocketError socketError);
225 void setLocalPort(quint16 port);
226 void setLocalAddress(const QHostAddress &address);
227 void setPeerPort(quint16 port);
228 void setPeerAddress(const QHostAddress &address);
229 void setPeerName(const QString &name);
230
231 QAbstractSocket(SocketType socketType, QAbstractSocketPrivate &dd, QObject *parent = nullptr);
232
233private:
234 Q_DECLARE_PRIVATE(QAbstractSocket)
235 Q_DISABLE_COPY(QAbstractSocket)
236
237 Q_PRIVATE_SLOT(d_func(), void _q_connectToNextAddress())
238 Q_PRIVATE_SLOT(d_func(), void _q_startConnecting(const QHostInfo &))
239 Q_PRIVATE_SLOT(d_func(), void _q_abortConnectionAttempt())
240 Q_PRIVATE_SLOT(d_func(), void _q_testConnection())
241};
242
243
244Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocket::BindMode)
245Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocket::PauseModes)
246
247#ifndef QT_NO_DEBUG_STREAM
248Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketError);
249Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketState);
250#endif
251
252QT_END_NAMESPACE
253
254Q_DECLARE_METATYPE(QAbstractSocket::SocketState)
255Q_DECLARE_METATYPE(QAbstractSocket::SocketError)
256
257#endif // QABSTRACTSOCKET_H
258