src/network/socket/qnativesocketengine.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   192 
   192 
   193     socketError = error;
   193     socketError = error;
   194 
   194 
   195     switch (errorString) {
   195     switch (errorString) {
   196     case NonBlockingInitFailedErrorString:
   196     case NonBlockingInitFailedErrorString:
   197         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Unable to initialize non-blocking socket"));
   197         socketErrorString = QNativeSocketEngine::tr("Unable to initialize non-blocking socket");
   198         break;
   198         break;
   199     case BroadcastingInitFailedErrorString:
   199     case BroadcastingInitFailedErrorString:
   200         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Unable to initialize broadcast socket"));
   200         socketErrorString = QNativeSocketEngine::tr("Unable to initialize broadcast socket");
   201         break;
   201         break;
   202     case NoIpV6ErrorString:
   202     case NoIpV6ErrorString:
   203         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Attempt to use IPv6 socket on a platform with no IPv6 support"));
   203         socketErrorString = QNativeSocketEngine::tr("Attempt to use IPv6 socket on a platform with no IPv6 support");
   204         break;
   204         break;
   205     case RemoteHostClosedErrorString:
   205     case RemoteHostClosedErrorString:
   206         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "The remote host closed the connection"));
   206         socketErrorString = QNativeSocketEngine::tr("The remote host closed the connection");
   207         break;
   207         break;
   208     case TimeOutErrorString:
   208     case TimeOutErrorString:
   209         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Network operation timed out"));
   209         socketErrorString = QNativeSocketEngine::tr("Network operation timed out");
   210         break;
   210         break;
   211     case ResourceErrorString:
   211     case ResourceErrorString:
   212         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Out of resources"));
   212         socketErrorString = QNativeSocketEngine::tr("Out of resources");
   213         break;
   213         break;
   214     case OperationUnsupportedErrorString:
   214     case OperationUnsupportedErrorString:
   215         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Unsupported socket operation"));
   215         socketErrorString = QNativeSocketEngine::tr("Unsupported socket operation");
   216         break;
   216         break;
   217     case ProtocolUnsupportedErrorString:
   217     case ProtocolUnsupportedErrorString:
   218         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Protocol type not supported"));
   218         socketErrorString = QNativeSocketEngine::tr("Protocol type not supported");
   219         break;
   219         break;
   220     case InvalidSocketErrorString:
   220     case InvalidSocketErrorString:
   221         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Invalid socket descriptor"));
   221         socketErrorString = QNativeSocketEngine::tr("Invalid socket descriptor");
   222         break;
   222         break;
   223     case HostUnreachableErrorString:
   223     case HostUnreachableErrorString:
   224         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Host unreachable"));
   224         socketErrorString = QNativeSocketEngine::tr("Host unreachable");
   225         break;
   225         break;
   226     case NetworkUnreachableErrorString:
   226     case NetworkUnreachableErrorString:
   227         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Network unreachable"));
   227         socketErrorString = QNativeSocketEngine::tr("Network unreachable");
   228         break;
   228         break;
   229     case AccessErrorString:
   229     case AccessErrorString:
   230         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Permission denied"));
   230         socketErrorString = QNativeSocketEngine::tr("Permission denied");
   231         break;
   231         break;
   232     case ConnectionTimeOutErrorString:
   232     case ConnectionTimeOutErrorString:
   233         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Connection timed out"));
   233         socketErrorString = QNativeSocketEngine::tr("Connection timed out");
   234         break;
   234         break;
   235     case ConnectionRefusedErrorString:
   235     case ConnectionRefusedErrorString:
   236         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Connection refused"));
   236         socketErrorString = QNativeSocketEngine::tr("Connection refused");
   237         break;
   237         break;
   238     case AddressInuseErrorString:
   238     case AddressInuseErrorString:
   239         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "The bound address is already in use"));
   239         socketErrorString = QNativeSocketEngine::tr("The bound address is already in use");
   240         break;
   240         break;
   241     case AddressNotAvailableErrorString:
   241     case AddressNotAvailableErrorString:
   242         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "The address is not available"));
   242         socketErrorString = QNativeSocketEngine::tr("The address is not available");
   243         break;
   243         break;
   244     case AddressProtectedErrorString:
   244     case AddressProtectedErrorString:
   245         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "The address is protected"));
   245         socketErrorString = QNativeSocketEngine::tr("The address is protected");
   246         break;
   246         break;
   247     case DatagramTooLargeErrorString:
   247     case DatagramTooLargeErrorString:
   248         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Datagram was too large to send"));
   248         socketErrorString = QNativeSocketEngine::tr("Datagram was too large to send");
   249         break;
   249         break;
   250     case SendDatagramErrorString:
   250     case SendDatagramErrorString:
   251         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Unable to send a message"));
   251         socketErrorString = QNativeSocketEngine::tr("Unable to send a message");
   252         break;
   252         break;
   253     case ReceiveDatagramErrorString:
   253     case ReceiveDatagramErrorString:
   254         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Unable to receive a message"));
   254         socketErrorString = QNativeSocketEngine::tr("Unable to receive a message");
   255         break;
   255         break;
   256     case WriteErrorString:
   256     case WriteErrorString:
   257         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Unable to write"));
   257         socketErrorString = QNativeSocketEngine::tr("Unable to write");
   258         break;
   258         break;
   259     case ReadErrorString:
   259     case ReadErrorString:
   260         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Network error"));
   260         socketErrorString = QNativeSocketEngine::tr("Network error");
   261         break;
   261         break;
   262     case PortInuseErrorString:
   262     case PortInuseErrorString:
   263         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Another socket is already listening on the same port"));
   263         socketErrorString = QNativeSocketEngine::tr("Another socket is already listening on the same port");
   264         break;
   264         break;
   265     case NotSocketErrorString:
   265     case NotSocketErrorString:
   266         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Operation on non-socket"));
   266         socketErrorString = QNativeSocketEngine::tr("Operation on non-socket");
   267         break;
   267         break;
   268     case InvalidProxyTypeString:
   268     case InvalidProxyTypeString:
   269         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "The proxy type is invalid for this operation"));
   269         socketErrorString = QNativeSocketEngine::tr("The proxy type is invalid for this operation");
   270         break;
   270         break;
   271     case UnknownSocketErrorString:
   271     case UnknownSocketErrorString:
   272         socketErrorString = QLatin1String(QT_TRANSLATE_NOOP("QNativeSocketEngine", "Unknown error"));
   272         socketErrorString = QNativeSocketEngine::tr("Unknown error");
   273         break;
   273         break;
   274     }
   274     }
   275 }
   275 }
   276 
   276 
   277 bool QNativeSocketEnginePrivate::checkProxy(const QHostAddress &address)
   277 bool QNativeSocketEnginePrivate::checkProxy(const QHostAddress &address)
   752     Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::write(), -1);
   752     Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::write(), -1);
   753     Q_CHECK_STATE(QNativeSocketEngine::write(), QAbstractSocket::ConnectedState, -1);
   753     Q_CHECK_STATE(QNativeSocketEngine::write(), QAbstractSocket::ConnectedState, -1);
   754     return d->nativeWrite(data, size);
   754     return d->nativeWrite(data, size);
   755 }
   755 }
   756 
   756 
       
   757 
       
   758 qint64 QNativeSocketEngine::bytesToWrite() const
       
   759 {
       
   760     return 0;
       
   761 }
       
   762 
   757 /*!
   763 /*!
   758     Reads up to \a maxSize bytes into \a data from the socket.
   764     Reads up to \a maxSize bytes into \a data from the socket.
   759     Returns the number of bytes read, or -1 if an error occurred.
   765     Returns the number of bytes read, or -1 if an error occurred.
   760 */
   766 */
   761 qint64 QNativeSocketEngine::read(char *data, qint64 maxSize)
   767 qint64 QNativeSocketEngine::read(char *data, qint64 maxSize)
   868     is to create a QSocketNotifier, passing the socket descriptor
   874     is to create a QSocketNotifier, passing the socket descriptor
   869     returned by socketDescriptor() to its constructor.
   875     returned by socketDescriptor() to its constructor.
   870 */
   876 */
   871 bool QNativeSocketEngine::waitForWrite(int msecs, bool *timedOut)
   877 bool QNativeSocketEngine::waitForWrite(int msecs, bool *timedOut)
   872 {
   878 {
   873     Q_D(const QNativeSocketEngine);
   879     Q_D(QNativeSocketEngine);
   874     Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::waitForWrite(), false);
   880     Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::waitForWrite(), false);
   875     Q_CHECK_NOT_STATE(QNativeSocketEngine::waitForWrite(),
   881     Q_CHECK_NOT_STATE(QNativeSocketEngine::waitForWrite(),
   876                       QAbstractSocket::UnconnectedState, false);
   882                       QAbstractSocket::UnconnectedState, false);
   877 
   883 
   878     if (timedOut)
   884     if (timedOut)
   885 #if defined (Q_WS_WIN)
   891 #if defined (Q_WS_WIN)
   886     if (ret > 0) {
   892     if (ret > 0) {
   887         setState(QAbstractSocket::ConnectedState);
   893         setState(QAbstractSocket::ConnectedState);
   888         d_func()->fetchConnectionParameters();
   894         d_func()->fetchConnectionParameters();
   889         return true;
   895         return true;
       
   896     } else {
       
   897         int value = 0;
       
   898         int valueSize = sizeof(value);
       
   899         if (::getsockopt(d->socketDescriptor, SOL_SOCKET, SO_ERROR, (char *) &value, &valueSize) == 0) {
       
   900             if (value == WSAECONNREFUSED) {
       
   901                 d->setError(QAbstractSocket::ConnectionRefusedError, QNativeSocketEnginePrivate::ConnectionRefusedErrorString);
       
   902                 d->socketState = QAbstractSocket::UnconnectedState;
       
   903                 return false;
       
   904             } else if (value == WSAETIMEDOUT) {
       
   905                 d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::ConnectionTimeOutErrorString);
       
   906                 d->socketState = QAbstractSocket::UnconnectedState;
       
   907                 return false;
       
   908             } else if (value == WSAEHOSTUNREACH) {
       
   909                 d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::HostUnreachableErrorString);
       
   910                 d->socketState = QAbstractSocket::UnconnectedState;
       
   911                 return false;
       
   912             }
       
   913         }
   890     }
   914     }
   891 #endif
   915 #endif
   892 
   916 
   893     if (ret == 0) {
   917     if (ret == 0) {
   894         if (timedOut)
   918         if (timedOut)
   905 
   929 
   906 bool QNativeSocketEngine::waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
   930 bool QNativeSocketEngine::waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
   907                                       bool checkRead, bool checkWrite,
   931                                       bool checkRead, bool checkWrite,
   908                                       int msecs, bool *timedOut)
   932                                       int msecs, bool *timedOut)
   909 {
   933 {
   910     Q_D(const QNativeSocketEngine);
   934     Q_D(QNativeSocketEngine);
   911     Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::waitForWrite(), false);
   935     Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::waitForWrite(), false);
   912     Q_CHECK_NOT_STATE(QNativeSocketEngine::waitForReadOrWrite(),
   936     Q_CHECK_NOT_STATE(QNativeSocketEngine::waitForReadOrWrite(),
   913                       QAbstractSocket::UnconnectedState, false);
   937                       QAbstractSocket::UnconnectedState, false);
   914 
   938 
   915     int ret = d->nativeSelect(msecs, checkRead, checkWrite, readyToRead, readyToWrite);
   939     int ret = d->nativeSelect(msecs, checkRead, checkWrite, readyToRead, readyToWrite);
   919 #if defined (Q_WS_WIN)
   943 #if defined (Q_WS_WIN)
   920     if (checkWrite && ((readyToWrite && *readyToWrite) || !readyToWrite) && ret > 0) {
   944     if (checkWrite && ((readyToWrite && *readyToWrite) || !readyToWrite) && ret > 0) {
   921         setState(QAbstractSocket::ConnectedState);
   945         setState(QAbstractSocket::ConnectedState);
   922         d_func()->fetchConnectionParameters();
   946         d_func()->fetchConnectionParameters();
   923         return true;
   947         return true;
       
   948     } else {
       
   949         int value = 0;
       
   950         int valueSize = sizeof(value);
       
   951         if (::getsockopt(d->socketDescriptor, SOL_SOCKET, SO_ERROR, (char *) &value, &valueSize) == 0) {
       
   952             if (value == WSAECONNREFUSED) {
       
   953                 d->setError(QAbstractSocket::ConnectionRefusedError, QNativeSocketEnginePrivate::ConnectionRefusedErrorString);
       
   954                 d->socketState = QAbstractSocket::UnconnectedState;
       
   955                 return false;
       
   956             } else if (value == WSAETIMEDOUT) {
       
   957                 d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::ConnectionTimeOutErrorString);
       
   958                 d->socketState = QAbstractSocket::UnconnectedState;
       
   959                 return false;
       
   960             } else if (value == WSAEHOSTUNREACH) {
       
   961                 d->setError(QAbstractSocket::NetworkError, QNativeSocketEnginePrivate::HostUnreachableErrorString);
       
   962                 d->socketState = QAbstractSocket::UnconnectedState;
       
   963                 return false;
       
   964             }
       
   965         }
   924     }
   966     }
   925 #endif
   967 #endif
   926     if (ret == 0) {
   968     if (ret == 0) {
   927         if (timedOut)
   969         if (timedOut)
   928             *timedOut = true;
   970             *timedOut = true;