📄 qnativesocketengine.cpp
字号:
Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::read(), -1); Q_CHECK_STATES(QNativeSocketEngine::read(), QAbstractSocket::ConnectedState, QAbstractSocket::BoundState, -1); qint64 readBytes = d->nativeRead(data, maxSize); // Handle remote close if (readBytes == 0 && d->socketType == QAbstractSocket::TcpSocket) { d->setError(QAbstractSocket::RemoteHostClosedError, QNativeSocketEnginePrivate::RemoteHostClosedErrorString); close(); return -1; } return readBytes;}/*! Closes the socket. In order to use the socket again, initialize() must be called.*/void QNativeSocketEngine::close(){ Q_D(QNativeSocketEngine); if (d->readNotifier) d->readNotifier->setEnabled(false); if (d->writeNotifier) d->writeNotifier->setEnabled(false); if (d->exceptNotifier) d->exceptNotifier->setEnabled(false); if(d->socketDescriptor != -1) { d->nativeClose(); d->socketDescriptor = -1; } d->socketState = QAbstractSocket::UnconnectedState; d->hasSetSocketError = false; d->localPort = 0; d->localAddress.clear(); d->peerPort = 0; d->peerAddress.clear(); if (d->readNotifier) { delete d->readNotifier; d->readNotifier = 0; } if (d->writeNotifier) { delete d->writeNotifier; d->writeNotifier = 0; } if (d->exceptNotifier) { delete d->exceptNotifier; d->exceptNotifier = 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) const{ 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; } return ret > 0;}/*! 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) const{ Q_D(const 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); if (ret == 0) { if (timedOut) *timedOut = true; d->setError(QAbstractSocket::SocketTimeoutError, QNativeSocketEnginePrivate::TimeOutErrorString); return false; } return ret > 0;}bool QNativeSocketEngine::waitForReadOrWrite(bool *readyToRead, bool *readyToWrite, bool checkRead, bool checkWrite, int msecs, bool *timedOut) const{ 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); if (ret == 0) { if (timedOut) *timedOut = true; d->setError(QAbstractSocket::SocketTimeoutError, QNativeSocketEnginePrivate::TimeOutErrorString); return false; } return ret > 0;}/*! Returns the size of the operating system's socket receive buffer. Depending on the operating system, this size may be different from what has been set earlier with setReceiveBufferSize().*/qint64 QNativeSocketEngine::receiveBufferSize() const{ Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::receiveBufferSize(), -1); return option(ReceiveBufferSocketOption);}/*! Sets the size of the operating system receive buffer to \a size. For clients, this should be set before connectToHost() is called; otherwise it will have no effect. For servers, it should be called before listen(). The operating system receive buffer size effectively limits two things: how much data can be in transit at any one moment, and how much data can be received in one iteration of the main event loop. Setting the size of the receive buffer may have an impact on the socket's performance. The default value is operating system-dependent.*/void QNativeSocketEngine::setReceiveBufferSize(qint64 size){ Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::setReceiveBufferSize(), Q_VOID); setOption(ReceiveBufferSocketOption, size);}/*! Returns the size of the operating system send buffer. Depending on the operating system, this size may be different from what has been set earlier with setSendBufferSize().*/qint64 QNativeSocketEngine::sendBufferSize() const{ Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::setSendBufferSize(), -1); return option(SendBufferSocketOption);}/*! Sets the size of the operating system send buffer to \a size. The operating system send buffer size effectively limits how much data can be in transit at any one moment. Setting the size of the send buffer may have an impact on the socket's performance. The default value is operating system-dependent.*/void QNativeSocketEngine::setSendBufferSize(qint64 size){ Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::setSendBufferSize(), Q_VOID); setOption(SendBufferSocketOption, size);}/*! Sets the option \a option to the value \a value.*/bool QNativeSocketEngine::setOption(SocketOption option, int value){ Q_D(QNativeSocketEngine); return d->setOption(option, value);}/*! Returns the value of the option \a socketOption.*/int QNativeSocketEngine::option(SocketOption socketOption) const{ Q_D(const QNativeSocketEngine); return d->option(socketOption);}bool QNativeSocketEngine::isReadNotificationEnabled() const{ Q_D(const QNativeSocketEngine); return d->readNotifier && d->readNotifier->isEnabled();}class QReadNotifier : public QSocketNotifier{public: QReadNotifier(int fd, QNativeSocketEngine *parent) : QSocketNotifier(fd, QSocketNotifier::Read, parent) { engine = parent; }protected: bool event(QEvent *); QNativeSocketEngine *engine;};bool QReadNotifier::event(QEvent *e){ if (e->type() == QEvent::SockAct) { engine->readNotification(); return true; } return QSocketNotifier::event(e);}class QWriteNotifier : public QSocketNotifier{public: QWriteNotifier(int fd, QNativeSocketEngine *parent) : QSocketNotifier(fd, QSocketNotifier::Write, parent) { engine = parent; }protected: bool event(QEvent *); QNativeSocketEngine *engine;};bool QWriteNotifier::event(QEvent *e){ if (e->type() == QEvent::SockAct) { engine->writeNotification(); return true; } return QSocketNotifier::event(e);}class QExceptionNotifier : public QSocketNotifier{public: QExceptionNotifier(int fd, QNativeSocketEngine *parent) : QSocketNotifier(fd, QSocketNotifier::Exception, parent) { engine = parent; }protected: bool event(QEvent *); QNativeSocketEngine *engine;};bool QExceptionNotifier::event(QEvent *e){ if (e->type() == QEvent::SockAct) { engine->exceptionNotification(); return true; } return QSocketNotifier::event(e);}void QNativeSocketEngine::setReadNotificationEnabled(bool enable){ Q_D(QNativeSocketEngine); if (d->readNotifier) { d->readNotifier->setEnabled(enable); } else if (enable && d->threadData->eventDispatcher) { d->readNotifier = new QReadNotifier(d->socketDescriptor, this); d->readNotifier->setEnabled(true); }}bool QNativeSocketEngine::isWriteNotificationEnabled() const{ Q_D(const QNativeSocketEngine); return d->writeNotifier && d->writeNotifier->isEnabled();}void QNativeSocketEngine::setWriteNotificationEnabled(bool enable){ Q_D(QNativeSocketEngine); if (d->writeNotifier) { d->writeNotifier->setEnabled(enable); } else if (enable && d->threadData->eventDispatcher) { d->writeNotifier = new QWriteNotifier(d->socketDescriptor, this); d->writeNotifier->setEnabled(true); }}bool QNativeSocketEngine::isExceptionNotificationEnabled() const{ Q_D(const QNativeSocketEngine); return d->exceptNotifier && d->exceptNotifier->isEnabled();}void QNativeSocketEngine::setExceptionNotificationEnabled(bool enable){ Q_D(QNativeSocketEngine); if (d->exceptNotifier) { d->exceptNotifier->setEnabled(enable); } else if (enable && d->threadData->eventDispatcher) { d->exceptNotifier = new QExceptionNotifier(d->socketDescriptor, this); d->exceptNotifier->setEnabled(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -