serial_port_service.hpp 5.6 KB

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