123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- #include "asterisk.h"
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/time.h>
- #include <assert.h>
- #include <string.h>
- #include <errno.h>
- #include "asterisk/utils.h"
- #include "asterisk/poll-compat.h"
- unsigned int ast_FD_SETSIZE = FD_SETSIZE;
- #ifndef MAX
- #define MAX(a,b) a > b ? a : b
- #endif
- #if defined(AST_POLL_COMPAT)
- static int map_poll_spec(struct pollfd *pArray, unsigned long n_fds,
- ast_fdset *pReadSet, ast_fdset *pWriteSet, ast_fdset *pExceptSet)
- {
- register unsigned long i;
- register struct pollfd *pCur;
- register int max_fd = -1;
-
- for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
-
- if (pCur->fd < 0) {
- continue;
- }
- if (pCur->events & POLLIN) {
-
- FD_SET(pCur->fd, pReadSet);
- }
- if (pCur->events & POLLOUT) {
-
- FD_SET(pCur->fd, pWriteSet);
- }
- if (pCur->events & POLLPRI) {
-
- FD_SET(pCur->fd, pExceptSet);
- }
- max_fd = MAX(max_fd, pCur->fd);
- }
- return max_fd;
- }
- #ifdef AST_POLL_COMPAT
- static struct timeval *map_timeout(int poll_timeout, struct timeval *pSelTimeout)
- {
- struct timeval *pResult;
-
- assert(pSelTimeout != NULL);
- switch (poll_timeout) {
- case -1:
-
- pResult = (struct timeval *) NULL;
- break;
- case 0:
-
- pSelTimeout->tv_sec = 0;
- pSelTimeout->tv_usec = 0;
- pResult = pSelTimeout;
- break;
- default:
-
- pSelTimeout->tv_sec = poll_timeout / 1000;
- poll_timeout %= 1000;
- pSelTimeout->tv_usec = poll_timeout * 1000;
- pResult = pSelTimeout;
- break;
- }
- return pResult;
- }
- #endif
- static void map_select_results(struct pollfd *pArray, unsigned long n_fds,
- ast_fdset *pReadSet, ast_fdset *pWriteSet, ast_fdset *pExceptSet)
- {
- register unsigned long i;
- register struct pollfd *pCur;
- for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) {
-
- if (pCur->fd < 0) {
- continue;
- }
-
- pCur->revents = 0;
- if (FD_ISSET(pCur->fd, (fd_set *) pExceptSet)) {
- pCur->revents |= POLLPRI;
- } else if (FD_ISSET(pCur->fd, (fd_set *) pReadSet)) {
- pCur->revents |= POLLIN;
- }
- if (FD_ISSET(pCur->fd, (fd_set *) pWriteSet)) {
- pCur->revents |= POLLOUT;
- }
- }
- return;
- }
- #endif
- #ifdef AST_POLL_COMPAT
- int ast_internal_poll(struct pollfd *pArray, unsigned long n_fds, int timeout)
- {
- ast_fdset read_descs;
- ast_fdset write_descs;
- ast_fdset except_descs;
- struct timeval stime;
- int ready_descriptors;
- int max_fd = 0;
- struct timeval *pTimeout;
- int save_errno;
- FD_ZERO(&read_descs);
- FD_ZERO(&write_descs);
- FD_ZERO(&except_descs);
-
- if (pArray) {
- max_fd = map_poll_spec (pArray, n_fds,
- &read_descs, &write_descs, &except_descs);
- }
-
- pTimeout = map_timeout (timeout, &stime);
-
- ready_descriptors = ast_select(max_fd + 1, &read_descs, &write_descs,
- &except_descs, pTimeout);
- save_errno = errno;
- if (ready_descriptors >= 0) {
- map_select_results (pArray, n_fds,
- &read_descs, &write_descs, &except_descs);
- }
- errno = save_errno;
- return ready_descriptors;
- }
- #endif
- int ast_poll2(struct pollfd *pArray, unsigned long n_fds, struct timeval *tv)
- {
- #if !defined(AST_POLL_COMPAT)
- struct timeval start = ast_tvnow();
- #if defined(HAVE_PPOLL)
- struct timespec ts = { tv ? tv->tv_sec : 0, tv ? tv->tv_usec * 1000 : 0 };
- int res = ppoll(pArray, n_fds, tv ? &ts : NULL, NULL);
- #else
- int res = poll(pArray, n_fds, tv ? tv->tv_sec * 1000 + tv->tv_usec / 1000 : -1);
- #endif
- struct timeval after = ast_tvnow();
- if (res > 0 && tv && ast_tvdiff_ms(ast_tvadd(*tv, start), after) > 0) {
- *tv = ast_tvsub(*tv, ast_tvsub(after, start));
- } else if (res > 0 && tv) {
- *tv = ast_tv(0, 0);
- }
- return res;
- #else
- ast_fdset read_descs, write_descs, except_descs;
- int ready_descriptors, max_fd = 0;
- FD_ZERO(&read_descs);
- FD_ZERO(&write_descs);
- FD_ZERO(&except_descs);
- if (pArray) {
- max_fd = map_poll_spec(pArray, n_fds, &read_descs, &write_descs, &except_descs);
- }
- ready_descriptors = ast_select(max_fd + 1, &read_descs, &write_descs, &except_descs, tv);
- if (ready_descriptors >= 0) {
- map_select_results(pArray, n_fds, &read_descs, &write_descs, &except_descs);
- }
- return ready_descriptors;
- #endif
- }
|