win_fd_set_adapter.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // detail/win_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_WIN_FD_SET_ADAPTER_HPP
  11. #define ASIO_DETAIL_WIN_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) || defined(__CYGWIN__)
  17. #include "asio/detail/noncopyable.hpp"
  18. #include "asio/detail/reactor_op_queue.hpp"
  19. #include "asio/detail/socket_types.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail {
  23. // Adapts the FD_SET type to meet the Descriptor_Set concept's requirements.
  24. class win_fd_set_adapter : noncopyable
  25. {
  26. public:
  27. enum { default_fd_set_size = 1024 };
  28. win_fd_set_adapter()
  29. : capacity_(default_fd_set_size),
  30. max_descriptor_(invalid_socket)
  31. {
  32. fd_set_ = static_cast<win_fd_set*>(::operator new(
  33. sizeof(win_fd_set) - sizeof(SOCKET)
  34. + sizeof(SOCKET) * (capacity_)));
  35. fd_set_->fd_count = 0;
  36. }
  37. ~win_fd_set_adapter()
  38. {
  39. ::operator delete(fd_set_);
  40. }
  41. void reset()
  42. {
  43. fd_set_->fd_count = 0;
  44. max_descriptor_ = invalid_socket;
  45. }
  46. bool set(socket_type descriptor)
  47. {
  48. for (u_int i = 0; i < fd_set_->fd_count; ++i)
  49. if (fd_set_->fd_array[i] == descriptor)
  50. return true;
  51. reserve(fd_set_->fd_count + 1);
  52. fd_set_->fd_array[fd_set_->fd_count++] = descriptor;
  53. return true;
  54. }
  55. void set(reactor_op_queue<socket_type>& operations, op_queue<operation>&)
  56. {
  57. reactor_op_queue<socket_type>::iterator i = operations.begin();
  58. while (i != operations.end())
  59. {
  60. reactor_op_queue<socket_type>::iterator op_iter = i++;
  61. reserve(fd_set_->fd_count + 1);
  62. fd_set_->fd_array[fd_set_->fd_count++] = op_iter->first;
  63. }
  64. }
  65. bool is_set(socket_type descriptor) const
  66. {
  67. return !!__WSAFDIsSet(descriptor,
  68. const_cast<fd_set*>(reinterpret_cast<const fd_set*>(fd_set_)));
  69. }
  70. operator fd_set*()
  71. {
  72. return reinterpret_cast<fd_set*>(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. for (u_int i = 0; i < fd_set_->fd_count; ++i)
  82. operations.perform_operations(fd_set_->fd_array[i], ops);
  83. }
  84. private:
  85. // This structure is defined to be compatible with the Windows API fd_set
  86. // structure, but without being dependent on the value of FD_SETSIZE. We use
  87. // the "struct hack" to allow the number of descriptors to be varied at
  88. // runtime.
  89. struct win_fd_set
  90. {
  91. u_int fd_count;
  92. SOCKET fd_array[1];
  93. };
  94. // Increase the fd_set_ capacity to at least the specified number of elements.
  95. void reserve(u_int n)
  96. {
  97. if (n <= capacity_)
  98. return;
  99. u_int new_capacity = capacity_ + capacity_ / 2;
  100. if (new_capacity < n)
  101. new_capacity = n;
  102. win_fd_set* new_fd_set = static_cast<win_fd_set*>(::operator new(
  103. sizeof(win_fd_set) - sizeof(SOCKET)
  104. + sizeof(SOCKET) * (new_capacity)));
  105. new_fd_set->fd_count = fd_set_->fd_count;
  106. for (u_int i = 0; i < fd_set_->fd_count; ++i)
  107. new_fd_set->fd_array[i] = fd_set_->fd_array[i];
  108. ::operator delete(fd_set_);
  109. fd_set_ = new_fd_set;
  110. capacity_ = new_capacity;
  111. }
  112. win_fd_set* fd_set_;
  113. u_int capacity_;
  114. socket_type max_descriptor_;
  115. };
  116. } // namespace detail
  117. } // namespace asio
  118. #include "asio/detail/pop_options.hpp"
  119. #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  120. #endif // ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP