Ignore:
Timestamp:
Feb 13, 2010, 1:10:15 AM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

network: Adopted to 4.6.1 changes.

Location:
trunk/src/network/socket
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/network/socket/qlocalserver_os2.cpp

    r564 r568  
    9191
    9292    // create the unix socket
    93     listenSocket = qSocket(PF_UNIX, SOCK_STREAM, 0);
     93    listenSocket = ocket(PF_UNIX, SOCK_STREAM, 0);
    9494    if (-1 == listenSocket) {
    9595        setError(QLatin1String("QLocalServer::listen"));
     
    110110
    111111    // bind
    112     if(-1 == qBind(listenSocket, (sockaddr *)&addr, sizeof(sockaddr_un))) {
     112    if(-1 == ind(listenSocket, (sockaddr *)&addr, sizeof(sockaddr_un))) {
    113113        setError(QLatin1String("QLocalServer::listen"));
    114114        // if address is in use already, just close the socket, but do not delete the file
     
    123123
    124124    // listen for connections
    125     if (-1 == qListen(listenSocket, 50)) {
     125    if (-1 == isten(listenSocket, 50)) {
    126126        setError(QLatin1String("QLocalServer::listen"));
    127127        closeServer();
     
    135135                                         QSocketNotifier::Read, q);
    136136    q->connect(socketNotifier, SIGNAL(activated(int)),
    137                q, SLOT(_q_socketActivated()));
     137               q, SLOT(_q_()));
    138138    socketNotifier->setEnabled(maxPendingConnections > 0);
    139139    return true;
     
    151151    listenSocket = -1;
    152152
    153     if (socketNotifier)
     153    if (socketNotifier) {
     154        socketNotifier->setEnabled(false); // Otherwise, closed socket is checked before deleter runs
    154155        socketNotifier->deleteLater();
    155     socketNotifier = 0;
     156        socketNotifier = 0;
     157    }
    156158
    157159    if (!fullServerName.isEmpty())
     
    165167    Accept the new socket.
    166168 */
    167 void QLocalServerPrivate::_q_socketActivated()
     169void QLocalServerPrivate::_q_()
    168170{
    169171    Q_Q(QLocalServer);
     
    172174
    173175    ::sockaddr_un addr;
    174     QT_SOCKLEN_T length = sizeof(sockaddr_un);
    175     int connectedSocket = qAccept(listenSocket, (sockaddr *)&addr, &length);
     176    length = sizeof(sockaddr_un);
     177    int connectedSocket = ccept(listenSocket, (sockaddr *)&addr, &length);
    176178    if(-1 == connectedSocket) {
    177179        setError(QLatin1String("QLocalSocket::activated"));
     
    194196    timeout.tv_usec = (msec % 1000) * 1000;
    195197
    196     // timeout can not be 0 or else select will return an error.
    197     if (0 == msec)
    198         timeout.tv_usec = 1000;
    199 
    200198    int result = -1;
    201     // on Linux timeout will be updated by select, but _not_ on other systems.
    202     QTime timer;
    203     timer.start();
    204     while (pendingConnections.isEmpty() && (-1 == msec || timer.elapsed() < msec)) {
    205         result = ::select(listenSocket + 1, &readfds, 0, 0, &timeout);
    206         if (-1 == result && errno != EINTR) {
    207             setError(QLatin1String("QLocalServer::waitForNewConnection"));
    208             closeServer();
    209             break;
    210         }
    211         if (result > 0)
    212             _q_socketActivated();
    213     }
     199    result = ::select(listenSocket + 1, &readfds, 0, 0, (msec == -1) ? 0 : &timeout);
     200    if (-1 == result) {
     201        setError(QLatin1String("QLocalServer::waitForNewConnection"));
     202        closeServer();
     203    }
     204    if (result > 0)
     205        _q_onNewConnection();
    214206    if (timedOut)
    215207        *timedOut = (result == 0);
  • trunk/src/network/socket/qlocalsocket_os2.cpp

    r564 r568  
    235235
    236236    // create the socket
    237     if (-1 == (d->connectingSocket = qSocket(PF_UNIX, SOCK_STREAM, 0))) {
     237    if (-1 == (d->connectingSocket = ocket(PF_UNIX, SOCK_STREAM, 0))) {
    238238        d->errorOccurred(UnsupportedSocketOperationError,
    239239                        QLatin1String("QLocalSocket::connectToServer"));
     
    285285    ::memcpy(name.sun_path, connectingPathName.toLatin1().data(),
    286286             connectingPathName.toLatin1().size() + 1);
    287     if (-1 == qConnect(connectingSocket, (struct sockaddr *)&name, sizeof(name))) {
     287    if (-1 == onnect(connectingSocket, (struct sockaddr *)&name, sizeof(name))) {
    288288        QString function = QLatin1String("QLocalSocket::connectToServer");
    289289        switch (errno)
     
    306306            // Try again later, all of the sockets listening are full
    307307            if (!delayConnect) {
    308                 delayConnect = new QSocketNotifier(connectingSocket, QSocketNotifier::Write);
     308                delayConnect = new QSocketNotifier(connectingSocket, QSocketNotifier::Write);
    309309                q->connect(delayConnect, SIGNAL(activated(int)), q, SLOT(_q_connectToSocket()));
    310310            }
     
    325325
    326326    // connected!
    327     if (delayConnect) {
    328         delayConnect->setEnabled(false);
    329         delete delayConnect;
    330         delayConnect = 0;
    331     }
     327    cancelDelayedConnect();
     328
    332329    serverName = connectingName;
    333330    fullServerName = connectingPathName;
     
    376373}
    377374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
     385
     386
    378387quintptr QLocalSocket::socketDescriptor() const
    379388{
     
    422431    Q_D(QLocalSocket);
    423432    d->unixSocket.close();
    424     if (d->delayConnect) {
    425         d->delayConnect->setEnabled(false);
    426         delete d->delayConnect;
    427         d->delayConnect = 0;
    428         d->connectTimer->stop();
    429         delete d->connectTimer;
    430         d->connectTimer = 0;
    431     }
     433    d->cancelDelayedConnect();
    432434    if (d->connectingSocket != -1)
    433435        ::close(d->connectingSocket);
     
    515517        return (state() == ConnectedState);
    516518
    517     fd_set readfds;
    518     FD_ZERO(&readfds);
    519     FD_SET(d->connectingSocket, &readfds);
     519    fd_set fds;
     520    FD_ZERO(&fds);
     521    FD_SET(d->connectingSocket, &fds);
    520522
    521523    timeval timeout;
     
    533535    while (state() == ConnectingState
    534536           && (-1 == msec || timer.elapsed() < msec)) {
    535         result = ::select(d->connectingSocket + 1, &readfds, 0, 0, &timeout);
     537        result = ::select(d->connectingSocket + 1, &fds, 0, 0, &timeout);
    536538        if (-1 == result && errno != EINTR) {
    537539            d->errorOccurred( QLocalSocket::UnknownSocketError,
  • trunk/src/network/socket/qnativesocketengine_os2.cpp

    r564 r568  
    5353#include <fcntl.h>
    5454#include <sys/filio.h>
     55
    5556
    5657#if defined QNATIVESOCKETENGINE_DEBUG
     
    109110    \a port and \a addr if they are non-null.
    110111*/
    111 static inline void qt_socket_getPortAndAddress(struct sockaddr *sa, quint16 *port, QHostAddress *addr)
    112 {
    113     struct sockaddr_in *sa4 = (struct sockaddr_in *)sa;
     112static inline void qt_socket_getPortAndAddress(const qt_sockaddr *s, quint16 *port, QHostAddress *addr)
     113{
    114114    if (port)
    115         *port = ntohs(sa4->sin_port);
     115        *port = ntohs(ssin_port);
    116116    if (addr) {
    117117        QHostAddress tmpAddress;
    118         tmpAddress.setAddress(ntohl(sa4->sin_addr.s_addr));
     118        tmpAddress.setAddress(ntohl(ssin_addr.s_addr));
    119119        *addr = tmpAddress;
    120120    }
     
    132132    int protocol = AF_INET;
    133133    int type = (socketType == QAbstractSocket::UdpSocket) ? SOCK_DGRAM : SOCK_STREAM;
    134     int socket = qt_socket_socket(protocol, type, 0);
     134    int socket = socket(protocol, type, 0);
    135135
    136136    if (socket <= 0) {
     
    159159    // Ensure that the socket is closed on exec*().
    160160    ::fcntl(socket, F_SETFD, FD_CLOEXEC);
     161
    161162    socketDescriptor = socket;
    162163    return true;
     
    173174
    174175    int n = -1;
     176
     177
    175178    switch (opt) {
    176179    case QNativeSocketEngine::ReceiveBufferSocketOption:
     
    192195        n = SO_OOBINLINE;
    193196        break;
     197
     198
     199
     200
     201
     202
     203
    194204    }
    195205
    196206    int v = -1;
    197     QT_SOCKOPTLEN_T len = sizeof(v);
    198     if (getsockopt(socketDescriptor, SOL_SOCKET, n, (char *) &v, &len) != -1)
     207    len = sizeof(v);
     208    if (, n, (char *) &v, &len) != -1)
    199209        return v;
     210
    200211    return -1;
    201212}
     
    212223
    213224    int n = 0;
     225
     226
    214227    switch (opt) {
    215228    case QNativeSocketEngine::ReceiveBufferSocketOption:
     
    252265        n = SO_OOBINLINE;
    253266        break;
    254     }
    255 
    256     return ::setsockopt(socketDescriptor, SOL_SOCKET, n, (char *) &v, sizeof(v)) == 0;
     267    case QNativeSocketEngine::LowDelayOption:
     268        level = IPPROTO_TCP;
     269        n = TCP_NODELAY;
     270        break;
     271    case QNativeSocketEngine::KeepAliveOption:
     272        n = SO_KEEPALIVE;
     273        break;
     274    }
     275
     276    return ::setsockopt(socketDescriptor, level, n, (char *) &v, sizeof(v)) == 0;
    257277}
    258278
    259279bool QNativeSocketEnginePrivate::nativeConnect(const QHostAddress &addr, quint16 port)
    260280{
     281
     282
     283
     284
    261285    struct sockaddr_in sockAddrIPv4;
    262286    struct sockaddr *sockAddrPtr = 0;
    263     QT_SOCKLEN_T sockAddrSize = 0;
     287    sockAddrSize = 0;
    264288
    265289    if (addr.protocol() == QAbstractSocket::IPv4Protocol) {
     
    275299    }
    276300
    277     int connectResult = QT_SOCKET_CONNECT(socketDescriptor, sockAddrPtr, sockAddrSize);
     301    int connectResult = (socketDescriptor, sockAddrPtr, sockAddrSize);
    278302    if (connectResult == -1) {
    279303        switch (errno) {
     
    347371    struct sockaddr_in sockAddrIPv4;
    348372    struct sockaddr *sockAddrPtr = 0;
    349     QT_SOCKLEN_T sockAddrSize = 0;
     373    sockAddrSize = 0;
    350374
    351375    if (address.protocol() == QAbstractSocket::IPv4Protocol) {
     
    360384    }
    361385
    362     int bindResult = QT_SOCKET_BIND(socketDescriptor, sockAddrPtr, sockAddrSize);
     386    int bindResult = ::bind(socketDescriptor, sockAddrPtr, sockAddrSize);
     387
    363388    if (bindResult < 0) {
    364389        switch(errno) {
     
    397422bool QNativeSocketEnginePrivate::nativeListen(int backlog)
    398423{
    399     if (qt_socket_listen(socketDescriptor, backlog) < 0) {
     424    if (listen(socketDescriptor, backlog) < 0) {
    400425        switch (errno) {
    401426        case EADDRINUSE:
     
    424449int QNativeSocketEnginePrivate::nativeAccept()
    425450{
    426     int acceptedDescriptor = qt_socket_accept(socketDescriptor, 0, 0);
     451    int acceptedDescriptor = accept(socketDescriptor, 0, 0);
    427452#if defined (QNATIVESOCKETENGINE_DEBUG)
    428453    qDebug("QNativeSocketEnginePrivate::nativeAccept() == %i", acceptedDescriptor);
    429454#endif
    430     // Ensure that the socket is closed on exec*()
    431     ::fcntl(acceptedDescriptor, F_SETFD, FD_CLOEXEC);
     455    //check if we have vaild descriptor at all
     456    if(acceptedDescriptor > 0) {
     457        // Ensure that the socket is closed on exec*()
     458        ::fcntl(acceptedDescriptor, F_SETFD, FD_CLOEXEC);
     459    }
    432460    return acceptedDescriptor;
    433461}
     
    435463qint64 QNativeSocketEnginePrivate::nativeBytesAvailable() const
    436464{
    437     /*
    438       Apparently, there is not consistency among different operating
    439       systems on how to use FIONREAD.
    440 
    441       FreeBSD, Linux and Solaris all expect the 3rd argument to
    442       ioctl() to be an int, which is normally 32-bit even on 64-bit
    443       machines.
    444 
    445       IRIX, on the other hand, expects a size_t, which is 64-bit on
    446       64-bit machines.
    447 
    448       So, the solution is to use size_t initialized to zero to make
    449       sure all bits are set to zero, preventing underflow with the
    450       FreeBSD/Linux/Solaris ioctls.
    451     */
    452     size_t nbytes = 0;
     465    int nbytes = 0;
    453466    // gives shorter than true amounts on Unix domain sockets.
    454467    qint64 available = 0;
    455     if (::ioctl(socketDescriptor, FIONREAD, (char *) &nbytes) >= 0)
    456         available = (qint64) *((int *) &nbytes);
     468if (::ioctl(socketDescriptor, FIONREAD, (char *) &nbytes) >= 0)
     469        available = (qint64) ;
    457470
    458471#if defined (QNATIVESOCKETENGINE_DEBUG)
     
    465478{
    466479    // Create a sockaddr struct and reset its port number.
    467     struct sockaddr storage;
    468     sockaddr *storagePtr = reinterpret_cast<sockaddr *>(&storage);
    469     storagePtr->sa_family = 0;
    470 
    471     sockaddr_in *storagePtrIPv4 = reinterpret_cast<sockaddr_in *>(&storage);
    472     storagePtrIPv4->sin_port = 0;
    473     QT_SOCKLEN_T storageSize = sizeof(storage);
     480    qt_sockaddr storage;
     481    int storageSize = sizeof(storage);
     482    memset(&storage, 0, storageSize);
    474483
    475484    // Peek 0 bytes into the next message. The size of the message may
     
    478487    do {
    479488        char c;
    480         readBytes = ::recvfrom(socketDescriptor, &c, 1, MSG_PEEK, storagePtr, &storageSize);
     489        readBytes = ::recvfrom(socketDescriptor, &c, 1, MSG_PEEK, , &storageSize);
    481490    } while (readBytes == -1 && errno == EINTR);
    482491
     
    496505    QVarLengthArray<char, 8192> udpMessagePeekBuffer(8192);
    497506    ssize_t recvResult = -1;
     507
    498508    for (;;) {
    499509        // the data written to udpMessagePeekBuffer is discarded, so
     
    521531                                                    QHostAddress *address, quint16 *port)
    522532{
    523     struct sockaddr_in aa;
     533    aa;
    524534    memset(&aa, 0, sizeof(aa));
    525     QT_SOCKLEN_T sz;
     535    sz;
    526536    sz = sizeof(aa);
    527537
     
    530540        char c;
    531541        recvFromResult = ::recvfrom(socketDescriptor, maxSize ? data : &c, maxSize ? maxSize : 1,
    532                                     0, (struct sockaddr *)&aa, &sz);
     542                                    0, a, &sz);
    533543    } while (recvFromResult == -1 && errno == EINTR);
    534544
     
    536546        setError(QAbstractSocket::NetworkError, ReceiveDatagramErrorString);
    537547    } else if (port || address) {
    538         qt_socket_getPortAndAddress((struct sockaddr *) &aa, port, address);
     548        qt_socket_getPortAndAddress(&aa, port, address);
    539549    }
    540550
     
    554564    struct sockaddr_in sockAddrIPv4;
    555565    struct sockaddr *sockAddrPtr = 0;
    556     QT_SOCKLEN_T sockAddrSize = 0;
     566    sockAddrSize = 0;
    557567
    558568    if (host.protocol() == QAbstractSocket::IPv4Protocol) {
    559         memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4));
    560         sockAddrIPv4.sin_family = AF_INET;
    561         sockAddrIPv4.sin_port = htons(port);
    562         sockAddrIPv4.sin_addr.s_addr = htonl(host.toIPv4Address());
    563         sockAddrSize = sizeof(sockAddrIPv4);
    564         sockAddrPtr = (struct sockaddr *)&sockAddrIPv4;
     569memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4));
     570sockAddrIPv4.sin_family = AF_INET;
     571sockAddrIPv4.sin_port = htons(port);
     572sockAddrIPv4.sin_addr.s_addr = htonl(host.toIPv4Address());
     573sockAddrSize = sizeof(sockAddrIPv4);
     574sockAddrPtr = (struct sockaddr *)&sockAddrIPv4;
    565575    }
    566576
    567577    // ignore the SIGPIPE signal
    568578    qt_ignore_sigpipe();
    569 
    570     ssize_t sentBytes;
    571     do {
    572         sentBytes = ::sendto(socketDescriptor, data, len,
    573                              0, sockAddrPtr, sockAddrSize);
    574     } while (sentBytes == -1 && errno == EINTR);
     579    ssize_t sentBytes = ::sendto(socketDescriptor, data, len,
     580                                       0, sockAddrPtr, sockAddrSize);
    575581
    576582    if (sentBytes < 0) {
     
    603609        return false;
    604610
    605     struct sockaddr_in sa;
    606     struct sockaddr *sockAddrPtr = (struct sockaddr *) &sa;
    607     QT_SOCKLEN_T sockAddrSize = sizeof(sa);
     611    qt_sockaddr sa;
     612    int sockAddrSize = sizeof(sa);
    608613
    609614    // Determine local address
    610615    memset(&sa, 0, sizeof(sa));
    611     if (::getsockname(socketDescriptor, sockAddrPtr, &sockAddrSize) == 0) {
    612         qt_socket_getPortAndAddress(sockAddrPtr, &localPort, &localAddress);
     616    if (::getsockname(socketDescriptor, , &sockAddrSize) == 0) {
     617        qt_socket_getPortAndAddress(, &localPort, &localAddress);
    613618
    614619        // Determine protocol family
    615         switch (sockAddrPtr->sa_family) {
     620        switch (ssa_family) {
    616621        case AF_INET:
    617622            socketProtocol = QAbstractSocket::IPv4Protocol;
     
    628633
    629634    // Determine the remote address
    630     if (!::getpeername(socketDescriptor, sockAddrPtr, &sockAddrSize))
    631         qt_socket_getPortAndAddress(sockAddrPtr, &peerPort, &peerAddress);
     635    if (!::getpeername(socketDescriptor, , &sockAddrSize))
     636        qt_socket_getPortAndAddress(, &peerPort, &peerAddress);
    632637
    633638    // Determine the socket type (UDP/TCP)
    634639    int value = 0;
    635     QT_SOCKOPTLEN_T valueSize = sizeof(int);
     640    valueSize = sizeof(int);
    636641    if (::getsockopt(socketDescriptor, SOL_SOCKET, SO_TYPE, &value, &valueSize) == 0) {
    637642        if (value == SOCK_STREAM)
     
    679684    ssize_t writtenBytes;
    680685    do {
    681         writtenBytes = ::write(socketDescriptor, data, len);
     686    writtenBytes = ::write(socketDescriptor, data, len);
    682687    } while (writtenBytes < 0 && errno == EINTR);
    683688
     
    766771    tv.tv_usec = (timeout % 1000) * 1000;
    767772
    768     QTime timer;
    769     timer.start();
    770 
    771773    int retval;
    772     do {
    773         if (selectForRead)
    774             retval = select(socketDescriptor + 1, &fds, 0, 0, timeout < 0 ? 0 : &tv);
    775         else
    776             retval = select(socketDescriptor + 1, 0, &fds, 0, timeout < 0 ? 0 : &tv);
    777 
    778         if (retval != -1 || errno != EINTR)
    779             break;
    780 
    781         if (timeout > 0) {
    782             // recalculate the timeout
    783             int t = timeout - timer.elapsed();
    784             if (t < 0) {
    785                 // oops, timeout turned negative?
    786                 retval = -1;
    787                 break;
    788             }
    789 
    790             tv.tv_sec = t / 1000;
    791             tv.tv_usec = (t % 1000) * 1000;
    792         }
    793     } while (true);
     774    if (selectForRead)
     775        retval = ::select(socketDescriptor + 1, &fds, 0, 0, timeout < 0 ? 0 : &tv);
     776    else
     777        retval = ::select(socketDescriptor + 1, 0, &fds, 0, timeout < 0 ? 0 : &tv);
    794778
    795779    return retval;
     
    797781
    798782int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool checkWrite,
    799                                       bool *selectForRead, bool *selectForWrite) const
     783      bool *selectForRead, bool *selectForWrite) const
    800784{
    801785    fd_set fdread;
     
    813797    tv.tv_usec = (timeout % 1000) * 1000;
    814798
     799
    815800    QTime timer;
    816801    timer.start();
    817802
    818     int ret;
    819803    do {
    820         ret = select(socketDescriptor + 1, &fdread, &fdwrite, 0, timeout < 0 ? 0 : &tv);
     804        ret = ::select(socketDescriptor + 1, &fdread, &fdwrite, 0, timeout < 0 ? 0 : &tv);
     805
    821806        if (ret != -1 || errno != EINTR)
    822807            break;
     
    835820        }
    836821    } while (true);
     822
    837823    if (ret <= 0)
    838824        return ret;
     
    840826    *selectForRead = FD_ISSET(socketDescriptor, &fdread);
    841827    *selectForWrite = FD_ISSET(socketDescriptor, &fdwrite);
     828
    842829    return ret;
    843830}
Note: See TracChangeset for help on using the changeset viewer.