select.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef __ZMQ_SELECT_HPP_INCLUDED__
  25. #define __ZMQ_SELECT_HPP_INCLUDED__
  26. // poller.hpp decides which polling mechanism to use.
  27. #include "poller.hpp"
  28. #if defined ZMQ_IOTHREAD_POLLER_USE_SELECT
  29. #include <stddef.h>
  30. #include <vector>
  31. #include <map>
  32. #if defined ZMQ_HAVE_WINDOWS
  33. #elif defined ZMQ_HAVE_OPENVMS
  34. #include <sys/types.h>
  35. #include <sys/time.h>
  36. #else
  37. #include <sys/select.h>
  38. #endif
  39. #include "ctx.hpp"
  40. #include "fd.hpp"
  41. #include "poller_base.hpp"
  42. namespace zmq
  43. {
  44. struct i_poll_events;
  45. // Implements socket polling mechanism using POSIX.1-2001 select()
  46. // function.
  47. class select_t ZMQ_FINAL : public worker_poller_base_t
  48. {
  49. public:
  50. typedef fd_t handle_t;
  51. select_t (const thread_ctx_t &ctx_);
  52. ~select_t () ZMQ_FINAL;
  53. // "poller" concept.
  54. handle_t add_fd (fd_t fd_, zmq::i_poll_events *events_);
  55. void rm_fd (handle_t handle_);
  56. void set_pollin (handle_t handle_);
  57. void reset_pollin (handle_t handle_);
  58. void set_pollout (handle_t handle_);
  59. void reset_pollout (handle_t handle_);
  60. void stop ();
  61. static int max_fds ();
  62. private:
  63. // Main event loop.
  64. void loop () ZMQ_FINAL;
  65. // Internal state.
  66. struct fds_set_t
  67. {
  68. fds_set_t ();
  69. fds_set_t (const fds_set_t &other_);
  70. fds_set_t &operator= (const fds_set_t &other_);
  71. // Convenience method to descriptor from all sets.
  72. void remove_fd (const fd_t &fd_);
  73. fd_set read;
  74. fd_set write;
  75. fd_set error;
  76. };
  77. struct fd_entry_t
  78. {
  79. fd_t fd;
  80. zmq::i_poll_events *events;
  81. };
  82. typedef std::vector<fd_entry_t> fd_entries_t;
  83. void trigger_events (const fd_entries_t &fd_entries_,
  84. const fds_set_t &local_fds_set_,
  85. int event_count_);
  86. struct family_entry_t
  87. {
  88. family_entry_t ();
  89. fd_entries_t fd_entries;
  90. fds_set_t fds_set;
  91. bool has_retired;
  92. };
  93. void select_family_entry (family_entry_t &family_entry_,
  94. int max_fd_,
  95. bool use_timeout_,
  96. struct timeval &tv_);
  97. #if defined ZMQ_HAVE_WINDOWS
  98. typedef std::map<u_short, family_entry_t> family_entries_t;
  99. struct wsa_events_t
  100. {
  101. wsa_events_t ();
  102. ~wsa_events_t ();
  103. // read, write, error and readwrite
  104. WSAEVENT events[4];
  105. };
  106. family_entries_t _family_entries;
  107. // See loop for details.
  108. family_entries_t::iterator _current_family_entry_it;
  109. int try_retire_fd_entry (family_entries_t::iterator family_entry_it_,
  110. zmq::fd_t &handle_);
  111. static const size_t fd_family_cache_size = 8;
  112. std::pair<fd_t, u_short> _fd_family_cache[fd_family_cache_size];
  113. u_short get_fd_family (fd_t fd_);
  114. // Socket's family or AF_UNSPEC on error.
  115. static u_short determine_fd_family (fd_t fd_);
  116. #else
  117. // on non-Windows, we can treat all fds as one family
  118. family_entry_t _family_entry;
  119. fd_t _max_fd;
  120. #endif
  121. void cleanup_retired ();
  122. bool cleanup_retired (family_entry_t &family_entry_);
  123. // Checks if an fd_entry_t is retired.
  124. static bool is_retired_fd (const fd_entry_t &entry_);
  125. static fd_entries_t::iterator
  126. find_fd_entry_by_handle (fd_entries_t &fd_entries_, handle_t handle_);
  127. ZMQ_NON_COPYABLE_NOR_MOVABLE (select_t)
  128. };
  129. typedef select_t poller_t;
  130. }
  131. #endif
  132. #endif