1 //
2 // detail/posix_fd_set_adapter.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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 
11 #ifndef BOOST_ASIO_DETAIL_POSIX_FD_SET_ADAPTER_HPP
12 #define BOOST_ASIO_DETAIL_POSIX_FD_SET_ADAPTER_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include <boost/asio/detail/config.hpp>
19 
20 #if !defined(BOOST_ASIO_WINDOWS) \
21   && !defined(__CYGWIN__) \
22   && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
23 
24 #include <cstring>
25 #include <boost/asio/detail/noncopyable.hpp>
26 #include <boost/asio/detail/reactor_op_queue.hpp>
27 #include <boost/asio/detail/socket_types.hpp>
28 
29 #include <boost/asio/detail/push_options.hpp>
30 
31 namespace boost {
32 namespace asio {
33 namespace detail {
34 
35 // Adapts the FD_SET type to meet the Descriptor_Set concept's requirements.
36 class posix_fd_set_adapter : noncopyable
37 {
38 public:
posix_fd_set_adapter()39   posix_fd_set_adapter()
40     : max_descriptor_(invalid_socket)
41   {
42     using namespace std; // Needed for memset on Solaris.
43     FD_ZERO(&fd_set_);
44   }
45 
reset()46   void reset()
47   {
48     using namespace std; // Needed for memset on Solaris.
49     FD_ZERO(&fd_set_);
50   }
51 
set(socket_type descriptor)52   bool set(socket_type descriptor)
53   {
54     if (descriptor < (socket_type)FD_SETSIZE)
55     {
56       if (max_descriptor_ == invalid_socket || descriptor > max_descriptor_)
57         max_descriptor_ = descriptor;
58       FD_SET(descriptor, &fd_set_);
59       return true;
60     }
61     return false;
62   }
63 
set(reactor_op_queue<socket_type> & operations,op_queue<operation> & ops)64   void set(reactor_op_queue<socket_type>& operations, op_queue<operation>& ops)
65   {
66     reactor_op_queue<socket_type>::iterator i = operations.begin();
67     while (i != operations.end())
68     {
69       reactor_op_queue<socket_type>::iterator op_iter = i++;
70       if (!set(op_iter->first))
71       {
72         boost::system::error_code ec(error::fd_set_failure);
73         operations.cancel_operations(op_iter, ops, ec);
74       }
75     }
76   }
77 
is_set(socket_type descriptor) const78   bool is_set(socket_type descriptor) const
79   {
80     return FD_ISSET(descriptor, &fd_set_) != 0;
81   }
82 
operator fd_set*()83   operator fd_set*()
84   {
85     return &fd_set_;
86   }
87 
max_descriptor() const88   socket_type max_descriptor() const
89   {
90     return max_descriptor_;
91   }
92 
perform(reactor_op_queue<socket_type> & operations,op_queue<operation> & ops) const93   void perform(reactor_op_queue<socket_type>& operations,
94       op_queue<operation>& ops) const
95   {
96     reactor_op_queue<socket_type>::iterator i = operations.begin();
97     while (i != operations.end())
98     {
99       reactor_op_queue<socket_type>::iterator op_iter = i++;
100       if (is_set(op_iter->first))
101         operations.perform_operations(op_iter, ops);
102     }
103   }
104 
105 private:
106   mutable fd_set fd_set_;
107   socket_type max_descriptor_;
108 };
109 
110 } // namespace detail
111 } // namespace asio
112 } // namespace boost
113 
114 #include <boost/asio/detail/pop_options.hpp>
115 
116 #endif // !defined(BOOST_ASIO_WINDOWS)
117        // && !defined(__CYGWIN__)
118        // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
119 
120 #endif // BOOST_ASIO_DETAIL_POSIX_FD_SET_ADAPTER_HPP
121