|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 //#define QABSTRACTSOCKET_DEBUG |
|
43 |
|
44 /*! |
|
45 \class QAbstractSocket |
|
46 |
|
47 \brief The QAbstractSocket class provides the base functionality |
|
48 common to all socket types. |
|
49 |
|
50 \reentrant |
|
51 \ingroup network |
|
52 \inmodule QtNetwork |
|
53 |
|
54 QAbstractSocket is the base class for QTcpSocket and QUdpSocket |
|
55 and contains all common functionality of these two classes. If |
|
56 you need a socket, you have two options: |
|
57 |
|
58 \list |
|
59 \i Instantiate QTcpSocket or QUdpSocket. |
|
60 \i Create a native socket descriptor, instantiate |
|
61 QAbstractSocket, and call setSocketDescriptor() to wrap the |
|
62 native socket. |
|
63 \endlist |
|
64 |
|
65 TCP (Transmission Control Protocol) is a reliable, |
|
66 stream-oriented, connection-oriented transport protocol. UDP |
|
67 (User Datagram Protocol) is an unreliable, datagram-oriented, |
|
68 connectionless protocol. In practice, this means that TCP is |
|
69 better suited for continuous transmission of data, whereas the |
|
70 more lightweight UDP can be used when reliability isn't |
|
71 important. |
|
72 |
|
73 QAbstractSocket's API unifies most of the differences between the |
|
74 two protocols. For example, although UDP is connectionless, |
|
75 connectToHost() establishes a virtual connection for UDP sockets, |
|
76 enabling you to use QAbstractSocket in more or less the same way |
|
77 regardless of the underlying protocol. Internally, |
|
78 QAbstractSocket remembers the address and port passed to |
|
79 connectToHost(), and functions like read() and write() use these |
|
80 values. |
|
81 |
|
82 At any time, QAbstractSocket has a state (returned by |
|
83 state()). The initial state is UnconnectedState. After |
|
84 calling connectToHost(), the socket first enters |
|
85 HostLookupState. If the host is found, QAbstractSocket enters |
|
86 ConnectingState and emits the hostFound() signal. When the |
|
87 connection has been established, it enters ConnectedState and |
|
88 emits connected(). If an error occurs at any stage, error() is |
|
89 emitted. Whenever the state changes, stateChanged() is emitted. |
|
90 For convenience, isValid() returns true if the socket is ready for |
|
91 reading and writing, but note that the socket's state must be |
|
92 ConnectedState before reading and writing can occur. |
|
93 |
|
94 Read or write data by calling read() or write(), or use the |
|
95 convenience functions readLine() and readAll(). QAbstractSocket |
|
96 also inherits getChar(), putChar(), and ungetChar() from |
|
97 QIODevice, which work on single bytes. The bytesWritten() signal |
|
98 is emitted when data has been written to the socket (i.e., when |
|
99 the client has read the data). Note that Qt does not limit the |
|
100 write buffer size. You can monitor its size by listening to this |
|
101 signal. |
|
102 |
|
103 The readyRead() signal is emitted every time a new chunk of data |
|
104 has arrived. bytesAvailable() then returns the number of bytes |
|
105 that are available for reading. Typically, you would connect the |
|
106 readyRead() signal to a slot and read all available data there. |
|
107 If you don't read all the data at once, the remaining data will |
|
108 still be available later, and any new incoming data will be |
|
109 appended to QAbstractSocket's internal read buffer. To limit the |
|
110 size of the read buffer, call setReadBufferSize(). |
|
111 |
|
112 To close the socket, call disconnectFromHost(). QAbstractSocket enters |
|
113 QAbstractSocket::ClosingState. After all pending data has been written to |
|
114 the socket, QAbstractSocket actually closes the socket, enters |
|
115 QAbstractSocket::ClosedState, and emits disconnected(). If you want to |
|
116 abort a connection immediately, discarding all pending data, call abort() |
|
117 instead. If the remote host closes the connection, QAbstractSocket will |
|
118 emit error(QAbstractSocket::RemoteHostClosedError), during which the socket |
|
119 state will still be ConnectedState, and then the disconnected() signal |
|
120 will be emitted. |
|
121 |
|
122 The port and address of the connected peer is fetched by calling |
|
123 peerPort() and peerAddress(). peerName() returns the host name of |
|
124 the peer, as passed to connectToHost(). localPort() and |
|
125 localAddress() return the port and address of the local socket. |
|
126 |
|
127 QAbstractSocket provides a set of functions that suspend the |
|
128 calling thread until certain signals are emitted. These functions |
|
129 can be used to implement blocking sockets: |
|
130 |
|
131 \list |
|
132 \o waitForConnected() blocks until a connection has been established. |
|
133 |
|
134 \o waitForReadyRead() blocks until new data is available for |
|
135 reading. |
|
136 |
|
137 \o waitForBytesWritten() blocks until one payload of data has been |
|
138 written to the socket. |
|
139 |
|
140 \o waitForDisconnected() blocks until the connection has closed. |
|
141 \endlist |
|
142 |
|
143 We show an example: |
|
144 |
|
145 \snippet doc/src/snippets/network/tcpwait.cpp 0 |
|
146 |
|
147 If \l{QIODevice::}{waitForReadyRead()} returns false, the |
|
148 connection has been closed or an error has occurred. |
|
149 |
|
150 Programming with a blocking socket is radically different from |
|
151 programming with a non-blocking socket. A blocking socket doesn't |
|
152 require an event loop and typically leads to simpler code. |
|
153 However, in a GUI application, blocking sockets should only be |
|
154 used in non-GUI threads, to avoid freezing the user interface. |
|
155 See the \l network/fortuneclient and \l network/blockingfortuneclient |
|
156 examples for an overview of both approaches. |
|
157 |
|
158 \note We discourage the use of the blocking functions together |
|
159 with signals. One of the two possibilities should be used. |
|
160 |
|
161 QAbstractSocket can be used with QTextStream and QDataStream's |
|
162 stream operators (operator<<() and operator>>()). There is one |
|
163 issue to be aware of, though: You must make sure that enough data |
|
164 is available before attempting to read it using operator>>(). |
|
165 |
|
166 \sa QFtp, QNetworkAccessManager, QTcpServer |
|
167 */ |
|
168 |
|
169 /*! |
|
170 \fn void QAbstractSocket::hostFound() |
|
171 |
|
172 This signal is emitted after connectToHost() has been called and |
|
173 the host lookup has succeeded. |
|
174 |
|
175 \note Since Qt 4.6.3 QAbstractSocket may emit hostFound() |
|
176 directly from the connectToHost() call since a DNS result could have been |
|
177 cached. |
|
178 |
|
179 \sa connected() |
|
180 */ |
|
181 |
|
182 /*! |
|
183 \fn void QAbstractSocket::connected() |
|
184 |
|
185 This signal is emitted after connectToHost() has been called and |
|
186 a connection has been successfully established. |
|
187 |
|
188 \note On some operating systems the connected() signal may |
|
189 be directly emitted from the connectToHost() call for connections |
|
190 to the localhost. |
|
191 |
|
192 \sa connectToHost(), disconnected() |
|
193 */ |
|
194 |
|
195 /*! |
|
196 \fn void QAbstractSocket::disconnected() |
|
197 |
|
198 This signal is emitted when the socket has been disconnected. |
|
199 |
|
200 \warning If you need to delete the sender() of this signal in a slot connected |
|
201 to it, use the \l{QObject::deleteLater()}{deleteLater()} function. |
|
202 |
|
203 \sa connectToHost(), disconnectFromHost(), abort() |
|
204 */ |
|
205 |
|
206 /*! |
|
207 \fn void QAbstractSocket::error(QAbstractSocket::SocketError socketError) |
|
208 |
|
209 This signal is emitted after an error occurred. The \a socketError |
|
210 parameter describes the type of error that occurred. |
|
211 |
|
212 QAbstractSocket::SocketError is not a registered metatype, so for queued |
|
213 connections, you will have to register it with Q_DECLARE_METATYPE() and |
|
214 qRegisterMetaType(). |
|
215 |
|
216 \sa error(), errorString(), {Creating Custom Qt Types} |
|
217 */ |
|
218 |
|
219 /*! |
|
220 \fn void QAbstractSocket::stateChanged(QAbstractSocket::SocketState socketState) |
|
221 |
|
222 This signal is emitted whenever QAbstractSocket's state changes. |
|
223 The \a socketState parameter is the new state. |
|
224 |
|
225 QAbstractSocket::SocketState is not a registered metatype, so for queued |
|
226 connections, you will have to register it with Q_REGISTER_METATYPE() and |
|
227 qRegisterMetaType(). |
|
228 |
|
229 \sa state(), {Creating Custom Qt Types} |
|
230 */ |
|
231 |
|
232 /*! |
|
233 \fn void QAbstractSocket::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator) |
|
234 \since 4.3 |
|
235 |
|
236 This signal can be emitted when a \a proxy that requires |
|
237 authentication is used. The \a authenticator object can then be |
|
238 filled in with the required details to allow authentication and |
|
239 continue the connection. |
|
240 |
|
241 \note It is not possible to use a QueuedConnection to connect to |
|
242 this signal, as the connection will fail if the authenticator has |
|
243 not been filled in with new information when the signal returns. |
|
244 |
|
245 \sa QAuthenticator, QNetworkProxy |
|
246 */ |
|
247 |
|
248 /*! |
|
249 \enum QAbstractSocket::NetworkLayerProtocol |
|
250 |
|
251 This enum describes the network layer protocol values used in Qt. |
|
252 |
|
253 \value IPv4Protocol IPv4 |
|
254 \value IPv6Protocol IPv6 |
|
255 \value UnknownNetworkLayerProtocol Other than IPv4 and IPv6 |
|
256 |
|
257 \sa QHostAddress::protocol() |
|
258 */ |
|
259 |
|
260 /*! |
|
261 \enum QAbstractSocket::SocketType |
|
262 |
|
263 This enum describes the transport layer protocol. |
|
264 |
|
265 \value TcpSocket TCP |
|
266 \value UdpSocket UDP |
|
267 \value UnknownSocketType Other than TCP and UDP |
|
268 |
|
269 \sa QAbstractSocket::socketType() |
|
270 */ |
|
271 |
|
272 /*! |
|
273 \enum QAbstractSocket::SocketError |
|
274 |
|
275 This enum describes the socket errors that can occur. |
|
276 |
|
277 \value ConnectionRefusedError The connection was refused by the |
|
278 peer (or timed out). |
|
279 \value RemoteHostClosedError The remote host closed the |
|
280 connection. Note that the client socket (i.e., this socket) |
|
281 will be closed after the remote close notification has |
|
282 been sent. |
|
283 \value HostNotFoundError The host address was not found. |
|
284 \value SocketAccessError The socket operation failed because the |
|
285 application lacked the required privileges. |
|
286 \value SocketResourceError The local system ran out of resources |
|
287 (e.g., too many sockets). |
|
288 \value SocketTimeoutError The socket operation timed out. |
|
289 \value DatagramTooLargeError The datagram was larger than the |
|
290 operating system's limit (which can be as low as 8192 |
|
291 bytes). |
|
292 \value NetworkError An error occurred with the network (e.g., the |
|
293 network cable was accidentally plugged out). |
|
294 \value AddressInUseError The address specified to QUdpSocket::bind() is |
|
295 already in use and was set to be exclusive. |
|
296 \value SocketAddressNotAvailableError The address specified to |
|
297 QUdpSocket::bind() does not belong to the host. |
|
298 \value UnsupportedSocketOperationError The requested socket operation is |
|
299 not supported by the local operating system (e.g., lack of |
|
300 IPv6 support). |
|
301 \value ProxyAuthenticationRequiredError The socket is using a proxy, and |
|
302 the proxy requires authentication. |
|
303 \value SslHandshakeFailedError The SSL/TLS handshake failed, so |
|
304 the connection was closed (only used in QSslSocket) |
|
305 \value UnfinishedSocketOperationError Used by QAbstractSocketEngine only, |
|
306 The last operation attempted has not finished yet (still in progress in |
|
307 the background). |
|
308 \value ProxyConnectionRefusedError Could not contact the proxy server because |
|
309 the connection to that server was denied |
|
310 \value ProxyConnectionClosedError The connection to the proxy server was closed |
|
311 unexpectedly (before the connection to the final peer was established) |
|
312 \value ProxyConnectionTimeoutError The connection to the proxy server timed out |
|
313 or the proxy server stopped responding in the authentication phase. |
|
314 \value ProxyNotFoundError The proxy address set with setProxy() (or the application |
|
315 proxy) was not found. |
|
316 \value ProxyProtocolError The connection negotiation with the proxy server |
|
317 because the response from the proxy server could not be understood. |
|
318 |
|
319 \value UnknownSocketError An unidentified error occurred. |
|
320 \sa QAbstractSocket::error() |
|
321 */ |
|
322 |
|
323 /*! |
|
324 \enum QAbstractSocket::SocketState |
|
325 |
|
326 This enum describes the different states in which a socket can be. |
|
327 |
|
328 \value UnconnectedState The socket is not connected. |
|
329 \value HostLookupState The socket is performing a host name lookup. |
|
330 \value ConnectingState The socket has started establishing a connection. |
|
331 \value ConnectedState A connection is established. |
|
332 \value BoundState The socket is bound to an address and port (for servers). |
|
333 \value ClosingState The socket is about to close (data may still |
|
334 be waiting to be written). |
|
335 \value ListeningState For internal use only. |
|
336 \omitvalue Idle |
|
337 \omitvalue HostLookup |
|
338 \omitvalue Connecting |
|
339 \omitvalue Connected |
|
340 \omitvalue Closing |
|
341 \omitvalue Connection |
|
342 |
|
343 \sa QAbstractSocket::state() |
|
344 */ |
|
345 |
|
346 /*! |
|
347 \enum QAbstractSocket::SocketOption |
|
348 \since 4.6 |
|
349 |
|
350 This enum represents the options that can be set on a socket. |
|
351 If desired, they can be set after having received the connected() signal from |
|
352 the socket or after having received a new socket from a QTcpServer. |
|
353 |
|
354 \value LowDelayOption Try to optimize the socket for low latency. For a QTcpSocket |
|
355 this would set the TCP_NODELAY option and disable Nagle's algorithm. Set this to 1 |
|
356 to enable. |
|
357 \value KeepAliveOption Set this to 1 to enable the SO_KEEPALIVE socket option |
|
358 |
|
359 \sa QAbstractSocket::setSocketOption(), QAbstractSocket::socketOption() |
|
360 */ |
|
361 |
|
362 #include "qabstractsocket.h" |
|
363 #include "qabstractsocket_p.h" |
|
364 |
|
365 #include "private/qhostinfo_p.h" |
|
366 |
|
367 #include <qabstracteventdispatcher.h> |
|
368 #include <qdatetime.h> |
|
369 #include <qhostaddress.h> |
|
370 #include <qhostinfo.h> |
|
371 #include <qmetaobject.h> |
|
372 #include <qpointer.h> |
|
373 #include <qtimer.h> |
|
374 |
|
375 #ifndef QT_NO_OPENSSL |
|
376 #include <QtNetwork/qsslsocket.h> |
|
377 #endif |
|
378 |
|
379 #include <private/qthread_p.h> |
|
380 |
|
381 #ifdef QABSTRACTSOCKET_DEBUG |
|
382 #include <qdebug.h> |
|
383 #endif |
|
384 |
|
385 #include <time.h> |
|
386 |
|
387 #define Q_CHECK_SOCKETENGINE(returnValue) do { \ |
|
388 if (!d->socketEngine) { \ |
|
389 return returnValue; \ |
|
390 } } while (0) |
|
391 |
|
392 #ifndef QABSTRACTSOCKET_BUFFERSIZE |
|
393 #define QABSTRACTSOCKET_BUFFERSIZE 32768 |
|
394 #endif |
|
395 #define QT_CONNECT_TIMEOUT 30000 |
|
396 #define QT_TRANSFER_TIMEOUT 120000 |
|
397 |
|
398 QT_BEGIN_NAMESPACE |
|
399 |
|
400 #if defined QABSTRACTSOCKET_DEBUG |
|
401 QT_BEGIN_INCLUDE_NAMESPACE |
|
402 #include <qstring.h> |
|
403 #include <ctype.h> |
|
404 QT_END_INCLUDE_NAMESPACE |
|
405 |
|
406 /* |
|
407 Returns a human readable representation of the first \a len |
|
408 characters in \a data. |
|
409 */ |
|
410 static QByteArray qt_prettyDebug(const char *data, int len, int maxLength) |
|
411 { |
|
412 if (!data) return "(null)"; |
|
413 QByteArray out; |
|
414 for (int i = 0; i < len; ++i) { |
|
415 char c = data[i]; |
|
416 if (isprint(int(uchar(c)))) { |
|
417 out += c; |
|
418 } else switch (c) { |
|
419 case '\n': out += "\\n"; break; |
|
420 case '\r': out += "\\r"; break; |
|
421 case '\t': out += "\\t"; break; |
|
422 default: |
|
423 QString tmp; |
|
424 tmp.sprintf("\\%o", c); |
|
425 out += tmp.toLatin1(); |
|
426 } |
|
427 } |
|
428 |
|
429 if (len < maxLength) |
|
430 out += "..."; |
|
431 |
|
432 return out; |
|
433 } |
|
434 #endif |
|
435 |
|
436 static bool isProxyError(QAbstractSocket::SocketError error) |
|
437 { |
|
438 switch (error) { |
|
439 case QAbstractSocket::ProxyAuthenticationRequiredError: |
|
440 case QAbstractSocket::ProxyConnectionRefusedError: |
|
441 case QAbstractSocket::ProxyConnectionClosedError: |
|
442 case QAbstractSocket::ProxyConnectionTimeoutError: |
|
443 case QAbstractSocket::ProxyNotFoundError: |
|
444 case QAbstractSocket::ProxyProtocolError: |
|
445 return true; |
|
446 default: |
|
447 return false; |
|
448 } |
|
449 } |
|
450 |
|
451 /*! \internal |
|
452 |
|
453 Constructs a QAbstractSocketPrivate. Initializes all members. |
|
454 */ |
|
455 QAbstractSocketPrivate::QAbstractSocketPrivate() |
|
456 : readSocketNotifierCalled(false), |
|
457 readSocketNotifierState(false), |
|
458 readSocketNotifierStateSet(false), |
|
459 emittedReadyRead(false), |
|
460 emittedBytesWritten(false), |
|
461 abortCalled(false), |
|
462 closeCalled(false), |
|
463 pendingClose(false), |
|
464 port(0), |
|
465 localPort(0), |
|
466 peerPort(0), |
|
467 socketEngine(0), |
|
468 cachedSocketDescriptor(-1), |
|
469 #ifdef Q_OS_LINUX |
|
470 addToBytesAvailable(0), |
|
471 #endif |
|
472 readBufferMaxSize(0), |
|
473 readBuffer(QABSTRACTSOCKET_BUFFERSIZE), |
|
474 writeBuffer(QABSTRACTSOCKET_BUFFERSIZE), |
|
475 isBuffered(false), |
|
476 blockingTimeout(30000), |
|
477 connectTimer(0), |
|
478 disconnectTimer(0), |
|
479 connectTimeElapsed(0), |
|
480 hostLookupId(-1), |
|
481 socketType(QAbstractSocket::UnknownSocketType), |
|
482 state(QAbstractSocket::UnconnectedState), |
|
483 socketError(QAbstractSocket::UnknownSocketError) |
|
484 { |
|
485 } |
|
486 |
|
487 /*! \internal |
|
488 |
|
489 Destructs the QAbstractSocket. If the socket layer is open, it |
|
490 will be reset. |
|
491 */ |
|
492 QAbstractSocketPrivate::~QAbstractSocketPrivate() |
|
493 { |
|
494 } |
|
495 |
|
496 /*! \internal |
|
497 |
|
498 Resets the socket layer, clears the read and write buffers and |
|
499 deletes any socket notifiers. |
|
500 */ |
|
501 void QAbstractSocketPrivate::resetSocketLayer() |
|
502 { |
|
503 #if defined (QABSTRACTSOCKET_DEBUG) |
|
504 qDebug("QAbstractSocketPrivate::resetSocketLayer()"); |
|
505 #endif |
|
506 |
|
507 if (socketEngine) { |
|
508 socketEngine->close(); |
|
509 socketEngine->disconnect(); |
|
510 delete socketEngine; |
|
511 socketEngine = 0; |
|
512 cachedSocketDescriptor = -1; |
|
513 } |
|
514 if (connectTimer) |
|
515 connectTimer->stop(); |
|
516 if (disconnectTimer) |
|
517 disconnectTimer->stop(); |
|
518 } |
|
519 |
|
520 /*! \internal |
|
521 |
|
522 Initializes the socket layer to by of type \a type, using the |
|
523 network layer protocol \a protocol. Resets the socket layer first |
|
524 if it's already initialized. Sets up the socket notifiers. |
|
525 */ |
|
526 bool QAbstractSocketPrivate::initSocketLayer(QAbstractSocket::NetworkLayerProtocol protocol) |
|
527 { |
|
528 #ifdef QT_NO_NETWORKPROXY |
|
529 // this is here to avoid a duplication of the call to createSocketEngine below |
|
530 static const QNetworkProxy &proxyInUse = *(QNetworkProxy *)0; |
|
531 #endif |
|
532 |
|
533 Q_Q(QAbstractSocket); |
|
534 #if defined (QABSTRACTSOCKET_DEBUG) |
|
535 QString typeStr; |
|
536 if (q->socketType() == QAbstractSocket::TcpSocket) typeStr = QLatin1String("TcpSocket"); |
|
537 else if (q->socketType() == QAbstractSocket::UdpSocket) typeStr = QLatin1String("UdpSocket"); |
|
538 else typeStr = QLatin1String("UnknownSocketType"); |
|
539 QString protocolStr; |
|
540 if (protocol == QAbstractSocket::IPv4Protocol) protocolStr = QLatin1String("IPv4Protocol"); |
|
541 else if (protocol == QAbstractSocket::IPv6Protocol) protocolStr = QLatin1String("IPv6Protocol"); |
|
542 else protocolStr = QLatin1String("UnknownNetworkLayerProtocol"); |
|
543 #endif |
|
544 |
|
545 resetSocketLayer(); |
|
546 socketEngine = QAbstractSocketEngine::createSocketEngine(q->socketType(), proxyInUse, q); |
|
547 if (!socketEngine) { |
|
548 socketError = QAbstractSocket::UnsupportedSocketOperationError; |
|
549 q->setErrorString(QAbstractSocket::tr("Operation on socket is not supported")); |
|
550 return false; |
|
551 } |
|
552 if (!socketEngine->initialize(q->socketType(), protocol)) { |
|
553 #if defined (QABSTRACTSOCKET_DEBUG) |
|
554 qDebug("QAbstractSocketPrivate::initSocketLayer(%s, %s) failed (%s)", |
|
555 typeStr.toLatin1().constData(), protocolStr.toLatin1().constData(), |
|
556 socketEngine->errorString().toLatin1().constData()); |
|
557 #endif |
|
558 socketError = socketEngine->error(); |
|
559 q->setErrorString(socketEngine->errorString()); |
|
560 return false; |
|
561 } |
|
562 |
|
563 if (threadData->eventDispatcher) |
|
564 socketEngine->setReceiver(this); |
|
565 |
|
566 #if defined (QABSTRACTSOCKET_DEBUG) |
|
567 qDebug("QAbstractSocketPrivate::initSocketLayer(%s, %s) success", |
|
568 typeStr.toLatin1().constData(), protocolStr.toLatin1().constData()); |
|
569 #endif |
|
570 return true; |
|
571 } |
|
572 |
|
573 /*! \internal |
|
574 |
|
575 Slot connected to the read socket notifier. This slot is called |
|
576 when new data is available for reading, or when the socket has |
|
577 been closed. Handles recursive calls. |
|
578 */ |
|
579 bool QAbstractSocketPrivate::canReadNotification() |
|
580 { |
|
581 Q_Q(QAbstractSocket); |
|
582 #if defined (QABSTRACTSOCKET_DEBUG) |
|
583 qDebug("QAbstractSocketPrivate::canReadNotification()"); |
|
584 #endif |
|
585 |
|
586 // Prevent recursive calls |
|
587 if (readSocketNotifierCalled) { |
|
588 if (!readSocketNotifierStateSet) { |
|
589 readSocketNotifierStateSet = true; |
|
590 readSocketNotifierState = socketEngine->isReadNotificationEnabled(); |
|
591 socketEngine->setReadNotificationEnabled(false); |
|
592 } |
|
593 } |
|
594 readSocketNotifierCalled = true; |
|
595 |
|
596 if (!isBuffered) |
|
597 socketEngine->setReadNotificationEnabled(false); |
|
598 |
|
599 // If buffered, read data from the socket into the read buffer |
|
600 qint64 newBytes = 0; |
|
601 if (isBuffered) { |
|
602 // Return if there is no space in the buffer |
|
603 if (readBufferMaxSize && readBuffer.size() >= readBufferMaxSize) { |
|
604 #if defined (QABSTRACTSOCKET_DEBUG) |
|
605 qDebug("QAbstractSocketPrivate::canReadNotification() buffer is full"); |
|
606 #endif |
|
607 readSocketNotifierCalled = false; |
|
608 return false; |
|
609 } |
|
610 |
|
611 // If reading from the socket fails after getting a read |
|
612 // notification, close the socket. |
|
613 newBytes = readBuffer.size(); |
|
614 if (!readFromSocket()) { |
|
615 #if defined (QABSTRACTSOCKET_DEBUG) |
|
616 qDebug("QAbstractSocketPrivate::canReadNotification() disconnecting socket"); |
|
617 #endif |
|
618 q->disconnectFromHost(); |
|
619 readSocketNotifierCalled = false; |
|
620 return false; |
|
621 } |
|
622 newBytes = readBuffer.size() - newBytes; |
|
623 |
|
624 // If read buffer is full, disable the read socket notifier. |
|
625 if (readBufferMaxSize && readBuffer.size() == readBufferMaxSize) { |
|
626 socketEngine->setReadNotificationEnabled(false); |
|
627 } |
|
628 } |
|
629 |
|
630 // only emit readyRead() when not recursing, and only if there is data available |
|
631 bool hasData = newBytes > 0 |
|
632 #ifndef QT_NO_UDPSOCKET |
|
633 || (!isBuffered && socketEngine && socketEngine->hasPendingDatagrams()) |
|
634 #endif |
|
635 ; |
|
636 |
|
637 if (!emittedReadyRead && hasData) { |
|
638 emittedReadyRead = true; |
|
639 emit q->readyRead(); |
|
640 emittedReadyRead = false; |
|
641 } |
|
642 |
|
643 // If we were closed as a result of the readyRead() signal, |
|
644 // return. |
|
645 if (state == QAbstractSocket::UnconnectedState || state == QAbstractSocket::ClosingState) { |
|
646 #if defined (QABSTRACTSOCKET_DEBUG) |
|
647 qDebug("QAbstractSocketPrivate::canReadNotification() socket is closing - returning"); |
|
648 #endif |
|
649 readSocketNotifierCalled = false; |
|
650 return true; |
|
651 } |
|
652 |
|
653 if (!hasData && socketEngine) |
|
654 socketEngine->setReadNotificationEnabled(true); |
|
655 |
|
656 // reset the read socket notifier state if we reentered inside the |
|
657 // readyRead() connected slot. |
|
658 if (readSocketNotifierStateSet && socketEngine && |
|
659 readSocketNotifierState != socketEngine->isReadNotificationEnabled()) { |
|
660 socketEngine->setReadNotificationEnabled(readSocketNotifierState); |
|
661 readSocketNotifierStateSet = false; |
|
662 } |
|
663 readSocketNotifierCalled = false; |
|
664 return true; |
|
665 } |
|
666 |
|
667 /*! \internal |
|
668 |
|
669 Slot connected to the write socket notifier. It's called during a |
|
670 delayed connect or when the socket is ready for writing. |
|
671 */ |
|
672 bool QAbstractSocketPrivate::canWriteNotification() |
|
673 { |
|
674 #if defined (Q_OS_WIN) |
|
675 if (socketEngine && socketEngine->isWriteNotificationEnabled()) |
|
676 socketEngine->setWriteNotificationEnabled(false); |
|
677 #endif |
|
678 |
|
679 #if defined (QABSTRACTSOCKET_DEBUG) |
|
680 qDebug("QAbstractSocketPrivate::canWriteNotification() flushing"); |
|
681 #endif |
|
682 int tmp = writeBuffer.size(); |
|
683 flush(); |
|
684 |
|
685 if (socketEngine) { |
|
686 #if defined (Q_OS_WIN) |
|
687 if (!writeBuffer.isEmpty()) |
|
688 socketEngine->setWriteNotificationEnabled(true); |
|
689 #else |
|
690 if (writeBuffer.isEmpty() && socketEngine->bytesToWrite() == 0) |
|
691 socketEngine->setWriteNotificationEnabled(false); |
|
692 #endif |
|
693 } |
|
694 |
|
695 return (writeBuffer.size() < tmp); |
|
696 } |
|
697 |
|
698 /*! \internal |
|
699 |
|
700 Slot connected to a notification of connection status |
|
701 change. Either we finished connecting or we failed to connect. |
|
702 */ |
|
703 void QAbstractSocketPrivate::connectionNotification() |
|
704 { |
|
705 // If in connecting state, check if the connection has been |
|
706 // established, otherwise flush pending data. |
|
707 if (state == QAbstractSocket::ConnectingState) { |
|
708 #if defined (QABSTRACTSOCKET_DEBUG) |
|
709 qDebug("QAbstractSocketPrivate::connectionNotification() testing connection"); |
|
710 #endif |
|
711 _q_testConnection(); |
|
712 } |
|
713 } |
|
714 |
|
715 /*! \internal |
|
716 |
|
717 Writes pending data in the write buffers to the socket. The |
|
718 function writes as much as it can without blocking. |
|
719 |
|
720 It is usually invoked by canWriteNotification after one or more |
|
721 calls to write(). |
|
722 |
|
723 Emits bytesWritten(). |
|
724 */ |
|
725 bool QAbstractSocketPrivate::flush() |
|
726 { |
|
727 Q_Q(QAbstractSocket); |
|
728 if (!socketEngine || !socketEngine->isValid() || (writeBuffer.isEmpty() |
|
729 && socketEngine->bytesToWrite() == 0)) { |
|
730 #if defined (QABSTRACTSOCKET_DEBUG) |
|
731 qDebug("QAbstractSocketPrivate::flush() nothing to do: valid ? %s, writeBuffer.isEmpty() ? %s", |
|
732 socketEngine->isValid() ? "yes" : "no", writeBuffer.isEmpty() ? "yes" : "no"); |
|
733 #endif |
|
734 |
|
735 // this covers the case when the buffer was empty, but we had to wait for the socket engine to finish |
|
736 if (state == QAbstractSocket::ClosingState) |
|
737 q->disconnectFromHost(); |
|
738 |
|
739 return false; |
|
740 } |
|
741 |
|
742 int nextSize = writeBuffer.nextDataBlockSize(); |
|
743 const char *ptr = writeBuffer.readPointer(); |
|
744 |
|
745 // Attempt to write it all in one chunk. |
|
746 qint64 written = socketEngine->write(ptr, nextSize); |
|
747 if (written < 0) { |
|
748 socketError = socketEngine->error(); |
|
749 q->setErrorString(socketEngine->errorString()); |
|
750 emit q->error(socketError); |
|
751 // an unexpected error so close the socket. |
|
752 #if defined (QABSTRACTSOCKET_DEBUG) |
|
753 qDebug() << "QAbstractSocketPrivate::flush() write error, aborting." << socketEngine->errorString(); |
|
754 #endif |
|
755 q->abort(); |
|
756 return false; |
|
757 } |
|
758 |
|
759 #if defined (QABSTRACTSOCKET_DEBUG) |
|
760 qDebug("QAbstractSocketPrivate::flush() %lld bytes written to the network", |
|
761 written); |
|
762 #endif |
|
763 |
|
764 // Remove what we wrote so far. |
|
765 writeBuffer.free(written); |
|
766 if (written > 0) { |
|
767 // Don't emit bytesWritten() recursively. |
|
768 if (!emittedBytesWritten) { |
|
769 emittedBytesWritten = true; |
|
770 emit q->bytesWritten(written); |
|
771 emittedBytesWritten = false; |
|
772 } |
|
773 } |
|
774 |
|
775 if (writeBuffer.isEmpty() && socketEngine && socketEngine->isWriteNotificationEnabled() |
|
776 && !socketEngine->bytesToWrite()) |
|
777 socketEngine->setWriteNotificationEnabled(false); |
|
778 if (state == QAbstractSocket::ClosingState) |
|
779 q->disconnectFromHost(); |
|
780 |
|
781 return true; |
|
782 } |
|
783 |
|
784 #ifndef QT_NO_NETWORKPROXY |
|
785 /*! \internal |
|
786 |
|
787 Resolve the proxy to its final value. |
|
788 */ |
|
789 void QAbstractSocketPrivate::resolveProxy(const QString &hostname, quint16 port) |
|
790 { |
|
791 QHostAddress parsed; |
|
792 if (hostname == QLatin1String("localhost") |
|
793 || hostname.startsWith(QLatin1String("localhost.")) |
|
794 || (parsed.setAddress(hostname) |
|
795 && (parsed == QHostAddress::LocalHost |
|
796 || parsed == QHostAddress::LocalHostIPv6))) { |
|
797 proxyInUse = QNetworkProxy::NoProxy; |
|
798 return; |
|
799 } |
|
800 |
|
801 QList<QNetworkProxy> proxies; |
|
802 |
|
803 if (proxy.type() != QNetworkProxy::DefaultProxy) { |
|
804 // a non-default proxy was set with setProxy |
|
805 proxies << proxy; |
|
806 } else { |
|
807 // try the application settings instead |
|
808 QNetworkProxyQuery query(hostname, port, QString(), |
|
809 socketType == QAbstractSocket::TcpSocket ? |
|
810 QNetworkProxyQuery::TcpSocket : |
|
811 QNetworkProxyQuery::UdpSocket); |
|
812 proxies = QNetworkProxyFactory::proxyForQuery(query); |
|
813 } |
|
814 |
|
815 // return the first that we can use |
|
816 foreach (const QNetworkProxy &p, proxies) { |
|
817 if (socketType == QAbstractSocket::UdpSocket && |
|
818 (p.capabilities() & QNetworkProxy::UdpTunnelingCapability) == 0) |
|
819 continue; |
|
820 |
|
821 if (socketType == QAbstractSocket::TcpSocket && |
|
822 (p.capabilities() & QNetworkProxy::TunnelingCapability) == 0) |
|
823 continue; |
|
824 |
|
825 proxyInUse = p; |
|
826 return; |
|
827 } |
|
828 |
|
829 // no proxy found |
|
830 // DefaultProxy here will raise an error |
|
831 proxyInUse = QNetworkProxy(); |
|
832 } |
|
833 |
|
834 /*! |
|
835 \internal |
|
836 |
|
837 Starts the connection to \a host, like _q_startConnecting below, |
|
838 but without hostname resolution. |
|
839 */ |
|
840 void QAbstractSocketPrivate::startConnectingByName(const QString &host) |
|
841 { |
|
842 Q_Q(QAbstractSocket); |
|
843 if (state == QAbstractSocket::ConnectingState || state == QAbstractSocket::ConnectedState) |
|
844 return; |
|
845 |
|
846 #if defined(QABSTRACTSOCKET_DEBUG) |
|
847 qDebug("QAbstractSocketPrivate::startConnectingByName(host == %s)", qPrintable(host)); |
|
848 #endif |
|
849 |
|
850 // ### Let the socket engine drive this? |
|
851 state = QAbstractSocket::ConnectingState; |
|
852 emit q->stateChanged(state); |
|
853 |
|
854 connectTimeElapsed = 0; |
|
855 |
|
856 if (initSocketLayer(QAbstractSocket::UnknownNetworkLayerProtocol)) { |
|
857 if (socketEngine->connectToHostByName(host, port) || |
|
858 socketEngine->state() == QAbstractSocket::ConnectingState) { |
|
859 cachedSocketDescriptor = socketEngine->socketDescriptor(); |
|
860 |
|
861 return; |
|
862 } |
|
863 |
|
864 // failed to connect |
|
865 socketError = socketEngine->error(); |
|
866 q->setErrorString(socketEngine->errorString()); |
|
867 } |
|
868 |
|
869 state = QAbstractSocket::UnconnectedState; |
|
870 emit q->error(socketError); |
|
871 emit q->stateChanged(state); |
|
872 } |
|
873 |
|
874 #endif |
|
875 |
|
876 /*! \internal |
|
877 |
|
878 Slot connected to QHostInfo::lookupHost() in connectToHost(). This |
|
879 function starts the process of connecting to any number of |
|
880 candidate IP addresses for the host, if it was found. Calls |
|
881 _q_connectToNextAddress(). |
|
882 */ |
|
883 void QAbstractSocketPrivate::_q_startConnecting(const QHostInfo &hostInfo) |
|
884 { |
|
885 Q_Q(QAbstractSocket); |
|
886 if (state != QAbstractSocket::HostLookupState) |
|
887 return; |
|
888 |
|
889 if (hostLookupId != -1 && hostLookupId != hostInfo.lookupId()) { |
|
890 qWarning("QAbstractSocketPrivate::_q_startConnecting() received hostInfo for wrong lookup ID %d expected %d", hostInfo.lookupId(), hostLookupId); |
|
891 } |
|
892 |
|
893 addresses = hostInfo.addresses(); |
|
894 |
|
895 #if defined(QABSTRACTSOCKET_DEBUG) |
|
896 QString s = QLatin1String("{"); |
|
897 for (int i = 0; i < addresses.count(); ++i) { |
|
898 if (i != 0) s += QLatin1String(", "); |
|
899 s += addresses.at(i).toString(); |
|
900 } |
|
901 s += QLatin1Char('}'); |
|
902 qDebug("QAbstractSocketPrivate::_q_startConnecting(hostInfo == %s)", s.toLatin1().constData()); |
|
903 #endif |
|
904 |
|
905 // Try all addresses twice. |
|
906 addresses += addresses; |
|
907 |
|
908 // If there are no addresses in the host list, report this to the |
|
909 // user. |
|
910 if (addresses.isEmpty()) { |
|
911 #if defined(QABSTRACTSOCKET_DEBUG) |
|
912 qDebug("QAbstractSocketPrivate::_q_startConnecting(), host not found"); |
|
913 #endif |
|
914 state = QAbstractSocket::UnconnectedState; |
|
915 socketError = QAbstractSocket::HostNotFoundError; |
|
916 q->setErrorString(QAbstractSocket::tr("Host not found")); |
|
917 emit q->stateChanged(state); |
|
918 emit q->error(QAbstractSocket::HostNotFoundError); |
|
919 return; |
|
920 } |
|
921 |
|
922 // Enter Connecting state (see also sn_write, which is called by |
|
923 // the write socket notifier after connect()) |
|
924 state = QAbstractSocket::ConnectingState; |
|
925 emit q->stateChanged(state); |
|
926 |
|
927 // Report the successful host lookup |
|
928 emit q->hostFound(); |
|
929 |
|
930 // Reset the total time spent connecting. |
|
931 connectTimeElapsed = 0; |
|
932 |
|
933 // The addresses returned by the lookup will be tested one after |
|
934 // another by _q_connectToNextAddress(). |
|
935 _q_connectToNextAddress(); |
|
936 } |
|
937 |
|
938 /*! \internal |
|
939 |
|
940 Called by a queued or direct connection from _q_startConnecting() or |
|
941 _q_testConnection(), this function takes the first address of the |
|
942 pending addresses list and tries to connect to it. If the |
|
943 connection succeeds, QAbstractSocket will emit |
|
944 connected(). Otherwise, error(ConnectionRefusedError) or |
|
945 error(SocketTimeoutError) is emitted. |
|
946 */ |
|
947 void QAbstractSocketPrivate::_q_connectToNextAddress() |
|
948 { |
|
949 Q_Q(QAbstractSocket); |
|
950 do { |
|
951 // Check for more pending addresses |
|
952 if (addresses.isEmpty()) { |
|
953 #if defined(QABSTRACTSOCKET_DEBUG) |
|
954 qDebug("QAbstractSocketPrivate::_q_connectToNextAddress(), all addresses failed."); |
|
955 #endif |
|
956 state = QAbstractSocket::UnconnectedState; |
|
957 if (socketEngine) { |
|
958 if ((socketEngine->error() == QAbstractSocket::UnknownSocketError |
|
959 #ifdef Q_OS_AIX |
|
960 // On AIX, the second connect call will result in EINVAL and not |
|
961 // ECONNECTIONREFUSED; although the meaning is the same. |
|
962 || socketEngine->error() == QAbstractSocket::UnsupportedSocketOperationError |
|
963 #endif |
|
964 ) && socketEngine->state() == QAbstractSocket::ConnectingState) { |
|
965 socketError = QAbstractSocket::ConnectionRefusedError; |
|
966 q->setErrorString(QAbstractSocket::tr("Connection refused")); |
|
967 } else { |
|
968 socketError = socketEngine->error(); |
|
969 q->setErrorString(socketEngine->errorString()); |
|
970 } |
|
971 } else { |
|
972 // socketError = QAbstractSocket::ConnectionRefusedError; |
|
973 // q->setErrorString(QAbstractSocket::tr("Connection refused")); |
|
974 } |
|
975 emit q->stateChanged(state); |
|
976 emit q->error(socketError); |
|
977 return; |
|
978 } |
|
979 |
|
980 // Pick the first host address candidate |
|
981 host = addresses.takeFirst(); |
|
982 #if defined(QABSTRACTSOCKET_DEBUG) |
|
983 qDebug("QAbstractSocketPrivate::_q_connectToNextAddress(), connecting to %s:%i, %d left to try", |
|
984 host.toString().toLatin1().constData(), port, addresses.count()); |
|
985 #endif |
|
986 |
|
987 #if defined(QT_NO_IPV6) |
|
988 if (host.protocol() == QAbstractSocket::IPv6Protocol) { |
|
989 // If we have no IPv6 support, then we will not be able to |
|
990 // connect. So we just pretend we didn't see this address. |
|
991 #if defined(QABSTRACTSOCKET_DEBUG) |
|
992 qDebug("QAbstractSocketPrivate::_q_connectToNextAddress(), skipping IPv6 entry"); |
|
993 #endif |
|
994 continue; |
|
995 } |
|
996 #endif |
|
997 |
|
998 if (!initSocketLayer(host.protocol())) { |
|
999 // hope that the next address is better |
|
1000 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1001 qDebug("QAbstractSocketPrivate::_q_connectToNextAddress(), failed to initialize sock layer"); |
|
1002 #endif |
|
1003 continue; |
|
1004 } |
|
1005 |
|
1006 // Tries to connect to the address. If it succeeds immediately |
|
1007 // (localhost address on BSD or any UDP connect), emit |
|
1008 // connected() and return. |
|
1009 if (socketEngine->connectToHost(host, port)) { |
|
1010 //_q_testConnection(); |
|
1011 fetchConnectionParameters(); |
|
1012 return; |
|
1013 } |
|
1014 |
|
1015 // cache the socket descriptor even if we're not fully connected yet |
|
1016 cachedSocketDescriptor = socketEngine->socketDescriptor(); |
|
1017 |
|
1018 // Check that we're in delayed connection state. If not, try |
|
1019 // the next address |
|
1020 if (socketEngine->state() != QAbstractSocket::ConnectingState) { |
|
1021 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1022 qDebug("QAbstractSocketPrivate::_q_connectToNextAddress(), connection failed (%s)", |
|
1023 socketEngine->errorString().toLatin1().constData()); |
|
1024 #endif |
|
1025 continue; |
|
1026 } |
|
1027 |
|
1028 // Start the connect timer. |
|
1029 if (threadData->eventDispatcher) { |
|
1030 if (!connectTimer) { |
|
1031 connectTimer = new QTimer(q); |
|
1032 QObject::connect(connectTimer, SIGNAL(timeout()), |
|
1033 q, SLOT(_q_abortConnectionAttempt()), |
|
1034 Qt::DirectConnection); |
|
1035 } |
|
1036 connectTimer->start(QT_CONNECT_TIMEOUT); |
|
1037 } |
|
1038 |
|
1039 // Wait for a write notification that will eventually call |
|
1040 // _q_testConnection(). |
|
1041 socketEngine->setWriteNotificationEnabled(true); |
|
1042 break; |
|
1043 } while (state != QAbstractSocket::ConnectedState); |
|
1044 } |
|
1045 |
|
1046 /*! \internal |
|
1047 |
|
1048 Tests if a connection has been established. If it has, connected() |
|
1049 is emitted. Otherwise, _q_connectToNextAddress() is invoked. |
|
1050 */ |
|
1051 void QAbstractSocketPrivate::_q_testConnection() |
|
1052 { |
|
1053 if (socketEngine) { |
|
1054 if (threadData->eventDispatcher) { |
|
1055 if (connectTimer) |
|
1056 connectTimer->stop(); |
|
1057 } |
|
1058 |
|
1059 if (socketEngine->state() == QAbstractSocket::ConnectedState) { |
|
1060 // Fetch the parameters if our connection is completed; |
|
1061 // otherwise, fall out and try the next address. |
|
1062 fetchConnectionParameters(); |
|
1063 if (pendingClose) { |
|
1064 q_func()->disconnectFromHost(); |
|
1065 pendingClose = false; |
|
1066 } |
|
1067 return; |
|
1068 } |
|
1069 |
|
1070 // don't retry the other addresses if we had a proxy error |
|
1071 if (isProxyError(socketEngine->error())) |
|
1072 addresses.clear(); |
|
1073 } |
|
1074 |
|
1075 if (threadData->eventDispatcher) { |
|
1076 if (connectTimer) |
|
1077 connectTimer->stop(); |
|
1078 } |
|
1079 |
|
1080 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1081 qDebug("QAbstractSocketPrivate::_q_testConnection() connection failed," |
|
1082 " checking for alternative addresses"); |
|
1083 #endif |
|
1084 _q_connectToNextAddress(); |
|
1085 } |
|
1086 |
|
1087 /*! \internal |
|
1088 |
|
1089 This function is called after a certain number of seconds has |
|
1090 passed while waiting for a connection. It simply tests the |
|
1091 connection, and continues to the next address if the connection |
|
1092 failed. |
|
1093 */ |
|
1094 void QAbstractSocketPrivate::_q_abortConnectionAttempt() |
|
1095 { |
|
1096 Q_Q(QAbstractSocket); |
|
1097 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1098 qDebug("QAbstractSocketPrivate::_q_abortConnectionAttempt() (timed out)"); |
|
1099 #endif |
|
1100 if (socketEngine) |
|
1101 socketEngine->setWriteNotificationEnabled(false); |
|
1102 |
|
1103 connectTimer->stop(); |
|
1104 |
|
1105 if (addresses.isEmpty()) { |
|
1106 state = QAbstractSocket::UnconnectedState; |
|
1107 socketError = QAbstractSocket::SocketTimeoutError; |
|
1108 q->setErrorString(QAbstractSocket::tr("Connection timed out")); |
|
1109 emit q->stateChanged(state); |
|
1110 emit q->error(socketError); |
|
1111 } else { |
|
1112 _q_connectToNextAddress(); |
|
1113 } |
|
1114 } |
|
1115 |
|
1116 void QAbstractSocketPrivate::_q_forceDisconnect() |
|
1117 { |
|
1118 Q_Q(QAbstractSocket); |
|
1119 if (socketEngine && socketEngine->isValid() && state == QAbstractSocket::ClosingState) { |
|
1120 socketEngine->close(); |
|
1121 q->disconnectFromHost(); |
|
1122 } |
|
1123 } |
|
1124 |
|
1125 /*! \internal |
|
1126 |
|
1127 Reads data from the socket layer into the read buffer. Returns |
|
1128 true on success; otherwise false. |
|
1129 */ |
|
1130 bool QAbstractSocketPrivate::readFromSocket() |
|
1131 { |
|
1132 Q_Q(QAbstractSocket); |
|
1133 // Find how many bytes we can read from the socket layer. |
|
1134 qint64 bytesToRead = socketEngine->bytesAvailable(); |
|
1135 #ifdef Q_OS_LINUX |
|
1136 if (bytesToRead > 0) // ### See setSocketDescriptor() |
|
1137 bytesToRead += addToBytesAvailable; |
|
1138 #endif |
|
1139 if (bytesToRead == 0) { |
|
1140 // Under heavy load, certain conditions can trigger read notifications |
|
1141 // for socket notifiers on which there is no activity. If we continue |
|
1142 // to read 0 bytes from the socket, we will trigger behavior similar |
|
1143 // to that which signals a remote close. When we hit this condition, |
|
1144 // we try to read 4k of data from the socket, which will give us either |
|
1145 // an EAGAIN/EWOULDBLOCK if the connection is alive (i.e., the remote |
|
1146 // host has _not_ disappeared). |
|
1147 bytesToRead = 4096; |
|
1148 } |
|
1149 if (readBufferMaxSize && bytesToRead > (readBufferMaxSize - readBuffer.size())) |
|
1150 bytesToRead = readBufferMaxSize - readBuffer.size(); |
|
1151 |
|
1152 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1153 qDebug("QAbstractSocketPrivate::readFromSocket() about to read %d bytes", |
|
1154 int(bytesToRead)); |
|
1155 #endif |
|
1156 |
|
1157 // Read from the socket, store data in the read buffer. |
|
1158 char *ptr = readBuffer.reserve(bytesToRead); |
|
1159 qint64 readBytes = socketEngine->read(ptr, bytesToRead); |
|
1160 if (readBytes == -2) { |
|
1161 // No bytes currently available for reading. |
|
1162 readBuffer.chop(bytesToRead); |
|
1163 return true; |
|
1164 } |
|
1165 readBuffer.chop(int(bytesToRead - (readBytes < 0 ? qint64(0) : readBytes))); |
|
1166 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1167 qDebug("QAbstractSocketPrivate::readFromSocket() got %d bytes, buffer size = %d", |
|
1168 int(readBytes), readBuffer.size()); |
|
1169 #endif |
|
1170 |
|
1171 if (!socketEngine->isValid()) { |
|
1172 socketError = socketEngine->error(); |
|
1173 q->setErrorString(socketEngine->errorString()); |
|
1174 emit q->error(socketError); |
|
1175 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1176 qDebug("QAbstractSocketPrivate::readFromSocket() read failed: %s", |
|
1177 q->errorString().toLatin1().constData()); |
|
1178 #endif |
|
1179 resetSocketLayer(); |
|
1180 return false; |
|
1181 } |
|
1182 |
|
1183 return true; |
|
1184 } |
|
1185 |
|
1186 /*! \internal |
|
1187 |
|
1188 Sets up the internal state after the connection has succeeded. |
|
1189 */ |
|
1190 void QAbstractSocketPrivate::fetchConnectionParameters() |
|
1191 { |
|
1192 Q_Q(QAbstractSocket); |
|
1193 |
|
1194 peerName = hostName; |
|
1195 if (socketEngine) { |
|
1196 socketEngine->setReadNotificationEnabled(true); |
|
1197 socketEngine->setWriteNotificationEnabled(true); |
|
1198 localPort = socketEngine->localPort(); |
|
1199 peerPort = socketEngine->peerPort(); |
|
1200 localAddress = socketEngine->localAddress(); |
|
1201 peerAddress = socketEngine->peerAddress(); |
|
1202 cachedSocketDescriptor = socketEngine->socketDescriptor(); |
|
1203 } |
|
1204 |
|
1205 state = QAbstractSocket::ConnectedState; |
|
1206 emit q->stateChanged(state); |
|
1207 emit q->connected(); |
|
1208 |
|
1209 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1210 qDebug("QAbstractSocketPrivate::fetchConnectionParameters() connection to %s:%i established", |
|
1211 host.toString().toLatin1().constData(), port); |
|
1212 #endif |
|
1213 } |
|
1214 |
|
1215 /*! \internal |
|
1216 |
|
1217 Constructs a new abstract socket of type \a socketType. The \a |
|
1218 parent argument is passed to QObject's constructor. |
|
1219 */ |
|
1220 QAbstractSocket::QAbstractSocket(SocketType socketType, |
|
1221 QAbstractSocketPrivate &dd, QObject *parent) |
|
1222 : QIODevice(dd, parent) |
|
1223 { |
|
1224 Q_D(QAbstractSocket); |
|
1225 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1226 qDebug("QAbstractSocket::QAbstractSocket(%sSocket, QAbstractSocketPrivate == %p, parent == %p)", |
|
1227 socketType == TcpSocket ? "Tcp" : socketType == UdpSocket |
|
1228 ? "Udp" : "Unknown", &dd, parent); |
|
1229 #endif |
|
1230 d->socketType = socketType; |
|
1231 } |
|
1232 |
|
1233 /*! |
|
1234 Creates a new abstract socket of type \a socketType. The \a |
|
1235 parent argument is passed to QObject's constructor. |
|
1236 |
|
1237 \sa socketType(), QTcpSocket, QUdpSocket |
|
1238 */ |
|
1239 QAbstractSocket::QAbstractSocket(SocketType socketType, QObject *parent) |
|
1240 : QIODevice(*new QAbstractSocketPrivate, parent) |
|
1241 { |
|
1242 Q_D(QAbstractSocket); |
|
1243 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1244 qDebug("QAbstractSocket::QAbstractSocket(%p)", parent); |
|
1245 #endif |
|
1246 d->socketType = socketType; |
|
1247 } |
|
1248 |
|
1249 /*! |
|
1250 Destroys the socket. |
|
1251 */ |
|
1252 QAbstractSocket::~QAbstractSocket() |
|
1253 { |
|
1254 Q_D(QAbstractSocket); |
|
1255 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1256 qDebug("QAbstractSocket::~QAbstractSocket()"); |
|
1257 #endif |
|
1258 if (d->state != UnconnectedState) |
|
1259 abort(); |
|
1260 } |
|
1261 |
|
1262 /*! |
|
1263 Returns true if the socket is valid and ready for use; otherwise |
|
1264 returns false. |
|
1265 |
|
1266 \bold{Note:} The socket's state must be ConnectedState before reading and |
|
1267 writing can occur. |
|
1268 |
|
1269 \sa state() |
|
1270 */ |
|
1271 bool QAbstractSocket::isValid() const |
|
1272 { |
|
1273 return d_func()->socketEngine ? d_func()->socketEngine->isValid() : isOpen(); |
|
1274 } |
|
1275 |
|
1276 /*! |
|
1277 Attempts to make a connection to \a hostName on the given \a port. |
|
1278 |
|
1279 The socket is opened in the given \a openMode and first enters |
|
1280 HostLookupState, then performs a host name lookup of \a hostName. |
|
1281 If the lookup succeeds, hostFound() is emitted and QAbstractSocket |
|
1282 enters ConnectingState. It then attempts to connect to the address |
|
1283 or addresses returned by the lookup. Finally, if a connection is |
|
1284 established, QAbstractSocket enters ConnectedState and |
|
1285 emits connected(). |
|
1286 |
|
1287 At any point, the socket can emit error() to signal that an error |
|
1288 occurred. |
|
1289 |
|
1290 \a hostName may be an IP address in string form (e.g., |
|
1291 "43.195.83.32"), or it may be a host name (e.g., |
|
1292 "example.com"). QAbstractSocket will do a lookup only if |
|
1293 required. \a port is in native byte order. |
|
1294 |
|
1295 \sa state(), peerName(), peerAddress(), peerPort(), waitForConnected() |
|
1296 */ |
|
1297 void QAbstractSocket::connectToHost(const QString &hostName, quint16 port, |
|
1298 OpenMode openMode) |
|
1299 { |
|
1300 QMetaObject::invokeMethod(this, "connectToHostImplementation", |
|
1301 Qt::DirectConnection, |
|
1302 Q_ARG(QString, hostName), |
|
1303 Q_ARG(quint16, port), |
|
1304 Q_ARG(OpenMode, openMode)); |
|
1305 } |
|
1306 |
|
1307 /*! |
|
1308 \since 4.1 |
|
1309 |
|
1310 Contains the implementation of connectToHost(). |
|
1311 |
|
1312 Attempts to make a connection to \a hostName on the given \a |
|
1313 port. The socket is opened in the given \a openMode. |
|
1314 */ |
|
1315 void QAbstractSocket::connectToHostImplementation(const QString &hostName, quint16 port, |
|
1316 OpenMode openMode) |
|
1317 { |
|
1318 Q_D(QAbstractSocket); |
|
1319 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1320 qDebug("QAbstractSocket::connectToHost(\"%s\", %i, %i)...", qPrintable(hostName), port, |
|
1321 (int) openMode); |
|
1322 #endif |
|
1323 |
|
1324 if (d->state == ConnectedState || d->state == ConnectingState |
|
1325 || d->state == ClosingState || d->state == HostLookupState) { |
|
1326 qWarning("QAbstractSocket::connectToHost() called when already looking up or connecting/connected to \"%s\"", qPrintable(hostName)); |
|
1327 return; |
|
1328 } |
|
1329 |
|
1330 d->hostName = hostName; |
|
1331 d->port = port; |
|
1332 d->state = UnconnectedState; |
|
1333 d->readBuffer.clear(); |
|
1334 d->writeBuffer.clear(); |
|
1335 d->abortCalled = false; |
|
1336 d->closeCalled = false; |
|
1337 d->pendingClose = false; |
|
1338 d->localPort = 0; |
|
1339 d->peerPort = 0; |
|
1340 d->localAddress.clear(); |
|
1341 d->peerAddress.clear(); |
|
1342 d->peerName = hostName; |
|
1343 #ifdef Q_OS_LINUX |
|
1344 // ### See setSocketDescriptor(). |
|
1345 d->addToBytesAvailable = 0; |
|
1346 #endif |
|
1347 if (d->hostLookupId != -1) { |
|
1348 QHostInfo::abortHostLookup(d->hostLookupId); |
|
1349 d->hostLookupId = -1; |
|
1350 } |
|
1351 |
|
1352 #ifndef QT_NO_NETWORKPROXY |
|
1353 // Get the proxy information |
|
1354 d->resolveProxy(hostName, port); |
|
1355 if (d->proxyInUse.type() == QNetworkProxy::DefaultProxy) { |
|
1356 // failed to setup the proxy |
|
1357 d->socketError = QAbstractSocket::UnsupportedSocketOperationError; |
|
1358 setErrorString(QAbstractSocket::tr("Operation on socket is not supported")); |
|
1359 emit error(d->socketError); |
|
1360 return; |
|
1361 } |
|
1362 #endif |
|
1363 |
|
1364 if (!d_func()->isBuffered) |
|
1365 openMode |= QAbstractSocket::Unbuffered; |
|
1366 QIODevice::open(openMode); |
|
1367 d->state = HostLookupState; |
|
1368 emit stateChanged(d->state); |
|
1369 |
|
1370 QHostAddress temp; |
|
1371 if (temp.setAddress(hostName)) { |
|
1372 QHostInfo info; |
|
1373 info.setAddresses(QList<QHostAddress>() << temp); |
|
1374 d->_q_startConnecting(info); |
|
1375 #ifndef QT_NO_NETWORKPROXY |
|
1376 } else if (d->proxyInUse.capabilities() & QNetworkProxy::HostNameLookupCapability) { |
|
1377 // the proxy supports connection by name, so use it |
|
1378 d->startConnectingByName(hostName); |
|
1379 return; |
|
1380 #endif |
|
1381 } else { |
|
1382 if (d->threadData->eventDispatcher) { |
|
1383 // this internal API for QHostInfo either immediatly gives us the desired |
|
1384 // QHostInfo from cache or later calls the _q_startConnecting slot. |
|
1385 bool immediateResultValid = false; |
|
1386 QHostInfo hostInfo = qt_qhostinfo_lookup(hostName, |
|
1387 this, |
|
1388 SLOT(_q_startConnecting(QHostInfo)), |
|
1389 &immediateResultValid, |
|
1390 &d->hostLookupId); |
|
1391 if (immediateResultValid) { |
|
1392 d->hostLookupId = -1; |
|
1393 d->_q_startConnecting(hostInfo); |
|
1394 } |
|
1395 } |
|
1396 } |
|
1397 |
|
1398 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1399 qDebug("QAbstractSocket::connectToHost(\"%s\", %i) == %s%s", hostName.toLatin1().constData(), port, |
|
1400 (d->state == ConnectedState) ? "true" : "false", |
|
1401 (d->state == ConnectingState || d->state == HostLookupState) |
|
1402 ? " (connection in progress)" : ""); |
|
1403 #endif |
|
1404 } |
|
1405 |
|
1406 /*! \overload |
|
1407 |
|
1408 Attempts to make a connection to \a address on port \a port. |
|
1409 */ |
|
1410 void QAbstractSocket::connectToHost(const QHostAddress &address, quint16 port, |
|
1411 OpenMode openMode) |
|
1412 { |
|
1413 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1414 qDebug("QAbstractSocket::connectToHost([%s], %i, %i)...", |
|
1415 address.toString().toLatin1().constData(), port, (int) openMode); |
|
1416 #endif |
|
1417 connectToHost(address.toString(), port, openMode); |
|
1418 } |
|
1419 |
|
1420 /*! |
|
1421 Returns the number of bytes that are waiting to be written. The |
|
1422 bytes are written when control goes back to the event loop or |
|
1423 when flush() is called. |
|
1424 |
|
1425 \sa bytesAvailable(), flush() |
|
1426 */ |
|
1427 qint64 QAbstractSocket::bytesToWrite() const |
|
1428 { |
|
1429 Q_D(const QAbstractSocket); |
|
1430 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1431 qDebug("QAbstractSocket::bytesToWrite() == %i", d->writeBuffer.size()); |
|
1432 #endif |
|
1433 return (qint64)d->writeBuffer.size(); |
|
1434 } |
|
1435 |
|
1436 /*! |
|
1437 Returns the number of incoming bytes that are waiting to be read. |
|
1438 |
|
1439 \sa bytesToWrite(), read() |
|
1440 */ |
|
1441 qint64 QAbstractSocket::bytesAvailable() const |
|
1442 { |
|
1443 Q_D(const QAbstractSocket); |
|
1444 qint64 available = QIODevice::bytesAvailable(); |
|
1445 if (d->isBuffered) |
|
1446 available += (qint64) d->readBuffer.size(); |
|
1447 else if (d->socketEngine && d->socketEngine->isValid()) |
|
1448 available += d->socketEngine->bytesAvailable(); |
|
1449 #if defined(QABSTRACTSOCKET_DEBUG) |
|
1450 qDebug("QAbstractSocket::bytesAvailable() == %llu", available); |
|
1451 #endif |
|
1452 return available; |
|
1453 } |
|
1454 |
|
1455 /*! |
|
1456 Returns the host port number (in native byte order) of the local |
|
1457 socket if available; otherwise returns 0. |
|
1458 |
|
1459 \sa localAddress(), peerPort(), setLocalPort() |
|
1460 */ |
|
1461 quint16 QAbstractSocket::localPort() const |
|
1462 { |
|
1463 Q_D(const QAbstractSocket); |
|
1464 return d->localPort; |
|
1465 } |
|
1466 |
|
1467 /*! |
|
1468 Returns the host address of the local socket if available; |
|
1469 otherwise returns QHostAddress::Null. |
|
1470 |
|
1471 This is normally the main IP address of the host, but can be |
|
1472 QHostAddress::LocalHost (127.0.0.1) for connections to the |
|
1473 local host. |
|
1474 |
|
1475 \sa localPort(), peerAddress(), setLocalAddress() |
|
1476 */ |
|
1477 QHostAddress QAbstractSocket::localAddress() const |
|
1478 { |
|
1479 Q_D(const QAbstractSocket); |
|
1480 return d->localAddress; |
|
1481 } |
|
1482 |
|
1483 /*! |
|
1484 Returns the port of the connected peer if the socket is in |
|
1485 ConnectedState; otherwise returns 0. |
|
1486 |
|
1487 \sa peerAddress(), localPort(), setPeerPort() |
|
1488 */ |
|
1489 quint16 QAbstractSocket::peerPort() const |
|
1490 { |
|
1491 Q_D(const QAbstractSocket); |
|
1492 return d->peerPort; |
|
1493 } |
|
1494 |
|
1495 /*! |
|
1496 Returns the address of the connected peer if the socket is in |
|
1497 ConnectedState; otherwise returns QHostAddress::Null. |
|
1498 |
|
1499 \sa peerName(), peerPort(), localAddress(), setPeerAddress() |
|
1500 */ |
|
1501 QHostAddress QAbstractSocket::peerAddress() const |
|
1502 { |
|
1503 Q_D(const QAbstractSocket); |
|
1504 return d->peerAddress; |
|
1505 } |
|
1506 |
|
1507 /*! |
|
1508 Returns the name of the peer as specified by connectToHost(), or |
|
1509 an empty QString if connectToHost() has not been called. |
|
1510 |
|
1511 \sa peerAddress(), peerPort(), setPeerName() |
|
1512 */ |
|
1513 QString QAbstractSocket::peerName() const |
|
1514 { |
|
1515 Q_D(const QAbstractSocket); |
|
1516 return d->peerName.isEmpty() ? d->hostName : d->peerName; |
|
1517 } |
|
1518 |
|
1519 /*! |
|
1520 Returns true if a line of data can be read from the socket; |
|
1521 otherwise returns false. |
|
1522 |
|
1523 \sa readLine() |
|
1524 */ |
|
1525 bool QAbstractSocket::canReadLine() const |
|
1526 { |
|
1527 bool hasLine = d_func()->readBuffer.canReadLine(); |
|
1528 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1529 qDebug("QAbstractSocket::canReadLine() == %s, buffer size = %d, size = %d", hasLine ? "true" : "false", |
|
1530 d_func()->readBuffer.size(), d_func()->buffer.size()); |
|
1531 #endif |
|
1532 return hasLine || QIODevice::canReadLine(); |
|
1533 } |
|
1534 |
|
1535 /*! |
|
1536 Returns the native socket descriptor of the QAbstractSocket object |
|
1537 if this is available; otherwise returns -1. |
|
1538 |
|
1539 If the socket is using QNetworkProxy, the returned descriptor |
|
1540 may not be usable with native socket functions. |
|
1541 |
|
1542 The socket descriptor is not available when QAbstractSocket is in |
|
1543 UnconnectedState. |
|
1544 |
|
1545 \sa setSocketDescriptor() |
|
1546 */ |
|
1547 int QAbstractSocket::socketDescriptor() const |
|
1548 { |
|
1549 Q_D(const QAbstractSocket); |
|
1550 return d->cachedSocketDescriptor; |
|
1551 } |
|
1552 |
|
1553 /*! |
|
1554 Initializes QAbstractSocket with the native socket descriptor \a |
|
1555 socketDescriptor. Returns true if \a socketDescriptor is accepted |
|
1556 as a valid socket descriptor; otherwise returns false. |
|
1557 The socket is opened in the mode specified by \a openMode, and |
|
1558 enters the socket state specified by \a socketState. |
|
1559 |
|
1560 \bold{Note:} It is not possible to initialize two abstract sockets |
|
1561 with the same native socket descriptor. |
|
1562 |
|
1563 \sa socketDescriptor() |
|
1564 */ |
|
1565 bool QAbstractSocket::setSocketDescriptor(int socketDescriptor, SocketState socketState, |
|
1566 OpenMode openMode) |
|
1567 { |
|
1568 Q_D(QAbstractSocket); |
|
1569 #ifndef QT_NO_OPENSSL |
|
1570 if (QSslSocket *socket = qobject_cast<QSslSocket *>(this)) |
|
1571 return socket->setSocketDescriptor(socketDescriptor, socketState, openMode); |
|
1572 #endif |
|
1573 |
|
1574 d->resetSocketLayer(); |
|
1575 d->socketEngine = QAbstractSocketEngine::createSocketEngine(socketDescriptor, this); |
|
1576 if (!d->socketEngine) { |
|
1577 d->socketError = UnsupportedSocketOperationError; |
|
1578 setErrorString(tr("Operation on socket is not supported")); |
|
1579 return false; |
|
1580 } |
|
1581 bool result = d->socketEngine->initialize(socketDescriptor, socketState); |
|
1582 if (!result) { |
|
1583 d->socketError = d->socketEngine->error(); |
|
1584 setErrorString(d->socketEngine->errorString()); |
|
1585 return false; |
|
1586 } |
|
1587 |
|
1588 if (d->threadData->eventDispatcher) |
|
1589 d->socketEngine->setReceiver(d); |
|
1590 |
|
1591 QIODevice::open(openMode); |
|
1592 |
|
1593 if (d->state != socketState) { |
|
1594 d->state = socketState; |
|
1595 emit stateChanged(d->state); |
|
1596 } |
|
1597 |
|
1598 d->pendingClose = false; |
|
1599 d->socketEngine->setReadNotificationEnabled(true); |
|
1600 d->localPort = d->socketEngine->localPort(); |
|
1601 d->peerPort = d->socketEngine->peerPort(); |
|
1602 d->localAddress = d->socketEngine->localAddress(); |
|
1603 d->peerAddress = d->socketEngine->peerAddress(); |
|
1604 d->cachedSocketDescriptor = socketDescriptor; |
|
1605 |
|
1606 #ifdef Q_OS_LINUX |
|
1607 // ### This is a workaround for certain broken Linux kernels, when using |
|
1608 // QTcpSocket with a Unix domain socket. It was introduced around 2.6.9, |
|
1609 // and fixed at some point after that. |
|
1610 // http://archive.linux-usenet.com/index-t-73300.html |
|
1611 // We can provide a better workaround for this: readFromSocket() can loop |
|
1612 // while reading, but this must happen without triggering an implicit |
|
1613 // close because of reading after the socket has closed. |
|
1614 d->addToBytesAvailable = 4096; |
|
1615 #endif |
|
1616 |
|
1617 return true; |
|
1618 } |
|
1619 |
|
1620 /*! |
|
1621 \since 4.6 |
|
1622 Sets the given \a option to the value described by \a value. |
|
1623 |
|
1624 \sa socketOption() |
|
1625 */ |
|
1626 void QAbstractSocket::setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value) |
|
1627 { |
|
1628 #ifndef QT_NO_OPENSSL |
|
1629 if (QSslSocket *sslSocket = qobject_cast<QSslSocket*>(this)) { |
|
1630 sslSocket->setSocketOption(option, value); |
|
1631 return; |
|
1632 } |
|
1633 #endif |
|
1634 |
|
1635 if (!d_func()->socketEngine) |
|
1636 return; |
|
1637 |
|
1638 switch (option) { |
|
1639 case LowDelayOption: |
|
1640 d_func()->socketEngine->setOption(QAbstractSocketEngine::LowDelayOption, value.toInt()); |
|
1641 break; |
|
1642 |
|
1643 case KeepAliveOption: |
|
1644 d_func()->socketEngine->setOption(QAbstractSocketEngine::KeepAliveOption, value.toInt()); |
|
1645 break; |
|
1646 } |
|
1647 } |
|
1648 |
|
1649 /*! |
|
1650 \since 4.6 |
|
1651 Returns the value of the \a option option. |
|
1652 |
|
1653 \sa setSocketOption() |
|
1654 */ |
|
1655 QVariant QAbstractSocket::socketOption(QAbstractSocket::SocketOption option) |
|
1656 { |
|
1657 #ifndef QT_NO_OPENSSL |
|
1658 if (QSslSocket *sslSocket = qobject_cast<QSslSocket*>(this)) { |
|
1659 return sslSocket->socketOption(option); |
|
1660 } |
|
1661 #endif |
|
1662 |
|
1663 if (!d_func()->socketEngine) |
|
1664 return QVariant(); |
|
1665 |
|
1666 int ret = -1; |
|
1667 switch (option) { |
|
1668 case LowDelayOption: |
|
1669 ret = d_func()->socketEngine->option(QAbstractSocketEngine::LowDelayOption); |
|
1670 break; |
|
1671 |
|
1672 case KeepAliveOption: |
|
1673 ret = d_func()->socketEngine->option(QAbstractSocketEngine::KeepAliveOption); |
|
1674 break; |
|
1675 } |
|
1676 if (ret == -1) |
|
1677 return QVariant(); |
|
1678 else |
|
1679 return QVariant(ret); |
|
1680 } |
|
1681 |
|
1682 |
|
1683 /* |
|
1684 Returns the difference between msecs and elapsed. If msecs is -1, |
|
1685 however, -1 is returned. |
|
1686 */ |
|
1687 static int qt_timeout_value(int msecs, int elapsed) |
|
1688 { |
|
1689 if (msecs == -1) |
|
1690 return -1; |
|
1691 |
|
1692 int timeout = msecs - elapsed; |
|
1693 return timeout < 0 ? 0 : timeout; |
|
1694 } |
|
1695 |
|
1696 /*! |
|
1697 Waits until the socket is connected, up to \a msecs |
|
1698 milliseconds. If the connection has been established, this |
|
1699 function returns true; otherwise it returns false. In the case |
|
1700 where it returns false, you can call error() to determine |
|
1701 the cause of the error. |
|
1702 |
|
1703 The following example waits up to one second for a connection |
|
1704 to be established: |
|
1705 |
|
1706 \snippet doc/src/snippets/code/src_network_socket_qabstractsocket.cpp 0 |
|
1707 |
|
1708 If msecs is -1, this function will not time out. |
|
1709 |
|
1710 \note This function may wait slightly longer than \a msecs, |
|
1711 depending on the time it takes to complete the host lookup. |
|
1712 |
|
1713 \note Multiple calls to this functions do not accumulate the time. |
|
1714 If the function times out, the connecting process will be aborted. |
|
1715 |
|
1716 \sa connectToHost(), connected() |
|
1717 */ |
|
1718 bool QAbstractSocket::waitForConnected(int msecs) |
|
1719 { |
|
1720 Q_D(QAbstractSocket); |
|
1721 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1722 qDebug("QAbstractSocket::waitForConnected(%i)", msecs); |
|
1723 #endif |
|
1724 |
|
1725 if (state() == ConnectedState) { |
|
1726 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1727 qDebug("QAbstractSocket::waitForConnected(%i) already connected", msecs); |
|
1728 #endif |
|
1729 return true; |
|
1730 } |
|
1731 |
|
1732 #ifndef QT_NO_OPENSSL |
|
1733 // Manual polymorphism; this function is not virtual, but has an overload |
|
1734 // in QSslSocket. |
|
1735 if (QSslSocket *socket = qobject_cast<QSslSocket *>(this)) |
|
1736 return socket->waitForConnected(msecs); |
|
1737 #endif |
|
1738 |
|
1739 bool wasPendingClose = d->pendingClose; |
|
1740 d->pendingClose = false; |
|
1741 QTime stopWatch; |
|
1742 stopWatch.start(); |
|
1743 |
|
1744 if (d->state == HostLookupState) { |
|
1745 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1746 qDebug("QAbstractSocket::waitForConnected(%i) doing host name lookup", msecs); |
|
1747 #endif |
|
1748 QHostInfo::abortHostLookup(d->hostLookupId); |
|
1749 d->hostLookupId = -1; |
|
1750 d->_q_startConnecting(QHostInfo::fromName(d->hostName)); |
|
1751 } |
|
1752 if (state() == UnconnectedState) |
|
1753 return false; // connect not im progress anymore! |
|
1754 |
|
1755 bool timedOut = true; |
|
1756 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1757 int attempt = 1; |
|
1758 #endif |
|
1759 while (state() == ConnectingState && (msecs == -1 || stopWatch.elapsed() < msecs)) { |
|
1760 int timeout = qt_timeout_value(msecs, stopWatch.elapsed()); |
|
1761 if (msecs != -1 && timeout > QT_CONNECT_TIMEOUT) |
|
1762 timeout = QT_CONNECT_TIMEOUT; |
|
1763 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1764 qDebug("QAbstractSocket::waitForConnected(%i) waiting %.2f secs for connection attempt #%i", |
|
1765 msecs, timeout / 1000.0, attempt++); |
|
1766 #endif |
|
1767 timedOut = false; |
|
1768 |
|
1769 if (d->socketEngine && d->socketEngine->waitForWrite(timeout, &timedOut) && !timedOut) { |
|
1770 d->_q_testConnection(); |
|
1771 } else { |
|
1772 d->_q_connectToNextAddress(); |
|
1773 } |
|
1774 } |
|
1775 |
|
1776 if ((timedOut && state() != ConnectedState) || state() == ConnectingState) { |
|
1777 d->socketError = SocketTimeoutError; |
|
1778 d->state = UnconnectedState; |
|
1779 emit stateChanged(d->state); |
|
1780 d->resetSocketLayer(); |
|
1781 setErrorString(tr("Socket operation timed out")); |
|
1782 } |
|
1783 |
|
1784 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1785 qDebug("QAbstractSocket::waitForConnected(%i) == %s", msecs, |
|
1786 state() == ConnectedState ? "true" : "false"); |
|
1787 #endif |
|
1788 if (state() != ConnectedState) |
|
1789 return false; |
|
1790 if (wasPendingClose) |
|
1791 disconnectFromHost(); |
|
1792 return true; |
|
1793 } |
|
1794 |
|
1795 /*! |
|
1796 This function blocks until new data is available for reading and the |
|
1797 \l{QIODevice::}{readyRead()} signal has been emitted. The function |
|
1798 will timeout after \a msecs milliseconds; the default timeout is |
|
1799 30000 milliseconds. |
|
1800 |
|
1801 The function returns true if the readyRead() signal is emitted and |
|
1802 there is new data available for reading; otherwise it returns false |
|
1803 (if an error occurred or the operation timed out). |
|
1804 |
|
1805 \sa waitForBytesWritten() |
|
1806 */ |
|
1807 bool QAbstractSocket::waitForReadyRead(int msecs) |
|
1808 { |
|
1809 Q_D(QAbstractSocket); |
|
1810 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1811 qDebug("QAbstractSocket::waitForReadyRead(%i)", msecs); |
|
1812 #endif |
|
1813 |
|
1814 // require calling connectToHost() before waitForReadyRead() |
|
1815 if (state() == UnconnectedState) { |
|
1816 /* If all you have is a QIODevice pointer to an abstractsocket, you cannot check |
|
1817 this, so you cannot avoid this warning. */ |
|
1818 // qWarning("QAbstractSocket::waitForReadyRead() is not allowed in UnconnectedState"); |
|
1819 return false; |
|
1820 } |
|
1821 |
|
1822 QTime stopWatch; |
|
1823 stopWatch.start(); |
|
1824 |
|
1825 // handle a socket in connecting state |
|
1826 if (state() == HostLookupState || state() == ConnectingState) { |
|
1827 if (!waitForConnected(msecs)) |
|
1828 return false; |
|
1829 } |
|
1830 |
|
1831 Q_ASSERT(d->socketEngine); |
|
1832 forever { |
|
1833 bool readyToRead = false; |
|
1834 bool readyToWrite = false; |
|
1835 if (!d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite, true, !d->writeBuffer.isEmpty(), |
|
1836 qt_timeout_value(msecs, stopWatch.elapsed()))) { |
|
1837 d->socketError = d->socketEngine->error(); |
|
1838 setErrorString(d->socketEngine->errorString()); |
|
1839 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1840 qDebug("QAbstractSocket::waitForReadyRead(%i) failed (%i, %s)", |
|
1841 msecs, d->socketError, errorString().toLatin1().constData()); |
|
1842 #endif |
|
1843 emit error(d->socketError); |
|
1844 if (d->socketError != SocketTimeoutError) |
|
1845 close(); |
|
1846 return false; |
|
1847 } |
|
1848 |
|
1849 if (readyToRead) { |
|
1850 if (d->canReadNotification()) |
|
1851 return true; |
|
1852 } |
|
1853 |
|
1854 if (readyToWrite) |
|
1855 d->canWriteNotification(); |
|
1856 |
|
1857 if (state() != ConnectedState) |
|
1858 return false; |
|
1859 } |
|
1860 return false; |
|
1861 } |
|
1862 |
|
1863 /*! \reimp |
|
1864 */ |
|
1865 bool QAbstractSocket::waitForBytesWritten(int msecs) |
|
1866 { |
|
1867 Q_D(QAbstractSocket); |
|
1868 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1869 qDebug("QAbstractSocket::waitForBytesWritten(%i)", msecs); |
|
1870 #endif |
|
1871 |
|
1872 // require calling connectToHost() before waitForBytesWritten() |
|
1873 if (state() == UnconnectedState) { |
|
1874 qWarning("QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState"); |
|
1875 return false; |
|
1876 } |
|
1877 |
|
1878 if (d->writeBuffer.isEmpty()) |
|
1879 return false; |
|
1880 |
|
1881 QTime stopWatch; |
|
1882 stopWatch.start(); |
|
1883 |
|
1884 // handle a socket in connecting state |
|
1885 if (state() == HostLookupState || state() == ConnectingState) { |
|
1886 if (!waitForConnected(msecs)) |
|
1887 return false; |
|
1888 } |
|
1889 |
|
1890 forever { |
|
1891 bool readyToRead = false; |
|
1892 bool readyToWrite = false; |
|
1893 if (!d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite, true, !d->writeBuffer.isEmpty(), |
|
1894 qt_timeout_value(msecs, stopWatch.elapsed()))) { |
|
1895 d->socketError = d->socketEngine->error(); |
|
1896 setErrorString(d->socketEngine->errorString()); |
|
1897 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1898 qDebug("QAbstractSocket::waitForBytesWritten(%i) failed (%i, %s)", |
|
1899 msecs, d->socketError, errorString().toLatin1().constData()); |
|
1900 #endif |
|
1901 emit error(d->socketError); |
|
1902 if (d->socketError != SocketTimeoutError) |
|
1903 close(); |
|
1904 return false; |
|
1905 } |
|
1906 |
|
1907 if (readyToRead) { |
|
1908 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1909 qDebug("QAbstractSocket::waitForBytesWritten calls canReadNotification"); |
|
1910 #endif |
|
1911 if(!d->canReadNotification()) |
|
1912 return false; |
|
1913 } |
|
1914 |
|
1915 |
|
1916 if (readyToWrite) { |
|
1917 if (d->canWriteNotification()) { |
|
1918 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1919 qDebug("QAbstractSocket::waitForBytesWritten returns true"); |
|
1920 #endif |
|
1921 return true; |
|
1922 } |
|
1923 } |
|
1924 |
|
1925 if (state() != ConnectedState) |
|
1926 return false; |
|
1927 } |
|
1928 return false; |
|
1929 } |
|
1930 |
|
1931 /*! |
|
1932 Waits until the socket has disconnected, up to \a msecs |
|
1933 milliseconds. If the connection has been disconnected, this |
|
1934 function returns true; otherwise it returns false. In the case |
|
1935 where it returns false, you can call error() to determine |
|
1936 the cause of the error. |
|
1937 |
|
1938 The following example waits up to one second for a connection |
|
1939 to be closed: |
|
1940 |
|
1941 \snippet doc/src/snippets/code/src_network_socket_qabstractsocket.cpp 1 |
|
1942 |
|
1943 If msecs is -1, this function will not time out. |
|
1944 |
|
1945 \sa disconnectFromHost(), close() |
|
1946 */ |
|
1947 bool QAbstractSocket::waitForDisconnected(int msecs) |
|
1948 { |
|
1949 Q_D(QAbstractSocket); |
|
1950 #ifndef QT_NO_OPENSSL |
|
1951 // Manual polymorphism; this function is not virtual, but has an overload |
|
1952 // in QSslSocket. |
|
1953 if (QSslSocket *socket = qobject_cast<QSslSocket *>(this)) |
|
1954 return socket->waitForDisconnected(msecs); |
|
1955 #endif |
|
1956 |
|
1957 // require calling connectToHost() before waitForDisconnected() |
|
1958 if (state() == UnconnectedState) { |
|
1959 qWarning("QAbstractSocket::waitForDisconnected() is not allowed in UnconnectedState"); |
|
1960 return false; |
|
1961 } |
|
1962 |
|
1963 QTime stopWatch; |
|
1964 stopWatch.start(); |
|
1965 |
|
1966 // handle a socket in connecting state |
|
1967 if (state() == HostLookupState || state() == ConnectingState) { |
|
1968 if (!waitForConnected(msecs)) |
|
1969 return false; |
|
1970 if (state() == UnconnectedState) |
|
1971 return true; |
|
1972 } |
|
1973 |
|
1974 forever { |
|
1975 bool readyToRead = false; |
|
1976 bool readyToWrite = false; |
|
1977 if (!d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite, state() == ConnectedState, |
|
1978 !d->writeBuffer.isEmpty(), |
|
1979 qt_timeout_value(msecs, stopWatch.elapsed()))) { |
|
1980 d->socketError = d->socketEngine->error(); |
|
1981 setErrorString(d->socketEngine->errorString()); |
|
1982 #if defined (QABSTRACTSOCKET_DEBUG) |
|
1983 qDebug("QAbstractSocket::waitForReadyRead(%i) failed (%i, %s)", |
|
1984 msecs, d->socketError, errorString().toLatin1().constData()); |
|
1985 #endif |
|
1986 emit error(d->socketError); |
|
1987 if (d->socketError != SocketTimeoutError) |
|
1988 close(); |
|
1989 return false; |
|
1990 } |
|
1991 |
|
1992 if (readyToRead) |
|
1993 d->canReadNotification(); |
|
1994 if (readyToWrite) |
|
1995 d->canWriteNotification(); |
|
1996 |
|
1997 if (state() == UnconnectedState) |
|
1998 return true; |
|
1999 } |
|
2000 return false; |
|
2001 } |
|
2002 |
|
2003 /*! |
|
2004 Aborts the current connection and resets the socket. Unlike disconnectFromHost(), |
|
2005 this function immediately closes the socket, discarding any pending data in the |
|
2006 write buffer. |
|
2007 |
|
2008 \sa disconnectFromHost(), close() |
|
2009 */ |
|
2010 void QAbstractSocket::abort() |
|
2011 { |
|
2012 Q_D(QAbstractSocket); |
|
2013 #if defined (QABSTRACTSOCKET_DEBUG) |
|
2014 qDebug("QAbstractSocket::abort()"); |
|
2015 #endif |
|
2016 if (d->state == UnconnectedState) |
|
2017 return; |
|
2018 #ifndef QT_NO_OPENSSL |
|
2019 if (QSslSocket *socket = qobject_cast<QSslSocket *>(this)) { |
|
2020 socket->abort(); |
|
2021 return; |
|
2022 } |
|
2023 #endif |
|
2024 if (d->connectTimer) { |
|
2025 d->connectTimer->stop(); |
|
2026 delete d->connectTimer; |
|
2027 d->connectTimer = 0; |
|
2028 } |
|
2029 |
|
2030 d->writeBuffer.clear(); |
|
2031 d->abortCalled = true; |
|
2032 close(); |
|
2033 } |
|
2034 |
|
2035 /*! \reimp |
|
2036 */ |
|
2037 bool QAbstractSocket::isSequential() const |
|
2038 { |
|
2039 return true; |
|
2040 } |
|
2041 |
|
2042 /*! \reimp |
|
2043 |
|
2044 Returns true if no more data is currently |
|
2045 available for reading; otherwise returns false. |
|
2046 |
|
2047 This function is most commonly used when reading data from the |
|
2048 socket in a loop. For example: |
|
2049 |
|
2050 \snippet doc/src/snippets/code/src_network_socket_qabstractsocket.cpp 2 |
|
2051 |
|
2052 \sa bytesAvailable(), readyRead() |
|
2053 */ |
|
2054 bool QAbstractSocket::atEnd() const |
|
2055 { |
|
2056 return QIODevice::atEnd() && (!isOpen() || d_func()->readBuffer.isEmpty()); |
|
2057 } |
|
2058 |
|
2059 /*! |
|
2060 This function writes as much as possible from the internal write buffer to |
|
2061 the underlying network socket, without blocking. If any data was written, |
|
2062 this function returns true; otherwise false is returned. |
|
2063 |
|
2064 Call this function if you need QAbstractSocket to start sending buffered |
|
2065 data immediately. The number of bytes successfully written depends on the |
|
2066 operating system. In most cases, you do not need to call this function, |
|
2067 because QAbstractSocket will start sending data automatically once control |
|
2068 goes back to the event loop. In the absence of an event loop, call |
|
2069 waitForBytesWritten() instead. |
|
2070 |
|
2071 \sa write(), waitForBytesWritten() |
|
2072 */ |
|
2073 // Note! docs copied to QSslSocket::flush() |
|
2074 bool QAbstractSocket::flush() |
|
2075 { |
|
2076 Q_D(QAbstractSocket); |
|
2077 #ifndef QT_NO_OPENSSL |
|
2078 // Manual polymorphism; flush() isn't virtual, but QSslSocket overloads |
|
2079 // it. |
|
2080 if (QSslSocket *socket = qobject_cast<QSslSocket *>(this)) |
|
2081 return socket->flush(); |
|
2082 #endif |
|
2083 Q_CHECK_SOCKETENGINE(false); |
|
2084 return d->flush(); |
|
2085 } |
|
2086 |
|
2087 /*! \reimp |
|
2088 */ |
|
2089 qint64 QAbstractSocket::readData(char *data, qint64 maxSize) |
|
2090 { |
|
2091 Q_D(QAbstractSocket); |
|
2092 if (d->socketEngine && !d->socketEngine->isReadNotificationEnabled() && d->socketEngine->isValid()) |
|
2093 d->socketEngine->setReadNotificationEnabled(true); |
|
2094 |
|
2095 if (!d->isBuffered) { |
|
2096 if (!d->socketEngine) |
|
2097 return -1; // no socket engine is probably EOF |
|
2098 qint64 readBytes = d->socketEngine->read(data, maxSize); |
|
2099 if (readBytes < 0) { |
|
2100 d->socketError = d->socketEngine->error(); |
|
2101 setErrorString(d->socketEngine->errorString()); |
|
2102 } |
|
2103 if (!d->socketEngine->isReadNotificationEnabled()) |
|
2104 d->socketEngine->setReadNotificationEnabled(true); |
|
2105 #if defined (QABSTRACTSOCKET_DEBUG) |
|
2106 qDebug("QAbstractSocket::readData(%p \"%s\", %lli) == %lld", |
|
2107 data, qt_prettyDebug(data, 32, readBytes).data(), maxSize, |
|
2108 readBytes); |
|
2109 #endif |
|
2110 return readBytes; |
|
2111 } |
|
2112 |
|
2113 if (d->readBuffer.isEmpty()) |
|
2114 // if we're still connected, return 0 indicating there may be more data in the future |
|
2115 // if we're not connected, return -1 indicating EOF |
|
2116 return d->state == QAbstractSocket::ConnectedState ? qint64(0) : qint64(-1); |
|
2117 |
|
2118 // If readFromSocket() read data, copy it to its destination. |
|
2119 if (maxSize == 1) { |
|
2120 *data = d->readBuffer.getChar(); |
|
2121 #if defined (QABSTRACTSOCKET_DEBUG) |
|
2122 qDebug("QAbstractSocket::readData(%p '%c (0x%.2x)', 1) == 1", |
|
2123 data, isprint(int(uchar(*data))) ? *data : '?', *data); |
|
2124 #endif |
|
2125 return 1; |
|
2126 } |
|
2127 |
|
2128 qint64 bytesToRead = qMin(qint64(d->readBuffer.size()), maxSize); |
|
2129 qint64 readSoFar = 0; |
|
2130 while (readSoFar < bytesToRead) { |
|
2131 const char *ptr = d->readBuffer.readPointer(); |
|
2132 int bytesToReadFromThisBlock = qMin(int(bytesToRead - readSoFar), |
|
2133 d->readBuffer.nextDataBlockSize()); |
|
2134 memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock); |
|
2135 readSoFar += bytesToReadFromThisBlock; |
|
2136 d->readBuffer.free(bytesToReadFromThisBlock); |
|
2137 } |
|
2138 |
|
2139 #if defined (QABSTRACTSOCKET_DEBUG) |
|
2140 qDebug("QAbstractSocket::readData(%p \"%s\", %lli) == %lld", |
|
2141 data, qt_prettyDebug(data, qMin<qint64>(32, readSoFar), readSoFar).data(), |
|
2142 maxSize, readSoFar); |
|
2143 #endif |
|
2144 return readSoFar; |
|
2145 } |
|
2146 |
|
2147 /*! \reimp |
|
2148 */ |
|
2149 qint64 QAbstractSocket::readLineData(char *data, qint64 maxlen) |
|
2150 { |
|
2151 return QIODevice::readLineData(data, maxlen); |
|
2152 } |
|
2153 |
|
2154 /*! \reimp |
|
2155 */ |
|
2156 qint64 QAbstractSocket::writeData(const char *data, qint64 size) |
|
2157 { |
|
2158 Q_D(QAbstractSocket); |
|
2159 if (d->state == QAbstractSocket::UnconnectedState) { |
|
2160 d->socketError = QAbstractSocket::UnknownSocketError; |
|
2161 setErrorString(tr("Socket is not connected")); |
|
2162 return -1; |
|
2163 } |
|
2164 |
|
2165 if (!d->isBuffered) { |
|
2166 qint64 written = d->socketEngine->write(data, size); |
|
2167 if (written < 0) { |
|
2168 d->socketError = d->socketEngine->error(); |
|
2169 setErrorString(d->socketEngine->errorString()); |
|
2170 } else if (!d->writeBuffer.isEmpty()) { |
|
2171 d->socketEngine->setWriteNotificationEnabled(true); |
|
2172 } |
|
2173 |
|
2174 #if defined (QABSTRACTSOCKET_DEBUG) |
|
2175 qDebug("QAbstractSocket::writeData(%p \"%s\", %lli) == %lli", data, |
|
2176 qt_prettyDebug(data, qMin((int)size, 32), size).data(), |
|
2177 size, written); |
|
2178 #endif |
|
2179 if (written >= 0) |
|
2180 emit bytesWritten(written); |
|
2181 return written; |
|
2182 } |
|
2183 |
|
2184 char *ptr = d->writeBuffer.reserve(size); |
|
2185 if (size == 1) |
|
2186 *ptr = *data; |
|
2187 else |
|
2188 memcpy(ptr, data, size); |
|
2189 |
|
2190 qint64 written = size; |
|
2191 |
|
2192 if (d->socketEngine && !d->writeBuffer.isEmpty()) |
|
2193 d->socketEngine->setWriteNotificationEnabled(true); |
|
2194 |
|
2195 #if defined (QABSTRACTSOCKET_DEBUG) |
|
2196 qDebug("QAbstractSocket::writeData(%p \"%s\", %lli) == %lli", data, |
|
2197 qt_prettyDebug(data, qMin((int)size, 32), size).data(), |
|
2198 size, written); |
|
2199 #endif |
|
2200 return written; |
|
2201 } |
|
2202 |
|
2203 /*! |
|
2204 \since 4.1 |
|
2205 |
|
2206 Sets the port on the local side of a connection to \a port. |
|
2207 |
|
2208 You can call this function in a subclass of QAbstractSocket to |
|
2209 change the return value of the localPort() function after a |
|
2210 connection has been established. This feature is commonly used by |
|
2211 proxy connections for virtual connection settings. |
|
2212 |
|
2213 Note that this function does not bind the local port of the socket |
|
2214 prior to a connection (e.g., QUdpSocket::bind()). |
|
2215 |
|
2216 \sa localAddress(), setLocalAddress(), setPeerPort() |
|
2217 */ |
|
2218 void QAbstractSocket::setLocalPort(quint16 port) |
|
2219 { |
|
2220 Q_D(QAbstractSocket); |
|
2221 d->localPort = port; |
|
2222 } |
|
2223 |
|
2224 /*! |
|
2225 \since 4.1 |
|
2226 |
|
2227 Sets the address on the local side of a connection to |
|
2228 \a address. |
|
2229 |
|
2230 You can call this function in a subclass of QAbstractSocket to |
|
2231 change the return value of the localAddress() function after a |
|
2232 connection has been established. This feature is commonly used by |
|
2233 proxy connections for virtual connection settings. |
|
2234 |
|
2235 Note that this function does not bind the local address of the socket |
|
2236 prior to a connection (e.g., QUdpSocket::bind()). |
|
2237 |
|
2238 \sa localAddress(), setLocalPort(), setPeerAddress() |
|
2239 */ |
|
2240 void QAbstractSocket::setLocalAddress(const QHostAddress &address) |
|
2241 { |
|
2242 Q_D(QAbstractSocket); |
|
2243 d->localAddress = address; |
|
2244 } |
|
2245 |
|
2246 /*! |
|
2247 \since 4.1 |
|
2248 |
|
2249 Sets the port of the remote side of the connection to |
|
2250 \a port. |
|
2251 |
|
2252 You can call this function in a subclass of QAbstractSocket to |
|
2253 change the return value of the peerPort() function after a |
|
2254 connection has been established. This feature is commonly used by |
|
2255 proxy connections for virtual connection settings. |
|
2256 |
|
2257 \sa peerPort(), setPeerAddress(), setLocalPort() |
|
2258 */ |
|
2259 void QAbstractSocket::setPeerPort(quint16 port) |
|
2260 { |
|
2261 Q_D(QAbstractSocket); |
|
2262 d->peerPort = port; |
|
2263 } |
|
2264 |
|
2265 /*! |
|
2266 \since 4.1 |
|
2267 |
|
2268 Sets the address of the remote side of the connection |
|
2269 to \a address. |
|
2270 |
|
2271 You can call this function in a subclass of QAbstractSocket to |
|
2272 change the return value of the peerAddress() function after a |
|
2273 connection has been established. This feature is commonly used by |
|
2274 proxy connections for virtual connection settings. |
|
2275 |
|
2276 \sa peerAddress(), setPeerPort(), setLocalAddress() |
|
2277 */ |
|
2278 void QAbstractSocket::setPeerAddress(const QHostAddress &address) |
|
2279 { |
|
2280 Q_D(QAbstractSocket); |
|
2281 d->peerAddress = address; |
|
2282 } |
|
2283 |
|
2284 /*! |
|
2285 \since 4.1 |
|
2286 |
|
2287 Sets the host name of the remote peer to \a name. |
|
2288 |
|
2289 You can call this function in a subclass of QAbstractSocket to |
|
2290 change the return value of the peerName() function after a |
|
2291 connection has been established. This feature is commonly used by |
|
2292 proxy connections for virtual connection settings. |
|
2293 |
|
2294 \sa peerName() |
|
2295 */ |
|
2296 void QAbstractSocket::setPeerName(const QString &name) |
|
2297 { |
|
2298 Q_D(QAbstractSocket); |
|
2299 d->peerName = name; |
|
2300 } |
|
2301 |
|
2302 /*! |
|
2303 Closes the I/O device for the socket, disconnects the socket's connection with the |
|
2304 host, closes the socket, and resets the name, address, port number and underlying |
|
2305 socket descriptor. |
|
2306 |
|
2307 See QIODevice::close() for a description of the actions that occur when an I/O |
|
2308 device is closed. |
|
2309 |
|
2310 \sa abort() |
|
2311 */ |
|
2312 void QAbstractSocket::close() |
|
2313 { |
|
2314 Q_D(QAbstractSocket); |
|
2315 #if defined(QABSTRACTSOCKET_DEBUG) |
|
2316 qDebug("QAbstractSocket::close()"); |
|
2317 #endif |
|
2318 QIODevice::close(); |
|
2319 if (d->state != UnconnectedState) { |
|
2320 d->closeCalled = true; |
|
2321 disconnectFromHost(); |
|
2322 } |
|
2323 |
|
2324 d->localPort = 0; |
|
2325 d->peerPort = 0; |
|
2326 d->localAddress.clear(); |
|
2327 d->peerAddress.clear(); |
|
2328 d->peerName.clear(); |
|
2329 d->cachedSocketDescriptor = -1; |
|
2330 } |
|
2331 |
|
2332 /*! |
|
2333 Attempts to close the socket. If there is pending data waiting to |
|
2334 be written, QAbstractSocket will enter ClosingState and wait |
|
2335 until all data has been written. Eventually, it will enter |
|
2336 UnconnectedState and emit the disconnected() signal. |
|
2337 |
|
2338 \sa connectToHost() |
|
2339 */ |
|
2340 void QAbstractSocket::disconnectFromHost() |
|
2341 { |
|
2342 QMetaObject::invokeMethod(this, "disconnectFromHostImplementation", |
|
2343 Qt::DirectConnection); |
|
2344 } |
|
2345 |
|
2346 /*! |
|
2347 \since 4.1 |
|
2348 |
|
2349 Contains the implementation of disconnectFromHost(). |
|
2350 */ |
|
2351 void QAbstractSocket::disconnectFromHostImplementation() |
|
2352 { |
|
2353 Q_D(QAbstractSocket); |
|
2354 #if defined(QABSTRACTSOCKET_DEBUG) |
|
2355 qDebug("QAbstractSocket::disconnectFromHost()"); |
|
2356 #endif |
|
2357 |
|
2358 if (d->state == UnconnectedState) { |
|
2359 #if defined(QABSTRACTSOCKET_DEBUG) |
|
2360 qDebug("QAbstractSocket::disconnectFromHost() was called on an unconnected socket"); |
|
2361 #endif |
|
2362 return; |
|
2363 } |
|
2364 |
|
2365 if (!d->abortCalled && (d->state == ConnectingState || d->state == HostLookupState)) { |
|
2366 #if defined(QABSTRACTSOCKET_DEBUG) |
|
2367 qDebug("QAbstractSocket::disconnectFromHost() but we're still connecting"); |
|
2368 #endif |
|
2369 d->pendingClose = true; |
|
2370 return; |
|
2371 } |
|
2372 |
|
2373 #ifdef QT3_SUPPORT |
|
2374 emit connectionClosed(); // compat signal |
|
2375 #endif |
|
2376 |
|
2377 // Disable and delete read notification |
|
2378 if (d->socketEngine) |
|
2379 d->socketEngine->setReadNotificationEnabled(false); |
|
2380 |
|
2381 if (d->abortCalled) { |
|
2382 #if defined(QABSTRACTSOCKET_DEBUG) |
|
2383 qDebug("QAbstractSocket::disconnectFromHost() aborting immediately"); |
|
2384 #endif |
|
2385 } else { |
|
2386 // Perhaps emit closing() |
|
2387 if (d->state != ClosingState) { |
|
2388 d->state = ClosingState; |
|
2389 #if defined(QABSTRACTSOCKET_DEBUG) |
|
2390 qDebug("QAbstractSocket::disconnectFromHost() emits stateChanged()(ClosingState)"); |
|
2391 #endif |
|
2392 emit stateChanged(d->state); |
|
2393 } else { |
|
2394 #if defined(QABSTRACTSOCKET_DEBUG) |
|
2395 qDebug("QAbstractSocket::disconnectFromHost() return from delayed close"); |
|
2396 #endif |
|
2397 } |
|
2398 |
|
2399 // Wait for pending data to be written. |
|
2400 if (d->socketEngine && d->socketEngine->isValid() && (d->writeBuffer.size() > 0 |
|
2401 || d->socketEngine->bytesToWrite() > 0)) { |
|
2402 // hack: when we are waiting for the socket engine to write bytes (only |
|
2403 // possible when using Socks5 or HTTP socket engine), then close |
|
2404 // anyway after 2 seconds. This is to prevent a timeout on Mac, where we |
|
2405 // sometimes just did not get the write notifier from the underlying |
|
2406 // CFSocket and no progress was made. |
|
2407 if (d->writeBuffer.size() == 0 && d->socketEngine->bytesToWrite() > 0) { |
|
2408 if (!d->disconnectTimer) { |
|
2409 d->disconnectTimer = new QTimer(this); |
|
2410 connect(d->disconnectTimer, SIGNAL(timeout()), this, |
|
2411 SLOT(_q_forceDisconnect()), Qt::DirectConnection); |
|
2412 } |
|
2413 if (!d->disconnectTimer->isActive()) |
|
2414 d->disconnectTimer->start(2000); |
|
2415 } |
|
2416 d->socketEngine->setWriteNotificationEnabled(true); |
|
2417 |
|
2418 #if defined(QABSTRACTSOCKET_DEBUG) |
|
2419 qDebug("QAbstractSocket::disconnectFromHost() delaying disconnect"); |
|
2420 #endif |
|
2421 return; |
|
2422 } else { |
|
2423 #if defined(QABSTRACTSOCKET_DEBUG) |
|
2424 qDebug("QAbstractSocket::disconnectFromHost() disconnecting immediately"); |
|
2425 #endif |
|
2426 } |
|
2427 } |
|
2428 |
|
2429 SocketState previousState = d->state; |
|
2430 d->resetSocketLayer(); |
|
2431 d->state = UnconnectedState; |
|
2432 emit stateChanged(d->state); |
|
2433 emit readChannelFinished(); // we got an EOF |
|
2434 |
|
2435 #ifdef QT3_SUPPORT |
|
2436 emit delayedCloseFinished(); // compat signal |
|
2437 #endif |
|
2438 // only emit disconnected if we were connected before |
|
2439 if (previousState == ConnectedState || previousState == ClosingState) |
|
2440 emit disconnected(); |
|
2441 |
|
2442 d->localPort = 0; |
|
2443 d->peerPort = 0; |
|
2444 d->localAddress.clear(); |
|
2445 d->peerAddress.clear(); |
|
2446 |
|
2447 #if defined(QABSTRACTSOCKET_DEBUG) |
|
2448 qDebug("QAbstractSocket::disconnectFromHost() disconnected!"); |
|
2449 #endif |
|
2450 |
|
2451 if (d->closeCalled) { |
|
2452 #if defined(QABSTRACTSOCKET_DEBUG) |
|
2453 qDebug("QAbstractSocket::disconnectFromHost() closed!"); |
|
2454 #endif |
|
2455 d->readBuffer.clear(); |
|
2456 d->writeBuffer.clear(); |
|
2457 QIODevice::close(); |
|
2458 } |
|
2459 } |
|
2460 |
|
2461 /*! |
|
2462 Returns the size of the internal read buffer. This limits the |
|
2463 amount of data that the client can receive before you call read() |
|
2464 or readAll(). |
|
2465 |
|
2466 A read buffer size of 0 (the default) means that the buffer has |
|
2467 no size limit, ensuring that no data is lost. |
|
2468 |
|
2469 \sa setReadBufferSize(), read() |
|
2470 */ |
|
2471 qint64 QAbstractSocket::readBufferSize() const |
|
2472 { |
|
2473 return d_func()->readBufferMaxSize; |
|
2474 } |
|
2475 |
|
2476 /*! |
|
2477 Sets the size of QAbstractSocket's internal read buffer to be \a |
|
2478 size bytes. |
|
2479 |
|
2480 If the buffer size is limited to a certain size, QAbstractSocket |
|
2481 won't buffer more than this size of data. Exceptionally, a buffer |
|
2482 size of 0 means that the read buffer is unlimited and all |
|
2483 incoming data is buffered. This is the default. |
|
2484 |
|
2485 This option is useful if you only read the data at certain points |
|
2486 in time (e.g., in a real-time streaming application) or if you |
|
2487 want to protect your socket against receiving too much data, |
|
2488 which may eventually cause your application to run out of memory. |
|
2489 |
|
2490 Only QTcpSocket uses QAbstractSocket's internal buffer; QUdpSocket |
|
2491 does not use any buffering at all, but rather relies on the |
|
2492 implicit buffering provided by the operating system. |
|
2493 Because of this, calling this function on QUdpSocket has no |
|
2494 effect. |
|
2495 |
|
2496 \sa readBufferSize(), read() |
|
2497 */ |
|
2498 void QAbstractSocket::setReadBufferSize(qint64 size) |
|
2499 { |
|
2500 Q_D(QAbstractSocket); |
|
2501 |
|
2502 #ifndef QT_NO_OPENSSL |
|
2503 // Manual polymorphism; setReadBufferSize() isn't virtual, but QSslSocket overloads |
|
2504 // it. |
|
2505 if (QSslSocket *socket = qobject_cast<QSslSocket *>(this)) { |
|
2506 socket->setReadBufferSize(size); |
|
2507 return; |
|
2508 } |
|
2509 #endif |
|
2510 |
|
2511 if (d->readBufferMaxSize == size) |
|
2512 return; |
|
2513 d->readBufferMaxSize = size; |
|
2514 if (!d->readSocketNotifierCalled && d->socketEngine) { |
|
2515 // ensure that the read notification is enabled if we've now got |
|
2516 // room in the read buffer |
|
2517 // but only if we're not inside canReadNotification -- that will take care on its own |
|
2518 if (size == 0 || d->readBuffer.size() < size) |
|
2519 d->socketEngine->setReadNotificationEnabled(true); |
|
2520 } |
|
2521 } |
|
2522 |
|
2523 /*! |
|
2524 Returns the state of the socket. |
|
2525 |
|
2526 \sa error() |
|
2527 */ |
|
2528 QAbstractSocket::SocketState QAbstractSocket::state() const |
|
2529 { |
|
2530 return d_func()->state; |
|
2531 } |
|
2532 |
|
2533 /*! |
|
2534 Sets the state of the socket to \a state. |
|
2535 |
|
2536 \sa state() |
|
2537 */ |
|
2538 void QAbstractSocket::setSocketState(SocketState state) |
|
2539 { |
|
2540 d_func()->state = state; |
|
2541 } |
|
2542 |
|
2543 /*! |
|
2544 Returns the socket type (TCP, UDP, or other). |
|
2545 |
|
2546 \sa QTcpSocket, QUdpSocket |
|
2547 */ |
|
2548 QAbstractSocket::SocketType QAbstractSocket::socketType() const |
|
2549 { |
|
2550 return d_func()->socketType; |
|
2551 } |
|
2552 |
|
2553 /*! |
|
2554 Returns the type of error that last occurred. |
|
2555 |
|
2556 \sa state(), errorString() |
|
2557 */ |
|
2558 QAbstractSocket::SocketError QAbstractSocket::error() const |
|
2559 { |
|
2560 return d_func()->socketError; |
|
2561 } |
|
2562 |
|
2563 /*! |
|
2564 Sets the type of error that last occurred to \a socketError. |
|
2565 |
|
2566 \sa setSocketState(), setErrorString() |
|
2567 */ |
|
2568 void QAbstractSocket::setSocketError(SocketError socketError) |
|
2569 { |
|
2570 d_func()->socketError = socketError; |
|
2571 } |
|
2572 |
|
2573 #ifndef QT_NO_NETWORKPROXY |
|
2574 /*! |
|
2575 \since 4.1 |
|
2576 |
|
2577 Sets the explicit network proxy for this socket to \a networkProxy. |
|
2578 |
|
2579 To disable the use of a proxy for this socket, use the |
|
2580 QNetworkProxy::NoProxy proxy type: |
|
2581 |
|
2582 \snippet doc/src/snippets/code/src_network_socket_qabstractsocket.cpp 3 |
|
2583 |
|
2584 The default value for the proxy is QNetworkProxy::DefaultProxy, |
|
2585 which means the socket will use the application settings: if a |
|
2586 proxy is set with QNetworkProxy::setApplicationProxy, it will use |
|
2587 that; otherwise, if a factory is set with |
|
2588 QNetworkProxyFactory::setApplicationProxyFactory, it will query |
|
2589 that factory with type QNetworkProxyQuery::TcpSocket. |
|
2590 |
|
2591 \sa proxy(), QNetworkProxy, QNetworkProxyFactory::queryProxy() |
|
2592 */ |
|
2593 void QAbstractSocket::setProxy(const QNetworkProxy &networkProxy) |
|
2594 { |
|
2595 Q_D(QAbstractSocket); |
|
2596 d->proxy = networkProxy; |
|
2597 } |
|
2598 |
|
2599 /*! |
|
2600 \since 4.1 |
|
2601 |
|
2602 Returns the network proxy for this socket. |
|
2603 By default QNetworkProxy::DefaultProxy is used, which means this |
|
2604 socket will query the default proxy settings for the application. |
|
2605 |
|
2606 \sa setProxy(), QNetworkProxy, QNetworkProxyFactory |
|
2607 */ |
|
2608 QNetworkProxy QAbstractSocket::proxy() const |
|
2609 { |
|
2610 Q_D(const QAbstractSocket); |
|
2611 return d->proxy; |
|
2612 } |
|
2613 #endif // QT_NO_NETWORKPROXY |
|
2614 |
|
2615 #ifdef QT3_SUPPORT |
|
2616 /*! |
|
2617 \enum QAbstractSocket::Error |
|
2618 \compat |
|
2619 |
|
2620 Use QAbstractSocket::SocketError instead. |
|
2621 |
|
2622 \value ErrConnectionRefused Use QAbstractSocket::ConnectionRefusedError instead. |
|
2623 \value ErrHostNotFound Use QAbstractSocket::HostNotFoundError instead. |
|
2624 \value ErrSocketRead Use QAbstractSocket::UnknownSocketError instead. |
|
2625 */ |
|
2626 |
|
2627 /*! |
|
2628 \typedef QAbstractSocket::State |
|
2629 \compat |
|
2630 |
|
2631 Use QAbstractSocket::SocketState instead. |
|
2632 |
|
2633 \table |
|
2634 \header \o Qt 3 enum value \o Qt 4 enum value |
|
2635 \row \o \c Idle \o \l UnconnectedState |
|
2636 \row \o \c HostLookup \o \l HostLookupState |
|
2637 \row \o \c Connecting \o \l ConnectingState |
|
2638 \row \o \c Connected \o \l ConnectedState |
|
2639 \row \o \c Closing \o \l ClosingState |
|
2640 \row \o \c Connection \o \l ConnectedState |
|
2641 \endtable |
|
2642 */ |
|
2643 |
|
2644 /*! |
|
2645 \fn int QAbstractSocket::socket() const |
|
2646 |
|
2647 Use socketDescriptor() instead. |
|
2648 */ |
|
2649 |
|
2650 /*! |
|
2651 \fn void QAbstractSocket::setSocket(int socket) |
|
2652 |
|
2653 Use setSocketDescriptor() instead. |
|
2654 */ |
|
2655 |
|
2656 /*! |
|
2657 \fn Q_ULONG QAbstractSocket::waitForMore(int msecs, bool *timeout = 0) const |
|
2658 |
|
2659 Use waitForReadyRead() instead. |
|
2660 |
|
2661 \oldcode |
|
2662 bool timeout; |
|
2663 Q_ULONG numBytes = socket->waitForMore(30000, &timeout); |
|
2664 \newcode |
|
2665 qint64 numBytes = 0; |
|
2666 if (socket->waitForReadyRead(msecs)) |
|
2667 numBytes = socket->bytesAvailable(); |
|
2668 bool timeout = (error() == QAbstractSocket::SocketTimeoutError); |
|
2669 \endcode |
|
2670 |
|
2671 \sa waitForReadyRead(), bytesAvailable(), error(), SocketTimeoutError |
|
2672 */ |
|
2673 |
|
2674 /*! |
|
2675 \fn void QAbstractSocket::connectionClosed() |
|
2676 |
|
2677 Use disconnected() instead. |
|
2678 */ |
|
2679 |
|
2680 /*! |
|
2681 \fn void QAbstractSocket::delayedCloseFinished() |
|
2682 |
|
2683 Use disconnected() instead. |
|
2684 */ |
|
2685 #endif // QT3_SUPPORT |
|
2686 |
|
2687 #ifndef QT_NO_DEBUG_STREAM |
|
2688 Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, QAbstractSocket::SocketError error) |
|
2689 { |
|
2690 switch (error) { |
|
2691 case QAbstractSocket::ConnectionRefusedError: |
|
2692 debug << "QAbstractSocket::ConnectionRefusedError"; |
|
2693 break; |
|
2694 case QAbstractSocket::RemoteHostClosedError: |
|
2695 debug << "QAbstractSocket::RemoteHostClosedError"; |
|
2696 break; |
|
2697 case QAbstractSocket::HostNotFoundError: |
|
2698 debug << "QAbstractSocket::HostNotFoundError"; |
|
2699 break; |
|
2700 case QAbstractSocket::SocketAccessError: |
|
2701 debug << "QAbstractSocket::SocketAccessError"; |
|
2702 break; |
|
2703 case QAbstractSocket::SocketResourceError: |
|
2704 debug << "QAbstractSocket::SocketResourceError"; |
|
2705 break; |
|
2706 case QAbstractSocket::SocketTimeoutError: |
|
2707 debug << "QAbstractSocket::SocketTimeoutError"; |
|
2708 break; |
|
2709 case QAbstractSocket::DatagramTooLargeError: |
|
2710 debug << "QAbstractSocket::DatagramTooLargeError"; |
|
2711 break; |
|
2712 case QAbstractSocket::NetworkError: |
|
2713 debug << "QAbstractSocket::NetworkError"; |
|
2714 break; |
|
2715 case QAbstractSocket::AddressInUseError: |
|
2716 debug << "QAbstractSocket::AddressInUseError"; |
|
2717 break; |
|
2718 case QAbstractSocket::SocketAddressNotAvailableError: |
|
2719 debug << "QAbstractSocket::SocketAddressNotAvailableError"; |
|
2720 break; |
|
2721 case QAbstractSocket::UnsupportedSocketOperationError: |
|
2722 debug << "QAbstractSocket::UnsupportedSocketOperationError"; |
|
2723 break; |
|
2724 case QAbstractSocket::UnfinishedSocketOperationError: |
|
2725 debug << "QAbstractSocket::UnfinishedSocketOperationError"; |
|
2726 break; |
|
2727 case QAbstractSocket::ProxyAuthenticationRequiredError: |
|
2728 debug << "QAbstractSocket::ProxyAuthenticationRequiredError"; |
|
2729 break; |
|
2730 case QAbstractSocket::UnknownSocketError: |
|
2731 debug << "QAbstractSocket::UnknownSocketError"; |
|
2732 break; |
|
2733 case QAbstractSocket::ProxyConnectionRefusedError: |
|
2734 debug << "QAbstractSocket::ProxyConnectionRefusedError"; |
|
2735 break; |
|
2736 case QAbstractSocket::ProxyConnectionClosedError: |
|
2737 debug << "QAbstractSocket::ProxyConnectionClosedError"; |
|
2738 break; |
|
2739 case QAbstractSocket::ProxyConnectionTimeoutError: |
|
2740 debug << "QAbstractSocket::ProxyConnectionTimeoutError"; |
|
2741 break; |
|
2742 case QAbstractSocket::ProxyNotFoundError: |
|
2743 debug << "QAbstractSocket::ProxyNotFoundError"; |
|
2744 break; |
|
2745 case QAbstractSocket::ProxyProtocolError: |
|
2746 debug << "QAbstractSocket::ProxyProtocolError"; |
|
2747 break; |
|
2748 default: |
|
2749 debug << "QAbstractSocket::SocketError(" << int(error) << ')'; |
|
2750 break; |
|
2751 } |
|
2752 return debug; |
|
2753 } |
|
2754 |
|
2755 Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, QAbstractSocket::SocketState state) |
|
2756 { |
|
2757 switch (state) { |
|
2758 case QAbstractSocket::UnconnectedState: |
|
2759 debug << "QAbstractSocket::UnconnectedState"; |
|
2760 break; |
|
2761 case QAbstractSocket::HostLookupState: |
|
2762 debug << "QAbstractSocket::HostLookupState"; |
|
2763 break; |
|
2764 case QAbstractSocket::ConnectingState: |
|
2765 debug << "QAbstractSocket::ConnectingState"; |
|
2766 break; |
|
2767 case QAbstractSocket::ConnectedState: |
|
2768 debug << "QAbstractSocket::ConnectedState"; |
|
2769 break; |
|
2770 case QAbstractSocket::BoundState: |
|
2771 debug << "QAbstractSocket::BoundState"; |
|
2772 break; |
|
2773 case QAbstractSocket::ListeningState: |
|
2774 debug << "QAbstractSocket::ListeningState"; |
|
2775 break; |
|
2776 case QAbstractSocket::ClosingState: |
|
2777 debug << "QAbstractSocket::ClosingState"; |
|
2778 break; |
|
2779 default: |
|
2780 debug << "QAbstractSocket::SocketState(" << int(state) << ')'; |
|
2781 break; |
|
2782 } |
|
2783 return debug; |
|
2784 } |
|
2785 #endif |
|
2786 |
|
2787 QT_END_NAMESPACE |
|
2788 |
|
2789 #include "moc_qabstractsocket.cpp" |