2 // detail/win_fd_set_adapter.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP
12 #define BOOST_ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
22 #include <boost/asio/detail/noncopyable.hpp>
23 #include <boost/asio/detail/reactor_op_queue.hpp>
24 #include <boost/asio/detail/socket_types.hpp>
26 #include <boost/asio/detail/push_options.hpp>
32 // Adapts the FD_SET type to meet the Descriptor_Set concept's requirements.
33 class win_fd_set_adapter : noncopyable
36 enum { default_fd_set_size = 1024 };
39 : capacity_(default_fd_set_size),
40 max_descriptor_(invalid_socket)
42 fd_set_ = static_cast<win_fd_set*>(::operator new(
43 sizeof(win_fd_set) - sizeof(SOCKET)
44 + sizeof(SOCKET) * (capacity_)));
45 fd_set_->fd_count = 0;
50 ::operator delete(fd_set_);
55 fd_set_->fd_count = 0;
56 max_descriptor_ = invalid_socket;
59 bool set(socket_type descriptor)
61 for (u_int i = 0; i < fd_set_->fd_count; ++i)
62 if (fd_set_->fd_array[i] == descriptor)
65 reserve(fd_set_->fd_count + 1);
66 fd_set_->fd_array[fd_set_->fd_count++] = descriptor;
70 void set(reactor_op_queue<socket_type>& operations, op_queue<operation>&)
72 reactor_op_queue<socket_type>::iterator i = operations.begin();
73 while (i != operations.end())
75 reactor_op_queue<socket_type>::iterator op_iter = i++;
76 reserve(fd_set_->fd_count + 1);
77 fd_set_->fd_array[fd_set_->fd_count++] = op_iter->first;
81 bool is_set(socket_type descriptor) const
83 return !!__WSAFDIsSet(descriptor,
84 const_cast<fd_set*>(reinterpret_cast<const fd_set*>(fd_set_)));
89 return reinterpret_cast<fd_set*>(fd_set_);
92 socket_type max_descriptor() const
94 return max_descriptor_;
97 void perform(reactor_op_queue<socket_type>& operations,
98 op_queue<operation>& ops) const
100 for (u_int i = 0; i < fd_set_->fd_count; ++i)
101 operations.perform_operations(fd_set_->fd_array[i], ops);
105 // This structure is defined to be compatible with the Windows API fd_set
106 // structure, but without being dependent on the value of FD_SETSIZE. We use
107 // the "struct hack" to allow the number of descriptors to be varied at
115 // Increase the fd_set_ capacity to at least the specified number of elements.
116 void reserve(u_int n)
121 u_int new_capacity = capacity_ + capacity_ / 2;
122 if (new_capacity < n)
125 win_fd_set* new_fd_set = static_cast<win_fd_set*>(::operator new(
126 sizeof(win_fd_set) - sizeof(SOCKET)
127 + sizeof(SOCKET) * (new_capacity)));
129 new_fd_set->fd_count = fd_set_->fd_count;
130 for (u_int i = 0; i < fd_set_->fd_count; ++i)
131 new_fd_set->fd_array[i] = fd_set_->fd_array[i];
133 ::operator delete(fd_set_);
134 fd_set_ = new_fd_set;
135 capacity_ = new_capacity;
140 socket_type max_descriptor_;
143 } // namespace detail
147 #include <boost/asio/detail/pop_options.hpp>
149 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
151 #endif // BOOST_ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP