serial_port_service.hpp 7.2 KB

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