serial_port_service.hpp 5.7 KB

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