win_iocp_serial_port_service.hpp 7.4 KB

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