0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the QtNetwork module of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
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 |
**
|
|
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.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
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 |
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 |
enum SocketOption {
|
|
120 |
LowDelayOption, // TCP_NODELAY
|
|
121 |
KeepAliveOption // SO_KEEPALIVE
|
|
122 |
};
|
|
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 |
|
|
156 |
// ### Qt 5: Make virtual?
|
|
157 |
void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
|
|
158 |
QVariant socketOption(QAbstractSocket::SocketOption option);
|
|
159 |
|
|
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())
|
|
219 |
|
|
220 |
#ifdef QT3_SUPPORT
|
|
221 |
public:
|
|
222 |
enum Error {
|
|
223 |
ErrConnectionRefused = ConnectionRefusedError,
|
|
224 |
ErrHostNotFound = HostNotFoundError,
|
|
225 |
ErrSocketRead = UnknownSocketError
|
|
226 |
};
|
|
227 |
inline QT3_SUPPORT int socket() const { return socketDescriptor(); }
|
|
228 |
inline QT3_SUPPORT void setSocket(int socket) { setSocketDescriptor(socket); }
|
|
229 |
inline QT3_SUPPORT qulonglong waitForMore(int msecs, bool *timeout = 0) const
|
|
230 |
{
|
|
231 |
QAbstractSocket *that = const_cast<QAbstractSocket *>(this);
|
|
232 |
if (that->waitForReadyRead(msecs))
|
|
233 |
return qulonglong(bytesAvailable());
|
|
234 |
if (error() == SocketTimeoutError && timeout)
|
|
235 |
*timeout = true;
|
|
236 |
return 0;
|
|
237 |
}
|
|
238 |
typedef SocketState State;
|
|
239 |
Q_SIGNALS:
|
|
240 |
QT_MOC_COMPAT void connectionClosed(); // same as disconnected()
|
|
241 |
QT_MOC_COMPAT void delayedCloseFinished(); // same as disconnected()
|
|
242 |
|
|
243 |
|
|
244 |
#endif
|
|
245 |
};
|
|
246 |
|
|
247 |
#ifndef QT_NO_DEBUG_STREAM
|
|
248 |
Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketError);
|
|
249 |
Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketState);
|
|
250 |
#endif
|
|
251 |
|
|
252 |
QT_END_NAMESPACE
|
|
253 |
|
|
254 |
QT_END_HEADER
|
|
255 |
|
|
256 |
#endif // QABSTRACTSOCKET_H
|