[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 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 | public:
|
---|
| 68 | enum SocketType {
|
---|
| 69 | TcpSocket,
|
---|
| 70 | UdpSocket,
|
---|
| 71 | UnknownSocketType = -1
|
---|
| 72 | };
|
---|
| 73 | enum NetworkLayerProtocol {
|
---|
| 74 | IPv4Protocol,
|
---|
| 75 | IPv6Protocol,
|
---|
| 76 | UnknownNetworkLayerProtocol = -1
|
---|
| 77 | };
|
---|
| 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 |
|
---|
| 99 | UnknownSocketError = -1
|
---|
| 100 | };
|
---|
| 101 | enum SocketState {
|
---|
| 102 | UnconnectedState,
|
---|
| 103 | HostLookupState,
|
---|
| 104 | ConnectingState,
|
---|
| 105 | ConnectedState,
|
---|
| 106 | BoundState,
|
---|
| 107 | ListeningState,
|
---|
| 108 | ClosingState
|
---|
| 109 | #ifdef QT3_SUPPORT
|
---|
| 110 | ,
|
---|
| 111 | Idle = UnconnectedState,
|
---|
| 112 | HostLookup = HostLookupState,
|
---|
| 113 | Connecting = ConnectingState,
|
---|
| 114 | Connected = ConnectedState,
|
---|
| 115 | Closing = ClosingState,
|
---|
| 116 | Connection = ConnectedState
|
---|
| 117 | #endif
|
---|
| 118 | };
|
---|
[561] | 119 | enum SocketOption {
|
---|
| 120 | LowDelayOption, // TCP_NODELAY
|
---|
| 121 | KeepAliveOption // SO_KEEPALIVE
|
---|
| 122 | };
|
---|
[2] | 123 |
|
---|
| 124 | QAbstractSocket(SocketType socketType, QObject *parent);
|
---|
| 125 | virtual ~QAbstractSocket();
|
---|
| 126 |
|
---|
| 127 | // ### Qt 5: Make connectToHost() and disconnectFromHost() virtual.
|
---|
| 128 | void connectToHost(const QString &hostName, quint16 port, OpenMode mode = ReadWrite);
|
---|
| 129 | void connectToHost(const QHostAddress &address, quint16 port, OpenMode mode = ReadWrite);
|
---|
| 130 | void disconnectFromHost();
|
---|
| 131 |
|
---|
| 132 | bool isValid() const;
|
---|
| 133 |
|
---|
| 134 | qint64 bytesAvailable() const;
|
---|
| 135 | qint64 bytesToWrite() const;
|
---|
| 136 |
|
---|
| 137 | bool canReadLine() const;
|
---|
| 138 |
|
---|
| 139 | quint16 localPort() const;
|
---|
| 140 | QHostAddress localAddress() const;
|
---|
| 141 | quint16 peerPort() const;
|
---|
| 142 | QHostAddress peerAddress() const;
|
---|
| 143 | QString peerName() const;
|
---|
| 144 |
|
---|
| 145 | // ### Qt 5: Make setReadBufferSize() virtual
|
---|
| 146 | qint64 readBufferSize() const;
|
---|
| 147 | void setReadBufferSize(qint64 size);
|
---|
| 148 |
|
---|
| 149 | void abort();
|
---|
| 150 |
|
---|
| 151 | // ### Qt 5: Make socketDescriptor() and setSocketDescriptor() virtual.
|
---|
| 152 | int socketDescriptor() const;
|
---|
| 153 | bool setSocketDescriptor(int socketDescriptor, SocketState state = ConnectedState,
|
---|
| 154 | OpenMode openMode = ReadWrite);
|
---|
| 155 |
|
---|
[561] | 156 | // ### Qt 5: Make virtual?
|
---|
| 157 | void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
|
---|
| 158 | QVariant socketOption(QAbstractSocket::SocketOption option);
|
---|
| 159 |
|
---|
[2] | 160 | SocketType socketType() const;
|
---|
| 161 | SocketState state() const;
|
---|
| 162 | SocketError error() const;
|
---|
| 163 |
|
---|
| 164 | // from QIODevice
|
---|
| 165 | void close();
|
---|
| 166 | bool isSequential() const;
|
---|
| 167 | bool atEnd() const;
|
---|
| 168 | bool flush();
|
---|
| 169 |
|
---|
| 170 | // for synchronous access
|
---|
| 171 | // ### Qt 5: Make waitForConnected() and waitForDisconnected() virtual.
|
---|
| 172 | bool waitForConnected(int msecs = 30000);
|
---|
| 173 | bool waitForReadyRead(int msecs = 30000);
|
---|
| 174 | bool waitForBytesWritten(int msecs = 30000);
|
---|
| 175 | bool waitForDisconnected(int msecs = 30000);
|
---|
| 176 |
|
---|
| 177 | #ifndef QT_NO_NETWORKPROXY
|
---|
| 178 | void setProxy(const QNetworkProxy &networkProxy);
|
---|
| 179 | QNetworkProxy proxy() const;
|
---|
| 180 | #endif
|
---|
| 181 |
|
---|
| 182 | Q_SIGNALS:
|
---|
| 183 | void hostFound();
|
---|
| 184 | void connected();
|
---|
| 185 | void disconnected();
|
---|
| 186 | void stateChanged(QAbstractSocket::SocketState);
|
---|
| 187 | void error(QAbstractSocket::SocketError);
|
---|
| 188 | #ifndef QT_NO_NETWORKPROXY
|
---|
| 189 | void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
|
---|
| 190 | #endif
|
---|
| 191 |
|
---|
| 192 | protected Q_SLOTS:
|
---|
| 193 | void connectToHostImplementation(const QString &hostName, quint16 port, OpenMode mode = ReadWrite);
|
---|
| 194 | void disconnectFromHostImplementation();
|
---|
| 195 |
|
---|
| 196 | protected:
|
---|
| 197 | qint64 readData(char *data, qint64 maxlen);
|
---|
| 198 | qint64 readLineData(char *data, qint64 maxlen);
|
---|
| 199 | qint64 writeData(const char *data, qint64 len);
|
---|
| 200 |
|
---|
| 201 | void setSocketState(SocketState state);
|
---|
| 202 | void setSocketError(SocketError socketError);
|
---|
| 203 | void setLocalPort(quint16 port);
|
---|
| 204 | void setLocalAddress(const QHostAddress &address);
|
---|
| 205 | void setPeerPort(quint16 port);
|
---|
| 206 | void setPeerAddress(const QHostAddress &address);
|
---|
| 207 | void setPeerName(const QString &name);
|
---|
| 208 |
|
---|
| 209 | QAbstractSocket(SocketType socketType, QAbstractSocketPrivate &dd, QObject *parent = 0);
|
---|
| 210 |
|
---|
| 211 | private:
|
---|
| 212 | Q_DECLARE_PRIVATE(QAbstractSocket)
|
---|
| 213 | Q_DISABLE_COPY(QAbstractSocket)
|
---|
| 214 |
|
---|
| 215 | Q_PRIVATE_SLOT(d_func(), void _q_connectToNextAddress())
|
---|
| 216 | Q_PRIVATE_SLOT(d_func(), void _q_startConnecting(const QHostInfo &))
|
---|
| 217 | Q_PRIVATE_SLOT(d_func(), void _q_abortConnectionAttempt())
|
---|
| 218 | Q_PRIVATE_SLOT(d_func(), void _q_testConnection())
|
---|
[561] | 219 | Q_PRIVATE_SLOT(d_func(), void _q_forceDisconnect())
|
---|
[2] | 220 |
|
---|
| 221 | #ifdef QT3_SUPPORT
|
---|
| 222 | public:
|
---|
| 223 | enum Error {
|
---|
| 224 | ErrConnectionRefused = ConnectionRefusedError,
|
---|
| 225 | ErrHostNotFound = HostNotFoundError,
|
---|
| 226 | ErrSocketRead = UnknownSocketError
|
---|
| 227 | };
|
---|
| 228 | inline QT3_SUPPORT int socket() const { return socketDescriptor(); }
|
---|
| 229 | inline QT3_SUPPORT void setSocket(int socket) { setSocketDescriptor(socket); }
|
---|
| 230 | inline QT3_SUPPORT qulonglong waitForMore(int msecs, bool *timeout = 0) const
|
---|
| 231 | {
|
---|
| 232 | QAbstractSocket *that = const_cast<QAbstractSocket *>(this);
|
---|
| 233 | if (that->waitForReadyRead(msecs))
|
---|
| 234 | return qulonglong(bytesAvailable());
|
---|
| 235 | if (error() == SocketTimeoutError && timeout)
|
---|
| 236 | *timeout = true;
|
---|
| 237 | return 0;
|
---|
| 238 | }
|
---|
| 239 | typedef SocketState State;
|
---|
| 240 | Q_SIGNALS:
|
---|
| 241 | QT_MOC_COMPAT void connectionClosed(); // same as disconnected()
|
---|
| 242 | QT_MOC_COMPAT void delayedCloseFinished(); // same as disconnected()
|
---|
| 243 |
|
---|
| 244 |
|
---|
| 245 | #endif
|
---|
| 246 | };
|
---|
| 247 |
|
---|
| 248 | #ifndef QT_NO_DEBUG_STREAM
|
---|
| 249 | Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketError);
|
---|
| 250 | Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketState);
|
---|
| 251 | #endif
|
---|
| 252 |
|
---|
| 253 | QT_END_NAMESPACE
|
---|
| 254 |
|
---|
| 255 | QT_END_HEADER
|
---|
| 256 |
|
---|
| 257 | #endif // QABSTRACTSOCKET_H
|
---|