📄 q3socketdevice_unix.cpp
字号:
/*! Specifies how many pending connections a server socket can have. Returns true if the operation was successful; otherwise returns false. A \a backlog value of 50 is quite common. The listen() call only applies to sockets where type() is \c Stream, i.e. not to \c Datagram sockets. listen() must not be called before bind() or after accept(). \sa bind(), accept()*/bool Q3SocketDevice::listen( int backlog ){ if ( !isValid() ) return false; if ( qt_socket_listen( fd, backlog ) >= 0 ) return true; if ( !e ) e = Impossible; return false;}/*! Extracts the first connection from the queue of pending connections for this socket and returns a new socket identifier. Returns -1 if the operation failed. \sa bind(), listen()*/int Q3SocketDevice::accept(){ if ( !isValid() ) return -1;#if !defined (QT_NO_IPV6) struct sockaddr_storage aa;#else struct sockaddr aa;#endif QT_SOCKLEN_T l = sizeof( aa ); bool done; int s; do { s = qt_socket_accept( fd, (struct sockaddr*)&aa, &l ); // we'll blithely throw away the stuff accept() wrote to aa done = true; if ( s < 0 && e == NoError ) { switch( errno ) { case EINTR: done = false; break;#if defined(EPROTO) case EPROTO:#endif#if defined(ENONET) case ENONET:#endif case ENOPROTOOPT: case EHOSTDOWN: case EOPNOTSUPP: case EHOSTUNREACH: case ENETDOWN: case ENETUNREACH: case ETIMEDOUT: // in all these cases, an error happened during connection // setup. we're not interested in what happened, so we // just treat it like the client-closed-quickly case. case EPERM: // firewalling wouldn't let us accept. we treat it like // the client-closed-quickly case. case EAGAIN:#if EAGAIN != EWOULDBLOCK case EWOULDBLOCK:#endif // the client closed the connection before we got around // to accept()ing it. break; case EBADF: case ENOTSOCK: e = Impossible; break; case EFAULT: e = InternalError; break; case ENOMEM: case ENOBUFS: e = NoResources; break; default: e = UnknownError; break; } } } while (!done); return s;}/*! Returns the number of bytes available for reading, or -1 if an error occurred. \warning On Microsoft Windows, we use the ioctlsocket() function to determine the number of bytes queued on the socket. According to Microsoft (KB Q125486), ioctlsocket() sometimes returns an incorrect number. The only safe way to determine the amount of data on the socket is to read it using readBlock(). QSocket has workarounds to deal with this problem.*/qint64 Q3SocketDevice::bytesAvailable() const{ if ( !isValid() ) return -1; /* Apparently, there is not consistency among different operating systems on how to use FIONREAD. FreeBSD, Linux and Solaris all expect the 3rd argument to ioctl() to be an int, which is normally 32-bit even on 64-bit machines. IRIX, on the other hand, expects a size_t, which is 64-bit on 64-bit machines. So, the solution is to use size_t initialized to zero to make sure all bits are set to zero, preventing underflow with the FreeBSD/Linux/Solaris ioctls. */ size_t nbytes = 0; // gives shorter than true amounts on Unix domain sockets. if ( ::ioctl(fd, FIONREAD, (char*)&nbytes) < 0 ) return -1; return (Q_LONG) *((int *) &nbytes) + QIODevice::bytesAvailable();}/*! Wait up to \a msecs milliseconds for more data to be available. If \a msecs is -1 the call will block indefinitely. Returns the number of bytes available for reading, or -1 if an error occurred. If \a timeout is non-null and no error occurred (i.e. it does not return -1): this function sets *\a timeout to true, if the reason for returning was that the timeout was reached; otherwise it sets *\a timeout to false. This is useful to find out if the peer closed the connection. \warning This is a blocking call and should be avoided in event driven applications. \sa bytesAvailable()*/Q_LONG Q3SocketDevice::waitForMore( int msecs, bool *timeout ) const{ if ( !isValid() ) return -1; if ( fd >= FD_SETSIZE ) return -1; fd_set fds; struct timeval tv; FD_ZERO( &fds ); FD_SET( fd, &fds ); tv.tv_sec = msecs / 1000; tv.tv_usec = (msecs % 1000) * 1000; int rv = select( fd+1, &fds, 0, 0, msecs < 0 ? 0 : &tv ); if ( rv < 0 ) return -1; if ( timeout ) { if ( rv == 0 ) *timeout = true; else *timeout = false; } return bytesAvailable();}/*! Reads \a maxlen bytes from the socket into \a data and returns the number of bytes read. Returns -1 if an error occurred.*/qint64 Q3SocketDevice::readData( char *data, qint64 maxlen ){#if defined(QT_CHECK_NULL) if ( data == 0 && maxlen != 0 ) { qWarning( "Q3SocketDevice::readBlock: Null pointer error" ); }#endif#if defined(QT_CHECK_STATE) if ( !isValid() ) { qWarning( "Q3SocketDevice::readBlock: Invalid socket" ); return -1; } if ( !isOpen() ) { qWarning( "Q3SocketDevice::readBlock: Device is not open" ); return -1; } if ( !isReadable() ) { qWarning( "Q3SocketDevice::readBlock: Read operation not permitted" ); return -1; }#endif bool done = false; int r = 0; while ( done == false ) { if ( t == Datagram ) {#if !defined(QT_NO_IPV6) struct sockaddr_storage aa;#else struct sockaddr_in aa;#endif memset( &aa, 0, sizeof(aa) ); QT_SOCKLEN_T sz; sz = sizeof( aa ); r = ::recvfrom( fd, data, maxlen, 0, (struct sockaddr *)&aa, &sz ); qt_socket_getportaddr( (struct sockaddr *)&aa, &pp, &pa); } else { r = ::read( fd, data, maxlen ); } done = true; if ( r == 0 && t == Stream && maxlen > 0 ) { // connection closed close(); } else if ( r >= 0 || errno == EAGAIN || errno == EWOULDBLOCK ) { // nothing } else if ( errno == EINTR ) { done = false; } else if ( e == NoError ) { switch( errno ) { case EIO: case EISDIR: case EBADF: case EINVAL: case EFAULT: case ENOTCONN: case ENOTSOCK: e = Impossible; break;#if defined(ENONET) case ENONET:#endif case EHOSTUNREACH: case ENETDOWN: case ENETUNREACH: case ETIMEDOUT: e = NetworkFailure; break; case EPIPE: case ECONNRESET: // connection closed close(); r = 0; break; default: e = UnknownError; break; } } } return r;}/*! Writes \a len bytes to the socket from \a data and returns the number of bytes written. Returns -1 if an error occurred. This is used for Q3SocketDevice::Stream sockets.*/qint64 Q3SocketDevice::writeData( const char *data, qint64 len ){ if ( data == 0 && len != 0 ) {#if defined(QT_CHECK_NULL) || defined(QSOCKETDEVICE_DEBUG) qWarning( "Q3SocketDevice::writeBlock: Null pointer error" );#endif return -1; } if ( !isValid() ) {#if defined(QT_CHECK_STATE) || defined(QSOCKETDEVICE_DEBUG) qWarning( "Q3SocketDevice::writeBlock: Invalid socket" );#endif return -1; } if ( !isOpen() ) {#if defined(QT_CHECK_STATE) || defined(QSOCKETDEVICE_DEBUG) qWarning( "Q3SocketDevice::writeBlock: Device is not open" );#endif return -1; } if ( !isWritable() ) {#if defined(QT_CHECK_STATE) || defined(QSOCKETDEVICE_DEBUG) qWarning( "Q3SocketDevice::writeBlock: Write operation not permitted" );#endif return -1; } bool done = false; int r = 0; bool timeout; while ( !done ) { r = ::write( fd, data, len ); done = true; if ( r < 0 && e == NoError && errno != EAGAIN && errno != EWOULDBLOCK ) { switch( errno ) { case EINTR: // signal - call read() or whatever again done = false; break; case EPIPE: case ECONNRESET: // connection closed close(); r = 0; break; case ENOSPC: case EIO: case EISDIR: case EBADF: case EINVAL: case EFAULT: case ENOTCONN: case ENOTSOCK: e = Impossible; break;#if defined(ENONET) case ENONET:#endif case EHOSTUNREACH: case ENETDOWN: case ENETUNREACH: case ETIMEDOUT: e = NetworkFailure; break; default: e = UnknownError; break; } } else if ( waitForMore( 0, &timeout ) == 0 ) { if ( !timeout ) { // connection closed close(); } } } return r;}/*! \overload Writes \a len bytes to the socket from \a data and returns the number of bytes written. Returns -1 if an error occurred. This is used for Q3SocketDevice::Datagram sockets. You must specify the \a host and \a port of the destination of the data.*/Q_LONG Q3SocketDevice::writeBlock( const char * data, Q_ULONG len, const QHostAddress & host, Q_UINT16 port ){ if ( t != Datagram ) {#if defined(QT_CHECK_STATE) || defined(QSOCKETDEVICE_DEBUG) qWarning( "Q3SocketDevice::sendBlock: Not datagram" );#endif return -1; // for now - later we can do t/tcp } if ( data == 0 && len != 0 ) {#if defined(QT_CHECK_NULL) || defined(QSOCKETDEVICE_DEBUG) qWarning( "Q3SocketDevice::sendBlock: Null pointer error" );#endif return -1; } if ( !isValid() ) {#if defined(QT_CHECK_STATE) || defined(QSOCKETDEVICE_DEBUG) qWarning( "Q3SocketDevice::sendBlock: Invalid socket" );#endif return -1; } if ( !isOpen() ) {#if defined(QT_CHECK_STATE) || defined(QSOCKETDEVICE_DEBUG) qWarning( "Q3SocketDevice::sendBlock: Device is not open" );#endif return -1; } if ( !isWritable() ) {#if defined(QT_CHECK_STATE) || defined(QSOCKETDEVICE_DEBUG) qWarning( "Q3SocketDevice::sendBlock: Write operation not permitted" );#endif return -1; } struct sockaddr_in a4; struct sockaddr *aa; QT_SOCKLEN_T slen;#if !defined(QT_NO_IPV6) struct sockaddr_in6 a6; if ( host.isIPv6Address() ) { memset( &a6, 0, sizeof(a6) ); a6.sin6_family = AF_INET6; a6.sin6_port = htons( port ); Q_IPV6ADDR tmp = host.toIPv6Address(); memcpy( &a6.sin6_addr.s6_addr, &tmp, sizeof(tmp) ); slen = sizeof( a6 ); aa = (struct sockaddr *)&a6; } else#endif if ( host.isIPv4Address() ) { memset( &a4, 0, sizeof(a4) ); a4.sin_family = AF_INET; a4.sin_port = htons( port ); a4.sin_addr.s_addr = htonl( host.toIPv4Address() ); slen = sizeof(a4); aa = (struct sockaddr *)&a4; } else { e = Impossible; return -1; } // we'd use MSG_DONTWAIT + MSG_NOSIGNAL if Stevens were right. // but apparently Stevens and most implementors disagree bool done = false; int r = 0; while ( !done ) { r = ::sendto( fd, data, len, 0, aa, slen); done = true; if ( r < 0 && e == NoError && errno != EAGAIN && errno != EWOULDBLOCK ) { switch( errno ) { case EINTR: // signal - call read() or whatever again done = false; break; case ENOSPC: case EPIPE: case EIO: case EISDIR: case EBADF: case EINVAL: case EFAULT: case ENOTCONN: case ENOTSOCK: e = Impossible; break;#if defined(ENONET) case ENONET:#endif case EHOSTUNREACH: case ENETDOWN: case ENETUNREACH: case ETIMEDOUT: e = NetworkFailure; break; default: e = UnknownError; break; } } } return r;}/*! Fetches information about both ends of the connection: whatever is available.*/void Q3SocketDevice::fetchConnectionParameters(){ if ( !isValid() ) { p = 0; a = QHostAddress(); pp = 0; pa = QHostAddress(); return; }#if !defined(QT_NO_IPV6) struct sockaddr_storage sa;#else struct sockaddr_in sa;#endif memset( &sa, 0, sizeof(sa) ); QT_SOCKLEN_T sz; sz = sizeof( sa ); if ( !::getsockname( fd, (struct sockaddr *)(&sa), &sz ) ) qt_socket_getportaddr( (struct sockaddr *)&sa, &p, &a ); sz = sizeof( sa ); if ( !::getpeername( fd, (struct sockaddr *)(&sa), &sz ) ) qt_socket_getportaddr( (struct sockaddr *)&sa, &pp, &pa );}/*! Returns the port number of the port this socket device is connected to. This may be 0 for a while, but is set to something sensible as soon as a sensible value is available. Note that for Datagram sockets, this is the source port of the last packet received, and that it is in native byte order.*/Q_UINT16 Q3SocketDevice::peerPort() const{ return pp;}/*! Returns the address of the port this socket device is connected to. This may be 0.0.0.0 for a while, but is set to something sensible as soon as a sensible value is available. Note that for Datagram sockets, this is the source port of the last packet received.*/QHostAddress Q3SocketDevice::peerAddress() const{ return pa;}#endif //QT_NO_NETWORK
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -