From 417586875fca4f596d7e5d485908237a6cb984b4 Mon Sep 17 00:00:00 2001 From: Louai Al-Khanji Date: Wed, 3 Feb 2016 20:12:44 -0800 Subject: QLocalSocket: Use poll instead of select on Unix Change-Id: I5399623d284ccd804bd1638da143ccdb973af9e2 Reviewed-by: Thiago Macieira --- src/network/socket/qlocalsocket_unix.cpp | 35 +++++++++++--------------------- 1 file changed, 12 insertions(+), 23 deletions(-) (limited to 'src/network') diff --git a/src/network/socket/qlocalsocket_unix.cpp b/src/network/socket/qlocalsocket_unix.cpp index 5c577512c3..c7997091a7 100644 --- a/src/network/socket/qlocalsocket_unix.cpp +++ b/src/network/socket/qlocalsocket_unix.cpp @@ -513,36 +513,25 @@ void QLocalSocket::setReadBufferSize(qint64 size) bool QLocalSocket::waitForConnected(int msec) { Q_D(QLocalSocket); + if (state() != ConnectingState) return (state() == ConnectedState); - fd_set fds; - FD_ZERO(&fds); - FD_SET(d->connectingSocket, &fds); + QElapsedTimer timer; + timer.start(); - timeval timeout; - timeout.tv_sec = msec / 1000; - timeout.tv_usec = (msec % 1000) * 1000; + pollfd pfd = qt_make_pollfd(d->connectingSocket, POLLIN); - // timeout can not be 0 or else select will return an error. - if (0 == msec) - timeout.tv_usec = 1000; + do { + const int timeout = (msec > 0) ? qMax(msec - timer.elapsed(), Q_INT64_C(0)) : msec; + const int result = qt_poll_msecs(&pfd, 1, timeout); - int result = -1; - // on Linux timeout will be updated by select, but _not_ on other systems. - QElapsedTimer timer; - timer.start(); - while (state() == ConnectingState - && (-1 == msec || timer.elapsed() < msec)) { - result = ::select(d->connectingSocket + 1, &fds, 0, 0, &timeout); - if (-1 == result && errno != EINTR) { - d->errorOccurred( QLocalSocket::UnknownSocketError, - QLatin1String("QLocalSocket::waitForConnected")); - break; - } - if (result > 0) + if (result == -1) + d->errorOccurred(QLocalSocket::UnknownSocketError, + QLatin1String("QLocalSocket::waitForConnected")); + else if (result > 0) d->_q_connectToSocket(); - } + } while (state() == ConnectingState && !timer.hasExpired(msec)); return (state() == ConnectedState); } -- cgit v1.2.3