405 #include "private/qhostinfo_p.h" 406 #include "private/qnetworksession_p.h" 417 #ifndef QT_NO_OPENSSL 418 #include <QtNetwork/qsslsocket.h> 421 #include <private/qthread_p.h> 423 #ifdef QABSTRACTSOCKET_DEBUG 429 #define Q_CHECK_SOCKETENGINE(returnValue) do { \ 430 if (!d->socketEngine) { \ 431 return returnValue; \ 434 #ifndef QABSTRACTSOCKET_BUFFERSIZE 435 #define QABSTRACTSOCKET_BUFFERSIZE 32768 437 #define QT_CONNECT_TIMEOUT 30000 438 #define QT_TRANSFER_TIMEOUT 120000 442 #if defined QABSTRACTSOCKET_DEBUG 452 static QByteArray qt_prettyDebug(
const char *
data,
int len,
int maxLength)
454 if (!data)
return "(null)";
456 for (
int i = 0; i < len; ++i) {
458 if (isprint(
int(
uchar(c)))) {
461 case '\n': out +=
"\\n";
break;
462 case '\r': out +=
"\\r";
break;
463 case '\t': out +=
"\\t";
break;
501 : readSocketNotifierCalled(false),
502 readSocketNotifierState(false),
503 readSocketNotifierStateSet(false),
504 emittedReadyRead(false),
505 emittedBytesWritten(false),
513 cachedSocketDescriptor(-1),
514 readBufferMaxSize(0),
518 blockingTimeout(30000),
521 connectTimeElapsed(0),
551 #if defined (QABSTRACTSOCKET_DEBUG) 552 qDebug(
"QAbstractSocketPrivate::resetSocketLayer()");
579 #ifdef QT_NO_NETWORKPROXY 585 #if defined (QABSTRACTSOCKET_DEBUG) 593 else protocolStr =
QLatin1String(
"UnknownNetworkLayerProtocol");
603 #ifndef QT_NO_BEARERMANAGEMENT 607 #ifndef QT_NO_NETWORKPROXY 612 #if defined (QABSTRACTSOCKET_DEBUG) 613 qDebug(
"QAbstractSocketPrivate::initSocketLayer(%s, %s) failed (%s)",
625 #if defined (QABSTRACTSOCKET_DEBUG) 626 qDebug(
"QAbstractSocketPrivate::initSocketLayer(%s, %s) success",
644 #if defined (QABSTRACTSOCKET_DEBUG) 645 qDebug(
"QAbstractSocketPrivate::canReadNotification()");
667 #if defined (QABSTRACTSOCKET_DEBUG) 668 qDebug(
"QAbstractSocketPrivate::canReadNotification() buffer is full");
677 #if defined (QABSTRACTSOCKET_DEBUG) 678 qDebug(
"QAbstractSocketPrivate::canReadNotification() disconnecting socket");
680 q->disconnectFromHost();
692 bool hasData = newBytes > 0
693 #ifndef QT_NO_UDPSOCKET 708 #if defined (QABSTRACTSOCKET_DEBUG) 709 qDebug(
"QAbstractSocketPrivate::canReadNotification() socket is closing - returning");
737 #if defined (Q_OS_WIN) 742 #if defined (QABSTRACTSOCKET_DEBUG) 743 qDebug(
"QAbstractSocketPrivate::canWriteNotification() flushing");
749 #if defined (Q_OS_WIN) 774 #if defined (QABSTRACTSOCKET_DEBUG) 775 qDebug(
"QAbstractSocketPrivate::connectionNotification() testing connection");
799 #if defined (QABSTRACTSOCKET_DEBUG) 800 qDebug(
"QAbstractSocketPrivate::flush() nothing to do: valid ? %s, writeBuffer.isEmpty() ? %s",
806 q->disconnectFromHost();
819 #if defined (QABSTRACTSOCKET_DEBUG) 828 #if defined (QABSTRACTSOCKET_DEBUG) 829 qDebug(
"QAbstractSocketPrivate::flush() %lld bytes written to the network",
840 emit q->bytesWritten(written);
848 q->disconnectFromHost();
853 #ifndef QT_NO_NETWORKPROXY 911 #if defined(QABSTRACTSOCKET_DEBUG) 912 qDebug(
"QAbstractSocketPrivate::startConnectingByName(host == %s)",
qPrintable(host));
958 qWarning(
"QAbstractSocketPrivate::_q_startConnecting() received hostInfo for wrong lookup ID %d expected %d", hostInfo.
lookupId(),
hostLookupId);
963 #if defined(QABSTRACTSOCKET_DEBUG) 965 for (
int i = 0; i < addresses.count(); ++i) {
967 s += addresses.
at(i).toString();
978 if (addresses.isEmpty()) {
979 #if defined(QABSTRACTSOCKET_DEBUG) 980 qDebug(
"QAbstractSocketPrivate::_q_startConnecting(), host not found");
1024 #if defined(QABSTRACTSOCKET_DEBUG) 1025 qDebug(
"QAbstractSocketPrivate::_q_connectToNextAddress(), all addresses failed.");
1053 #if defined(QABSTRACTSOCKET_DEBUG) 1054 qDebug(
"QAbstractSocketPrivate::_q_connectToNextAddress(), connecting to %s:%i, %d left to try",
1058 #if defined(QT_NO_IPV6) 1062 #if defined(QABSTRACTSOCKET_DEBUG) 1063 qDebug(
"QAbstractSocketPrivate::_q_connectToNextAddress(), skipping IPv6 entry");
1071 #if defined(QABSTRACTSOCKET_DEBUG) 1072 qDebug(
"QAbstractSocketPrivate::_q_connectToNextAddress(), failed to initialize sock layer");
1092 #if defined(QABSTRACTSOCKET_DEBUG) 1093 qDebug(
"QAbstractSocketPrivate::_q_connectToNextAddress(), connection failed (%s)",
1138 q_func()->disconnectFromHost();
1154 #if defined(QABSTRACTSOCKET_DEBUG) 1155 qDebug(
"QAbstractSocketPrivate::_q_testConnection() connection failed," 1156 " checking for alternative addresses");
1174 #if defined(QABSTRACTSOCKET_DEBUG) 1175 qDebug(
"QAbstractSocketPrivate::_q_abortConnectionAttempt() (timed out)");
1198 q->disconnectFromHost();
1215 if (bytesToRead == 0) {
1228 #if defined(QABSTRACTSOCKET_DEBUG) 1229 qDebug(
"QAbstractSocketPrivate::readFromSocket() about to read %d bytes",
1236 if (readBytes == -2) {
1242 #if defined(QABSTRACTSOCKET_DEBUG) 1243 qDebug(
"QAbstractSocketPrivate::readFromSocket() got %d bytes, buffer size = %d",
1251 #if defined(QABSTRACTSOCKET_DEBUG) 1252 qDebug(
"QAbstractSocketPrivate::readFromSocket() read failed: %s",
1253 q->errorString().toLatin1().constData());
1286 emit q->connected();
1288 #if defined(QABSTRACTSOCKET_DEBUG) 1289 qDebug(
"QAbstractSocketPrivate::fetchConnectionParameters() connection to %s:%i established",
1320 return socket->d_func()->socketEngine;
1337 #if defined(QABSTRACTSOCKET_DEBUG) 1338 qDebug(
"QAbstractSocket::QAbstractSocket(%sSocket, QAbstractSocketPrivate == %p, parent == %p)",
1340 ?
"Udp" :
"Unknown", &dd, parent);
1355 #if defined(QABSTRACTSOCKET_DEBUG) 1356 qDebug(
"QAbstractSocket::QAbstractSocket(%p)", parent);
1367 #if defined(QABSTRACTSOCKET_DEBUG) 1368 qDebug(
"QAbstractSocket::~QAbstractSocket()");
1385 return d_func()->socketEngine ? d_func()->socketEngine->isValid() :
isOpen();
1416 Q_ARG(OpenMode, openMode));
1434 #if defined(QABSTRACTSOCKET_DEBUG) 1435 qDebug(
"QAbstractSocket::connectToHost(\"%s\", %i, %i)...",
qPrintable(hostName), port,
1441 qWarning(
"QAbstractSocket::connectToHost() called when already looking up or connecting/connected to \"%s\"",
qPrintable(hostName));
1445 d->hostName = hostName;
1448 d->readBuffer.clear();
1449 d->writeBuffer.clear();
1450 d->abortCalled =
false;
1451 d->closeCalled =
false;
1452 d->pendingClose =
false;
1455 d->localAddress.clear();
1456 d->peerAddress.clear();
1457 d->peerName = hostName;
1458 if (
d->hostLookupId != -1) {
1460 d->hostLookupId = -1;
1463 #ifndef QT_NO_NETWORKPROXY 1465 d->resolveProxy(hostName, port);
1476 d->isBuffered =
false;
1477 else if (!d_func()->isBuffered)
1488 d->_q_startConnecting(info);
1489 #ifndef QT_NO_NETWORKPROXY 1492 d->startConnectingByName(hostName);
1496 if (
d->threadData->eventDispatcher) {
1499 bool immediateResultValid =
false;
1503 &immediateResultValid,
1505 if (immediateResultValid) {
1506 d->hostLookupId = -1;
1507 d->_q_startConnecting(hostInfo);
1512 #if defined(QABSTRACTSOCKET_DEBUG) 1516 ?
" (connection in progress)" :
"");
1530 #if defined(QABSTRACTSOCKET_DEBUG) 1531 qDebug(
"QAbstractSocket::connectToHost([%s], %i, %i)...",
1547 #if defined(QABSTRACTSOCKET_DEBUG) 1548 qDebug(
"QAbstractSocket::bytesToWrite() == %i",
d->writeBuffer.size());
1550 return (
qint64)
d->writeBuffer.size();
1563 available += (
qint64)
d->readBuffer.size();
1565 if (!
d->isBuffered &&
d->socketEngine &&
d->socketEngine->isValid())
1566 available +=
d->socketEngine->bytesAvailable();
1568 #if defined(QABSTRACTSOCKET_DEBUG) 1569 qDebug(
"QAbstractSocket::bytesAvailable() == %llu", available);
1583 return d->localPort;
1599 return d->localAddress;
1623 return d->peerAddress;
1635 return d->peerName.isEmpty() ?
d->hostName :
d->peerName;
1646 bool hasLine = d_func()->readBuffer.canReadLine();
1647 #if defined (QABSTRACTSOCKET_DEBUG) 1648 qDebug(
"QAbstractSocket::canReadLine() == %s, buffer size = %d, size = %d", hasLine ?
"true" :
"false",
1649 d_func()->readBuffer.size(), d_func()->buffer.size());
1669 return d->cachedSocketDescriptor;
1688 #ifndef QT_NO_OPENSSL 1689 if (
QSslSocket *socket = qobject_cast<QSslSocket *>(
this))
1690 return socket->setSocketDescriptor(socketDescriptor, socketState, openMode);
1693 d->resetSocketLayer();
1695 if (!
d->socketEngine) {
1700 #ifndef QT_NO_BEARERMANAGEMENT 1702 d->socketEngine->setProperty(
"_q_networksession",
property(
"_q_networksession"));
1704 bool result =
d->socketEngine->initialize(socketDescriptor, socketState);
1706 d->socketError =
d->socketEngine->error();
1711 if (
d->threadData->eventDispatcher)
1712 d->socketEngine->setReceiver(
d);
1716 if (
d->state != socketState) {
1717 d->state = socketState;
1721 d->pendingClose =
false;
1722 d->socketEngine->setReadNotificationEnabled(
true);
1723 d->localPort =
d->socketEngine->localPort();
1724 d->peerPort =
d->socketEngine->peerPort();
1725 d->localAddress =
d->socketEngine->localAddress();
1726 d->peerAddress =
d->socketEngine->peerAddress();
1743 #ifndef QT_NO_OPENSSL 1744 if (
QSslSocket *sslSocket = qobject_cast<QSslSocket*>(
this)) {
1745 sslSocket->setSocketOption(option, value);
1750 if (!d_func()->socketEngine)
1783 #ifndef QT_NO_OPENSSL 1784 if (
QSslSocket *sslSocket = qobject_cast<QSslSocket*>(
this)) {
1785 return sslSocket->socketOption(option);
1789 if (!d_func()->socketEngine)
1825 int timeout = msecs -
elapsed;
1826 return timeout < 0 ? 0 : timeout;
1854 #if defined (QABSTRACTSOCKET_DEBUG) 1855 qDebug(
"QAbstractSocket::waitForConnected(%i)", msecs);
1859 #if defined (QABSTRACTSOCKET_DEBUG) 1860 qDebug(
"QAbstractSocket::waitForConnected(%i) already connected", msecs);
1865 #ifndef QT_NO_OPENSSL 1868 if (
QSslSocket *socket = qobject_cast<QSslSocket *>(
this))
1869 return socket->waitForConnected(msecs);
1872 bool wasPendingClose =
d->pendingClose;
1873 d->pendingClose =
false;
1878 #if defined (QABSTRACTSOCKET_DEBUG) 1879 qDebug(
"QAbstractSocket::waitForConnected(%i) doing host name lookup", msecs);
1882 d->hostLookupId = -1;
1883 #ifndef QT_NO_BEARERMANAGEMENT 1896 bool timedOut =
true;
1897 #if defined (QABSTRACTSOCKET_DEBUG) 1904 #if defined (QABSTRACTSOCKET_DEBUG) 1905 qDebug(
"QAbstractSocket::waitForConnected(%i) waiting %.2f secs for connection attempt #%i",
1906 msecs, timeout / 1000.0, attempt++);
1910 if (
d->socketEngine &&
d->socketEngine->waitForWrite(timeout, &timedOut) && !timedOut) {
1911 d->_q_testConnection();
1913 d->_q_connectToNextAddress();
1921 d->resetSocketLayer();
1925 #if defined (QABSTRACTSOCKET_DEBUG) 1926 qDebug(
"QAbstractSocket::waitForConnected(%i) == %s", msecs,
1931 if (wasPendingClose)
1951 #if defined (QABSTRACTSOCKET_DEBUG) 1952 qDebug(
"QAbstractSocket::waitForReadyRead(%i)", msecs);
1974 bool readyToRead =
false;
1975 bool readyToWrite =
false;
1976 if (!
d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite,
true, !
d->writeBuffer.isEmpty(),
1978 d->socketError =
d->socketEngine->error();
1980 #if defined (QABSTRACTSOCKET_DEBUG) 1981 qDebug(
"QAbstractSocket::waitForReadyRead(%i) failed (%i, %s)",
1982 msecs,
d->socketError,
errorString().toLatin1().constData());
1991 if (
d->canReadNotification())
1996 d->canWriteNotification();
2009 #if defined (QABSTRACTSOCKET_DEBUG) 2010 qDebug(
"QAbstractSocket::waitForBytesWritten(%i)", msecs);
2015 qWarning(
"QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState");
2019 if (
d->writeBuffer.isEmpty())
2032 bool readyToRead =
false;
2033 bool readyToWrite =
false;
2034 if (!
d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite,
true, !
d->writeBuffer.isEmpty(),
2036 d->socketError =
d->socketEngine->error();
2038 #if defined (QABSTRACTSOCKET_DEBUG) 2039 qDebug(
"QAbstractSocket::waitForBytesWritten(%i) failed (%i, %s)",
2040 msecs,
d->socketError,
errorString().toLatin1().constData());
2049 #if defined (QABSTRACTSOCKET_DEBUG) 2050 qDebug(
"QAbstractSocket::waitForBytesWritten calls canReadNotification");
2052 if(!
d->canReadNotification())
2058 if (
d->canWriteNotification()) {
2059 #if defined (QABSTRACTSOCKET_DEBUG) 2060 qDebug(
"QAbstractSocket::waitForBytesWritten returns true");
2091 #ifndef QT_NO_OPENSSL 2094 if (
QSslSocket *socket = qobject_cast<QSslSocket *>(
this))
2095 return socket->waitForDisconnected(msecs);
2100 qWarning(
"QAbstractSocket::waitForDisconnected() is not allowed in UnconnectedState");
2116 bool readyToRead =
false;
2117 bool readyToWrite =
false;
2118 if (!
d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite,
state() ==
ConnectedState,
2119 !
d->writeBuffer.isEmpty(),
2121 d->socketError =
d->socketEngine->error();
2123 #if defined (QABSTRACTSOCKET_DEBUG) 2124 qDebug(
"QAbstractSocket::waitForReadyRead(%i) failed (%i, %s)",
2125 msecs,
d->socketError,
errorString().toLatin1().constData());
2134 d->canReadNotification();
2136 d->canWriteNotification();
2154 #if defined (QABSTRACTSOCKET_DEBUG) 2155 qDebug(
"QAbstractSocket::abort()");
2159 #ifndef QT_NO_OPENSSL 2160 if (
QSslSocket *socket = qobject_cast<QSslSocket *>(
this)) {
2165 if (
d->connectTimer) {
2166 d->connectTimer->stop();
2167 delete d->connectTimer;
2168 d->connectTimer = 0;
2171 d->writeBuffer.clear();
2172 d->abortCalled =
true;
2221 #ifndef QT_NO_OPENSSL 2224 if (
QSslSocket *socket = qobject_cast<QSslSocket *>(
this))
2225 return socket->flush();
2238 if (
d->isBuffered &&
d->readBuffer.isEmpty())
2244 if (maxSize == 1 && !
d->readBuffer.isEmpty()) {
2245 *data =
d->readBuffer.getChar();
2246 #if defined (QABSTRACTSOCKET_DEBUG) 2247 qDebug(
"QAbstractSocket::readData(%p '%c (0x%.2x)', 1) == 1 [char buffer]",
2248 data, isprint(
int(
uchar(*data))) ? *data :
'?', *data);
2250 if (
d->readBuffer.isEmpty() &&
d->socketEngine &&
d->socketEngine->isValid())
2251 d->socketEngine->setReadNotificationEnabled(
true);
2259 &&
d->readBuffer.size() < maxSize
2260 &&
d->readBufferMaxSize > 0
2261 && maxSize <
d->readBufferMaxSize
2263 &&
d->socketEngine->isValid()) {
2267 qint64 bytesToRead =
d->socketEngine->bytesAvailable();
2268 if (bytesToRead > 0) {
2269 char *
ptr =
d->readBuffer.reserve(bytesToRead);
2270 qint64 readBytes =
d->socketEngine->read(ptr, bytesToRead);
2271 if (readBytes == -2) {
2273 d->readBuffer.chop(bytesToRead);
2275 d->readBuffer.chop(
int(bytesToRead - (readBytes < 0 ?
qint64(0) : readBytes)));
2283 while (readSoFar < bytesToRead) {
2284 const char *
ptr =
d->readBuffer.readPointer();
2285 int bytesToReadFromThisBlock =
qMin(
int(bytesToRead - readSoFar),
2286 d->readBuffer.nextDataBlockSize());
2287 memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock);
2288 readSoFar += bytesToReadFromThisBlock;
2289 d->readBuffer.free(bytesToReadFromThisBlock);
2292 if (
d->socketEngine && !
d->socketEngine->isReadNotificationEnabled() &&
d->socketEngine->isValid())
2293 d->socketEngine->setReadNotificationEnabled(
true);
2295 if (readSoFar > 0) {
2298 #if defined (QABSTRACTSOCKET_DEBUG) 2299 qDebug(
"QAbstractSocket::readData(%p '%c (0x%.2x)', %lli) == %lli [buffer]",
2300 data, isprint(
int(
uchar(*data))) ? *data :
'?', *data, maxSize, readSoFar);
2303 if (
d->readBuffer.isEmpty() &&
d->socketEngine)
2304 d->socketEngine->setReadNotificationEnabled(
true);
2310 if (!
d->isBuffered) {
2311 if (!
d->socketEngine)
2313 if (!
d->socketEngine->isValid())
2317 qint64 readBytes =
d->socketEngine->read(data, maxSize);
2318 if (readBytes == -2) {
2321 }
else if (readBytes < 0) {
2322 d->socketError =
d->socketEngine->error();
2324 d->resetSocketLayer();
2326 }
else if (!
d->socketEngine->isReadNotificationEnabled()) {
2328 d->socketEngine->setReadNotificationEnabled(
true);
2331 #if defined (QABSTRACTSOCKET_DEBUG) 2332 qDebug(
"QAbstractSocket::readData(%p \"%s\", %lli) == %lld [engine]",
2333 data, qt_prettyDebug(data, 32, readBytes).
data(), maxSize,
2340 #if defined (QABSTRACTSOCKET_DEBUG) 2341 qDebug(
"QAbstractSocket::readData(%p \"%s\", %lli) == %lld [unreachable]",
2342 data, qt_prettyDebug(data, qMin<qint64>(32, readSoFar), readSoFar).
data(),
2343 maxSize, readSoFar);
2366 if (!
d->isBuffered &&
d->socketType ==
TcpSocket &&
d->writeBuffer.isEmpty()) {
2368 qint64 written =
d->socketEngine->write(data, size);
2370 d->socketError =
d->socketEngine->error();
2373 }
else if (written < size) {
2375 char *
ptr =
d->writeBuffer.reserve(size - written);
2376 memcpy(ptr, data + written, size - written);
2377 if (
d->socketEngine)
2378 d->socketEngine->setWriteNotificationEnabled(
true);
2381 }
else if (!
d->isBuffered &&
d->socketType !=
TcpSocket) {
2383 qint64 written =
d->socketEngine->write(data, size);
2385 d->socketError =
d->socketEngine->error();
2387 }
else if (!
d->writeBuffer.isEmpty()) {
2388 d->socketEngine->setWriteNotificationEnabled(
true);
2391 #if defined (QABSTRACTSOCKET_DEBUG) 2392 qDebug(
"QAbstractSocket::writeData(%p \"%s\", %lli) == %lli", data,
2393 qt_prettyDebug(data,
qMin((
int)size, 32), size).
data(),
2407 char *
ptr =
d->writeBuffer.reserve(size);
2411 memcpy(ptr, data, size);
2415 if (
d->socketEngine && !
d->writeBuffer.isEmpty())
2416 d->socketEngine->setWriteNotificationEnabled(
true);
2418 #if defined (QABSTRACTSOCKET_DEBUG) 2419 qDebug(
"QAbstractSocket::writeData(%p \"%s\", %lli) == %lli", data,
2420 qt_prettyDebug(data,
qMin((
int)size, 32), size).
data(),
2447 d->localPort = port;
2472 d->localAddress = address;
2516 d->peerAddress = address;
2553 #if defined(QABSTRACTSOCKET_DEBUG) 2554 qDebug(
"QAbstractSocket::close()");
2558 d->closeCalled =
true;
2564 d->localAddress.clear();
2565 d->peerAddress.clear();
2566 d->peerName.clear();
2567 d->cachedSocketDescriptor = -1;
2595 #if defined(QABSTRACTSOCKET_DEBUG) 2596 qDebug(
"QAbstractSocket::disconnectFromHost()");
2600 #if defined(QABSTRACTSOCKET_DEBUG) 2601 qDebug(
"QAbstractSocket::disconnectFromHost() was called on an unconnected socket");
2607 #if defined(QABSTRACTSOCKET_DEBUG) 2608 qDebug(
"QAbstractSocket::disconnectFromHost() but we're still connecting");
2610 d->pendingClose =
true;
2615 emit connectionClosed();
2619 if (
d->socketEngine)
2620 d->socketEngine->setReadNotificationEnabled(
false);
2622 if (
d->abortCalled) {
2623 #if defined(QABSTRACTSOCKET_DEBUG) 2624 qDebug(
"QAbstractSocket::disconnectFromHost() aborting immediately");
2628 d->hostLookupId = -1;
2634 #if defined(QABSTRACTSOCKET_DEBUG) 2635 qDebug(
"QAbstractSocket::disconnectFromHost() emits stateChanged()(ClosingState)");
2639 #if defined(QABSTRACTSOCKET_DEBUG) 2640 qDebug(
"QAbstractSocket::disconnectFromHost() return from delayed close");
2645 if (
d->socketEngine &&
d->socketEngine->isValid() && (
d->writeBuffer.size() > 0
2646 ||
d->socketEngine->bytesToWrite() > 0)) {
2652 if (
d->writeBuffer.size() == 0 &&
d->socketEngine->bytesToWrite() > 0) {
2653 if (!
d->disconnectTimer) {
2654 d->disconnectTimer =
new QTimer(
this);
2658 if (!
d->disconnectTimer->isActive())
2659 d->disconnectTimer->start(2000);
2661 d->socketEngine->setWriteNotificationEnabled(
true);
2663 #if defined(QABSTRACTSOCKET_DEBUG) 2664 qDebug(
"QAbstractSocket::disconnectFromHost() delaying disconnect");
2668 #if defined(QABSTRACTSOCKET_DEBUG) 2669 qDebug(
"QAbstractSocket::disconnectFromHost() disconnecting immediately");
2675 d->resetSocketLayer();
2681 emit delayedCloseFinished();
2689 d->localAddress.clear();
2690 d->peerAddress.clear();
2692 #if defined(QABSTRACTSOCKET_DEBUG) 2693 qDebug(
"QAbstractSocket::disconnectFromHost() disconnected!");
2696 if (
d->closeCalled) {
2697 #if defined(QABSTRACTSOCKET_DEBUG) 2698 qDebug(
"QAbstractSocket::disconnectFromHost() closed!");
2700 d->readBuffer.clear();
2701 d->writeBuffer.clear();
2718 return d_func()->readBufferMaxSize;
2747 #ifndef QT_NO_OPENSSL 2750 if (
QSslSocket *socket = qobject_cast<QSslSocket *>(
this)) {
2751 socket->setReadBufferSize(size);
2756 if (
d->readBufferMaxSize == size)
2758 d->readBufferMaxSize =
size;
2759 if (!
d->readSocketNotifierCalled &&
d->socketEngine) {
2764 d->socketEngine->setReadNotificationEnabled(
true);
2775 return d_func()->state;
2785 d_func()->state =
state;
2795 return d_func()->socketType;
2805 return d_func()->socketError;
2815 d_func()->socketError = socketError;
2818 #ifndef QT_NO_NETWORKPROXY 2844 d->proxy = networkProxy;
2864 #endif // QT_NO_NETWORKPROXY 2954 #endif // QT3_SUPPORT 2956 #ifndef QT_NO_DEBUG_STREAM 2961 debug <<
"QAbstractSocket::ConnectionRefusedError";
2964 debug <<
"QAbstractSocket::RemoteHostClosedError";
2967 debug <<
"QAbstractSocket::HostNotFoundError";
2970 debug <<
"QAbstractSocket::SocketAccessError";
2973 debug <<
"QAbstractSocket::SocketResourceError";
2976 debug <<
"QAbstractSocket::SocketTimeoutError";
2979 debug <<
"QAbstractSocket::DatagramTooLargeError";
2982 debug <<
"QAbstractSocket::NetworkError";
2985 debug <<
"QAbstractSocket::AddressInUseError";
2988 debug <<
"QAbstractSocket::SocketAddressNotAvailableError";
2991 debug <<
"QAbstractSocket::UnsupportedSocketOperationError";
2994 debug <<
"QAbstractSocket::UnfinishedSocketOperationError";
2997 debug <<
"QAbstractSocket::ProxyAuthenticationRequiredError";
3000 debug <<
"QAbstractSocket::UnknownSocketError";
3003 debug <<
"QAbstractSocket::ProxyConnectionRefusedError";
3006 debug <<
"QAbstractSocket::ProxyConnectionClosedError";
3009 debug <<
"QAbstractSocket::ProxyConnectionTimeoutError";
3012 debug <<
"QAbstractSocket::ProxyNotFoundError";
3015 debug <<
"QAbstractSocket::ProxyProtocolError";
3018 debug <<
"QAbstractSocket::SocketError(" << int(error) <<
')';
3028 debug <<
"QAbstractSocket::UnconnectedState";
3031 debug <<
"QAbstractSocket::HostLookupState";
3034 debug <<
"QAbstractSocket::ConnectingState";
3037 debug <<
"QAbstractSocket::ConnectedState";
3040 debug <<
"QAbstractSocket::BoundState";
3043 debug <<
"QAbstractSocket::ListeningState";
3046 debug <<
"QAbstractSocket::ClosingState";
3049 debug <<
"QAbstractSocket::SocketState(" << int(state) <<
')';
3058 #include "moc_qabstractsocket.cpp" The QVariant class acts like a union for the most common Qt data types.
QNetworkProxy::ProxyType type() const
Returns the proxy type for this instance.
virtual bool isReadNotificationEnabled() const =0
The QDebug class provides an output stream for debugging information.
void _q_forceDisconnect()
virtual void setExceptionNotificationEnabled(bool enable)=0
int lookupId() const
Returns the ID of this lookup.
virtual qint64 bytesToWrite() const =0
virtual qint64 size() const
For open random-access devices, this function returns the size of the device.
QString peerName() const
Returns the name of the peer as specified by connectToHost(), or an empty QString if connectToHost() ...
int cachedSocketDescriptor
int socketDescriptor() const
Returns the native socket descriptor of the QAbstractSocket object if this is available; otherwise re...
QString & sprintf(const char *format,...)
Safely builds a formatted string from the format string cformat and an arbitrary list of arguments...
static mach_timebase_info_data_t info
void setSocketError(SocketError socketError)
Sets the type of error that last occurred to socketError.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
static double elapsed(qint64 after, qint64 before)
#define QT_END_NAMESPACE
This macro expands to.
SocketType
This enum describes the transport layer protocol.
bool waitForBytesWritten(int msecs=30000)
Reimplemented Function
bool waitForReadyRead(int msecs=30000)
This function blocks until new data is available for reading and the QIODevice::readyRead() signal ha...
virtual int socketDescriptor() const =0
const QChar at(int i) const
Returns the character at the given index position in the string.
QHostAddress localAddress() const
Returns the host address of the local socket if available; otherwise returns QHostAddress::Null.
const char * readPointer() const
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
bool waitForDisconnected(int msecs=30000)
Waits until the socket has disconnected, up to msecs milliseconds.
qint64 bytesAvailable() const
Returns the number of incoming bytes that are waiting to be read.
bool flush()
This function writes as much as possible from the internal write buffer to the underlying network soc...
QList< QHostAddress > addresses
virtual bool connectToHostByName(const QString &name, quint16 port)=0
Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, QAbstractSocket::SocketError error)
qint64 readData(char *data, qint64 maxlen)
Reimplemented Function
static QAbstractSocketEngine * createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &, QObject *parent)
void readChannelFinished()
This signal is emitted when the input (reading) stream is closed in this device.
#define QABSTRACTSOCKET_BUFFERSIZE
void connectToHost(const QString &hostName, quint16 port, OpenMode mode=ReadWrite)
Attempts to make a connection to hostName on the given port.
int nextDataBlockSize() const
void setPeerName(const QString &name)
Sets the host name of the remote peer to name.
The QByteArray class provides an array of bytes.
QNetworkProxy proxy() const
Returns the network proxy for this socket.
SocketOption
This enum represents the options that can be set on a socket.
QString errorString() const
Returns a human-readable description of the last device error that occurred.
static void abortHostLookup(int lookupId)
Aborts the host lookup with the ID id, as returned by lookupHost().
The QSslSocket class provides an SSL encrypted socket for both clients and servers.
bool readFromSocket()
Reads data from the socket layer into the read buffer.
bool atEnd() const
Returns true if no more data is currently available for reading; otherwise returns false...
static void resumeSocketNotifiers(QAbstractSocket *)
bool setProperty(const char *name, const QVariant &value)
Sets the value of the object's name property to value.
quint16 peerPort() const
Returns the port of the connected peer if the socket is in ConnectedState; otherwise returns 0...
#define Q_ARG(type, data)
#define QT_END_INCLUDE_NAMESPACE
This macro is equivalent to QT_BEGIN_NAMESPACE.
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
void bytesWritten(qint64 bytes)
This signal is emitted every time a payload of data has been written to the device.
void connectToHostImplementation(const QString &hostName, quint16 port, OpenMode mode=ReadWrite)
Contains the implementation of connectToHost().
void resetSocketLayer()
Resets the socket layer, clears the read and write buffers and deletes any socket notifiers...
NetworkLayerProtocol
This enum describes the network layer protocol values used in Qt.
void abort()
Aborts the current connection and resets the socket.
The QString class provides a Unicode character string.
bool readSocketNotifierStateSet
virtual bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol=QAbstractSocket::IPv4Protocol)=0
void setSocketState(SocketState state)
Sets the state of the socket to state.
qint64 readLineData(char *data, qint64 maxlen)
Reimplemented Function
void disconnected()
This signal is emitted when the socket has been disconnected.
The QObject class is the base class of all Qt objects.
The QElapsedTimer class provides a fast way to calculate elapsed times.
SocketType socketType() const
Returns the socket type (TCP, UDP, or other).
static QList< QNetworkProxy > proxyForQuery(const QNetworkProxyQuery &query)
This function takes the query request, query, examines the details of the type of socket or request a...
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i.e.
qint64 readBufferSize() const
Returns the size of the internal read buffer.
QList< QHostAddress > addresses() const
Returns the list of IP addresses associated with hostName().
qint64 elapsed() const
Returns the number of milliseconds since this QElapsedTimer was last started.
void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value)
Sets the given option to the value described by value.
virtual qint64 readLineData(char *data, qint64 maxlen)
Reads up to maxSize characters into data and returns the number of characters read.
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
SocketState
This enum describes the different states in which a socket can be.
Q_CORE_EXPORT void qDebug(const char *,...)
void startConnectingByName(const QString &host)
Starts the connection to host, like _q_startConnecting below, but without hostname resolution...
QAbstractSocket::SocketState state
virtual ~QAbstractSocketPrivate()
Destructs the QAbstractSocket.
The QNetworkProxy class provides a network layer proxy.
char * reserve(int bytes)
#define QT_BEGIN_NAMESPACE
This macro expands to.
virtual bool isWriteNotificationEnabled() const =0
bool isOpen() const
Returns true if the device is open; otherwise returns false.
void _q_startConnecting(const QHostInfo &hostInfo)
Slot connected to QHostInfo::lookupHost() in connectToHost().
QHostAddress peerAddress() const
Capabilities capabilities() const
Returns the capabilities of this proxy server.
virtual void setWriteNotificationEnabled(bool enable)=0
T takeFirst()
Removes the first item in the list and returns it.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
void setAddresses(const QList< QHostAddress > &addresses)
Sets the list of addresses in this QHostInfo to addresses.
bool setSocketDescriptor(int socketDescriptor, SocketState state=ConnectedState, OpenMode openMode=ReadWrite)
Initializes QAbstractSocket with the native socket descriptor socketDescriptor.
SocketError
This enum describes the socket errors that can occur.
bool isSequential() const
Reimplemented Function
bool waitForConnected(int msecs=30000)
Waits until the socket is connected, up to msecs milliseconds.
Q_CORE_EXPORT void qWarning(const char *,...)
QAbstractSocket::SocketError socketError
static const char * data(const QByteArray &arr)
QHostAddress localAddress() const
virtual ~QAbstractSocket()
Destroys the socket.
static QAbstractSocketEngine * getSocketEngine(QAbstractSocket *)
The QHostInfo class provides static functions for host name lookups.
QString toString() const
Returns the address as a string.
static bool isProxyError(QAbstractSocket::SocketError error)
QVariant socketOption(QAbstractSocket::SocketOption option)
Returns the value of the option option.
const T * ptr(const T &t)
void setReadBufferSize(qint64 size)
Sets the size of QAbstractSocket's internal read buffer to be size bytes.
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
void clear()
Removes all items from the list.
void _q_abortConnectionAttempt()
This function is called after a certain number of seconds has passed while waiting for a connection...
static QHostInfo fromName(const QString &hostName, QSharedPointer< QNetworkSession > networkSession)
void close()
Closes the I/O device for the socket, disconnects the socket's connection with the host...
void setReceiver(QAbstractSocketEngineReceiver *receiver)
virtual qint64 write(const char *data, qint64 len)=0
#define QT_CONNECT_TIMEOUT
virtual bool hasPendingDatagrams() const =0
The QNetworkProxyQuery class is used to query the proxy settings for a socket.
const char * constData() const
Returns a pointer to the data stored in the byte array.
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
virtual bool isExceptionNotificationEnabled() const =0
void setLocalAddress(const QHostAddress &address)
Sets the address on the local side of a connection to address.
void setProxy(const QNetworkProxy &networkProxy)
Sets the explicit network proxy for this socket to networkProxy.
quint16 localPort() const
The QScopedValueRollback class resets a variable to its previous value on destruction.
static QHostInfo fromName(const QString &name)
Looks up the IP address(es) for the given host name.
QAbstractSocket::NetworkLayerProtocol protocol() const
Returns the network layer protocol of the host address.
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.
void disconnectFromHostImplementation()
Contains the implementation of disconnectFromHost().
quint16 localPort() const
Returns the host port number (in native byte order) of the local socket if available; otherwise retur...
OpenMode openMode() const
Returns the mode in which the device has been opened; i.e.
QAbstractSocket::SocketError error() const
void connectionNotification()
Slot connected to a notification of connection status change.
QObject * parent() const
Returns a pointer to the parent object.
void setErrorString(const QString &errorString)
Sets the human readable description of the last device error that occurred to str.
void _q_connectToNextAddress()
Called by a queued or direct connection from _q_startConnecting() or _q_testConnection(), this function takes the first address of the pending addresses list and tries to connect to it.
QAbstractSocketEngine * socketEngine
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
bool readSocketNotifierCalled
void setLocalPort(quint16 port)
Sets the port on the local side of a connection to port.
qint64 bytesToWrite() const
Returns the number of bytes that are waiting to be written.
QAbstractSocket::SocketType socketType
T qvariant_cast(const QVariant &)
QHostAddress peerAddress() const
Returns the address of the connected peer if the socket is in ConnectedState; otherwise returns QHost...
void fetchConnectionParameters()
Sets up the internal state after the connection has succeeded.
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
QVariant property(const char *name) const
Returns the value of the object's name property.
void disconnectFromHost()
Attempts to close the socket.
bool flush()
Writes pending data in the write buffers to the socket.
QString errorString() const
#define QT_BEGIN_INCLUDE_NAMESPACE
This macro is equivalent to QT_END_NAMESPACE.
static int qt_timeout_value(int msecs, int elapsed)
#define Q_CHECK_SOCKETENGINE(returnValue)
void stateChanged(QAbstractSocket::SocketState)
This signal is emitted whenever QAbstractSocket's state changes.
void setPeerAddress(const QHostAddress &address)
Sets the address of the remote side of the connection to address.
bool readSocketNotifierState
bool initSocketLayer(QAbstractSocket::NetworkLayerProtocol protocol)
Initializes the socket layer to by of type type, using the network layer protocol protocol...
void resolveProxy(const QString &hostName, quint16 port)
Resolve the proxy to its final value.
virtual qint64 read(char *data, qint64 maxlen)=0
virtual bool isValid() const =0
QAbstractSocketPrivate()
Constructs a QAbstractSocketPrivate.
virtual bool canReadLine() const
Returns true if a complete line of data can be read from the device; otherwise returns false...
The QHostAddress class provides an IP address.
void _q_testConnection()
Tests if a connection has been established.
qint64 writeData(const char *data, qint64 len)
Reimplemented Function
The QTimer class provides repetitive and single-shot timers.
QAbstractSocket::SocketState state() const
static void pauseSocketNotifiers(QAbstractSocket *)
QHostAddress localAddress
SocketError error() const
Returns the type of error that last occurred.
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
virtual void setReadNotificationEnabled(bool enable)=0
The QIODevice class is the base interface class of all I/O devices in Qt.
void stop()
Stops the timer.
bool canReadNotification()
Slot connected to the read socket notifier.
void start(int msec)
Starts or restarts the timer with a timeout interval of msec milliseconds.
#define qPrintable(string)
virtual bool connectToHost(const QHostAddress &address, quint16 port)=0
bool canReadLine() const
Returns true if a line of data can be read from the socket; otherwise returns false.
bool canWriteNotification()
Slot connected to the write socket notifier.
The QAbstractSocket class provides the base functionality common to all socket types.
virtual qint64 bytesAvailable() const =0
void setPeerPort(quint16 port)
Sets the port of the remote side of the connection to port.
QAbstractEventDispatcher * eventDispatcher
void start()
Starts this timer.
QAbstractSocket(SocketType socketType, QObject *parent)
Creates a new abstract socket of type socketType.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
SocketState state() const
Returns the state of the socket.
QHostInfo qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id)
bool isValid() const
Returns true if the socket is valid and ready for use; otherwise returns false.
#define forever
This macro is provided for convenience for writing infinite loops.