serial_port_service.hpp 6.5 KB

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