📄 qnativesocketengine.cpp
字号:
// Set the send and receive buffer sizes to a magic size, found // most optimal for our platforms. setReceiveBufferSize(49152); setSendBufferSize(49152); d->socketType = socketType; d->socketProtocol = protocol; return true;}/*! \overload Initializes the socket using \a socketDescriptor instead of creating a new one. The socket type and network layer protocol are determined automatically. The socket's state is set to \a socketState. If the socket type is either TCP or UDP, it is made non-blocking. UDP sockets are also broadcast enabled. */bool QNativeSocketEngine::initialize(int socketDescriptor, QAbstractSocket::SocketState socketState){ Q_D(QNativeSocketEngine); if (isValid()) close(); d->socketDescriptor = socketDescriptor; // determine socket type and protocol if (!d->fetchConnectionParameters()) {#if defined (QNATIVESOCKETENGINE_DEBUG) qDebug("QNativeSocketEngine::initialize(socketDescriptor == %i) failed: %s", socketDescriptor, d->socketErrorString.toLatin1().constData());#endif d->socketDescriptor = -1; return false; } if (d->socketType != QAbstractSocket::UnknownSocketType) { // Make the socket nonblocking. if (!setOption(NonBlockingSocketOption, 1)) { d->setError(QAbstractSocket::UnsupportedSocketOperationError, QNativeSocketEnginePrivate::NonBlockingInitFailedErrorString); close(); return false; } // Set the broadcasting flag if it's a UDP socket. if (d->socketType == QAbstractSocket::UdpSocket && !setOption(BroadcastSocketOption, 1)) { d->setError(QAbstractSocket::UnsupportedSocketOperationError, QNativeSocketEnginePrivate::BroadcastingInitFailedErrorString); close(); return false; } } d->socketState = socketState; return true;}/*! Returns true if the socket is valid; otherwise returns false. A socket is valid if it has not been successfully initialized, or if it has been closed.*/bool QNativeSocketEngine::isValid() const{ Q_D(const QNativeSocketEngine); return d->socketDescriptor != -1;}/*! Returns the native socket descriptor. Any use of this descriptor stands the risk of being non-portable.*/int QNativeSocketEngine::socketDescriptor() const{ Q_D(const QNativeSocketEngine); return d->socketDescriptor;}/*! Connects to the IP address and port specified by \a address and \a port. If the connection is established, this function returns true and the socket enters ConnectedState. Otherwise, false is returned. If false is returned, state() should be called to see if the socket is in ConnectingState. If so, a delayed TCP connection is taking place, and connectToHost() must be called again later to determine if the connection was established successfully or not. The second connection attempt must be made when the socket is ready for writing. This state can be determined either by connecting a QSocketNotifier to the socket descriptor returned by socketDescriptor(), or by calling the blocking function waitForWrite(). Example: \code QNativeSocketEngine socketLayer; socketLayer.initialize(QAbstractSocket::TcpSocket, QAbstractSocket::IPv4Protocol); socketLayer.connectToHost(QHostAddress::LocalHost, 22); // returns false socketLayer.waitForWrite(); socketLayer.connectToHost(QHostAddress::LocalHost, 22); // returns true \endcode Otherwise, error() should be called to determine the cause of the error.*/bool QNativeSocketEngine::connectToHost(const QHostAddress &address, quint16 port){ Q_D(QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::connectToHost(), false);#if defined (QT_NO_IPV6) if (address.protocol() == QAbstractSocket::IPv6Protocol) { d->setError(QAbstractSocket::UnsupportedSocketOperationError, QNativeSocketEnginePrivate::NoIpV6ErrorString); return false; }#endif Q_CHECK_STATES(QNativeSocketEngine::connectToHost(), QAbstractSocket::UnconnectedState, QAbstractSocket::ConnectingState, false); bool connected = d->nativeConnect(address, port); if (connected) d->fetchConnectionParameters(); return connected;}/*! Binds the socket to the address \a address and port \a port. Returns true on success; otherwise false is returned. The port may be 0, in which case an arbitrary unused port is assigned automatically by the operating system. Servers call this function to set up the server's address and port. TCP servers must in addition call listen() after bind().*/bool QNativeSocketEngine::bind(const QHostAddress &address, quint16 port){ Q_D(QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::bind(), false);#if defined (QT_NO_IPV6) if (address.protocol() == QAbstractSocket::IPv6Protocol) { d->setError(QAbstractSocket::UnsupportedSocketOperationError, QNativeSocketEnginePrivate::NoIpV6ErrorString); return false; }#endif Q_CHECK_STATE(QNativeSocketEngine::bind(), QAbstractSocket::UnconnectedState, false); if (!d->nativeBind(address, port)) return false; d->fetchConnectionParameters(); return true;}/*! Prepares a TCP server for accepting incoming connections. This function must be called after bind(), and only by TCP sockets. After this function has been called, pending client connections are detected by checking if the socket is ready for reading. This can be done by either creating a QSocketNotifier, passing the socket descriptor returned by socketDescriptor(), or by calling the blocking function waitForRead(). Example: \code QNativeSocketEngine socketLayer; socketLayer.bind(QHostAddress::Any, 4000); socketLayer.listen(); if (socketLayer.waitForRead()) { int clientSocket = socketLayer.accept(); // a client is connected } \endcode \sa bind(), accept()*/bool QNativeSocketEngine::listen(){ Q_D(QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::listen(), false); Q_CHECK_STATE(QNativeSocketEngine::listen(), QAbstractSocket::BoundState, false); Q_CHECK_TYPE(QNativeSocketEngine::listen(), QAbstractSocket::TcpSocket, false); // We're using a backlog of 50. Most modern kernels support TCP // syncookies by default, and if they do, the backlog is ignored. // When there is no support for TCP syncookies, this value is // fine. return d->nativeListen(50);}/*! Accepts a pending connection from the socket, which must be in ListeningState, and returns its socket descriptor. If no pending connections are available, -1 is returned. \sa bind(), listen()*/int QNativeSocketEngine::accept(){ Q_D(QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::accept(), -1); Q_CHECK_STATE(QNativeSocketEngine::accept(), QAbstractSocket::ListeningState, false); Q_CHECK_TYPE(QNativeSocketEngine::accept(), QAbstractSocket::TcpSocket, false); return d->nativeAccept();}/*! 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();}/*! Returns the size of the pending datagram, or -1 if no datagram is pending. A datagram size of 0 is perfectly valid. This function is called by UDP sockets before receiveMessage(). For TCP sockets, call bytesAvailable().*/qint64 QNativeSocketEngine::pendingDatagramSize() const{ Q_D(const QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::pendingDatagramSize(), -1); Q_CHECK_TYPE(QNativeSocketEngine::pendingDatagramSize(), QAbstractSocket::UdpSocket, false); return d->nativePendingDatagramSize();}/*! Reads up to \a maxSize bytes of a datagram from the socket, stores it in \a data and returns the number of bytes read. The address and port of the sender are stored in \a address and \a port. If either of these pointers is 0, the corresponding value is discarded. To avoid unnecessarily loss of data, call pendingDatagramSize() to determine the size of the pending message before reading it. If \a maxSize is too small, the rest of the datagram will be lost. Returns -1 if an error occurred. \sa hasPendingDatagrams()*/qint64 QNativeSocketEngine::readDatagram(char *data, qint64 maxSize, QHostAddress *address, quint16 *port){ Q_D(QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::readDatagram(), -1); Q_CHECK_TYPE(QNativeSocketEngine::readDatagram(), QAbstractSocket::UdpSocket, false); return d->nativeReceiveDatagram(data, maxSize, address, port);}/*! Writes a UDP datagram of size \a size bytes to the socket from \a data to the address \a host on port \a port, and returns the number of bytes written, or -1 if an error occurred. Only one datagram is sent, and if there is too much data to fit into a single datagram, the operation will fail and error() will return QAbstractSocket::DatagramTooLargeError. Operating systems impose an upper limit to the size of a datagram, but this size is different on almost all platforms. Sending large datagrams is in general disadvised, as even if they are sent successfully, they are likely to be fragmented before arriving at their destination. Experience has shown that it is in general safe to send datagrams no larger than 512 bytes. \sa readDatagram()*/qint64 QNativeSocketEngine::writeDatagram(const char *data, qint64 size, const QHostAddress &host, quint16 port){ Q_D(QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::writeDatagram(), -1); Q_CHECK_TYPE(QNativeSocketEngine::writeDatagram(), QAbstractSocket::UdpSocket, -1); return d->nativeSendDatagram(data, size, host, port);}/*! Writes a block of \a size bytes from \a data to the socket. Returns the number of bytes written, or -1 if an error occurred.*/qint64 QNativeSocketEngine::write(const char *data, qint64 size){ Q_D(QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::write(), -1); Q_CHECK_STATE(QNativeSocketEngine::write(), QAbstractSocket::ConnectedState, -1); return d->nativeWrite(data, size);}/*! Reads up to \a maxSize bytes into \a data from the socket. Returns the number of bytes read, or -1 if an error occurred.*/qint64 QNativeSocketEngine::read(char *data, qint64 maxSize){ Q_D(QNativeSocketEngine);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -