class Connection, Network, Port: select() replaced by ppoll().
This commit is contained in:
@@ -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])
|
||||
|
||||
+18
-20
@@ -20,6 +20,7 @@
|
||||
#include "connection.h"
|
||||
#include "logger.h"
|
||||
#include <cstring>
|
||||
#include <poll.h>
|
||||
|
||||
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;
|
||||
|
||||
|
||||
+18
-20
@@ -20,6 +20,7 @@
|
||||
#include "network.h"
|
||||
#include "logger.h"
|
||||
#include "appl.h"
|
||||
#include <poll.h>
|
||||
|
||||
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;
|
||||
|
||||
+13
-7
@@ -24,6 +24,7 @@
|
||||
#include <sys/ioctl.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <poll.h>
|
||||
|
||||
|
||||
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))
|
||||
|
||||
+1
-1
@@ -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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user