From baf60895efc1d5c9a9bbacd67e518ea90a9e88d2 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Thu, 13 Nov 2014 16:31:32 +0100 Subject: [PATCH] class Connection, Network, Port: select() replaced by ppoll(). --- configure.ac | 1 + src/ebusd/connection.cpp | 38 ++++++++++++++++++-------------------- src/ebusd/network.cpp | 38 ++++++++++++++++++-------------------- src/lib/ebus/port.cpp | 20 +++++++++++++------- src/lib/ebus/port.h | 2 +- 5 files changed, 51 insertions(+), 48 deletions(-) diff --git a/configure.ac b/configure.ac index ecdd93d3..a5cebf73 100644 --- a/configure.ac +++ b/configure.ac @@ -7,6 +7,7 @@ AC_CHECK_HEADERS([arpa/inet.h \ dirent.h \ fcntl.h \ netdb.h \ + poll.h \ pthread.h \ sys/ioctl.h \ termios.h]) diff --git a/src/ebusd/connection.cpp b/src/ebusd/connection.cpp index f0d6b2d2..b82011cf 100644 --- a/src/ebusd/connection.cpp +++ b/src/ebusd/connection.cpp @@ -20,6 +20,7 @@ #include "connection.h" #include "logger.h" #include +#include extern LogInstance& L; @@ -35,39 +36,36 @@ void* Connection::run() { m_running = true; - int maxfd; - fd_set checkfds; - struct timeval timeout; + int ret, nfds = 2; + struct pollfd fds[nfds]; + struct timespec tdiff; - FD_ZERO(&checkfds); - FD_SET(m_notify.notifyFD(), &checkfds); - FD_SET(m_socket->getFD(), &checkfds); + // set select timeout 10 secs + tdiff.tv_sec = 10; + tdiff.tv_nsec = 0; - (m_notify.notifyFD() > m_socket->getFD()) ? - (maxfd = m_notify.notifyFD()) : (maxfd = m_socket->getFD()); + memset(fds, 0, sizeof(fds)); + + fds[0].fd = m_notify.notifyFD(); + fds[0].events = POLLIN; + + fds[1].fd = m_socket->getFD(); + fds[1].events = POLLIN; for (;;) { - fd_set readfds; - int ret; + // wait for new fd event + ret = ppoll(fds, nfds, &tdiff, NULL); - // set select timeout 10 secs - timeout.tv_sec = 10; - timeout.tv_usec = 0; - - // set readfds to inital checkfds - readfds = checkfds; - - ret = select(maxfd + 1, &readfds, NULL, NULL, &timeout); if (ret == 0) { continue; } // new data from notify - if (FD_ISSET(m_notify.notifyFD(), &readfds)) + if (fds[0].revents & POLLIN) break; // new data from socket - if (FD_ISSET(m_socket->getFD(), &readfds)) { + if (fds[1].revents & POLLIN) { char data[256]; size_t datalen; diff --git a/src/ebusd/network.cpp b/src/ebusd/network.cpp index 168ddd2e..f8448d4d 100644 --- a/src/ebusd/network.cpp +++ b/src/ebusd/network.cpp @@ -20,6 +20,7 @@ #include "network.h" #include "logger.h" #include "appl.h" +#include extern LogInstance& L; extern Appl& A; @@ -61,42 +62,39 @@ void* Network::run() m_running = true; - int maxfd; - fd_set checkfds; - struct timeval timeout; + int ret, nfds = 2; + struct pollfd fds[nfds]; + struct timespec tdiff; - FD_ZERO(&checkfds); - FD_SET(m_notify.notifyFD(), &checkfds); - FD_SET(m_tcpServer->getFD(), &checkfds); + // set select timeout 1 secs + tdiff.tv_sec = 1; + tdiff.tv_nsec = 0; - (m_notify.notifyFD() > m_tcpServer->getFD()) ? - (maxfd = m_notify.notifyFD()) : (maxfd = m_tcpServer->getFD()); + memset(fds, 0, sizeof(fds)); + + fds[0].fd = m_notify.notifyFD(); + fds[0].events = POLLIN; + + fds[1].fd = m_tcpServer->getFD(); + fds[1].events = POLLIN; for (;;) { - fd_set readfds; - int ret; + // wait for new fd event + ret = ppoll(fds, nfds, &tdiff, NULL); - // set select timeout 1 secs - timeout.tv_sec = 1; - timeout.tv_usec = 0; - - // set readfds to inital checkfds - readfds = checkfds; - - ret = select(maxfd + 1, &readfds, NULL, NULL, &timeout); if (ret == 0) { cleanConnections(); continue; } // new data from notify - if (FD_ISSET(m_notify.notifyFD(), &readfds)) { + if (fds[0].revents & POLLIN) { m_running = false; break; } // new data from socket - if (FD_ISSET(m_tcpServer->getFD(), &readfds)) { + if (fds[1].revents & POLLIN) { TCPSocket* socket = m_tcpServer->newSocket(); if (socket == NULL) continue; diff --git a/src/lib/ebus/port.cpp b/src/lib/ebus/port.cpp index b0d2c6c2..132ee039 100644 --- a/src/lib/ebus/port.cpp +++ b/src/lib/ebus/port.cpp @@ -24,6 +24,7 @@ #include #include #include +#include namespace libebus @@ -68,18 +69,23 @@ ssize_t Device::recvBytes(const long timeout, size_t maxCount) return -1; // TODO RESULT_ERR_DEVICE if (timeout > 0) { - fd_set readfds; - struct timeval tdiff; + int ret, nfds = 1; + struct pollfd fds[nfds]; + struct timespec tdiff; // set select timeout tdiff.tv_sec = 0; - tdiff.tv_usec = timeout; + tdiff.tv_nsec = timeout*1000; - FD_ZERO(&readfds); - FD_SET(m_fd, &readfds); + memset(fds, 0, sizeof(fds)); - if (select(m_fd + 1, &readfds, NULL, NULL, &tdiff) != 1) - return -2; // TODO RESULT_ERR_TIMEOUT + fds[0].fd = m_fd; + fds[0].events = POLLIN; + + ret = ppoll(fds, nfds, &tdiff, NULL); + + if (ret == -1) return -1; // TODO RESULT_ERR_DEVICE + if (ret == 0) return -2; // TODO RESULT_ERR_TIMEOUT } if (maxCount > sizeof(m_buffer)) diff --git a/src/lib/ebus/port.h b/src/lib/ebus/port.h index 7dc8e53a..83badc92 100644 --- a/src/lib/ebus/port.h +++ b/src/lib/ebus/port.h @@ -83,7 +83,7 @@ public: /** * @brief recvBytes read bytes from opened file descriptor. - * @param timeout max time out for new input data. + * @param timeoutmax time for new input data [usec]. * @param maxCount max size of receive buffer. * @return number of read bytes or -1 if an error has occured. */