Changeset 568 for trunk/src/network/socket
- Timestamp:
- Feb 13, 2010, 1:10:15 AM (15 years ago)
- Location:
- trunk/src/network/socket
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/network/socket/qlocalserver_os2.cpp
r564 r568 91 91 92 92 // create the unix socket 93 listenSocket = qSocket(PF_UNIX, SOCK_STREAM, 0);93 listenSocket = ocket(PF_UNIX, SOCK_STREAM, 0); 94 94 if (-1 == listenSocket) { 95 95 setError(QLatin1String("QLocalServer::listen")); … … 110 110 111 111 // bind 112 if(-1 == qBind(listenSocket, (sockaddr *)&addr, sizeof(sockaddr_un))) {112 if(-1 == ind(listenSocket, (sockaddr *)&addr, sizeof(sockaddr_un))) { 113 113 setError(QLatin1String("QLocalServer::listen")); 114 114 // if address is in use already, just close the socket, but do not delete the file … … 123 123 124 124 // listen for connections 125 if (-1 == qListen(listenSocket, 50)) {125 if (-1 == isten(listenSocket, 50)) { 126 126 setError(QLatin1String("QLocalServer::listen")); 127 127 closeServer(); … … 135 135 QSocketNotifier::Read, q); 136 136 q->connect(socketNotifier, SIGNAL(activated(int)), 137 q, SLOT(_q_ socketActivated()));137 q, SLOT(_q_())); 138 138 socketNotifier->setEnabled(maxPendingConnections > 0); 139 139 return true; … … 151 151 listenSocket = -1; 152 152 153 if (socketNotifier) 153 if (socketNotifier) { 154 socketNotifier->setEnabled(false); // Otherwise, closed socket is checked before deleter runs 154 155 socketNotifier->deleteLater(); 155 socketNotifier = 0; 156 socketNotifier = 0; 157 } 156 158 157 159 if (!fullServerName.isEmpty()) … … 165 167 Accept the new socket. 166 168 */ 167 void QLocalServerPrivate::_q_ socketActivated()169 void QLocalServerPrivate::_q_() 168 170 { 169 171 Q_Q(QLocalServer); … … 172 174 173 175 ::sockaddr_un addr; 174 QT_SOCKLEN_Tlength = sizeof(sockaddr_un);175 int connectedSocket = qAccept(listenSocket, (sockaddr *)&addr, &length);176 length = sizeof(sockaddr_un); 177 int connectedSocket = ccept(listenSocket, (sockaddr *)&addr, &length); 176 178 if(-1 == connectedSocket) { 177 179 setError(QLatin1String("QLocalSocket::activated")); … … 194 196 timeout.tv_usec = (msec % 1000) * 1000; 195 197 196 // timeout can not be 0 or else select will return an error.197 if (0 == msec)198 timeout.tv_usec = 1000;199 200 198 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(); 214 206 if (timedOut) 215 207 *timedOut = (result == 0); -
trunk/src/network/socket/qlocalsocket_os2.cpp
r564 r568 235 235 236 236 // 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))) { 238 238 d->errorOccurred(UnsupportedSocketOperationError, 239 239 QLatin1String("QLocalSocket::connectToServer")); … … 285 285 ::memcpy(name.sun_path, connectingPathName.toLatin1().data(), 286 286 connectingPathName.toLatin1().size() + 1); 287 if (-1 == qConnect(connectingSocket, (struct sockaddr *)&name, sizeof(name))) {287 if (-1 == onnect(connectingSocket, (struct sockaddr *)&name, sizeof(name))) { 288 288 QString function = QLatin1String("QLocalSocket::connectToServer"); 289 289 switch (errno) … … 306 306 // Try again later, all of the sockets listening are full 307 307 if (!delayConnect) { 308 delayConnect = new QSocketNotifier(connectingSocket, QSocketNotifier::Write );308 delayConnect = new QSocketNotifier(connectingSocket, QSocketNotifier::Write); 309 309 q->connect(delayConnect, SIGNAL(activated(int)), q, SLOT(_q_connectToSocket())); 310 310 } … … 325 325 326 326 // connected! 327 if (delayConnect) { 328 delayConnect->setEnabled(false); 329 delete delayConnect; 330 delayConnect = 0; 331 } 327 cancelDelayedConnect(); 328 332 329 serverName = connectingName; 333 330 fullServerName = connectingPathName; … … 376 373 } 377 374 375 376 377 378 379 380 381 382 383 384 385 386 378 387 quintptr QLocalSocket::socketDescriptor() const 379 388 { … … 422 431 Q_D(QLocalSocket); 423 432 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(); 432 434 if (d->connectingSocket != -1) 433 435 ::close(d->connectingSocket); … … 515 517 return (state() == ConnectedState); 516 518 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); 520 522 521 523 timeval timeout; … … 533 535 while (state() == ConnectingState 534 536 && (-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); 536 538 if (-1 == result && errno != EINTR) { 537 539 d->errorOccurred( QLocalSocket::UnknownSocketError, -
trunk/src/network/socket/qnativesocketengine_os2.cpp
r564 r568 53 53 #include <fcntl.h> 54 54 #include <sys/filio.h> 55 55 56 56 57 #if defined QNATIVESOCKETENGINE_DEBUG … … 109 110 \a port and \a addr if they are non-null. 110 111 */ 111 static inline void qt_socket_getPortAndAddress(struct sockaddr *sa, quint16 *port, QHostAddress *addr) 112 { 113 struct sockaddr_in *sa4 = (struct sockaddr_in *)sa; 112 static inline void qt_socket_getPortAndAddress(const qt_sockaddr *s, quint16 *port, QHostAddress *addr) 113 { 114 114 if (port) 115 *port = ntohs(s a4->sin_port);115 *port = ntohs(ssin_port); 116 116 if (addr) { 117 117 QHostAddress tmpAddress; 118 tmpAddress.setAddress(ntohl(s a4->sin_addr.s_addr));118 tmpAddress.setAddress(ntohl(ssin_addr.s_addr)); 119 119 *addr = tmpAddress; 120 120 } … … 132 132 int protocol = AF_INET; 133 133 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); 135 135 136 136 if (socket <= 0) { … … 159 159 // Ensure that the socket is closed on exec*(). 160 160 ::fcntl(socket, F_SETFD, FD_CLOEXEC); 161 161 162 socketDescriptor = socket; 162 163 return true; … … 173 174 174 175 int n = -1; 176 177 175 178 switch (opt) { 176 179 case QNativeSocketEngine::ReceiveBufferSocketOption: … … 192 195 n = SO_OOBINLINE; 193 196 break; 197 198 199 200 201 202 203 194 204 } 195 205 196 206 int v = -1; 197 QT_SOCKOPTLEN_Tlen = sizeof(v);198 if ( getsockopt(socketDescriptor, SOL_SOCKET, n, (char *) &v, &len) != -1)207 len = sizeof(v); 208 if (, n, (char *) &v, &len) != -1) 199 209 return v; 210 200 211 return -1; 201 212 } … … 212 223 213 224 int n = 0; 225 226 214 227 switch (opt) { 215 228 case QNativeSocketEngine::ReceiveBufferSocketOption: … … 252 265 n = SO_OOBINLINE; 253 266 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; 257 277 } 258 278 259 279 bool QNativeSocketEnginePrivate::nativeConnect(const QHostAddress &addr, quint16 port) 260 280 { 281 282 283 284 261 285 struct sockaddr_in sockAddrIPv4; 262 286 struct sockaddr *sockAddrPtr = 0; 263 QT_SOCKLEN_TsockAddrSize = 0;287 sockAddrSize = 0; 264 288 265 289 if (addr.protocol() == QAbstractSocket::IPv4Protocol) { … … 275 299 } 276 300 277 int connectResult = QT_SOCKET_CONNECT(socketDescriptor, sockAddrPtr, sockAddrSize);301 int connectResult = (socketDescriptor, sockAddrPtr, sockAddrSize); 278 302 if (connectResult == -1) { 279 303 switch (errno) { … … 347 371 struct sockaddr_in sockAddrIPv4; 348 372 struct sockaddr *sockAddrPtr = 0; 349 QT_SOCKLEN_TsockAddrSize = 0;373 sockAddrSize = 0; 350 374 351 375 if (address.protocol() == QAbstractSocket::IPv4Protocol) { … … 360 384 } 361 385 362 int bindResult = QT_SOCKET_BIND(socketDescriptor, sockAddrPtr, sockAddrSize); 386 int bindResult = ::bind(socketDescriptor, sockAddrPtr, sockAddrSize); 387 363 388 if (bindResult < 0) { 364 389 switch(errno) { … … 397 422 bool QNativeSocketEnginePrivate::nativeListen(int backlog) 398 423 { 399 if ( qt_socket_listen(socketDescriptor, backlog) < 0) {424 if (listen(socketDescriptor, backlog) < 0) { 400 425 switch (errno) { 401 426 case EADDRINUSE: … … 424 449 int QNativeSocketEnginePrivate::nativeAccept() 425 450 { 426 int acceptedDescriptor = qt_socket_accept(socketDescriptor, 0, 0);451 int acceptedDescriptor = accept(socketDescriptor, 0, 0); 427 452 #if defined (QNATIVESOCKETENGINE_DEBUG) 428 453 qDebug("QNativeSocketEnginePrivate::nativeAccept() == %i", acceptedDescriptor); 429 454 #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 } 432 460 return acceptedDescriptor; 433 461 } … … 435 463 qint64 QNativeSocketEnginePrivate::nativeBytesAvailable() const 436 464 { 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; 453 466 // gives shorter than true amounts on Unix domain sockets. 454 467 qint64 available = 0; 455 456 available = (qint64) *((int *) &nbytes);468 if (::ioctl(socketDescriptor, FIONREAD, (char *) &nbytes) >= 0) 469 available = (qint64) ; 457 470 458 471 #if defined (QNATIVESOCKETENGINE_DEBUG) … … 465 478 { 466 479 // 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); 474 483 475 484 // Peek 0 bytes into the next message. The size of the message may … … 478 487 do { 479 488 char c; 480 readBytes = ::recvfrom(socketDescriptor, &c, 1, MSG_PEEK, storagePtr, &storageSize);489 readBytes = ::recvfrom(socketDescriptor, &c, 1, MSG_PEEK, , &storageSize); 481 490 } while (readBytes == -1 && errno == EINTR); 482 491 … … 496 505 QVarLengthArray<char, 8192> udpMessagePeekBuffer(8192); 497 506 ssize_t recvResult = -1; 507 498 508 for (;;) { 499 509 // the data written to udpMessagePeekBuffer is discarded, so … … 521 531 QHostAddress *address, quint16 *port) 522 532 { 523 struct sockaddr_inaa;533 aa; 524 534 memset(&aa, 0, sizeof(aa)); 525 QT_SOCKLEN_Tsz;535 sz; 526 536 sz = sizeof(aa); 527 537 … … 530 540 char c; 531 541 recvFromResult = ::recvfrom(socketDescriptor, maxSize ? data : &c, maxSize ? maxSize : 1, 532 0, (struct sockaddr *)&aa, &sz);542 0, a, &sz); 533 543 } while (recvFromResult == -1 && errno == EINTR); 534 544 … … 536 546 setError(QAbstractSocket::NetworkError, ReceiveDatagramErrorString); 537 547 } else if (port || address) { 538 qt_socket_getPortAndAddress( (struct sockaddr *)&aa, port, address);548 qt_socket_getPortAndAddress(&aa, port, address); 539 549 } 540 550 … … 554 564 struct sockaddr_in sockAddrIPv4; 555 565 struct sockaddr *sockAddrPtr = 0; 556 QT_SOCKLEN_TsockAddrSize = 0;566 sockAddrSize = 0; 557 567 558 568 if (host.protocol() == QAbstractSocket::IPv4Protocol) { 559 560 561 562 563 564 569 memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4)); 570 sockAddrIPv4.sin_family = AF_INET; 571 sockAddrIPv4.sin_port = htons(port); 572 sockAddrIPv4.sin_addr.s_addr = htonl(host.toIPv4Address()); 573 sockAddrSize = sizeof(sockAddrIPv4); 574 sockAddrPtr = (struct sockaddr *)&sockAddrIPv4; 565 575 } 566 576 567 577 // ignore the SIGPIPE signal 568 578 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); 575 581 576 582 if (sentBytes < 0) { … … 603 609 return false; 604 610 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); 608 613 609 614 // Determine local address 610 615 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); 613 618 614 619 // Determine protocol family 615 switch (s ockAddrPtr->sa_family) {620 switch (ssa_family) { 616 621 case AF_INET: 617 622 socketProtocol = QAbstractSocket::IPv4Protocol; … … 628 633 629 634 // 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); 632 637 633 638 // Determine the socket type (UDP/TCP) 634 639 int value = 0; 635 QT_SOCKOPTLEN_TvalueSize = sizeof(int);640 valueSize = sizeof(int); 636 641 if (::getsockopt(socketDescriptor, SOL_SOCKET, SO_TYPE, &value, &valueSize) == 0) { 637 642 if (value == SOCK_STREAM) … … 679 684 ssize_t writtenBytes; 680 685 do { 681 686 writtenBytes = ::write(socketDescriptor, data, len); 682 687 } while (writtenBytes < 0 && errno == EINTR); 683 688 … … 766 771 tv.tv_usec = (timeout % 1000) * 1000; 767 772 768 QTime timer;769 timer.start();770 771 773 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); 794 778 795 779 return retval; … … 797 781 798 782 int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool checkWrite, 799 783 bool *selectForRead, bool *selectForWrite) const 800 784 { 801 785 fd_set fdread; … … 813 797 tv.tv_usec = (timeout % 1000) * 1000; 814 798 799 815 800 QTime timer; 816 801 timer.start(); 817 802 818 int ret;819 803 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 821 806 if (ret != -1 || errno != EINTR) 822 807 break; … … 835 820 } 836 821 } while (true); 822 837 823 if (ret <= 0) 838 824 return ret; … … 840 826 *selectForRead = FD_ISSET(socketDescriptor, &fdread); 841 827 *selectForWrite = FD_ISSET(socketDescriptor, &fdwrite); 828 842 829 return ret; 843 830 }
Note:
See TracChangeset
for help on using the changeset viewer.