Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:45

0001 //
0002 // detail/win_fd_set_adapter.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP
0012 #define BOOST_ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
0021 
0022 #include <boost/asio/detail/noncopyable.hpp>
0023 #include <boost/asio/detail/reactor_op_queue.hpp>
0024 #include <boost/asio/detail/socket_types.hpp>
0025 
0026 #include <boost/asio/detail/push_options.hpp>
0027 
0028 namespace boost {
0029 namespace asio {
0030 namespace detail {
0031 
0032 // Adapts the FD_SET type to meet the Descriptor_Set concept's requirements.
0033 class win_fd_set_adapter : noncopyable
0034 {
0035 public:
0036   enum { default_fd_set_size = 1024 };
0037 
0038   win_fd_set_adapter()
0039     : capacity_(default_fd_set_size),
0040       max_descriptor_(invalid_socket)
0041   {
0042     fd_set_ = static_cast<win_fd_set*>(::operator new(
0043           sizeof(win_fd_set) - sizeof(SOCKET)
0044           + sizeof(SOCKET) * (capacity_)));
0045     fd_set_->fd_count = 0;
0046   }
0047 
0048   ~win_fd_set_adapter()
0049   {
0050     ::operator delete(fd_set_);
0051   }
0052 
0053   void reset()
0054   {
0055     fd_set_->fd_count = 0;
0056     max_descriptor_ = invalid_socket;
0057   }
0058 
0059   bool set(socket_type descriptor)
0060   {
0061     for (u_int i = 0; i < fd_set_->fd_count; ++i)
0062       if (fd_set_->fd_array[i] == descriptor)
0063         return true;
0064 
0065     reserve(fd_set_->fd_count + 1);
0066     fd_set_->fd_array[fd_set_->fd_count++] = descriptor;
0067     return true;
0068   }
0069 
0070   void set(reactor_op_queue<socket_type>& operations, op_queue<operation>&)
0071   {
0072     reactor_op_queue<socket_type>::iterator i = operations.begin();
0073     while (i != operations.end())
0074     {
0075       reactor_op_queue<socket_type>::iterator op_iter = i++;
0076       reserve(fd_set_->fd_count + 1);
0077       fd_set_->fd_array[fd_set_->fd_count++] = op_iter->first;
0078     }
0079   }
0080 
0081   bool is_set(socket_type descriptor) const
0082   {
0083     return !!__WSAFDIsSet(descriptor,
0084         const_cast<fd_set*>(reinterpret_cast<const fd_set*>(fd_set_)));
0085   }
0086 
0087   operator fd_set*()
0088   {
0089     return reinterpret_cast<fd_set*>(fd_set_);
0090   }
0091 
0092   socket_type max_descriptor() const
0093   {
0094     return max_descriptor_;
0095   }
0096 
0097   void perform(reactor_op_queue<socket_type>& operations,
0098       op_queue<operation>& ops) const
0099   {
0100     for (u_int i = 0; i < fd_set_->fd_count; ++i)
0101       operations.perform_operations(fd_set_->fd_array[i], ops);
0102   }
0103 
0104 private:
0105   // This structure is defined to be compatible with the Windows API fd_set
0106   // structure, but without being dependent on the value of FD_SETSIZE. We use
0107   // the "struct hack" to allow the number of descriptors to be varied at
0108   // runtime.
0109   struct win_fd_set
0110   {
0111     u_int fd_count;
0112     SOCKET fd_array[1];
0113   };
0114 
0115   // Increase the fd_set_ capacity to at least the specified number of elements.
0116   void reserve(u_int n)
0117   {
0118     if (n <= capacity_)
0119       return;
0120 
0121     u_int new_capacity = capacity_ + capacity_ / 2;
0122     if (new_capacity < n)
0123       new_capacity = n;
0124 
0125     win_fd_set* new_fd_set = static_cast<win_fd_set*>(::operator new(
0126           sizeof(win_fd_set) - sizeof(SOCKET)
0127           + sizeof(SOCKET) * (new_capacity)));
0128 
0129     new_fd_set->fd_count = fd_set_->fd_count;
0130     for (u_int i = 0; i < fd_set_->fd_count; ++i)
0131       new_fd_set->fd_array[i] = fd_set_->fd_array[i];
0132 
0133     ::operator delete(fd_set_);
0134     fd_set_ = new_fd_set;
0135     capacity_ = new_capacity;
0136   }
0137 
0138   win_fd_set* fd_set_;
0139   u_int capacity_;
0140   socket_type max_descriptor_;
0141 };
0142 
0143 } // namespace detail
0144 } // namespace asio
0145 } // namespace boost
0146 
0147 #include <boost/asio/detail/pop_options.hpp>
0148 
0149 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
0150 
0151 #endif // BOOST_ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP