socket_poller.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_SOCKET_POLLER_HPP_INCLUDED__
  25. #define __ZMQ_SOCKET_POLLER_HPP_INCLUDED__
  26. #include "poller.hpp"
  27. #if defined ZMQ_POLL_BASED_ON_POLL && !defined ZMQ_HAVE_WINDOWS
  28. #include <poll.h>
  29. #endif
  30. #if defined ZMQ_HAVE_WINDOWS
  31. #include "windows.hpp"
  32. #elif defined ZMQ_HAVE_VXWORKS
  33. #include <unistd.h>
  34. #include <sys/time.h>
  35. #include <strings.h>
  36. #else
  37. #include <unistd.h>
  38. #endif
  39. #include <vector>
  40. #include "socket_base.hpp"
  41. #include "signaler.hpp"
  42. #include "polling_util.hpp"
  43. namespace zmq
  44. {
  45. class socket_poller_t
  46. {
  47. public:
  48. socket_poller_t ();
  49. ~socket_poller_t ();
  50. typedef zmq_poller_event_t event_t;
  51. int add (socket_base_t *socket_, void *user_data_, short events_);
  52. int modify (const socket_base_t *socket_, short events_);
  53. int remove (socket_base_t *socket_);
  54. int add_fd (fd_t fd_, void *user_data_, short events_);
  55. int modify_fd (fd_t fd_, short events_);
  56. int remove_fd (fd_t fd_);
  57. // Returns the signaler's fd if there is one, otherwise errors.
  58. int signaler_fd (fd_t *fd_) const;
  59. int wait (event_t *events_, int n_events_, long timeout_);
  60. int size () const { return static_cast<int> (_items.size ()); };
  61. // Return false if object is not a socket.
  62. bool check_tag () const;
  63. private:
  64. typedef struct item_t
  65. {
  66. socket_base_t *socket;
  67. fd_t fd;
  68. void *user_data;
  69. short events;
  70. #if defined ZMQ_POLL_BASED_ON_POLL
  71. int pollfd_index;
  72. #endif
  73. } item_t;
  74. static void zero_trail_events (zmq::socket_poller_t::event_t *events_,
  75. int n_events_,
  76. int found_);
  77. #if defined ZMQ_POLL_BASED_ON_POLL
  78. int check_events (zmq::socket_poller_t::event_t *events_, int n_events_);
  79. #elif defined ZMQ_POLL_BASED_ON_SELECT
  80. int check_events (zmq::socket_poller_t::event_t *events_,
  81. int n_events_,
  82. fd_set &inset_,
  83. fd_set &outset_,
  84. fd_set &errset_);
  85. #endif
  86. static int adjust_timeout (zmq::clock_t &clock_,
  87. long timeout_,
  88. uint64_t &now_,
  89. uint64_t &end_,
  90. bool &first_pass_);
  91. static bool is_socket (const item_t &item, const socket_base_t *socket_)
  92. {
  93. return item.socket == socket_;
  94. }
  95. static bool is_fd (const item_t &item, fd_t fd_)
  96. {
  97. return !item.socket && item.fd == fd_;
  98. }
  99. int rebuild ();
  100. // Used to check whether the object is a socket_poller.
  101. uint32_t _tag;
  102. // Signaler used for thread safe sockets polling
  103. signaler_t *_signaler;
  104. // List of sockets
  105. typedef std::vector<item_t> items_t;
  106. items_t _items;
  107. // Does the pollset needs rebuilding?
  108. bool _need_rebuild;
  109. // Should the signaler be used for the thread safe polling?
  110. bool _use_signaler;
  111. // Size of the pollset
  112. int _pollset_size;
  113. #if defined ZMQ_POLL_BASED_ON_POLL
  114. pollfd *_pollfds;
  115. #elif defined ZMQ_POLL_BASED_ON_SELECT
  116. resizable_optimized_fd_set_t _pollset_in;
  117. resizable_optimized_fd_set_t _pollset_out;
  118. resizable_optimized_fd_set_t _pollset_err;
  119. zmq::fd_t _max_fd;
  120. #endif
  121. ZMQ_NON_COPYABLE_NOR_MOVABLE (socket_poller_t)
  122. };
  123. }
  124. #endif