bool QNativeSocketEngine::waitForReadOrWrite(bool *readyToRead, bool *readyToWrite, bool checkRead, bool checkWrite, int msecs, bool *timedOut) { Q_D(const QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::waitForWrite(), false); Q_CHECK_NOT_STATE(QNativeSocketEngine::waitForReadOrWrite(), QAbstractSocket::UnconnectedState, false); int ret = d->nativeSelect(msecs, checkRead, checkWrite, readyToRead, readyToWrite); // On Windows, the socket is in connected state if a call to // select(writable) is successful. In this case we should not // issue a second call to WSAConnect() #if defined (Q_WS_WIN) if (checkWrite && ((readyToWrite && *readyToWrite) || !readyToWrite) && ret > 0) { setState(QAbstractSocket::ConnectedState); d_func()->fetchConnectionParameters(); return true; } #endif if (ret == 0) { if (timedOut) *timedOut = true; d->setError(QAbstractSocket::SocketTimeoutError, QNativeSocketEnginePrivate::TimeOutErrorString); return false; } else if (state() == QAbstractSocket::ConnectingState) { connectToHost(d->peerAddress, d->peerPort); } return ret > 0; }
/*! Returns the number of bytes that are currently available for reading. On error, -1 is returned. For UDP sockets, this function returns the accumulated size of all pending datagrams, and it is therefore more useful for UDP sockets to call hasPendingDatagrams() and pendingDatagramSize(). */ qint64 QNativeSocketEngine::bytesAvailable() const { Q_D(const QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::bytesAvailable(), -1); Q_CHECK_NOT_STATE(QNativeSocketEngine::bytesAvailable(), QAbstractSocket::UnconnectedState, false); return d->nativeBytesAvailable(); }
/*! Returns true if there is at least one datagram pending. This function is only called by UDP sockets, where a datagram can have a size of 0. TCP sockets call bytesAvailable(). */ bool QNativeSocketEngine::hasPendingDatagrams() const { Q_D(const QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::hasPendingDatagrams(), false); Q_CHECK_NOT_STATE(QNativeSocketEngine::hasPendingDatagrams(), QAbstractSocket::UnconnectedState, false); Q_CHECK_TYPE(QNativeSocketEngine::hasPendingDatagrams(), QAbstractSocket::UdpSocket, false); return d->nativeHasPendingDatagrams(); }
/*! Waits for \a msecs milliseconds or until the socket is ready for writing. If \a timedOut is not 0 and \a msecs milliseconds have passed, the value of \a timedOut is set to true. Returns true if data is available for writing; otherwise returns false. This is a blocking function call; its use is disadvised in a single threaded application, as the whole thread will stop responding until the function returns. waitForWrite() is most useful when there is no event loop available. The general approach is to create a QSocketNotifier, passing the socket descriptor returned by socketDescriptor() to its constructor. */ bool QNativeSocketEngine::waitForWrite(int msecs, bool *timedOut) { Q_D(QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::waitForWrite(), false); Q_CHECK_NOT_STATE(QNativeSocketEngine::waitForWrite(), QAbstractSocket::UnconnectedState, false); if (timedOut) *timedOut = false; int ret = d->nativeSelect(msecs, false); // On Windows, the socket is in connected state if a call to // select(writable) is successful. In this case we should not // issue a second call to WSAConnect() #if defined (Q_WS_WIN) if (ret > 0) { setState(QAbstractSocket::ConnectedState); d_func()->fetchConnectionParameters(); return true; } else { int value = 0; int valueSize = sizeof(value); if (::getsockopt(d->socketDescriptor, SOL_SOCKET, SO_ERROR, (char *) &value, &valueSize) == 0) { if (value == WSAECONNREFUSED) { d->setError(QAbstractSocket::ConnectionRefusedError, QNativeSocketEnginePrivate::ConnectionRefusedErrorString); d->socketState = QAbstractSocket::UnconnectedState; return false; } else if (value == WSAETIMEDOUT) { d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::ConnectionTimeOutErrorString); d->socketState = QAbstractSocket::UnconnectedState; return false; } else if (value == WSAEHOSTUNREACH) { d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::HostUnreachableErrorString); d->socketState = QAbstractSocket::UnconnectedState; return false; } } } #endif if (ret == 0) { if (timedOut) *timedOut = true; d->setError(QAbstractSocket::SocketTimeoutError, QNativeSocketEnginePrivate::TimeOutErrorString); d->hasSetSocketError = false; // A timeout error is temporary in waitFor functions return false; } else if (state() == QAbstractSocket::ConnectingState) { connectToHost(d->peerAddress, d->peerPort); } return ret > 0; }
bool QNativeSocketEngine::waitForReadOrWrite(bool *readyToRead, bool *readyToWrite, bool checkRead, bool checkWrite, int msecs, bool *timedOut) { Q_D(QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::waitForWrite(), false); Q_CHECK_NOT_STATE(QNativeSocketEngine::waitForReadOrWrite(), QAbstractSocket::UnconnectedState, false); const int ret = d->nativeSelect(msecs, checkRead, checkWrite, readyToRead, readyToWrite); if (ret == 0) { if (timedOut) *timedOut = true; d->setError(QAbstractSocket::SocketTimeoutError, QNativeSocketEnginePrivate::TimeOutErrorString); d->hasSetSocketError = false; // A timeout error is temporary in waitFor functions return false; } else if (state() == QAbstractSocket::ConnectingState) { connectToHost(d->peerAddress, d->peerPort); } return ret > 0; }
/*! Waits for \a msecs milliseconds or until the socket is ready for reading. If \a timedOut is not 0 and \a msecs milliseconds have passed, the value of \a timedOut is set to true. Returns true if data is available for reading; otherwise returns false. This is a blocking function call; its use is disadvised in a single threaded application, as the whole thread will stop responding until the function returns. waitForRead() is most useful when there is no event loop available. The general approach is to create a QSocketNotifier, passing the socket descriptor returned by socketDescriptor() to its constructor. */ bool QNativeSocketEngine::waitForRead(int msecs, bool *timedOut) { Q_D(const QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::waitForRead(), false); Q_CHECK_NOT_STATE(QNativeSocketEngine::waitForRead(), QAbstractSocket::UnconnectedState, false); if (timedOut) *timedOut = false; int ret = d->nativeSelect(msecs, true); if (ret == 0) { if (timedOut) *timedOut = true; d->setError(QAbstractSocket::SocketTimeoutError, QNativeSocketEnginePrivate::TimeOutErrorString); return false; } else if (state() == QAbstractSocket::ConnectingState) { connectToHost(d->peerAddress, d->peerPort); } return ret > 0; }