You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.3 KiB
76 lines
1.3 KiB
#include <poll.h>
|
|
#include <errno.h>
|
|
|
|
#define POLLFD(ptr) ((struct pollfd *)(ptr))
|
|
#define SWAP(a, b) ((a)^=(b),(b)^=(a),(a)^=(b))
|
|
|
|
static
|
|
inline
|
|
int
|
|
sortEvents(const void * a, const void * b)
|
|
{
|
|
return POLLFD(a)->events > POLLFD(b)->events ?
|
|
-1 : POLLFD(a)->events < POLLFD(b)->events ?
|
|
1 : 0;
|
|
}
|
|
|
|
static
|
|
inline
|
|
int
|
|
sortRevents(const void * a, const void * b)
|
|
{
|
|
return POLLFD(a)->revents > POLLFD(b)->revents ?
|
|
-1 : POLLFD(a)->revents < POLLFD(b)->revents ?
|
|
1 : 0;
|
|
}
|
|
|
|
static
|
|
int
|
|
serverPoll(Server this) {
|
|
int events;
|
|
|
|
/**
|
|
* put all closed fds to end of array in O(this->nfds)
|
|
*/
|
|
struct pollfd * fda = &(this->fds[1]);
|
|
struct pollfd * fdb = &(this->fds[this->nfds-1]);
|
|
|
|
while (fda <= fdb) {
|
|
while (0 == fdb->fd && fda <= fdb) {
|
|
fdb--;
|
|
this->nfds--;
|
|
}
|
|
|
|
while (0 != fda->fd && fda <= fdb) fda++;
|
|
|
|
if (fda < fdb) {
|
|
memcpy(fda, fdb, sizeof(struct pollfd));
|
|
memset(fdb, 0, sizeof(struct pollfd));
|
|
fdb--;
|
|
this->nfds--;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* wait for handles to become ready
|
|
*/
|
|
if (-1 == (events = poll(this->fds, this->nfds, -1))) {
|
|
switch (errno) {
|
|
default:
|
|
case EBADF:
|
|
case EINVAL:
|
|
case ENOMEM:
|
|
doShutdown = 1;
|
|
/* Fallthrough */
|
|
|
|
case EINTR:
|
|
loggerLog(this->logger, LOGGER_CRIT,
|
|
"poll systemcall failed: [%s] - service terminated",
|
|
strerror(errno));
|
|
}
|
|
}
|
|
|
|
return events;
|
|
}
|
|
|
|
// vim: set ts=4 sw=4:
|