use poll() instead of select()

pull/36/head
Steffen Vogel 8 years ago
parent 20f654bbc3
commit 967e6ef5b3

@ -22,7 +22,7 @@
*/ */
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/select.h> #include <sys/poll.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
#include <net/if.h> #include <net/if.h>
@ -48,8 +48,8 @@ struct sEthernetSocket {
}; };
struct sEthernetHandleSet { struct sEthernetHandleSet {
fd_set handles; struct pollfd *handles;
int maxHandle; int nhandles;
}; };
EthernetHandleSet EthernetHandleSet
@ -58,20 +58,22 @@ EthernetHandleSet_new(void)
EthernetHandleSet result = (EthernetHandleSet) GLOBAL_MALLOC(sizeof(struct sEthernetHandleSet)); EthernetHandleSet result = (EthernetHandleSet) GLOBAL_MALLOC(sizeof(struct sEthernetHandleSet));
if (result != NULL) { if (result != NULL) {
FD_ZERO(&result->handles); result->handles = NULL;
result->maxHandle = -1; result->nhandles = 0;
} }
return result; return result;
} }
void void
EthernetHandleSet_addSocket(EthernetHandleSet self, const EthernetSocket sock) EthernetHandleSet_addSocket(EthernetHandleSet self, const EthernetSocket sock)
{ {
if (self != NULL && sock != NULL && sock->bpf != -1) { if (self != NULL && sock != NULL) {
FD_SET(sock->bpf, &self->handles); int i = self->nhandles++;
if (sock->bpf > self->maxHandle) { self->handles = realloc(self->handles, self->nhandles * sizeof(struct pollfd));
self->maxHandle = sock->bpf;
} self->handles[i].fd = sock->bpf;
self->handles[i].events = POLLIN;
} }
} }
@ -80,14 +82,11 @@ EthernetHandleSet_waitReady(EthernetHandleSet self, unsigned int timeoutMs)
{ {
int result; int result;
if ((self != NULL) && (self->maxHandle >= 0)) { if ((self != NULL) && (self->nhandles >= 0)) {
struct timeval timeout; result = poll(self->handles, self->nhandles, timeoutMs);
}
timeout.tv_sec = timeoutMs / 1000; else {
timeout.tv_usec = (timeoutMs % 1000) * 1000; result = -1;
result = select(self->maxHandle + 1, &self->handles, NULL, NULL, &timeout);
} else {
result = -1;
} }
return result; return result;
@ -96,9 +95,11 @@ EthernetHandleSet_waitReady(EthernetHandleSet self, unsigned int timeoutMs)
void void
EthernetHandleSet_destroy(EthernetHandleSet self) EthernetHandleSet_destroy(EthernetHandleSet self)
{ {
if (self->nhandles)
free(self->handles);
GLOBAL_FREEMEM(self); GLOBAL_FREEMEM(self);
} }
int int
activateBpdFilter(EthernetSocket self) activateBpdFilter(EthernetSocket self)
{ {

@ -23,7 +23,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/select.h> #include <sys/poll.h>
#include <linux/if_packet.h> #include <linux/if_packet.h>
#include <linux/if_ether.h> #include <linux/if_ether.h>
#include <linux/if_arp.h> #include <linux/if_arp.h>
@ -42,8 +42,8 @@ struct sEthernetSocket {
}; };
struct sEthernetHandleSet { struct sEthernetHandleSet {
fd_set handles; struct pollfd *handles;
int maxHandle; int nhandles;
}; };
EthernetHandleSet EthernetHandleSet
@ -52,8 +52,8 @@ EthernetHandleSet_new(void)
EthernetHandleSet result = (EthernetHandleSet) GLOBAL_MALLOC(sizeof(struct sEthernetHandleSet)); EthernetHandleSet result = (EthernetHandleSet) GLOBAL_MALLOC(sizeof(struct sEthernetHandleSet));
if (result != NULL) { if (result != NULL) {
FD_ZERO(&result->handles); result->handles = NULL;
result->maxHandle = -1; result->nhandles = 0;
} }
return result; return result;
@ -62,11 +62,12 @@ EthernetHandleSet_new(void)
void void
EthernetHandleSet_addSocket(EthernetHandleSet self, const EthernetSocket sock) EthernetHandleSet_addSocket(EthernetHandleSet self, const EthernetSocket sock)
{ {
if (self != NULL && sock != NULL && sock->rawSocket != -1) { if (self != NULL && sock != NULL) {
FD_SET(sock->rawSocket, &self->handles); int i = self->nhandles++;
if (sock->rawSocket > self->maxHandle) { self->handles = realloc(self->handles, self->nhandles * sizeof(struct pollfd));
self->maxHandle = sock->rawSocket;
} self->handles[i].fd = sock->rawSocket;
self->handles[i].events = POLLIN;
} }
} }
@ -75,12 +76,8 @@ EthernetHandleSet_waitReady(EthernetHandleSet self, unsigned int timeoutMs)
{ {
int result; int result;
if ((self != NULL) && (self->maxHandle >= 0)) { if ((self != NULL) && (self->nhandles >= 0)) {
struct timeval timeout; result = poll(self->handles, self->nhandles, timeoutMs);
timeout.tv_sec = timeoutMs / 1000;
timeout.tv_usec = (timeoutMs % 1000) * 1000;
result = select(self->maxHandle + 1, &self->handles, NULL, NULL, &timeout);
} }
else { else {
result = -1; result = -1;
@ -92,6 +89,9 @@ EthernetHandleSet_waitReady(EthernetHandleSet self, unsigned int timeoutMs)
void void
EthernetHandleSet_destroy(EthernetHandleSet self) EthernetHandleSet_destroy(EthernetHandleSet self)
{ {
if (self->nhandles)
free(self->handles);
GLOBAL_FREEMEM(self); GLOBAL_FREEMEM(self);
} }

Loading…
Cancel
Save