win_fd_set_adapter.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // win_fd_set_adapter.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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/push_options.hpp>
  16. #include <boost/asio/detail/socket_types.hpp>
  17. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  18. namespace boost {
  19. namespace asio {
  20. namespace detail {
  21. // Adapts the FD_SET type to meet the Descriptor_Set concept's requirements.
  22. class win_fd_set_adapter
  23. {
  24. public:
  25. enum { win_fd_set_size = 1024 };
  26. win_fd_set_adapter()
  27. : max_descriptor_(invalid_socket)
  28. {
  29. fd_set_.fd_count = 0;
  30. }
  31. bool set(socket_type descriptor)
  32. {
  33. for (u_int i = 0; i < fd_set_.fd_count; ++i)
  34. if (fd_set_.fd_array[i] == descriptor)
  35. return true;
  36. if (fd_set_.fd_count < win_fd_set_size)
  37. {
  38. fd_set_.fd_array[fd_set_.fd_count++] = descriptor;
  39. return true;
  40. }
  41. return false;
  42. }
  43. bool is_set(socket_type descriptor) const
  44. {
  45. return !!__WSAFDIsSet(descriptor,
  46. const_cast<fd_set*>(reinterpret_cast<const fd_set*>(&fd_set_)));
  47. }
  48. operator fd_set*()
  49. {
  50. return reinterpret_cast<fd_set*>(&fd_set_);
  51. }
  52. socket_type max_descriptor() const
  53. {
  54. return max_descriptor_;
  55. }
  56. private:
  57. // This structure is defined to be compatible with the Windows API fd_set
  58. // structure, but without being dependent on the value of FD_SETSIZE.
  59. struct win_fd_set
  60. {
  61. u_int fd_count;
  62. SOCKET fd_array[win_fd_set_size];
  63. };
  64. win_fd_set fd_set_;
  65. socket_type max_descriptor_;
  66. };
  67. } // namespace detail
  68. } // namespace asio
  69. } // namespace boost
  70. #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  71. #include <boost/asio/detail/pop_options.hpp>
  72. #endif // BOOST_ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP