sock_select.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #ifndef __PJ_SELECT_H__
  20. #define __PJ_SELECT_H__
  21. /**
  22. * @file sock_select.h
  23. * @brief Socket select().
  24. */
  25. #include <pj/types.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJ_SOCK_SELECT Socket select() API.
  29. * @ingroup PJ_IO
  30. * @{
  31. * This module provides portable abstraction for \a select() like API.
  32. * The abstraction is needed so that it can utilize various event
  33. * dispatching mechanisms that are available across platforms.
  34. *
  35. * The API is very similar to normal \a select() usage.
  36. *
  37. * \section pj_sock_select_examples_sec Examples
  38. *
  39. * For some examples on how to use the select API, please see:
  40. *
  41. * - Socket Select() test: \src{pjlib/src/pjlib-test/select.c}
  42. */
  43. /**
  44. * Portable structure declarations for pj_fd_set.
  45. * The implementation of pj_sock_select() does not use this structure
  46. * per-se, but instead it will use the native fd_set structure. However,
  47. * we must make sure that the size of pj_fd_set_t can accomodate the
  48. * native fd_set structure.
  49. */
  50. typedef struct pj_fd_set_t
  51. {
  52. pj_sock_t data[PJ_IOQUEUE_MAX_HANDLES+ 4]; /**< Opaque buffer for fd_set */
  53. } pj_fd_set_t;
  54. /**
  55. * Initialize the descriptor set pointed to by fdsetp to the null set.
  56. *
  57. * @param fdsetp The descriptor set.
  58. */
  59. PJ_DECL(void) PJ_FD_ZERO(pj_fd_set_t *fdsetp);
  60. /**
  61. * This is an internal function, application shouldn't use this.
  62. *
  63. * Get the number of descriptors in the set. This is defined in sock_select.c
  64. * This function will only return the number of sockets set from PJ_FD_SET
  65. * operation. When the set is modified by other means (such as by select()),
  66. * the count will not be reflected here.
  67. *
  68. * @param fdsetp The descriptor set.
  69. *
  70. * @return Number of descriptors in the set.
  71. */
  72. PJ_DECL(pj_size_t) PJ_FD_COUNT(const pj_fd_set_t *fdsetp);
  73. /**
  74. * Add the file descriptor fd to the set pointed to by fdsetp.
  75. * If the file descriptor fd is already in this set, there shall be no effect
  76. * on the set, nor will an error be returned.
  77. *
  78. * @param fd The socket descriptor.
  79. * @param fdsetp The descriptor set.
  80. */
  81. PJ_DECL(void) PJ_FD_SET(pj_sock_t fd, pj_fd_set_t *fdsetp);
  82. /**
  83. * Remove the file descriptor fd from the set pointed to by fdsetp.
  84. * If fd is not a member of this set, there shall be no effect on the set,
  85. * nor will an error be returned.
  86. *
  87. * @param fd The socket descriptor.
  88. * @param fdsetp The descriptor set.
  89. */
  90. PJ_DECL(void) PJ_FD_CLR(pj_sock_t fd, pj_fd_set_t *fdsetp);
  91. /**
  92. * Evaluate to non-zero if the file descriptor fd is a member of the set
  93. * pointed to by fdsetp, and shall evaluate to zero otherwise.
  94. *
  95. * @param fd The socket descriptor.
  96. * @param fdsetp The descriptor set.
  97. *
  98. * @return Nonzero if fd is member of the descriptor set.
  99. */
  100. PJ_DECL(pj_bool_t) PJ_FD_ISSET(pj_sock_t fd, const pj_fd_set_t *fdsetp);
  101. /**
  102. * This function wait for a number of file descriptors to change status.
  103. * The behaviour is the same as select() function call which appear in
  104. * standard BSD socket libraries.
  105. *
  106. * @param n On Unices, this specifies the highest-numbered
  107. * descriptor in any of the three set, plus 1. On Windows,
  108. * the value is ignored.
  109. * @param readfds Optional pointer to a set of sockets to be checked for
  110. * readability.
  111. * @param writefds Optional pointer to a set of sockets to be checked for
  112. * writability.
  113. * @param exceptfds Optional pointer to a set of sockets to be checked for
  114. * errors.
  115. * @param timeout Maximum time for select to wait, or null for blocking
  116. * operations.
  117. *
  118. * @return The total number of socket handles that are ready, or
  119. * zero if the time limit expired, or -1 if an error occurred.
  120. */
  121. PJ_DECL(int) pj_sock_select( int n,
  122. pj_fd_set_t *readfds,
  123. pj_fd_set_t *writefds,
  124. pj_fd_set_t *exceptfds,
  125. const pj_time_val *timeout);
  126. /**
  127. * @}
  128. */
  129. PJ_END_DECL
  130. #endif /* __PJ_SELECT_H__ */