win_fd_set_adapter.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // detail/win_fd_set_adapter.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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/socket_types.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  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. if (fd_set_->fd_count == capacity_)
  52. {
  53. u_int new_capacity = capacity_ + capacity_ / 2;
  54. win_fd_set* new_fd_set = static_cast<win_fd_set*>(::operator new(
  55. sizeof(win_fd_set) - sizeof(SOCKET)
  56. + sizeof(SOCKET) * (new_capacity)));
  57. new_fd_set->fd_count = fd_set_->fd_count;
  58. for (u_int i = 0; i < fd_set_->fd_count; ++i)
  59. new_fd_set->fd_array[i] = fd_set_->fd_array[i];
  60. ::operator delete(fd_set_);
  61. fd_set_ = new_fd_set;
  62. capacity_ = new_capacity;
  63. }
  64. fd_set_->fd_array[fd_set_->fd_count++] = descriptor;
  65. return true;
  66. }
  67. bool is_set(socket_type descriptor) const
  68. {
  69. return !!__WSAFDIsSet(descriptor,
  70. const_cast<fd_set*>(reinterpret_cast<const fd_set*>(fd_set_)));
  71. }
  72. operator fd_set*()
  73. {
  74. return reinterpret_cast<fd_set*>(fd_set_);
  75. }
  76. socket_type max_descriptor() const
  77. {
  78. return max_descriptor_;
  79. }
  80. private:
  81. // This structure is defined to be compatible with the Windows API fd_set
  82. // structure, but without being dependent on the value of FD_SETSIZE. We use
  83. // the "struct hack" to allow the number of descriptors to be varied at
  84. // runtime.
  85. struct win_fd_set
  86. {
  87. u_int fd_count;
  88. SOCKET fd_array[1];
  89. };
  90. win_fd_set* fd_set_;
  91. u_int capacity_;
  92. socket_type max_descriptor_;
  93. };
  94. } // namespace detail
  95. } // namespace asio
  96. } // namespace boost
  97. #include <boost/asio/detail/pop_options.hpp>
  98. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  99. #endif // BOOST_ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP