posix_fd_set_adapter.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // detail/posix_fd_set_adapter.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_POSIX_FD_SET_ADAPTER_HPP
  11. #define BOOST_ASIO_DETAIL_POSIX_FD_SET_ADAPTER_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if !defined(BOOST_ASIO_WINDOWS) \
  17. && !defined(__CYGWIN__) \
  18. && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  19. #include <cstring>
  20. #include <boost/asio/detail/noncopyable.hpp>
  21. #include <boost/asio/detail/reactor_op_queue.hpp>
  22. #include <boost/asio/detail/socket_types.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace detail {
  27. // Adapts the FD_SET type to meet the Descriptor_Set concept's requirements.
  28. class posix_fd_set_adapter : noncopyable
  29. {
  30. public:
  31. posix_fd_set_adapter()
  32. : max_descriptor_(invalid_socket)
  33. {
  34. using namespace std; // Needed for memset on Solaris.
  35. FD_ZERO(&fd_set_);
  36. }
  37. void reset()
  38. {
  39. using namespace std; // Needed for memset on Solaris.
  40. FD_ZERO(&fd_set_);
  41. }
  42. bool set(socket_type descriptor)
  43. {
  44. if (descriptor < (socket_type)FD_SETSIZE)
  45. {
  46. if (max_descriptor_ == invalid_socket || descriptor > max_descriptor_)
  47. max_descriptor_ = descriptor;
  48. FD_SET(descriptor, &fd_set_);
  49. return true;
  50. }
  51. return false;
  52. }
  53. void set(reactor_op_queue<socket_type>& operations, op_queue<operation>& ops)
  54. {
  55. reactor_op_queue<socket_type>::iterator i = operations.begin();
  56. while (i != operations.end())
  57. {
  58. reactor_op_queue<socket_type>::iterator op_iter = i++;
  59. if (!set(op_iter->first))
  60. {
  61. boost::system::error_code ec(error::fd_set_failure);
  62. operations.cancel_operations(op_iter, ops, ec);
  63. }
  64. }
  65. }
  66. bool is_set(socket_type descriptor) const
  67. {
  68. return FD_ISSET(descriptor, &fd_set_) != 0;
  69. }
  70. operator fd_set*()
  71. {
  72. return &fd_set_;
  73. }
  74. socket_type max_descriptor() const
  75. {
  76. return max_descriptor_;
  77. }
  78. void perform(reactor_op_queue<socket_type>& operations,
  79. op_queue<operation>& ops) const
  80. {
  81. reactor_op_queue<socket_type>::iterator i = operations.begin();
  82. while (i != operations.end())
  83. {
  84. reactor_op_queue<socket_type>::iterator op_iter = i++;
  85. if (is_set(op_iter->first))
  86. operations.perform_operations(op_iter, ops);
  87. }
  88. }
  89. private:
  90. mutable fd_set fd_set_;
  91. socket_type max_descriptor_;
  92. };
  93. } // namespace detail
  94. } // namespace asio
  95. } // namespace boost
  96. #include <boost/asio/detail/pop_options.hpp>
  97. #endif // !defined(BOOST_ASIO_WINDOWS)
  98. // && !defined(__CYGWIN__)
  99. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  100. #endif // BOOST_ASIO_DETAIL_POSIX_FD_SET_ADAPTER_HPP