| 1 | // Copyright (C) 2016 The Qt Company Ltd. | 
|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | 
|---|
| 3 |  | 
|---|
| 4 | #ifndef QABSTRACTSOCKET_H | 
|---|
| 5 | #define QABSTRACTSOCKET_H | 
|---|
| 6 |  | 
|---|
| 7 | #include <QtNetwork/qtnetworkglobal.h> | 
|---|
| 8 | #if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) | 
|---|
| 9 | #include <QtNetwork/qabstractsocket.h> | 
|---|
| 10 | #endif | 
|---|
| 11 | #ifdef Q_QDOC | 
|---|
| 12 | #include <QtNetwork/qhostaddress.h> | 
|---|
| 13 | #endif | 
|---|
| 14 | #include <QtCore/qiodevice.h> | 
|---|
| 15 | #include <QtCore/qobject.h> | 
|---|
| 16 | #ifndef QT_NO_DEBUG_STREAM | 
|---|
| 17 | #include <QtCore/qdebug.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | QT_BEGIN_NAMESPACE | 
|---|
| 21 |  | 
|---|
| 22 |  | 
|---|
| 23 | class QHostAddress; | 
|---|
| 24 | #ifndef QT_NO_NETWORKPROXY | 
|---|
| 25 | class QNetworkProxy; | 
|---|
| 26 | #endif | 
|---|
| 27 | class QAbstractSocketPrivate; | 
|---|
| 28 | class QAuthenticator; | 
|---|
| 29 |  | 
|---|
| 30 | class Q_NETWORK_EXPORT QAbstractSocket : public QIODevice | 
|---|
| 31 | { | 
|---|
| 32 | Q_OBJECT | 
|---|
| 33 | Q_MOC_INCLUDE(<QtNetwork/qauthenticator.h>) | 
|---|
| 34 |  | 
|---|
| 35 | public: | 
|---|
| 36 | enum SocketType { | 
|---|
| 37 | TcpSocket, | 
|---|
| 38 | UdpSocket, | 
|---|
| 39 | SctpSocket, | 
|---|
| 40 | UnknownSocketType = -1 | 
|---|
| 41 | }; | 
|---|
| 42 | Q_ENUM(SocketType) | 
|---|
| 43 |  | 
|---|
| 44 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) | 
|---|
| 45 | enum NetworkLayerProtocol { | 
|---|
| 46 | IPv4Protocol, | 
|---|
| 47 | IPv6Protocol, | 
|---|
| 48 | AnyIPProtocol, | 
|---|
| 49 | UnknownNetworkLayerProtocol = -1 | 
|---|
| 50 | }; | 
|---|
| 51 | Q_ENUM(NetworkLayerProtocol) | 
|---|
| 52 | #else | 
|---|
| 53 | // compatibility with Qt 4 to 6 | 
|---|
| 54 | using NetworkLayerProtocol = QHostAddress::NetworkLayerProtocol; | 
|---|
| 55 | static constexpr auto IPv4Protocol = QHostAddress::IPv4Protocol; | 
|---|
| 56 | static constexpr auto IPv6Protocol = QHostAddress::IPv6Protocol; | 
|---|
| 57 | static constexpr auto AnyIPProtocol = QHostAddress::AnyIPProtocol; | 
|---|
| 58 | static constexpr auto UnknownNetworkLayerProtocol = QHostAddress::UnknownNetworkLayerProtocol; | 
|---|
| 59 | #endif | 
|---|
| 60 |  | 
|---|
| 61 | enum SocketError { | 
|---|
| 62 | ConnectionRefusedError, | 
|---|
| 63 | RemoteHostClosedError, | 
|---|
| 64 | HostNotFoundError, | 
|---|
| 65 | SocketAccessError, | 
|---|
| 66 | SocketResourceError, | 
|---|
| 67 | SocketTimeoutError,                     /* 5 */ | 
|---|
| 68 | DatagramTooLargeError, | 
|---|
| 69 | NetworkError, | 
|---|
| 70 | AddressInUseError, | 
|---|
| 71 | SocketAddressNotAvailableError, | 
|---|
| 72 | UnsupportedSocketOperationError,        /* 10 */ | 
|---|
| 73 | UnfinishedSocketOperationError, | 
|---|
| 74 | ProxyAuthenticationRequiredError, | 
|---|
| 75 | SslHandshakeFailedError, | 
|---|
| 76 | ProxyConnectionRefusedError, | 
|---|
| 77 | ProxyConnectionClosedError,             /* 15 */ | 
|---|
| 78 | ProxyConnectionTimeoutError, | 
|---|
| 79 | ProxyNotFoundError, | 
|---|
| 80 | ProxyProtocolError, | 
|---|
| 81 | OperationError, | 
|---|
| 82 | SslInternalError,                       /* 20 */ | 
|---|
| 83 | SslInvalidUserDataError, | 
|---|
| 84 | TemporaryError, | 
|---|
| 85 |  | 
|---|
| 86 | UnknownSocketError = -1 | 
|---|
| 87 | }; | 
|---|
| 88 | Q_ENUM(SocketError) | 
|---|
| 89 | enum SocketState { | 
|---|
| 90 | UnconnectedState, | 
|---|
| 91 | HostLookupState, | 
|---|
| 92 | ConnectingState, | 
|---|
| 93 | ConnectedState, | 
|---|
| 94 | BoundState, | 
|---|
| 95 | ListeningState, | 
|---|
| 96 | ClosingState | 
|---|
| 97 | }; | 
|---|
| 98 | Q_ENUM(SocketState) | 
|---|
| 99 | enum SocketOption { | 
|---|
| 100 | LowDelayOption, // TCP_NODELAY | 
|---|
| 101 | KeepAliveOption, // SO_KEEPALIVE | 
|---|
| 102 | MulticastTtlOption, // IP_MULTICAST_TTL | 
|---|
| 103 | MulticastLoopbackOption, // IP_MULTICAST_LOOPBACK | 
|---|
| 104 | TypeOfServiceOption, //IP_TOS | 
|---|
| 105 | SendBufferSizeSocketOption,    //SO_SNDBUF | 
|---|
| 106 | ReceiveBufferSizeSocketOption,  //SO_RCVBUF | 
|---|
| 107 | PathMtuSocketOption // IP_MTU | 
|---|
| 108 | }; | 
|---|
| 109 | Q_ENUM(SocketOption) | 
|---|
| 110 | enum BindFlag { | 
|---|
| 111 | DefaultForPlatform = 0x0, | 
|---|
| 112 | ShareAddress = 0x1, | 
|---|
| 113 | DontShareAddress = 0x2, | 
|---|
| 114 | ReuseAddressHint = 0x4 | 
|---|
| 115 | }; | 
|---|
| 116 | Q_DECLARE_FLAGS(BindMode, BindFlag) | 
|---|
| 117 | enum PauseMode { | 
|---|
| 118 | PauseNever = 0x0, | 
|---|
| 119 | PauseOnSslErrors = 0x1 | 
|---|
| 120 | }; | 
|---|
| 121 | Q_DECLARE_FLAGS(PauseModes, PauseMode) | 
|---|
| 122 |  | 
|---|
| 123 | QAbstractSocket(SocketType socketType, QObject *parent); | 
|---|
| 124 | virtual ~QAbstractSocket(); | 
|---|
| 125 |  | 
|---|
| 126 | virtual void resume(); // to continue after proxy authentication required, SSL errors etc. | 
|---|
| 127 | PauseModes pauseMode() const; | 
|---|
| 128 | void setPauseMode(PauseModes pauseMode); | 
|---|
| 129 |  | 
|---|
| 130 | virtual bool bind(const QHostAddress &address, quint16 port = 0, | 
|---|
| 131 | BindMode mode = DefaultForPlatform); | 
|---|
| 132 | #if QT_VERSION >= QT_VERSION_CHECK(7,0,0) || defined(Q_QDOC) | 
|---|
| 133 | bool bind(QHostAddress::SpecialAddress addr, quint16 port = 0, BindMode mode = DefaultForPlatform) | 
|---|
| 134 | { return bind(QHostAddress(addr), port, mode); } | 
|---|
| 135 | bool bind(quint16 port = 0, BindMode mode = DefaultForPlatform) | 
|---|
| 136 | { return bind(QHostAddress::Any, port, mode); } | 
|---|
| 137 | #else | 
|---|
| 138 | bool bind(quint16 port = 0, BindMode mode = DefaultForPlatform); | 
|---|
| 139 | #endif | 
|---|
| 140 |  | 
|---|
| 141 | virtual void connectToHost(const QString &hostName, quint16 port, OpenMode mode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol); | 
|---|
| 142 | void connectToHost(const QHostAddress &address, quint16 port, OpenMode mode = ReadWrite); | 
|---|
| 143 | virtual void disconnectFromHost(); | 
|---|
| 144 |  | 
|---|
| 145 | bool isValid() const; | 
|---|
| 146 |  | 
|---|
| 147 | qint64 bytesAvailable() const override; | 
|---|
| 148 | qint64 bytesToWrite() const override; | 
|---|
| 149 |  | 
|---|
| 150 | quint16 localPort() const; | 
|---|
| 151 | QHostAddress localAddress() const; | 
|---|
| 152 | quint16 peerPort() const; | 
|---|
| 153 | QHostAddress peerAddress() const; | 
|---|
| 154 | QString peerName() const; | 
|---|
| 155 |  | 
|---|
| 156 | qint64 readBufferSize() const; | 
|---|
| 157 | virtual void setReadBufferSize(qint64 size); | 
|---|
| 158 |  | 
|---|
| 159 | void abort(); | 
|---|
| 160 |  | 
|---|
| 161 | virtual qintptr socketDescriptor() const; | 
|---|
| 162 | virtual bool setSocketDescriptor(qintptr socketDescriptor, SocketState state = ConnectedState, | 
|---|
| 163 | OpenMode openMode = ReadWrite); | 
|---|
| 164 |  | 
|---|
| 165 | virtual void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value); | 
|---|
| 166 | virtual QVariant socketOption(QAbstractSocket::SocketOption option); | 
|---|
| 167 |  | 
|---|
| 168 | SocketType socketType() const; | 
|---|
| 169 | SocketState state() const; | 
|---|
| 170 | SocketError error() const; | 
|---|
| 171 |  | 
|---|
| 172 | // from QIODevice | 
|---|
| 173 | void close() override; | 
|---|
| 174 | bool isSequential() const override; | 
|---|
| 175 | bool flush(); | 
|---|
| 176 |  | 
|---|
| 177 | // for synchronous access | 
|---|
| 178 | virtual bool waitForConnected(int msecs = 30000); | 
|---|
| 179 | bool waitForReadyRead(int msecs = 30000) override; | 
|---|
| 180 | bool waitForBytesWritten(int msecs = 30000) override; | 
|---|
| 181 | virtual bool waitForDisconnected(int msecs = 30000); | 
|---|
| 182 |  | 
|---|
| 183 | #ifndef QT_NO_NETWORKPROXY | 
|---|
| 184 | void setProxy(const QNetworkProxy &networkProxy); | 
|---|
| 185 | QNetworkProxy proxy() const; | 
|---|
| 186 | QString protocolTag() const; | 
|---|
| 187 | void setProtocolTag(const QString &tag); | 
|---|
| 188 | #endif | 
|---|
| 189 |  | 
|---|
| 190 | Q_SIGNALS: | 
|---|
| 191 | void hostFound(); | 
|---|
| 192 | void connected(); | 
|---|
| 193 | void disconnected(); | 
|---|
| 194 | void stateChanged(QAbstractSocket::SocketState); | 
|---|
| 195 | void errorOccurred(QAbstractSocket::SocketError); | 
|---|
| 196 | #ifndef QT_NO_NETWORKPROXY | 
|---|
| 197 | void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator); | 
|---|
| 198 | #endif | 
|---|
| 199 |  | 
|---|
| 200 | protected: | 
|---|
| 201 | qint64 readData(char *data, qint64 maxlen) override; | 
|---|
| 202 | qint64 readLineData(char *data, qint64 maxlen) override; | 
|---|
| 203 | qint64 skipData(qint64 maxSize) override; | 
|---|
| 204 | qint64 writeData(const char *data, qint64 len) override; | 
|---|
| 205 |  | 
|---|
| 206 | void setSocketState(SocketState state); | 
|---|
| 207 | void setSocketError(SocketError socketError); | 
|---|
| 208 | void setLocalPort(quint16 port); | 
|---|
| 209 | void setLocalAddress(const QHostAddress &address); | 
|---|
| 210 | void setPeerPort(quint16 port); | 
|---|
| 211 | void setPeerAddress(const QHostAddress &address); | 
|---|
| 212 | void setPeerName(const QString &name); | 
|---|
| 213 |  | 
|---|
| 214 | QAbstractSocket(SocketType socketType, QAbstractSocketPrivate &dd, QObject *parent = nullptr); | 
|---|
| 215 |  | 
|---|
| 216 | private: | 
|---|
| 217 | Q_DECLARE_PRIVATE(QAbstractSocket) | 
|---|
| 218 | Q_DISABLE_COPY_MOVE(QAbstractSocket) | 
|---|
| 219 |  | 
|---|
| 220 | Q_PRIVATE_SLOT(d_func(), void _q_connectToNextAddress()) | 
|---|
| 221 | Q_PRIVATE_SLOT(d_func(), void _q_startConnecting(const QHostInfo &)) | 
|---|
| 222 | Q_PRIVATE_SLOT(d_func(), void _q_abortConnectionAttempt()) | 
|---|
| 223 | Q_PRIVATE_SLOT(d_func(), void _q_testConnection()) | 
|---|
| 224 | }; | 
|---|
| 225 |  | 
|---|
| 226 |  | 
|---|
| 227 | Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocket::BindMode) | 
|---|
| 228 | Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocket::PauseModes) | 
|---|
| 229 |  | 
|---|
| 230 | #ifndef QT_NO_DEBUG_STREAM | 
|---|
| 231 | Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketError); | 
|---|
| 232 | Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketState); | 
|---|
| 233 | #endif | 
|---|
| 234 |  | 
|---|
| 235 | QT_END_NAMESPACE | 
|---|
| 236 |  | 
|---|
| 237 | QT_DECL_METATYPE_EXTERN_TAGGED(QAbstractSocket::SocketState, | 
|---|
| 238 | QAbstractSocket__SocketState, Q_NETWORK_EXPORT) | 
|---|
| 239 | QT_DECL_METATYPE_EXTERN_TAGGED(QAbstractSocket::SocketError, | 
|---|
| 240 | QAbstractSocket__SocketError, Q_NETWORK_EXPORT) | 
|---|
| 241 |  | 
|---|
| 242 | #endif // QABSTRACTSOCKET_H | 
|---|
| 243 |  | 
|---|