sock_select.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  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. #if defined(PJ_HAS_STRING_H) && PJ_HAS_STRING_H!=0
  25. # include <string.h>
  26. #endif
  27. #if defined(PJ_HAS_SYS_TIME_H) && PJ_HAS_SYS_TIME_H!=0
  28. # include <sys/time.h>
  29. #endif
  30. #ifdef _MSC_VER
  31. # pragma warning(disable: 4018) // Signed/unsigned mismatch in FD_*
  32. # pragma warning(disable: 4389) // Signed/unsigned mismatch in FD_*
  33. #endif
  34. #define PART_FDSET(ps) ((fd_set*)&ps->data[1])
  35. #define PART_FDSET_OR_NULL(ps) (ps ? PART_FDSET(ps) : NULL)
  36. #define PART_COUNT(ps) (ps->data[0])
  37. PJ_DEF(void) PJ_FD_ZERO(pj_fd_set_t *fdsetp)
  38. {
  39. PJ_CHECK_STACK();
  40. pj_assert(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set));
  41. FD_ZERO(PART_FDSET(fdsetp));
  42. PART_COUNT(fdsetp) = 0;
  43. }
  44. PJ_DEF(void) PJ_FD_SET(pj_sock_t fd, pj_fd_set_t *fdsetp)
  45. {
  46. PJ_CHECK_STACK();
  47. pj_assert(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set));
  48. if (!PJ_FD_ISSET(fd, fdsetp))
  49. ++PART_COUNT(fdsetp);
  50. FD_SET(fd, PART_FDSET(fdsetp));
  51. }
  52. PJ_DEF(void) PJ_FD_CLR(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. if (PJ_FD_ISSET(fd, fdsetp))
  57. --PART_COUNT(fdsetp);
  58. FD_CLR(fd, PART_FDSET(fdsetp));
  59. }
  60. PJ_DEF(pj_bool_t) PJ_FD_ISSET(pj_sock_t fd, const pj_fd_set_t *fdsetp)
  61. {
  62. PJ_CHECK_STACK();
  63. PJ_ASSERT_RETURN(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set),
  64. 0);
  65. return FD_ISSET(fd, PART_FDSET(fdsetp));
  66. }
  67. PJ_DEF(pj_size_t) PJ_FD_COUNT(const pj_fd_set_t *fdsetp)
  68. {
  69. return PART_COUNT(fdsetp);
  70. }
  71. PJ_DEF(int) pj_sock_select( int n,
  72. pj_fd_set_t *readfds,
  73. pj_fd_set_t *writefds,
  74. pj_fd_set_t *exceptfds,
  75. const pj_time_val *timeout)
  76. {
  77. struct timeval os_timeout, *p_os_timeout;
  78. PJ_CHECK_STACK();
  79. PJ_ASSERT_RETURN(sizeof(pj_fd_set_t)-sizeof(pj_sock_t) >= sizeof(fd_set),
  80. PJ_EBUG);
  81. if (timeout) {
  82. os_timeout.tv_sec = timeout->sec;
  83. os_timeout.tv_usec = timeout->msec * 1000;
  84. p_os_timeout = &os_timeout;
  85. } else {
  86. p_os_timeout = NULL;
  87. }
  88. return select(n, PART_FDSET_OR_NULL(readfds), PART_FDSET_OR_NULL(writefds),
  89. PART_FDSET_OR_NULL(exceptfds), p_os_timeout);
  90. }