1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2015 The Qt Company Ltd. 4 ** Contact: http://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 http://www.qt.io/terms-conditions. For further 15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free 20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and 21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the 22 ** following information to ensure the GNU Lesser General Public License 23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and 24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 25 ** 26 ** As a special exception, The Qt Company gives you certain additional 27 ** rights. These rights are described in The Qt Company LGPL Exception 28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 29 ** 30 ** GNU General Public License Usage 31 ** Alternatively, this file may be used under the terms of the GNU 32 ** General Public License version 3.0 as published by the Free Software 33 ** Foundation and appearing in the file LICENSE.GPL included in the 34 ** packaging of this file. Please review the following information to 35 ** ensure the GNU General Public License version 3.0 requirements will be 36 ** met: http://www.gnu.org/copyleft/gpl.html. 37 ** 38 ** $QT_END_LICENSE$ 39 ** 40 ****************************************************************************/ 41 42 #ifndef QABSTRACTSOCKET_H 43 #define QABSTRACTSOCKET_H 44 45 #include <QtCore/qiodevice.h> 46 #include <QtCore/qobject.h> 47 #ifndef QT_NO_DEBUG_STREAM 48 #include <QtCore/qdebug.h> 49 #endif 50 51 QT_BEGIN_HEADER 52 53 QT_BEGIN_NAMESPACE 54 55 QT_MODULE(Network) 56 57 class QHostAddress; 58 #ifndef QT_NO_NETWORKPROXY 59 class QNetworkProxy; 60 #endif 61 class QAbstractSocketPrivate; 62 class QAuthenticator; 63 64 class Q_NETWORK_EXPORT QAbstractSocket : public QIODevice 65 { 66 Q_OBJECT 67 Q_ENUMS(SocketType NetworkLayerProtocol SocketError SocketState SocketOption) 68 public: 69 enum SocketType { 70 TcpSocket, 71 UdpSocket, 72 UnknownSocketType = -1 73 }; 74 enum NetworkLayerProtocol { 75 IPv4Protocol, 76 IPv6Protocol, 77 UnknownNetworkLayerProtocol = -1 78 }; 79 enum SocketError { 80 ConnectionRefusedError, 81 RemoteHostClosedError, 82 HostNotFoundError, 83 SocketAccessError, 84 SocketResourceError, 85 SocketTimeoutError, /* 5 */ 86 DatagramTooLargeError, 87 NetworkError, 88 AddressInUseError, 89 SocketAddressNotAvailableError, 90 UnsupportedSocketOperationError, /* 10 */ 91 UnfinishedSocketOperationError, 92 ProxyAuthenticationRequiredError, 93 SslHandshakeFailedError, 94 ProxyConnectionRefusedError, 95 ProxyConnectionClosedError, /* 15 */ 96 ProxyConnectionTimeoutError, 97 ProxyNotFoundError, 98 ProxyProtocolError, 99 100 UnknownSocketError = -1 101 }; 102 enum SocketState { 103 UnconnectedState, 104 HostLookupState, 105 ConnectingState, 106 ConnectedState, 107 BoundState, 108 ListeningState, 109 ClosingState 110 #ifdef QT3_SUPPORT 111 , 112 Idle = UnconnectedState, 113 HostLookup = HostLookupState, 114 Connecting = ConnectingState, 115 Connected = ConnectedState, 116 Closing = ClosingState, 117 Connection = ConnectedState 118 #endif 119 }; 120 enum SocketOption { 121 LowDelayOption, // TCP_NODELAY 122 KeepAliveOption, // SO_KEEPALIVE 123 MulticastTtlOption, // IP_MULTICAST_TTL 124 MulticastLoopbackOption // IP_MULTICAST_LOOPBACK 125 }; 126 127 QAbstractSocket(SocketType socketType, QObject *parent); 128 virtual ~QAbstractSocket(); 129 130 // ### Qt 5: Make connectToHost() and disconnectFromHost() virtual. 131 void connectToHost(const QString &hostName, quint16 port, OpenMode mode = ReadWrite); 132 void connectToHost(const QHostAddress &address, quint16 port, OpenMode mode = ReadWrite); 133 void disconnectFromHost(); 134 135 bool isValid() const; 136 137 qint64 bytesAvailable() const; 138 qint64 bytesToWrite() const; 139 140 bool canReadLine() const; 141 142 quint16 localPort() const; 143 QHostAddress localAddress() const; 144 quint16 peerPort() const; 145 QHostAddress peerAddress() const; 146 QString peerName() const; 147 148 // ### Qt 5: Make setReadBufferSize() virtual 149 qint64 readBufferSize() const; 150 void setReadBufferSize(qint64 size); 151 152 void abort(); 153 154 // ### Qt 5: Make socketDescriptor() and setSocketDescriptor() virtual. 155 int socketDescriptor() const; 156 bool setSocketDescriptor(int socketDescriptor, SocketState state = ConnectedState, 157 OpenMode openMode = ReadWrite); 158 159 // ### Qt 5: Make virtual? 160 void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value); 161 QVariant socketOption(QAbstractSocket::SocketOption option); 162 163 SocketType socketType() const; 164 SocketState state() const; 165 SocketError error() const; 166 167 // from QIODevice 168 void close(); 169 bool isSequential() const; 170 bool atEnd() const; 171 bool flush(); 172 173 // for synchronous access 174 // ### Qt 5: Make waitForConnected() and waitForDisconnected() virtual. 175 bool waitForConnected(int msecs = 30000); 176 bool waitForReadyRead(int msecs = 30000); 177 bool waitForBytesWritten(int msecs = 30000); 178 bool waitForDisconnected(int msecs = 30000); 179 180 #ifndef QT_NO_NETWORKPROXY 181 void setProxy(const QNetworkProxy &networkProxy); 182 QNetworkProxy proxy() const; 183 #endif 184 185 Q_SIGNALS: 186 void hostFound(); 187 void connected(); 188 void disconnected(); 189 void stateChanged(QAbstractSocket::SocketState); 190 void error(QAbstractSocket::SocketError); 191 #ifndef QT_NO_NETWORKPROXY 192 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator); 193 #endif 194 195 protected Q_SLOTS: 196 void connectToHostImplementation(const QString &hostName, quint16 port, OpenMode mode = ReadWrite); 197 void disconnectFromHostImplementation(); 198 199 protected: 200 qint64 readData(char *data, qint64 maxlen); 201 qint64 readLineData(char *data, qint64 maxlen); 202 qint64 writeData(const char *data, qint64 len); 203 204 void setSocketState(SocketState state); 205 void setSocketError(SocketError socketError); 206 void setLocalPort(quint16 port); 207 void setLocalAddress(const QHostAddress &address); 208 void setPeerPort(quint16 port); 209 void setPeerAddress(const QHostAddress &address); 210 void setPeerName(const QString &name); 211 212 QAbstractSocket(SocketType socketType, QAbstractSocketPrivate &dd, QObject *parent = 0); 213 214 private: 215 Q_DECLARE_PRIVATE(QAbstractSocket) 216 Q_DISABLE_COPY(QAbstractSocket) 217 218 Q_PRIVATE_SLOT(d_func(), void _q_connectToNextAddress()) 219 Q_PRIVATE_SLOT(d_func(), void _q_startConnecting(const QHostInfo &)) 220 Q_PRIVATE_SLOT(d_func(), void _q_abortConnectionAttempt()) 221 Q_PRIVATE_SLOT(d_func(), void _q_testConnection()) 222 Q_PRIVATE_SLOT(d_func(), void _q_forceDisconnect()) 223 224 #ifdef QT3_SUPPORT 225 public: 226 enum Error { 227 ErrConnectionRefused = ConnectionRefusedError, 228 ErrHostNotFound = HostNotFoundError, 229 ErrSocketRead = UnknownSocketError 230 }; socket()231 inline QT3_SUPPORT int socket() const { return socketDescriptor(); } setSocket(int socket)232 inline QT3_SUPPORT void setSocket(int socket) { setSocketDescriptor(socket); } 233 inline QT3_SUPPORT qulonglong waitForMore(int msecs, bool *timeout = 0) const 234 { 235 QAbstractSocket *that = const_cast<QAbstractSocket *>(this); 236 if (that->waitForReadyRead(msecs)) 237 return qulonglong(bytesAvailable()); 238 if (error() == SocketTimeoutError && timeout) 239 *timeout = true; 240 return 0; 241 } 242 typedef SocketState State; 243 Q_SIGNALS: 244 QT_MOC_COMPAT void connectionClosed(); // same as disconnected() 245 QT_MOC_COMPAT void delayedCloseFinished(); // same as disconnected() 246 247 248 #endif 249 }; 250 251 #ifndef QT_NO_DEBUG_STREAM 252 Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketError); 253 Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketState); 254 #endif 255 256 QT_END_NAMESPACE 257 258 QT_END_HEADER 259 260 #endif // QABSTRACTSOCKET_H 261