⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qabstractsocket.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    qDebug("QAbstractSocket::bytesToWrite() == %i", d->writeBuffer.size());#endif    return (qint64)d->writeBuffer.size();}/*!    Returns the number of incoming bytes that are waiting to be read.    \sa bytesToWrite(), read()*/qint64 QAbstractSocket::bytesAvailable() const{    Q_D(const QAbstractSocket);    qint64 available = QIODevice::bytesAvailable();    if (d->isBuffered)        available += (qint64) d->readBuffer.size();    else if (d->socketEngine && d->socketEngine->isValid())        available += d->socketEngine->bytesAvailable();#if defined(QABSTRACTSOCKET_DEBUG)    qDebug("QAbstractSocket::bytesAvailable() == %llu", available);#endif    return available;}/*!    Returns the host port number (in native byte order) of the local    socket if available; otherwise returns 0.    \sa localAddress(), peerPort(), setLocalPort()*/quint16 QAbstractSocket::localPort() const{    Q_D(const QAbstractSocket);    return d->localPort;}/*!    Returns the host address of the local socket if available;    otherwise returns QHostAddress::Null.    This is normally the main IP address of the host, but can be    QHostAddress::LocalHost (127.0.0.1) for connections to the    local host.    \sa localPort(), peerAddress(), setLocalAddress()*/QHostAddress QAbstractSocket::localAddress() const{    Q_D(const QAbstractSocket);    return d->localAddress;}/*!    Returns the port of the connected peer if the socket is in    ConnectedState; otherwise returns 0.    \sa peerAddress(), localPort(), setPeerPort()*/quint16 QAbstractSocket::peerPort() const{    Q_D(const QAbstractSocket);    return d->peerPort;}/*!    Returns the address of the connected peer if the socket is in    ConnectedState; otherwise returns QHostAddress::Null.    \sa peerName(), peerPort(), localAddress(), setPeerAddress()*/QHostAddress QAbstractSocket::peerAddress() const{    Q_D(const QAbstractSocket);    return d->peerAddress;}/*!    Returns the name of the peer as specified by connectToHost(), or    an empty QString if connectToHost() has not been called.    \sa peerAddress(), peerPort(), setPeerName()*/QString QAbstractSocket::peerName() const{    Q_D(const QAbstractSocket);    return d->peerName.isEmpty() ? d->hostName : d->peerName;}/*!    Returns true if a line of data can be read from the socket;    otherwise returns false.    \sa readLine()*/bool QAbstractSocket::canReadLine() const{    bool hasLine = d_func()->readBuffer.canReadLine();#if defined (QABSTRACTSOCKET_DEBUG)    qDebug("QAbstractSocket::canReadLine() == %s, buffer size = %d, size = %d", hasLine ? "true" : "false",           d_func()->readBuffer.size(), d_func()->buffer.size());#endif    return hasLine || QIODevice::canReadLine();}/*!    Returns the native socket descriptor of the QAbstractSocket object    if this is available; otherwise returns -1.    If the socket is using QNetworkProxy, the returned descriptor    may not be usable with native socket functions.    The socket descriptor is not available when QAbstractSocket is in    UnconnectedState.    \sa setSocketDescriptor()*/int QAbstractSocket::socketDescriptor() const{    Q_D(const QAbstractSocket);    return d->cachedSocketDescriptor;}/*!    Initializes QAbstractSocket with the native socket descriptor \a    socketDescriptor. Returns true if \a socketDescriptor is accepted    as a valid socket descriptor; otherwise returns false.    The socket is opened in the mode specified by \a openMode, and    enters the socket state specified by \a socketState.    \bold{Note:} It is not possible to initialize two abstract sockets    with the same native socket descriptor.    \sa socketDescriptor()*/bool QAbstractSocket::setSocketDescriptor(int socketDescriptor, SocketState socketState,                                          OpenMode openMode){    Q_D(QAbstractSocket);#ifndef QT_NO_OPENSSL    if (QSslSocket *socket = qobject_cast<QSslSocket *>(this))        return socket->setSocketDescriptor(socketDescriptor, socketState, openMode);#endif    d->resetSocketLayer();    d->socketEngine = QAbstractSocketEngine::createSocketEngine(socketDescriptor, this);    bool result = d->socketEngine->initialize(socketDescriptor, socketState);    if (!result) {        d->socketError = d->socketEngine->error();        setErrorString(d->socketEngine->errorString());        return false;    }    if (d->threadData->eventDispatcher)        d->socketEngine->setReceiver(d);    setOpenMode(openMode);    if (d->state != socketState) {        d->state = socketState;        emit stateChanged(d->state);    }    d->socketEngine->setReadNotificationEnabled(true);    d->localPort = d->socketEngine->localPort();    d->peerPort = d->socketEngine->peerPort();    d->localAddress = d->socketEngine->localAddress();    d->peerAddress = d->socketEngine->peerAddress();    d->cachedSocketDescriptor = socketDescriptor;#ifdef Q_OS_LINUX    // ### This is a workaround for certain broken Linux kernels, when using    // QTcpSocket with a Unix domain socket. It was introduced around 2.6.9,    // and fixed at some point after that.    // http://archive.linux-usenet.com/index-t-73300.html    // We can provide a better workaround for this: readFromSocket() can loop    // while reading, but this must happen without triggering an implicit    // close because of reading after the socket has closed.    d->addToBytesAvailable = 4096;#endif    return true;}/*   Returns the difference between msecs and elapsed. If msecs is -1,   however, -1 is returned.*/static int qt_timeout_value(int msecs, int elapsed){    if (msecs == -1)        return -1;    int timeout = msecs - elapsed;    return timeout < 0 ? 0 : timeout;}/*!    Waits until the socket is connected, up to \a msecs    milliseconds. If the connection has been established, 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 established:    \code        socket->connectToHost("imap", 143);        if (socket->waitForConnected(1000))            qDebug("Connected!");    \endcode    If msecs is -1, this function will not time out.    \sa connectToHost(), connected()*/bool QAbstractSocket::waitForConnected(int msecs){    Q_D(QAbstractSocket);#if defined (QABSTRACTSOCKET_DEBUG)    qDebug("QAbstractSocket::waitForConnected(%i)", msecs);#endif    if (state() == ConnectedState) {#if defined (QABSTRACTSOCKET_DEBUG)        qDebug("QAbstractSocket::waitForConnected(%i) already connected", msecs);#endif        return true;    }#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->waitForConnected(msecs);#endif    QTime stopWatch;    stopWatch.start();    if (d->state == HostLookupState) {#if defined (QABSTRACTSOCKET_DEBUG)        qDebug("QAbstractSocket::waitForConnected(%i) doing host name lookup", msecs);#endif        QHostInfo::abortHostLookup(d->hostLookupId);        d->hostLookupId = -1;        d->_q_startConnecting(QHostInfo::fromName(d->hostName));    } else {#if defined (QABSTRACTSOCKET_DEBUG)        qDebug("QAbstractSocket::waitForConnected(%i) testing connection", msecs);#endif        d->_q_testConnection();    }    if (state() == UnconnectedState)        return false;    bool timedOut = true;#if defined (QABSTRACTSOCKET_DEBUG)    int attempt = 1;#endif    while (state() == ConnectingState && (msecs == -1 || stopWatch.elapsed() < msecs)) {        int timeout = qt_timeout_value(msecs, stopWatch.elapsed());        if (msecs != -1 && timeout > QT_CONNECT_TIMEOUT)            timeout = QT_CONNECT_TIMEOUT;#if defined (QABSTRACTSOCKET_DEBUG)        qDebug("QAbstractSocket::waitForConnected(%i) waiting %.2f secs for connection attempt #%i",               msecs, timeout / 1000.0, attempt++);#endif        timedOut = false;        if (d->socketEngine->waitForWrite(timeout, &timedOut) && !timedOut) {            d->_q_testConnection();        } else {            d->_q_connectToNextAddress();        }    }    if ((timedOut && state() != ConnectedState) || state() == ConnectingState) {        d->socketError = SocketTimeoutError;        setSocketState(UnconnectedState);        d->resetSocketLayer();        setErrorString(tr("Socket operation timed out"));    }#if defined (QABSTRACTSOCKET_DEBUG)    qDebug("QAbstractSocket::waitForConnected(%i) == %s", msecs,           state() == ConnectedState ? "true" : "false");#endif    return state() == ConnectedState;}/*!    This function blocks until data is available for reading and the    \l{QIODevice::}{readyRead()} signal has been emitted. The function    will timeout after \a msecs milliseconds; the default timeout is    3000 milliseconds.    The function returns true if the readyRead() signal is emitted and    there is data available for reading; otherwise it returns false    (if an error occurred or the operation timed out).    \sa waitForBytesWritten() */bool QAbstractSocket::waitForReadyRead(int msecs){    Q_D(QAbstractSocket);#if defined (QABSTRACTSOCKET_DEBUG)    qDebug("QAbstractSocket::waitForReadyRead(%i)", msecs);#endif    // require calling connectToHost() before waitForReadyRead()    if (state() == UnconnectedState) {        /* If all you have is a QIODevice pointer to an abstractsocket, you cannot check           this, so you cannot avoid this warning. *///        qWarning("QAbstractSocket::waitForReadyRead() 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;    }    Q_ASSERT(d->socketEngine);    forever {        bool readyToRead = false;        bool readyToWrite = false;        if (!d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite, true, !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) {            if (d->canReadNotification())                return true;        }        if (readyToWrite)            d->canWriteNotification();        if (state() != ConnectedState)            return false;    }    return false;}/*! \reimp */bool QAbstractSocket::waitForBytesWritten(int msecs){    Q_D(QAbstractSocket);#if defined (QABSTRACTSOCKET_DEBUG)    qDebug("QAbstractSocket::waitForBytesWritten(%i)", msecs);#endif    // require calling connectToHost() before waitForBytesWritten()    if (state() == UnconnectedState) {        qWarning("QAbstractSocket::waitForBytesWritten() is not allowed in UnconnectedState");        return false;    }    if (d->writeBuffer.isEmpty())        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, true, !d->writeBuffer.isEmpty(),                                               qt_timeout_value(msecs, stopWatch.elapsed()))) {            d->socketError = d->socketEngine->error();            setErrorString(d->socketEngine->errorString());#if defined (QABSTRACTSOCKET_DEBUG)            qDebug("QAbstractSocket::waitForBytesWritten(%i) failed (%i, %s)",                   msecs, d->socketError, errorString().toLatin1().constData());#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -