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