| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pj/sock_select.h> |
| 20 |
#include <pj/compat/socket.h> |
| 21 |
#include <pj/os.h> |
| 22 |
#include <pj/assert.h> |
| 23 |
#include <pj/errno.h> |
| 24 |
|
| 25 |
#if defined(PJ_HAS_STRING_H) && PJ_HAS_STRING_H!=0 |
| 26 |
# include <string.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#if defined(PJ_HAS_SYS_TIME_H) && PJ_HAS_SYS_TIME_H!=0 |
| 30 |
# include <sys/time.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifdef _MSC_VER |
| 34 |
# pragma warning(disable: 4018) // Signed/unsigned mismatch in FD_* |
| 35 |
# pragma warning(disable: 4389) // Signed/unsigned mismatch in FD_* |
| 36 |
#endif |
| 37 |
|
| 38 |
#define PART_FDSET(ps) ((fd_set*)&ps->data[1]) |
| 39 |
#define PART_FDSET_OR_NULL(ps) (ps ? PART_FDSET(ps) : NULL) |
| 40 |
#define PART_COUNT(ps) (ps->data[0]) |
| 41 |
|
| 42 |
PJ_DEF(void) PJ_FD_ZERO(pj_fd_set_t *fdsetp) |
| 43 |
{ |
| 44 |
PJ_CHECK_STACK(); |
| 45 |
pj_assert(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set)); |
| 46 |
|
| 47 |
FD_ZERO(PART_FDSET(fdsetp)); |
| 48 |
PART_COUNT(fdsetp) = 0; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
PJ_DEF(void) PJ_FD_SET(pj_sock_t fd, pj_fd_set_t *fdsetp) |
| 53 |
{ |
| 54 |
PJ_CHECK_STACK(); |
| 55 |
pj_assert(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set)); |
| 56 |
|
| 57 |
if (!PJ_FD_ISSET(fd, fdsetp)) |
| 58 |
++PART_COUNT(fdsetp); |
| 59 |
FD_SET(fd, PART_FDSET(fdsetp)); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
PJ_DEF(void) PJ_FD_CLR(pj_sock_t fd, pj_fd_set_t *fdsetp) |
| 64 |
{ |
| 65 |
PJ_CHECK_STACK(); |
| 66 |
pj_assert(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set)); |
| 67 |
|
| 68 |
if (PJ_FD_ISSET(fd, fdsetp)) |
| 69 |
--PART_COUNT(fdsetp); |
| 70 |
FD_CLR(fd, PART_FDSET(fdsetp)); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
PJ_DEF(pj_bool_t) PJ_FD_ISSET(pj_sock_t fd, const pj_fd_set_t *fdsetp) |
| 75 |
{ |
| 76 |
PJ_CHECK_STACK(); |
| 77 |
PJ_ASSERT_RETURN(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set), |
| 78 |
0); |
| 79 |
|
| 80 |
return FD_ISSET(fd, PART_FDSET(fdsetp)); |
| 81 |
} |
| 82 |
|
| 83 |
PJ_DEF(pj_size_t) PJ_FD_COUNT(const pj_fd_set_t *fdsetp) |
| 84 |
{ |
| 85 |
return PART_COUNT(fdsetp); |
| 86 |
} |
| 87 |
|
| 88 |
PJ_DEF(int) pj_sock_select( int n, |
| 89 |
pj_fd_set_t *readfds, |
| 90 |
pj_fd_set_t *writefds, |
| 91 |
pj_fd_set_t *exceptfds, |
| 92 |
const pj_time_val *timeout) |
| 93 |
{ |
| 94 |
struct timeval os_timeout, *p_os_timeout; |
| 95 |
|
| 96 |
PJ_CHECK_STACK(); |
| 97 |
|
| 98 |
PJ_ASSERT_RETURN(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set), |
| 99 |
PJ_EBUG); |
| 100 |
|
| 101 |
if (timeout) { |
| 102 |
os_timeout.tv_sec = timeout->sec; |
| 103 |
os_timeout.tv_usec = timeout->msec * 1000; |
| 104 |
p_os_timeout = &os_timeout; |
| 105 |
} else { |
| 106 |
p_os_timeout = NULL; |
| 107 |
} |
| 108 |
|
| 109 |
return select(n, PART_FDSET_OR_NULL(readfds), PART_FDSET_OR_NULL(writefds), |
| 110 |
PART_FDSET_OR_NULL(exceptfds), p_os_timeout); |
| 111 |
} |
| 112 |
|