Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/win_iocp_serial_port_service.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
0007 //
0008 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0009 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 
0012 #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_SERIAL_PORT_SERVICE_HPP
0013 #define BOOST_ASIO_DETAIL_WIN_IOCP_SERIAL_PORT_SERVICE_HPP
0014 
0015 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0016 # pragma once
0017 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0018 
0019 #include <boost/asio/detail/config.hpp>
0020 
0021 #if defined(BOOST_ASIO_HAS_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT)
0022 
0023 #include <string>
0024 #include <boost/asio/error.hpp>
0025 #include <boost/asio/execution_context.hpp>
0026 #include <boost/asio/detail/win_iocp_handle_service.hpp>
0027 
0028 #include <boost/asio/detail/push_options.hpp>
0029 
0030 namespace boost {
0031 namespace asio {
0032 namespace detail {
0033 
0034 // Extend win_iocp_handle_service to provide serial port support.
0035 class win_iocp_serial_port_service :
0036   public execution_context_service_base<win_iocp_serial_port_service>
0037 {
0038 public:
0039   // The native type of a serial port.
0040   typedef win_iocp_handle_service::native_handle_type native_handle_type;
0041 
0042   // The implementation type of the serial port.
0043   typedef win_iocp_handle_service::implementation_type implementation_type;
0044 
0045   // Constructor.
0046   BOOST_ASIO_DECL win_iocp_serial_port_service(execution_context& context);
0047 
0048   // Destroy all user-defined handler objects owned by the service.
0049   BOOST_ASIO_DECL void shutdown();
0050 
0051   // Construct a new serial port implementation.
0052   void construct(implementation_type& impl)
0053   {
0054     handle_service_.construct(impl);
0055   }
0056 
0057   // Move-construct a new serial port implementation.
0058   void move_construct(implementation_type& impl,
0059       implementation_type& other_impl)
0060   {
0061     handle_service_.move_construct(impl, other_impl);
0062   }
0063 
0064   // Move-assign from another serial port implementation.
0065   void move_assign(implementation_type& impl,
0066       win_iocp_serial_port_service& other_service,
0067       implementation_type& other_impl)
0068   {
0069     handle_service_.move_assign(impl,
0070         other_service.handle_service_, other_impl);
0071   }
0072 
0073   // Destroy a serial port implementation.
0074   void destroy(implementation_type& impl)
0075   {
0076     handle_service_.destroy(impl);
0077   }
0078 
0079   // Open the serial port using the specified device name.
0080   BOOST_ASIO_DECL boost::system::error_code open(implementation_type& impl,
0081       const std::string& device, boost::system::error_code& ec);
0082 
0083   // Assign a native handle to a serial port implementation.
0084   boost::system::error_code assign(implementation_type& impl,
0085       const native_handle_type& handle, boost::system::error_code& ec)
0086   {
0087     return handle_service_.assign(impl, handle, ec);
0088   }
0089 
0090   // Determine whether the serial port is open.
0091   bool is_open(const implementation_type& impl) const
0092   {
0093     return handle_service_.is_open(impl);
0094   }
0095 
0096   // Destroy a serial port implementation.
0097   boost::system::error_code close(implementation_type& impl,
0098       boost::system::error_code& ec)
0099   {
0100     return handle_service_.close(impl, ec);
0101   }
0102 
0103   // Get the native serial port representation.
0104   native_handle_type native_handle(implementation_type& impl)
0105   {
0106     return handle_service_.native_handle(impl);
0107   }
0108 
0109   // Cancel all operations associated with the handle.
0110   boost::system::error_code cancel(implementation_type& impl,
0111       boost::system::error_code& ec)
0112   {
0113     return handle_service_.cancel(impl, ec);
0114   }
0115 
0116   // Set an option on the serial port.
0117   template <typename SettableSerialPortOption>
0118   boost::system::error_code set_option(implementation_type& impl,
0119       const SettableSerialPortOption& option, boost::system::error_code& ec)
0120   {
0121     return do_set_option(impl,
0122         &win_iocp_serial_port_service::store_option<SettableSerialPortOption>,
0123         &option, ec);
0124   }
0125 
0126   // Get an option from the serial port.
0127   template <typename GettableSerialPortOption>
0128   boost::system::error_code get_option(const implementation_type& impl,
0129       GettableSerialPortOption& option, boost::system::error_code& ec) const
0130   {
0131     return do_get_option(impl,
0132         &win_iocp_serial_port_service::load_option<GettableSerialPortOption>,
0133         &option, ec);
0134   }
0135 
0136   // Send a break sequence to the serial port.
0137   boost::system::error_code send_break(implementation_type&,
0138       boost::system::error_code& ec)
0139   {
0140     ec = boost::asio::error::operation_not_supported;
0141     BOOST_ASIO_ERROR_LOCATION(ec);
0142     return ec;
0143   }
0144 
0145   // Write the given data. Returns the number of bytes sent.
0146   template <typename ConstBufferSequence>
0147   size_t write_some(implementation_type& impl,
0148       const ConstBufferSequence& buffers, boost::system::error_code& ec)
0149   {
0150     return handle_service_.write_some(impl, buffers, ec);
0151   }
0152 
0153   // Start an asynchronous write. The data being written must be valid for the
0154   // lifetime of the asynchronous operation.
0155   template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
0156   void async_write_some(implementation_type& impl,
0157       const ConstBufferSequence& buffers,
0158       Handler& handler, const IoExecutor& io_ex)
0159   {
0160     handle_service_.async_write_some(impl, buffers, handler, io_ex);
0161   }
0162 
0163   // Read some data. Returns the number of bytes received.
0164   template <typename MutableBufferSequence>
0165   size_t read_some(implementation_type& impl,
0166       const MutableBufferSequence& buffers, boost::system::error_code& ec)
0167   {
0168     return handle_service_.read_some(impl, buffers, ec);
0169   }
0170 
0171   // Start an asynchronous read. The buffer for the data being received must be
0172   // valid for the lifetime of the asynchronous operation.
0173   template <typename MutableBufferSequence,
0174       typename Handler, typename IoExecutor>
0175   void async_read_some(implementation_type& impl,
0176       const MutableBufferSequence& buffers,
0177       Handler& handler, const IoExecutor& io_ex)
0178   {
0179     handle_service_.async_read_some(impl, buffers, handler, io_ex);
0180   }
0181 
0182 private:
0183   // Function pointer type for storing a serial port option.
0184   typedef boost::system::error_code (*store_function_type)(
0185       const void*, ::DCB&, boost::system::error_code&);
0186 
0187   // Helper function template to store a serial port option.
0188   template <typename SettableSerialPortOption>
0189   static boost::system::error_code store_option(const void* option,
0190       ::DCB& storage, boost::system::error_code& ec)
0191   {
0192     static_cast<const SettableSerialPortOption*>(option)->store(storage, ec);
0193     return ec;
0194   }
0195 
0196   // Helper function to set a serial port option.
0197   BOOST_ASIO_DECL boost::system::error_code do_set_option(
0198       implementation_type& impl, store_function_type store,
0199       const void* option, boost::system::error_code& ec);
0200 
0201   // Function pointer type for loading a serial port option.
0202   typedef boost::system::error_code (*load_function_type)(
0203       void*, const ::DCB&, boost::system::error_code&);
0204 
0205   // Helper function template to load a serial port option.
0206   template <typename GettableSerialPortOption>
0207   static boost::system::error_code load_option(void* option,
0208       const ::DCB& storage, boost::system::error_code& ec)
0209   {
0210     static_cast<GettableSerialPortOption*>(option)->load(storage, ec);
0211     return ec;
0212   }
0213 
0214   // Helper function to get a serial port option.
0215   BOOST_ASIO_DECL boost::system::error_code do_get_option(
0216       const implementation_type& impl, load_function_type load,
0217       void* option, boost::system::error_code& ec) const;
0218 
0219   // The implementation used for initiating asynchronous operations.
0220   win_iocp_handle_service handle_service_;
0221 };
0222 
0223 } // namespace detail
0224 } // namespace asio
0225 } // namespace boost
0226 
0227 #include <boost/asio/detail/pop_options.hpp>
0228 
0229 #if defined(BOOST_ASIO_HEADER_ONLY)
0230 # include <boost/asio/detail/impl/win_iocp_serial_port_service.ipp>
0231 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0232 
0233 #endif // defined(BOOST_ASIO_HAS_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT)
0234 
0235 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SERIAL_PORT_SERVICE_HPP