Changeset 561 for trunk/src/network/socket/qabstractsocket.cpp
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/network/socket/qabstractsocket.cpp
r2 r561 2 2 ** 3 3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). 4 ** Contact: Qt Software Information (qt-info@nokia.com) 4 ** All rights reserved. 5 ** Contact: Nokia Corporation (qt-info@nokia.com) 5 6 ** 6 7 ** This file is part of the QtNetwork module of the Qt Toolkit. … … 21 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 22 23 ** 23 ** In addition, as a special exception, Nokia gives you certain 24 ** additional rights. These rights are described in the Nokia Qt LGPL 25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 26 ** package. 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 27 ** 28 28 ** GNU General Public License Usage … … 34 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 35 ** 36 ** If you are unsure which license is appropriate for your use, please37 ** contact the sales department at qt-sales@nokia.com.36 ** If you have questions regarding the use of this file, please contact 37 ** Nokia at qt-info@nokia.com. 38 38 ** $QT_END_LICENSE$ 39 39 ** … … 49 49 50 50 \reentrant 51 \ingroup io51 \ingroup network 52 52 \inmodule QtNetwork 53 53 … … 100 100 write buffer size. You can monitor its size by listening to this 101 101 signal. 102 102 103 103 The readyRead() signal is emitted every time a new chunk of data 104 104 has arrived. bytesAvailable() then returns the number of bytes … … 161 161 is available before attempting to read it using operator>>(). 162 162 163 \sa QFtp, Q Http, QTcpServer163 \sa QFtp, QNetworkAccessManager, QTcpServer 164 164 */ 165 165 … … 200 200 201 201 QAbstractSocket::SocketError is not a registered metatype, so for queued 202 connections, you will have to register it with Q_REGISTER_METATYPE. 203 204 \sa error(), errorString() 202 connections, you will have to register it with Q_DECLARE_METATYPE() and 203 qRegisterMetaType(). 204 205 \sa error(), errorString(), {Creating Custom Qt Types} 205 206 */ 206 207 … … 212 213 213 214 QAbstractSocket::SocketState is not a registered metatype, so for queued 214 connections, you will have to register it with Q_REGISTER_METATYPE. 215 216 \sa state() 215 connections, you will have to register it with Q_REGISTER_METATYPE() and 216 qRegisterMetaType(). 217 218 \sa state(), {Creating Custom Qt Types} 217 219 */ 218 220 … … 331 333 */ 332 334 335 /*! 336 \enum QAbstractSocket::SocketOption 337 \since 4.6 338 339 This enum represents the options that can be set on a socket. 340 If desired, they can be set after having received the connected() signal from 341 the socket or after having received a new socket from a QTcpServer. 342 343 \value LowDelayOption Try to optimize the socket for low latency. For a QTcpSocket 344 this would set the TCP_NODELAY option and disable Nagle's algorithm. Set this to 1 345 to enable. 346 \value KeepAliveOption Set this to 1 to enable the SO_KEEPALIVE socket option 347 348 \sa QAbstractSocket::setSocketOption(), QAbstractSocket::socketOption() 349 */ 350 333 351 #include "qabstractsocket.h" 334 352 #include "qabstractsocket_p.h" … … 436 454 socketEngine(0), 437 455 cachedSocketDescriptor(-1), 456 #ifdef Q_OS_LINUX 457 addToBytesAvailable(0), 458 #endif 438 459 readBufferMaxSize(0), 439 460 readBuffer(QABSTRACTSOCKET_BUFFERSIZE), … … 442 463 blockingTimeout(30000), 443 464 connectTimer(0), 465 disconnectTimer(0), 444 466 connectTimeElapsed(0), 445 467 hostLookupId(-1), 468 socketType(QAbstractSocket::UnknownSocketType), 446 469 state(QAbstractSocket::UnconnectedState), 447 470 socketError(QAbstractSocket::UnknownSocketError) … … 476 499 cachedSocketDescriptor = -1; 477 500 } 478 if (connectTimer) {501 if (connectTimer) 479 502 connectTimer->stop(); 480 } 503 if (disconnectTimer) 504 disconnectTimer->stop(); 481 505 } 482 506 … … 648 672 if (socketEngine) { 649 673 #if defined (Q_OS_WIN) 650 651 674 if (!writeBuffer.isEmpty()) 675 socketEngine->setWriteNotificationEnabled(true); 652 676 #else 653 if (writeBuffer.isEmpty())654 677 if (writeBuffer.isEmpty() && socketEngine->bytesToWrite() == 0) 678 socketEngine->setWriteNotificationEnabled(false); 655 679 #endif 656 680 } … … 689 713 { 690 714 Q_Q(QAbstractSocket); 691 if (!socketEngine || !socketEngine->isValid() || writeBuffer.isEmpty()) { 715 if (!socketEngine || !socketEngine->isValid() || (writeBuffer.isEmpty() 716 && socketEngine->bytesToWrite() == 0)) { 692 717 #if defined (QABSTRACTSOCKET_DEBUG) 693 718 qDebug("QAbstractSocketPrivate::flush() nothing to do: valid ? %s, writeBuffer.isEmpty() ? %s", 694 719 socketEngine->isValid() ? "yes" : "no", writeBuffer.isEmpty() ? "yes" : "no"); 695 720 #endif 721 722 // this covers the case when the buffer was empty, but we had to wait for the socket engine to finish 723 if (state == QAbstractSocket::ClosingState) 724 q->disconnectFromHost(); 725 696 726 return false; 697 727 } … … 730 760 } 731 761 732 if (writeBuffer.isEmpty() && socketEngine && socketEngine->isWriteNotificationEnabled()) 762 if (writeBuffer.isEmpty() && socketEngine && socketEngine->isWriteNotificationEnabled() 763 && !socketEngine->bytesToWrite()) 733 764 socketEngine->setWriteNotificationEnabled(false); 734 765 if (state == QAbstractSocket::ClosingState) … … 851 882 s += addresses.at(i).toString(); 852 883 } 853 s += "}";884 s += '}'; 854 885 qDebug("QAbstractSocketPrivate::_q_startConnecting(hostInfo == %s)", s.toLatin1().constData()); 855 886 #endif … … 1052 1083 if (socketEngine) 1053 1084 socketEngine->setWriteNotificationEnabled(false); 1085 1054 1086 connectTimer->stop(); 1055 1087 … … 1062 1094 } else { 1063 1095 _q_connectToNextAddress(); 1096 } 1097 } 1098 1099 void QAbstractSocketPrivate::_q_forceDisconnect() 1100 { 1101 Q_Q(QAbstractSocket); 1102 if (socketEngine && socketEngine->isValid() && state == QAbstractSocket::ClosingState) { 1103 socketEngine->close(); 1104 q->disconnectFromHost(); 1064 1105 } 1065 1106 } … … 1128 1169 /*! \internal 1129 1170 1130 Sets up the theinternal state after the connection has succeeded.1171 Sets up the internal state after the connection has succeeded. 1131 1172 */ 1132 1173 void QAbstractSocketPrivate::fetchConnectionParameters() … … 1232 1273 \a hostName may be an IP address in string form (e.g., 1233 1274 "43.195.83.32"), or it may be a host name (e.g., 1234 " qtsoftware.com"). QAbstractSocket will do a lookup only if1275 "example.com"). QAbstractSocket will do a lookup only if 1235 1276 required. \a port is in native byte order. 1236 1277 … … 1264 1305 #endif 1265 1306 1266 if (d->state == ConnectedState || d->state == ConnectingState) { 1267 qWarning("QAbstractSocket::connectToHost() called when already connecting/connected to \"%s\"", qPrintable(hostName)); 1307 if (d->state == ConnectedState || d->state == ConnectingState 1308 || d->state == ClosingState || d->state == HostLookupState) { 1309 qWarning("QAbstractSocket::connectToHost() called when already looking up or connecting/connected to \"%s\"", qPrintable(hostName)); 1268 1310 return; 1269 1311 } … … 1547 1589 } 1548 1590 1591 /*! 1592 \since 4.6 1593 Sets the given \a option to the value described by \a value. 1594 1595 \sa socketOption() 1596 */ 1597 void QAbstractSocket::setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value) 1598 { 1599 #ifndef QT_NO_OPENSSL 1600 if (QSslSocket *sslSocket = qobject_cast<QSslSocket*>(this)) { 1601 sslSocket->setSocketOption(option, value); 1602 return; 1603 } 1604 #endif 1605 1606 if (!d_func()->socketEngine) 1607 return; 1608 1609 switch (option) { 1610 case LowDelayOption: 1611 d_func()->socketEngine->setOption(QAbstractSocketEngine::LowDelayOption, value.toInt()); 1612 break; 1613 1614 case KeepAliveOption: 1615 d_func()->socketEngine->setOption(QAbstractSocketEngine::KeepAliveOption, value.toInt()); 1616 break; 1617 } 1618 } 1619 1620 /*! 1621 \since 4.6 1622 Returns the value of the \a option option. 1623 1624 \sa setSocketOption() 1625 */ 1626 QVariant QAbstractSocket::socketOption(QAbstractSocket::SocketOption option) 1627 { 1628 #ifndef QT_NO_OPENSSL 1629 if (QSslSocket *sslSocket = qobject_cast<QSslSocket*>(this)) { 1630 return sslSocket->socketOption(option); 1631 } 1632 #endif 1633 1634 if (!d_func()->socketEngine) 1635 return QVariant(); 1636 1637 int ret = -1; 1638 switch (option) { 1639 case LowDelayOption: 1640 ret = d_func()->socketEngine->option(QAbstractSocketEngine::LowDelayOption); 1641 break; 1642 1643 case KeepAliveOption: 1644 ret = d_func()->socketEngine->option(QAbstractSocketEngine::KeepAliveOption); 1645 break; 1646 } 1647 if (ret == -1) 1648 return QVariant(); 1649 else 1650 return QVariant(ret); 1651 } 1652 1653 1549 1654 /* 1550 1655 Returns the difference between msecs and elapsed. If msecs is -1, … … 1657 1762 1658 1763 /*! 1659 This function blocks until data is available for reading and the1764 This function blocks until new data is available for reading and the 1660 1765 \l{QIODevice::}{readyRead()} signal has been emitted. The function 1661 1766 will timeout after \a msecs milliseconds; the default timeout is … … 1663 1768 1664 1769 The function returns true if the readyRead() signal is emitted and 1665 there is data available for reading; otherwise it returns false1770 there is new data available for reading; otherwise it returns false 1666 1771 (if an error occurred or the operation timed out). 1667 1772 … … 1865 1970 1866 1971 /*! 1867 Aborts the current connection and resets the socket. Unlike 1868 disconnectFromHost(), this function immediately closes the socket, discarding1869 any pending data in thewrite buffer.1972 Aborts the current connection and resets the socket. Unlike disconnectFromHost(), 1973 this function immediately closes the socket, discarding any pending data in the 1974 write buffer. 1870 1975 1871 1976 \sa disconnectFromHost(), close() … … 2164 2269 2165 2270 /*! 2166 Disconnects the socket's connection with the host. 2271 Closes the I/O device for the socket, disconnects the socket's connection with the 2272 host, closes the socket, and resets the name, address, port number and underlying 2273 socket descriptor. 2274 2275 See QIODevice::close() for a description of the actions that occur when an I/O 2276 device is closed. 2167 2277 2168 2278 \sa abort() … … 2256 2366 2257 2367 // Wait for pending data to be written. 2258 if (d->socketEngine && d->socketEngine->isValid() && d->writeBuffer.size() > 0) { 2368 if (d->socketEngine && d->socketEngine->isValid() && (d->writeBuffer.size() > 0 2369 || d->socketEngine->bytesToWrite() > 0)) { 2370 // hack: when we are waiting for the socket engine to write bytes (only 2371 // possible when using Socks5 or HTTP socket engine), then close 2372 // anyway after 2 seconds. This is to prevent a timeout on Mac, where we 2373 // sometimes just did not get the write notifier from the underlying 2374 // CFSocket and no progress was made. 2375 if (d->writeBuffer.size() == 0 && d->socketEngine->bytesToWrite() > 0) { 2376 if (!d->disconnectTimer) { 2377 d->disconnectTimer = new QTimer(this); 2378 connect(d->disconnectTimer, SIGNAL(timeout()), this, 2379 SLOT(_q_forceDisconnect()), Qt::DirectConnection); 2380 } 2381 if (!d->disconnectTimer->isActive()) 2382 d->disconnectTimer->start(2000); 2383 } 2259 2384 d->socketEngine->setWriteNotificationEnabled(true); 2260 2385 … … 2457 2582 2458 2583 #ifdef QT3_SUPPORT 2459 /*! \enum QAbstractSocket::Error 2584 /*! 2585 \enum QAbstractSocket::Error 2460 2586 \compat 2461 2587 … … 2589 2715 break; 2590 2716 default: 2591 debug << "QAbstractSocket::SocketError(" << int(error) << ")";2717 debug << "QAbstractSocket::SocketError(" << int(error) << ')'; 2592 2718 break; 2593 2719 } … … 2620 2746 break; 2621 2747 default: 2622 debug << "QAbstractSocket::SocketState(" << int(state) << ")";2748 debug << "QAbstractSocket::SocketState(" << int(state) << ')'; 2623 2749 break; 2624 2750 }
Note:
See TracChangeset
for help on using the changeset viewer.