#include "qplatformdefs.h"
#include "qatomic.h"
-#include <poll.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
return ret;
}
-static inline int qt_safe_poll(int fd, int events, int timeout)
-{
- struct pollfd pd;
- pd.fd = fd;
- pd.events = events;
- return ::poll(&pd, 1, timeout);
-}
-
timeval qt_gettime(); // in qelapsedtimer_unix.cpp
Q_CORE_EXPORT int qt_safe_select(int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept,
void QLocalServerPrivate::waitForNewConnection(int msec, bool *timedOut)
{
- int result = qt_safe_poll(listenSocket, POLLIN, (msec == -1) ? 0 : msec);
- if (result == -1) {
+ fd_set readfds;
+ FD_ZERO(&readfds);
+ FD_SET(listenSocket, &readfds);
+
+ timeval timeout;
+ timeout.tv_sec = msec / 1000;
+ timeout.tv_usec = (msec % 1000) * 1000;
+
+ int result = qt_safe_select(listenSocket + 1, &readfds, 0, 0, (msec == -1) ? 0 : &timeout);
+ if (-1 == result) {
setError(QLatin1String("QLocalServer::waitForNewConnection"));
closeServer();
}
if (state() != ConnectingState)
return (state() == ConnectedState);
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(d->connectingSocket, &fds);
+
+ timeval timeout;
+ timeout.tv_sec = msec / 1000;
+ timeout.tv_usec = (msec % 1000) * 1000;
+
// timeout can not be 0 or else select will return an error.
if (msec == 0)
- msec = 1000;
+ timeout.tv_usec = 1000;
while (state() == ConnectingState && msec == -1) {
- int result = qt_safe_poll(d->connectingSocket, POLLIN, msec);
+ int result = qt_safe_select(d->connectingSocket + 1, &fds, 0, 0, &timeout);
if (result == -1 && errno != EINTR) {
d->errorOccurred( QLocalSocket::UnknownSocketError,
QLatin1String("QLocalSocket::waitForConnected"));
int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) const
{
- return qt_safe_poll(socketDescriptor, selectForRead ? POLLIN : POLLOUT, timeout < 0 ? 0 : timeout);
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(socketDescriptor, &fds);
+
+ struct timeval tv;
+ tv.tv_sec = timeout / 1000;
+ tv.tv_usec = (timeout % 1000) * 1000;
+
+ int retval;
+ if (selectForRead)
+ retval = qt_safe_select(socketDescriptor + 1, &fds, 0, 0, timeout < 0 ? 0 : &tv);
+ else
+ retval = qt_safe_select(socketDescriptor + 1, 0, &fds, 0, timeout < 0 ? 0 : &tv);
+
+ return retval;
}
int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool checkWrite,
bool *selectForRead, bool *selectForWrite) const
{
- struct pollfd pd;
- pd.fd = socketDescriptor;
- if (checkRead) {
- pd.events |= POLLIN;
- }
- if (checkWrite) {
- pd.events |= POLLOUT;
- }
+ fd_set fdread;
+ FD_ZERO(&fdread);
+ if (checkRead)
+ FD_SET(socketDescriptor, &fdread);
- int ret = ::poll(&pd, 1, timeout < 0 ? 0 : timeout);
+ fd_set fdwrite;
+ FD_ZERO(&fdwrite);
+ if (checkWrite)
+ FD_SET(socketDescriptor, &fdwrite);
+
+ struct timeval tv;
+ tv.tv_sec = timeout / 1000;
+ tv.tv_usec = (timeout % 1000) * 1000;
+
+ int ret;
+ ret = qt_safe_select(socketDescriptor + 1, &fdread, &fdwrite, 0, timeout < 0 ? 0 : &tv);
+
+ if (ret <= 0)
+ return ret;
+ *selectForRead = FD_ISSET(socketDescriptor, &fdread);
+ *selectForWrite = FD_ISSET(socketDescriptor, &fdwrite);
- if (ret > 0) {
- *selectForRead = (pd.revents & POLLIN);
- *selectForWrite = (pd.revents & POLLOUT);
- }
return ret;
}