qabstractsocket.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #ifndef QABSTRACTSOCKET_H
  40. #define QABSTRACTSOCKET_H
  41. #include <QtCore/qiodevice.h>
  42. #include <QtCore/qobject.h>
  43. #ifndef QT_NO_DEBUG_STREAM
  44. #include <QtCore/qdebug.h>
  45. #endif
  46. QT_BEGIN_NAMESPACE
  47. class QHostAddress;
  48. #ifndef QT_NO_NETWORKPROXY
  49. class QNetworkProxy;
  50. #endif
  51. class QAbstractSocketPrivate;
  52. class QAuthenticator;
  53. class Q_NETWORK_EXPORT QAbstractSocket : public QIODevice
  54. {
  55. Q_OBJECT
  56. public:
  57. enum SocketType {
  58. TcpSocket,
  59. UdpSocket,
  60. UnknownSocketType = -1
  61. };
  62. Q_ENUM(SocketType)
  63. enum NetworkLayerProtocol {
  64. IPv4Protocol,
  65. IPv6Protocol,
  66. AnyIPProtocol,
  67. UnknownNetworkLayerProtocol = -1
  68. };
  69. Q_ENUM(NetworkLayerProtocol)
  70. enum SocketError {
  71. ConnectionRefusedError,
  72. RemoteHostClosedError,
  73. HostNotFoundError,
  74. SocketAccessError,
  75. SocketResourceError,
  76. SocketTimeoutError, /* 5 */
  77. DatagramTooLargeError,
  78. NetworkError,
  79. AddressInUseError,
  80. SocketAddressNotAvailableError,
  81. UnsupportedSocketOperationError, /* 10 */
  82. UnfinishedSocketOperationError,
  83. ProxyAuthenticationRequiredError,
  84. SslHandshakeFailedError,
  85. ProxyConnectionRefusedError,
  86. ProxyConnectionClosedError, /* 15 */
  87. ProxyConnectionTimeoutError,
  88. ProxyNotFoundError,
  89. ProxyProtocolError,
  90. OperationError,
  91. SslInternalError, /* 20 */
  92. SslInvalidUserDataError,
  93. TemporaryError,
  94. UnknownSocketError = -1
  95. };
  96. Q_ENUM(SocketError)
  97. enum SocketState {
  98. UnconnectedState,
  99. HostLookupState,
  100. ConnectingState,
  101. ConnectedState,
  102. BoundState,
  103. ListeningState,
  104. ClosingState
  105. };
  106. Q_ENUM(SocketState)
  107. enum SocketOption {
  108. LowDelayOption, // TCP_NODELAY
  109. KeepAliveOption, // SO_KEEPALIVE
  110. MulticastTtlOption, // IP_MULTICAST_TTL
  111. MulticastLoopbackOption, // IP_MULTICAST_LOOPBACK
  112. TypeOfServiceOption, //IP_TOS
  113. SendBufferSizeSocketOption, //SO_SNDBUF
  114. ReceiveBufferSizeSocketOption //SO_RCVBUF
  115. };
  116. Q_ENUM(SocketOption)
  117. enum BindFlag {
  118. DefaultForPlatform = 0x0,
  119. ShareAddress = 0x1,
  120. DontShareAddress = 0x2,
  121. ReuseAddressHint = 0x4
  122. };
  123. Q_DECLARE_FLAGS(BindMode, BindFlag)
  124. enum PauseMode {
  125. PauseNever = 0x0,
  126. PauseOnSslErrors = 0x1
  127. };
  128. Q_DECLARE_FLAGS(PauseModes, PauseMode)
  129. QAbstractSocket(SocketType socketType, QObject *parent);
  130. virtual ~QAbstractSocket();
  131. virtual void resume(); // to continue after proxy authentication required, SSL errors etc.
  132. PauseModes pauseMode() const;
  133. void setPauseMode(PauseModes pauseMode);
  134. // ### Qt6: make the first one virtual
  135. bool bind(const QHostAddress &address, quint16 port = 0, BindMode mode = DefaultForPlatform);
  136. bool bind(quint16 port = 0, BindMode mode = DefaultForPlatform);
  137. // ### Qt6: de-virtualize connectToHost(QHostAddress) overload
  138. virtual void connectToHost(const QString &hostName, quint16 port, OpenMode mode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol);
  139. virtual void connectToHost(const QHostAddress &address, quint16 port, OpenMode mode = ReadWrite);
  140. virtual void disconnectFromHost();
  141. bool isValid() const;
  142. qint64 bytesAvailable() const Q_DECL_OVERRIDE;
  143. qint64 bytesToWrite() const Q_DECL_OVERRIDE;
  144. bool canReadLine() const Q_DECL_OVERRIDE; // ### Qt6: remove me
  145. quint16 localPort() const;
  146. QHostAddress localAddress() const;
  147. quint16 peerPort() const;
  148. QHostAddress peerAddress() const;
  149. QString peerName() const;
  150. qint64 readBufferSize() const;
  151. virtual void setReadBufferSize(qint64 size);
  152. void abort();
  153. virtual qintptr socketDescriptor() const;
  154. virtual bool setSocketDescriptor(qintptr socketDescriptor, SocketState state = ConnectedState,
  155. OpenMode openMode = ReadWrite);
  156. virtual void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
  157. virtual QVariant socketOption(QAbstractSocket::SocketOption option);
  158. SocketType socketType() const;
  159. SocketState state() const;
  160. SocketError error() const;
  161. // from QIODevice
  162. void close() Q_DECL_OVERRIDE;
  163. bool isSequential() const Q_DECL_OVERRIDE;
  164. bool atEnd() const Q_DECL_OVERRIDE; // ### Qt6: remove me
  165. bool flush();
  166. // for synchronous access
  167. virtual bool waitForConnected(int msecs = 30000);
  168. bool waitForReadyRead(int msecs = 30000) Q_DECL_OVERRIDE;
  169. bool waitForBytesWritten(int msecs = 30000) Q_DECL_OVERRIDE;
  170. virtual bool waitForDisconnected(int msecs = 30000);
  171. #ifndef QT_NO_NETWORKPROXY
  172. void setProxy(const QNetworkProxy &networkProxy);
  173. QNetworkProxy proxy() const;
  174. #endif
  175. Q_SIGNALS:
  176. void hostFound();
  177. void connected();
  178. void disconnected();
  179. void stateChanged(QAbstractSocket::SocketState);
  180. void error(QAbstractSocket::SocketError);
  181. #ifndef QT_NO_NETWORKPROXY
  182. void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
  183. #endif
  184. protected:
  185. qint64 readData(char *data, qint64 maxlen) Q_DECL_OVERRIDE;
  186. qint64 readLineData(char *data, qint64 maxlen) Q_DECL_OVERRIDE;
  187. qint64 writeData(const char *data, qint64 len) Q_DECL_OVERRIDE;
  188. void setSocketState(SocketState state);
  189. void setSocketError(SocketError socketError);
  190. void setLocalPort(quint16 port);
  191. void setLocalAddress(const QHostAddress &address);
  192. void setPeerPort(quint16 port);
  193. void setPeerAddress(const QHostAddress &address);
  194. void setPeerName(const QString &name);
  195. QAbstractSocket(SocketType socketType, QAbstractSocketPrivate &dd, QObject *parent = Q_NULLPTR);
  196. private:
  197. Q_DECLARE_PRIVATE(QAbstractSocket)
  198. Q_DISABLE_COPY(QAbstractSocket)
  199. Q_PRIVATE_SLOT(d_func(), void _q_connectToNextAddress())
  200. Q_PRIVATE_SLOT(d_func(), void _q_startConnecting(const QHostInfo &))
  201. Q_PRIVATE_SLOT(d_func(), void _q_abortConnectionAttempt())
  202. Q_PRIVATE_SLOT(d_func(), void _q_testConnection())
  203. Q_PRIVATE_SLOT(d_func(), void _q_forceDisconnect())
  204. };
  205. Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocket::BindMode)
  206. Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocket::PauseModes)
  207. #ifndef QT_NO_DEBUG_STREAM
  208. Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketError);
  209. Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketState);
  210. #endif
  211. QT_END_NAMESPACE
  212. Q_DECLARE_METATYPE(QAbstractSocket::SocketState)
  213. Q_DECLARE_METATYPE(QAbstractSocket::SocketError)
  214. #endif // QABSTRACTSOCKET_H