📄 qabstractsocket.cpp
字号:
emit error(d->socketError); if (d->socketError != SocketTimeoutError) close(); return false; } if (readyToRead) {#if defined (QABSTRACTSOCKET_DEBUG) qDebug("QAbstractSocket::waitForBytesWritten calls canReadNotification");#endif if(!d->canReadNotification()) return false; } if (readyToWrite) { if (d->canWriteNotification()) {#if defined (QABSTRACTSOCKET_DEBUG) qDebug("QAbstractSocket::waitForBytesWritten returns true");#endif return true; } } if (state() != ConnectedState) return false; } return false;}/*! Waits until the socket has disconnected, up to \a msecs milliseconds. If the connection has been disconnected, this function returns true; otherwise it returns false. In the case where it returns false, you can call error() to determine the cause of the error. The following example waits up to one second for a connection to be closed: \code socket->disconnectFromHost(); if (socket->state() == QAbstractSocket::UnconnectedState || socket->waitForDisconnected(1000)) qDebug("Disconnected!"); \endcode If msecs is -1, this function will not time out. \sa disconnectFromHost(), close()*/bool QAbstractSocket::waitForDisconnected(int msecs){ Q_D(QAbstractSocket);#ifndef QT_NO_OPENSSL // Manual polymorphism; this function is not virtual, but has an overload // in QSslSocket. if (QSslSocket *socket = qobject_cast<QSslSocket *>(this)) return socket->waitForDisconnected(msecs);#endif // require calling connectToHost() before waitForDisconnected() if (state() == UnconnectedState) { qWarning("QAbstractSocket::waitForDisconnected() is not allowed in UnconnectedState"); return false; } QTime stopWatch; stopWatch.start(); // handle a socket in connecting state if (state() == HostLookupState || state() == ConnectingState) { if (!waitForConnected(msecs)) return false; } forever { bool readyToRead = false; bool readyToWrite = false; if (!d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite, state() == ConnectedState, !d->writeBuffer.isEmpty(), qt_timeout_value(msecs, stopWatch.elapsed()))) { d->socketError = d->socketEngine->error(); setErrorString(d->socketEngine->errorString());#if defined (QABSTRACTSOCKET_DEBUG) qDebug("QAbstractSocket::waitForReadyRead(%i) failed (%i, %s)", msecs, d->socketError, errorString().toLatin1().constData());#endif emit error(d->socketError); if (d->socketError != SocketTimeoutError) close(); return false; } if (readyToRead) d->canReadNotification(); if (readyToWrite) d->canWriteNotification(); if (state() == UnconnectedState) return true; } return false;}/*! Aborts the current connection and resets the socket. Unlike disconnectFromHost(), this function immediately closes the socket, clearing any pending data in the write buffer. \sa disconnectFromHost(), close()*/void QAbstractSocket::abort(){ Q_D(QAbstractSocket);#if defined (QABSTRACTSOCKET_DEBUG) qDebug("QAbstractSocket::abort()");#endif if (d->state == UnconnectedState) return;#ifndef QT_NO_OPENSSL if (QSslSocket *socket = qobject_cast<QSslSocket *>(this)) { socket->abort(); return; }#endif if (d->connectTimer) { d->connectTimer->stop(); delete d->connectTimer; d->connectTimer = 0; } d->writeBuffer.clear(); close();}/*! \reimp*/bool QAbstractSocket::isSequential() const{ return true;}/*! \reimp Returns true if no more data is currently available for reading; otherwise returns false. This function is most commonly used when reading data from the socket in a loop. For example: \code // This slot is connected to QAbstractSocket::readyRead() void SocketClass::readyReadSlot() { while (!socket.atEnd()) { QByteArray data = socket.read(100); .... } } \endcode \sa bytesAvailable(), readyRead() */bool QAbstractSocket::atEnd() const{ return QIODevice::atEnd() && (!isOpen() || d_func()->readBuffer.isEmpty());}/*! This function writes as much as possible from the internal write buffer to the underlying network socket, without blocking. If any data was written, this function returns true; otherwise false is returned. Call this function if you need QAbstractSocket to start sending buffered data immediately. The number of bytes successfully written depends on the operating system. In most cases, you do not need to call this function, because QAbstractSocket will start sending data automatically once control goes back to the event loop. In the absence of an event loop, call waitForBytesWritten() instead. \sa write(), waitForBytesWritten()*/// Note! docs copied to QSslSocket::flush()bool QAbstractSocket::flush(){ Q_D(QAbstractSocket);#ifndef QT_NO_OPENSSL // Manual polymorphism; flush() isn't virtual, but QSslSocket overloads // it. if (QSslSocket *socket = qobject_cast<QSslSocket *>(this)) return socket->flush();#endif Q_CHECK_SOCKETENGINE(false); return d->flush();}/*! \reimp*/qint64 QAbstractSocket::readData(char *data, qint64 maxSize){ Q_D(QAbstractSocket); if (d->socketEngine && !d->socketEngine->isReadNotificationEnabled() && d->socketEngine->isValid()) d->socketEngine->setReadNotificationEnabled(true); if (!d->isBuffered) { qint64 readBytes = d->socketEngine->read(data, maxSize); if (readBytes < 0) { d->socketError = d->socketEngine->error(); setErrorString(d->socketEngine->errorString()); } if (!d->socketEngine->isReadNotificationEnabled()) d->socketEngine->setReadNotificationEnabled(true);#if defined (QABSTRACTSOCKET_DEBUG) qDebug("QAbstractSocket::readData(%p \"%s\", %lli) == %lld", data, qt_prettyDebug(data, 32, readBytes).data(), maxSize, readBytes);#endif return readBytes; } if (d->readBuffer.isEmpty()) return qint64(0); // If readFromSocket() read data, copy it to its destination. if (maxSize == 1) { *data = d->readBuffer.getChar();#if defined (QABSTRACTSOCKET_DEBUG) qDebug("QAbstractSocket::readData(%p '%c (0x%.2x)', 1) == 1", data, isprint(int(uchar(*data))) ? *data : '?', *data);#endif return 1; } qint64 bytesToRead = qMin(qint64(d->readBuffer.size()), maxSize); qint64 readSoFar = 0; while (readSoFar < bytesToRead) { const char *ptr = d->readBuffer.readPointer(); int bytesToReadFromThisBlock = qMin(int(bytesToRead - readSoFar), d->readBuffer.nextDataBlockSize()); memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock); readSoFar += bytesToReadFromThisBlock; d->readBuffer.free(bytesToReadFromThisBlock); }#if defined (QABSTRACTSOCKET_DEBUG) qDebug("QAbstractSocket::readData(%p \"%s\", %lli) == %lld", data, qt_prettyDebug(data, qMin<qint64>(32, readSoFar), readSoFar).data(), maxSize, readSoFar);#endif return readSoFar;}/*! \reimp*/qint64 QAbstractSocket::readLineData(char *data, qint64 maxlen){ return QIODevice::readLineData(data, maxlen);}/*! \reimp*/qint64 QAbstractSocket::writeData(const char *data, qint64 size){ Q_D(QAbstractSocket); if (d->state == QAbstractSocket::UnconnectedState) { d->socketError = QAbstractSocket::UnknownSocketError; setErrorString(tr("Socket is not connected")); return -1; } if (!d->isBuffered) { qint64 written = d->socketEngine->write(data, size); if (written < 0) { d->socketError = d->socketEngine->error(); setErrorString(d->socketEngine->errorString()); } else if (!d->writeBuffer.isEmpty()) { d->socketEngine->setWriteNotificationEnabled(true); }#if defined (QABSTRACTSOCKET_DEBUG) qDebug("QAbstractSocket::writeData(%p \"%s\", %lli) == %lli", data, qt_prettyDebug(data, qMin((int)size, 32), size).data(), size, written);#endif if (written >= 0) emit bytesWritten(written); return written; } char *ptr = d->writeBuffer.reserve(size); if (size == 1) *ptr = *data; else memcpy(ptr, data, size); qint64 written = size; if (d->socketEngine && !d->writeBuffer.isEmpty()) d->socketEngine->setWriteNotificationEnabled(true);#if defined (QABSTRACTSOCKET_DEBUG) qDebug("QAbstractSocket::writeData(%p \"%s\", %lli) == %lli", data, qt_prettyDebug(data, qMin((int)size, 32), size).data(), size, written);#endif return written;}/*! \since 4.1 Sets the port on the local side of a connection to \a port. You can call this function in a subclass of QAbstractSocket to change the return value of the localPort() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings. Note that this function does not bind the local port of the socket prior to a connection (e.g., QUdpSocket::bind()). \sa localAddress(), setLocalAddress(), setPeerPort()*/void QAbstractSocket::setLocalPort(quint16 port){ Q_D(QAbstractSocket); d->localPort = port;}/*! \since 4.1 Sets the address on the local side of a connection to \a address. You can call this function in a subclass of QAbstractSocket to change the return value of the localAddress() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings. Note that this function does not bind the local address of the socket prior to a connection (e.g., QUdpSocket::bind()). \sa localAddress(), setLocalPort(), setPeerAddress()*/void QAbstractSocket::setLocalAddress(const QHostAddress &address){ Q_D(QAbstractSocket); d->localAddress = address;}/*! \since 4.1 Sets the port of the remote side of the connection to \a port. You can call this function in a subclass of QAbstractSocket to change the return value of the peerPort() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings. \sa peerPort(), setPeerAddress(), setLocalPort()*/void QAbstractSocket::setPeerPort(quint16 port){ Q_D(QAbstractSocket); d->peerPort = port;}/*! \since 4.1 Sets the address of the remote side of the connection to \a address. You can call this function in a subclass of QAbstractSocket to change the return value of the peerAddress() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings. \sa peerAddress(), setPeerPort(), setLocalAddress()*/void QAbstractSocket::setPeerAddress(const QHostAddress &address){ Q_D(QAbstractSocket); d->peerAddress = address;}/*! \since 4.1 Sets the host name of the remote peer to \a name. You can call this function in a subclass of QAbstractSocket to change the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -