serial_port_service.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //
  2. // serial_port_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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. #ifndef BOOST_ASIO_SERIAL_PORT_SERVICE_HPP
  11. #define BOOST_ASIO_SERIAL_PORT_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_SERIAL_PORT) \
  17. || defined(GENERATING_DOCUMENTATION)
  18. #include <cstddef>
  19. #include <string>
  20. #include <boost/asio/async_result.hpp>
  21. #include <boost/asio/detail/reactive_serial_port_service.hpp>
  22. #include <boost/asio/detail/win_iocp_serial_port_service.hpp>
  23. #include <boost/asio/error.hpp>
  24. #include <boost/asio/io_service.hpp>
  25. #include <boost/asio/serial_port_base.hpp>
  26. #include <boost/asio/detail/push_options.hpp>
  27. namespace boost {
  28. namespace asio {
  29. /// Default service implementation for a serial port.
  30. class serial_port_service
  31. #if defined(GENERATING_DOCUMENTATION)
  32. : public boost::asio::io_service::service
  33. #else
  34. : public boost::asio::detail::service_base<serial_port_service>
  35. #endif
  36. {
  37. public:
  38. #if defined(GENERATING_DOCUMENTATION)
  39. /// The unique service identifier.
  40. static boost::asio::io_service::id id;
  41. #endif
  42. private:
  43. // The type of the platform-specific implementation.
  44. #if defined(BOOST_ASIO_HAS_IOCP)
  45. typedef detail::win_iocp_serial_port_service service_impl_type;
  46. #else
  47. typedef detail::reactive_serial_port_service service_impl_type;
  48. #endif
  49. public:
  50. /// The type of a serial port implementation.
  51. #if defined(GENERATING_DOCUMENTATION)
  52. typedef implementation_defined implementation_type;
  53. #else
  54. typedef service_impl_type::implementation_type implementation_type;
  55. #endif
  56. /// (Deprecated: Use native_handle_type.) The native handle type.
  57. #if defined(GENERATING_DOCUMENTATION)
  58. typedef implementation_defined native_type;
  59. #else
  60. typedef service_impl_type::native_handle_type native_type;
  61. #endif
  62. /// The native handle type.
  63. #if defined(GENERATING_DOCUMENTATION)
  64. typedef implementation_defined native_handle_type;
  65. #else
  66. typedef service_impl_type::native_handle_type native_handle_type;
  67. #endif
  68. /// Construct a new serial port service for the specified io_service.
  69. explicit serial_port_service(boost::asio::io_service& io_service)
  70. : boost::asio::detail::service_base<serial_port_service>(io_service),
  71. service_impl_(io_service)
  72. {
  73. }
  74. /// Construct a new serial port implementation.
  75. void construct(implementation_type& impl)
  76. {
  77. service_impl_.construct(impl);
  78. }
  79. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  80. /// Move-construct a new serial port implementation.
  81. void move_construct(implementation_type& impl,
  82. implementation_type& other_impl)
  83. {
  84. service_impl_.move_construct(impl, other_impl);
  85. }
  86. /// Move-assign from another serial port implementation.
  87. void move_assign(implementation_type& impl,
  88. serial_port_service& other_service,
  89. implementation_type& other_impl)
  90. {
  91. service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
  92. }
  93. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  94. /// Destroy a serial port implementation.
  95. void destroy(implementation_type& impl)
  96. {
  97. service_impl_.destroy(impl);
  98. }
  99. /// Open a serial port.
  100. boost::system::error_code open(implementation_type& impl,
  101. const std::string& device, boost::system::error_code& ec)
  102. {
  103. return service_impl_.open(impl, device, ec);
  104. }
  105. /// Assign an existing native handle to a serial port.
  106. boost::system::error_code assign(implementation_type& impl,
  107. const native_handle_type& handle, boost::system::error_code& ec)
  108. {
  109. return service_impl_.assign(impl, handle, ec);
  110. }
  111. /// Determine whether the handle is open.
  112. bool is_open(const implementation_type& impl) const
  113. {
  114. return service_impl_.is_open(impl);
  115. }
  116. /// Close a serial port implementation.
  117. boost::system::error_code close(implementation_type& impl,
  118. boost::system::error_code& ec)
  119. {
  120. return service_impl_.close(impl, ec);
  121. }
  122. /// (Deprecated: Use native_handle().) Get the native handle implementation.
  123. native_type native(implementation_type& impl)
  124. {
  125. return service_impl_.native_handle(impl);
  126. }
  127. /// Get the native handle implementation.
  128. native_handle_type native_handle(implementation_type& impl)
  129. {
  130. return service_impl_.native_handle(impl);
  131. }
  132. /// Cancel all asynchronous operations associated with the handle.
  133. boost::system::error_code cancel(implementation_type& impl,
  134. boost::system::error_code& ec)
  135. {
  136. return service_impl_.cancel(impl, ec);
  137. }
  138. /// Set a serial port option.
  139. template <typename SettableSerialPortOption>
  140. boost::system::error_code set_option(implementation_type& impl,
  141. const SettableSerialPortOption& option, boost::system::error_code& ec)
  142. {
  143. return service_impl_.set_option(impl, option, ec);
  144. }
  145. /// Get a serial port option.
  146. template <typename GettableSerialPortOption>
  147. boost::system::error_code get_option(const implementation_type& impl,
  148. GettableSerialPortOption& option, boost::system::error_code& ec) const
  149. {
  150. return service_impl_.get_option(impl, option, ec);
  151. }
  152. /// Send a break sequence to the serial port.
  153. boost::system::error_code send_break(implementation_type& impl,
  154. boost::system::error_code& ec)
  155. {
  156. return service_impl_.send_break(impl, ec);
  157. }
  158. /// Write the given data to the stream.
  159. template <typename ConstBufferSequence>
  160. std::size_t write_some(implementation_type& impl,
  161. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  162. {
  163. return service_impl_.write_some(impl, buffers, ec);
  164. }
  165. /// Start an asynchronous write.
  166. template <typename ConstBufferSequence, typename WriteHandler>
  167. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  168. void (boost::system::error_code, std::size_t))
  169. async_write_some(implementation_type& impl,
  170. const ConstBufferSequence& buffers,
  171. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  172. {
  173. detail::async_result_init<
  174. WriteHandler, void (boost::system::error_code, std::size_t)> init(
  175. BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
  176. service_impl_.async_write_some(impl, buffers, init.handler);
  177. return init.result.get();
  178. }
  179. /// Read some data from the stream.
  180. template <typename MutableBufferSequence>
  181. std::size_t read_some(implementation_type& impl,
  182. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  183. {
  184. return service_impl_.read_some(impl, buffers, ec);
  185. }
  186. /// Start an asynchronous read.
  187. template <typename MutableBufferSequence, typename ReadHandler>
  188. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  189. void (boost::system::error_code, std::size_t))
  190. async_read_some(implementation_type& impl,
  191. const MutableBufferSequence& buffers,
  192. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  193. {
  194. detail::async_result_init<
  195. ReadHandler, void (boost::system::error_code, std::size_t)> init(
  196. BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  197. service_impl_.async_read_some(impl, buffers, init.handler);
  198. return init.result.get();
  199. }
  200. private:
  201. // Destroy all user-defined handler objects owned by the service.
  202. void shutdown_service()
  203. {
  204. service_impl_.shutdown_service();
  205. }
  206. // The platform-specific implementation.
  207. service_impl_type service_impl_;
  208. };
  209. } // namespace asio
  210. } // namespace boost
  211. #include <boost/asio/detail/pop_options.hpp>
  212. #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
  213. // || defined(GENERATING_DOCUMENTATION)
  214. #endif // BOOST_ASIO_SERIAL_PORT_SERVICE_HPP