posix_fd_set_adapter.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // detail/posix_fd_set_adapter.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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 ASIO_DETAIL_POSIX_FD_SET_ADAPTER_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #if !defined(ASIO_WINDOWS) \
  17. && !defined(__CYGWIN__) \
  18. && !defined(ASIO_WINDOWS_RUNTIME)
  19. #include <cstring>
  20. #include "asio/detail/noncopyable.hpp"
  21. #include "asio/detail/reactor_op_queue.hpp"
  22. #include "asio/detail/socket_types.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. namespace detail {
  26. // Adapts the FD_SET type to meet the Descriptor_Set concept's requirements.
  27. class posix_fd_set_adapter : noncopyable
  28. {
  29. public:
  30. posix_fd_set_adapter()
  31. : max_descriptor_(invalid_socket)
  32. {
  33. using namespace std; // Needed for memset on Solaris.
  34. FD_ZERO(&fd_set_);
  35. }
  36. void reset()
  37. {
  38. using namespace std; // Needed for memset on Solaris.
  39. FD_ZERO(&fd_set_);
  40. }
  41. bool set(socket_type descriptor)
  42. {
  43. if (descriptor < (socket_type)FD_SETSIZE)
  44. {
  45. if (max_descriptor_ == invalid_socket || descriptor > max_descriptor_)
  46. max_descriptor_ = descriptor;
  47. FD_SET(descriptor, &fd_set_);
  48. return true;
  49. }
  50. return false;
  51. }
  52. void set(reactor_op_queue<socket_type>& operations, op_queue<operation>& ops)
  53. {
  54. reactor_op_queue<socket_type>::iterator i = operations.begin();
  55. while (i != operations.end())
  56. {
  57. reactor_op_queue<socket_type>::iterator op_iter = i++;
  58. if (!set(op_iter->first))
  59. {
  60. asio::error_code ec(error::fd_set_failure);
  61. operations.cancel_operations(op_iter, ops, ec);
  62. }
  63. }
  64. }
  65. bool is_set(socket_type descriptor) const
  66. {
  67. return FD_ISSET(descriptor, &fd_set_) != 0;
  68. }
  69. operator fd_set*()
  70. {
  71. return &fd_set_;
  72. }
  73. socket_type max_descriptor() const
  74. {
  75. return max_descriptor_;
  76. }
  77. void perform(reactor_op_queue<socket_type>& operations,
  78. op_queue<operation>& ops) const
  79. {
  80. reactor_op_queue<socket_type>::iterator i = operations.begin();
  81. while (i != operations.end())
  82. {
  83. reactor_op_queue<socket_type>::iterator op_iter = i++;
  84. if (is_set(op_iter->first))
  85. operations.perform_operations(op_iter, ops);
  86. }
  87. }
  88. private:
  89. mutable fd_set fd_set_;
  90. socket_type max_descriptor_;
  91. };
  92. } // namespace detail
  93. } // namespace asio
  94. #include "asio/detail/pop_options.hpp"
  95. #endif // !defined(ASIO_WINDOWS)
  96. // && !defined(__CYGWIN__)
  97. // && !defined(ASIO_WINDOWS_RUNTIME)
  98. #endif // ASIO_DETAIL_POSIX_FD_SET_ADAPTER_HPP