win_iocp_serial_port_service.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // detail/win_iocp_serial_port_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_SERIAL_PORT_SERVICE_HPP
  12. #define BOOST_ASIO_DETAIL_WIN_IOCP_SERIAL_PORT_SERVICE_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #if defined(BOOST_ASIO_HAS_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT)
  18. #include <string>
  19. #include <boost/asio/error.hpp>
  20. #include <boost/asio/execution_context.hpp>
  21. #include <boost/asio/detail/win_iocp_handle_service.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. // Extend win_iocp_handle_service to provide serial port support.
  27. class win_iocp_serial_port_service :
  28. public execution_context_service_base<win_iocp_serial_port_service>
  29. {
  30. public:
  31. // The native type of a serial port.
  32. typedef win_iocp_handle_service::native_handle_type native_handle_type;
  33. // The implementation type of the serial port.
  34. typedef win_iocp_handle_service::implementation_type implementation_type;
  35. // Constructor.
  36. BOOST_ASIO_DECL win_iocp_serial_port_service(execution_context& context);
  37. // Destroy all user-defined handler objects owned by the service.
  38. BOOST_ASIO_DECL void shutdown();
  39. // Construct a new serial port implementation.
  40. void construct(implementation_type& impl)
  41. {
  42. handle_service_.construct(impl);
  43. }
  44. // Move-construct a new serial port implementation.
  45. void move_construct(implementation_type& impl,
  46. implementation_type& other_impl)
  47. {
  48. handle_service_.move_construct(impl, other_impl);
  49. }
  50. // Move-assign from another serial port implementation.
  51. void move_assign(implementation_type& impl,
  52. win_iocp_serial_port_service& other_service,
  53. implementation_type& other_impl)
  54. {
  55. handle_service_.move_assign(impl,
  56. other_service.handle_service_, other_impl);
  57. }
  58. // Destroy a serial port implementation.
  59. void destroy(implementation_type& impl)
  60. {
  61. handle_service_.destroy(impl);
  62. }
  63. // Open the serial port using the specified device name.
  64. BOOST_ASIO_DECL boost::system::error_code open(implementation_type& impl,
  65. const std::string& device, boost::system::error_code& ec);
  66. // Assign a native handle to a serial port implementation.
  67. boost::system::error_code assign(implementation_type& impl,
  68. const native_handle_type& handle, boost::system::error_code& ec)
  69. {
  70. return handle_service_.assign(impl, handle, ec);
  71. }
  72. // Determine whether the serial port is open.
  73. bool is_open(const implementation_type& impl) const
  74. {
  75. return handle_service_.is_open(impl);
  76. }
  77. // Destroy a serial port implementation.
  78. boost::system::error_code close(implementation_type& impl,
  79. boost::system::error_code& ec)
  80. {
  81. return handle_service_.close(impl, ec);
  82. }
  83. // Get the native serial port representation.
  84. native_handle_type native_handle(implementation_type& impl)
  85. {
  86. return handle_service_.native_handle(impl);
  87. }
  88. // Cancel all operations associated with the handle.
  89. boost::system::error_code cancel(implementation_type& impl,
  90. boost::system::error_code& ec)
  91. {
  92. return handle_service_.cancel(impl, ec);
  93. }
  94. // Set an option on the serial port.
  95. template <typename SettableSerialPortOption>
  96. boost::system::error_code set_option(implementation_type& impl,
  97. const SettableSerialPortOption& option, boost::system::error_code& ec)
  98. {
  99. return do_set_option(impl,
  100. &win_iocp_serial_port_service::store_option<SettableSerialPortOption>,
  101. &option, ec);
  102. }
  103. // Get an option from the serial port.
  104. template <typename GettableSerialPortOption>
  105. boost::system::error_code get_option(const implementation_type& impl,
  106. GettableSerialPortOption& option, boost::system::error_code& ec) const
  107. {
  108. return do_get_option(impl,
  109. &win_iocp_serial_port_service::load_option<GettableSerialPortOption>,
  110. &option, ec);
  111. }
  112. // Send a break sequence to the serial port.
  113. boost::system::error_code send_break(implementation_type&,
  114. boost::system::error_code& ec)
  115. {
  116. ec = boost::asio::error::operation_not_supported;
  117. BOOST_ASIO_ERROR_LOCATION(ec);
  118. return ec;
  119. }
  120. // Write the given data. Returns the number of bytes sent.
  121. template <typename ConstBufferSequence>
  122. size_t write_some(implementation_type& impl,
  123. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  124. {
  125. return handle_service_.write_some(impl, buffers, ec);
  126. }
  127. // Start an asynchronous write. The data being written must be valid for the
  128. // lifetime of the asynchronous operation.
  129. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  130. void async_write_some(implementation_type& impl,
  131. const ConstBufferSequence& buffers,
  132. Handler& handler, const IoExecutor& io_ex)
  133. {
  134. handle_service_.async_write_some(impl, buffers, handler, io_ex);
  135. }
  136. // Read some data. Returns the number of bytes received.
  137. template <typename MutableBufferSequence>
  138. size_t read_some(implementation_type& impl,
  139. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  140. {
  141. return handle_service_.read_some(impl, buffers, ec);
  142. }
  143. // Start an asynchronous read. The buffer for the data being received must be
  144. // valid for the lifetime of the asynchronous operation.
  145. template <typename MutableBufferSequence,
  146. typename Handler, typename IoExecutor>
  147. void async_read_some(implementation_type& impl,
  148. const MutableBufferSequence& buffers,
  149. Handler& handler, const IoExecutor& io_ex)
  150. {
  151. handle_service_.async_read_some(impl, buffers, handler, io_ex);
  152. }
  153. private:
  154. // Function pointer type for storing a serial port option.
  155. typedef boost::system::error_code (*store_function_type)(
  156. const void*, ::DCB&, boost::system::error_code&);
  157. // Helper function template to store a serial port option.
  158. template <typename SettableSerialPortOption>
  159. static boost::system::error_code store_option(const void* option,
  160. ::DCB& storage, boost::system::error_code& ec)
  161. {
  162. static_cast<const SettableSerialPortOption*>(option)->store(storage, ec);
  163. return ec;
  164. }
  165. // Helper function to set a serial port option.
  166. BOOST_ASIO_DECL boost::system::error_code do_set_option(
  167. implementation_type& impl, store_function_type store,
  168. const void* option, boost::system::error_code& ec);
  169. // Function pointer type for loading a serial port option.
  170. typedef boost::system::error_code (*load_function_type)(
  171. void*, const ::DCB&, boost::system::error_code&);
  172. // Helper function template to load a serial port option.
  173. template <typename GettableSerialPortOption>
  174. static boost::system::error_code load_option(void* option,
  175. const ::DCB& storage, boost::system::error_code& ec)
  176. {
  177. static_cast<GettableSerialPortOption*>(option)->load(storage, ec);
  178. return ec;
  179. }
  180. // Helper function to get a serial port option.
  181. BOOST_ASIO_DECL boost::system::error_code do_get_option(
  182. const implementation_type& impl, load_function_type load,
  183. void* option, boost::system::error_code& ec) const;
  184. // The implementation used for initiating asynchronous operations.
  185. win_iocp_handle_service handle_service_;
  186. };
  187. } // namespace detail
  188. } // namespace asio
  189. } // namespace boost
  190. #include <boost/asio/detail/pop_options.hpp>
  191. #if defined(BOOST_ASIO_HEADER_ONLY)
  192. # include <boost/asio/detail/impl/win_iocp_serial_port_service.ipp>
  193. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  194. #endif // defined(BOOST_ASIO_HAS_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT)
  195. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SERIAL_PORT_SERVICE_HPP