win_fd_set_adapter.hpp 3.8 KB

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