2 // serial_port_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_SERIAL_PORT_SERVICE_HPP
12 #define BOOST_ASIO_SERIAL_PORT_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
22 #if defined(BOOST_ASIO_HAS_SERIAL_PORT) \
23 || defined(GENERATING_DOCUMENTATION)
27 #include <boost/asio/async_result.hpp>
28 #include <boost/asio/detail/reactive_serial_port_service.hpp>
29 #include <boost/asio/detail/win_iocp_serial_port_service.hpp>
30 #include <boost/asio/error.hpp>
31 #include <boost/asio/io_context.hpp>
32 #include <boost/asio/serial_port_base.hpp>
34 #include <boost/asio/detail/push_options.hpp>
39 /// Default service implementation for a serial port.
40 class serial_port_service
41 #if defined(GENERATING_DOCUMENTATION)
42 : public boost::asio::io_context::service
44 : public boost::asio::detail::service_base<serial_port_service>
48 #if defined(GENERATING_DOCUMENTATION)
49 /// The unique service identifier.
50 static boost::asio::io_context::id id;
54 // The type of the platform-specific implementation.
55 #if defined(BOOST_ASIO_HAS_IOCP)
56 typedef detail::win_iocp_serial_port_service service_impl_type;
58 typedef detail::reactive_serial_port_service service_impl_type;
62 /// The type of a serial port implementation.
63 #if defined(GENERATING_DOCUMENTATION)
64 typedef implementation_defined implementation_type;
66 typedef service_impl_type::implementation_type implementation_type;
69 /// The native handle type.
70 #if defined(GENERATING_DOCUMENTATION)
71 typedef implementation_defined native_handle_type;
73 typedef service_impl_type::native_handle_type native_handle_type;
76 /// Construct a new serial port service for the specified io_context.
77 explicit serial_port_service(boost::asio::io_context& io_context)
78 : boost::asio::detail::service_base<serial_port_service>(io_context),
79 service_impl_(io_context)
83 /// Construct a new serial port implementation.
84 void construct(implementation_type& impl)
86 service_impl_.construct(impl);
89 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
90 /// Move-construct a new serial port implementation.
91 void move_construct(implementation_type& impl,
92 implementation_type& other_impl)
94 service_impl_.move_construct(impl, other_impl);
97 /// Move-assign from another serial port implementation.
98 void move_assign(implementation_type& impl,
99 serial_port_service& other_service,
100 implementation_type& other_impl)
102 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
104 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
106 /// Destroy a serial port implementation.
107 void destroy(implementation_type& impl)
109 service_impl_.destroy(impl);
112 /// Open a serial port.
113 BOOST_ASIO_SYNC_OP_VOID open(implementation_type& impl,
114 const std::string& device, boost::system::error_code& ec)
116 service_impl_.open(impl, device, ec);
117 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
120 /// Assign an existing native handle to a serial port.
121 BOOST_ASIO_SYNC_OP_VOID assign(implementation_type& impl,
122 const native_handle_type& handle, boost::system::error_code& ec)
124 service_impl_.assign(impl, handle, ec);
125 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
128 /// Determine whether the handle is open.
129 bool is_open(const implementation_type& impl) const
131 return service_impl_.is_open(impl);
134 /// Close a serial port implementation.
135 BOOST_ASIO_SYNC_OP_VOID close(implementation_type& impl,
136 boost::system::error_code& ec)
138 service_impl_.close(impl, ec);
139 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
142 /// Get the native handle implementation.
143 native_handle_type native_handle(implementation_type& impl)
145 return service_impl_.native_handle(impl);
148 /// Cancel all asynchronous operations associated with the handle.
149 BOOST_ASIO_SYNC_OP_VOID cancel(implementation_type& impl,
150 boost::system::error_code& ec)
152 service_impl_.cancel(impl, ec);
153 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
156 /// Set a serial port option.
157 template <typename SettableSerialPortOption>
158 BOOST_ASIO_SYNC_OP_VOID set_option(implementation_type& impl,
159 const SettableSerialPortOption& option, boost::system::error_code& ec)
161 service_impl_.set_option(impl, option, ec);
162 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
165 /// Get a serial port option.
166 template <typename GettableSerialPortOption>
167 BOOST_ASIO_SYNC_OP_VOID get_option(const implementation_type& impl,
168 GettableSerialPortOption& option, boost::system::error_code& ec) const
170 service_impl_.get_option(impl, option, ec);
171 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
174 /// Send a break sequence to the serial port.
175 BOOST_ASIO_SYNC_OP_VOID send_break(implementation_type& impl,
176 boost::system::error_code& ec)
178 service_impl_.send_break(impl, ec);
179 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
182 /// Write the given data to the stream.
183 template <typename ConstBufferSequence>
184 std::size_t write_some(implementation_type& impl,
185 const ConstBufferSequence& buffers, boost::system::error_code& ec)
187 return service_impl_.write_some(impl, buffers, ec);
190 /// Start an asynchronous write.
191 template <typename ConstBufferSequence, typename WriteHandler>
192 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
193 void (boost::system::error_code, std::size_t))
194 async_write_some(implementation_type& impl,
195 const ConstBufferSequence& buffers,
196 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
198 async_completion<WriteHandler,
199 void (boost::system::error_code, std::size_t)> init(handler);
201 service_impl_.async_write_some(impl, buffers, init.completion_handler);
203 return init.result.get();
206 /// Read some data from the stream.
207 template <typename MutableBufferSequence>
208 std::size_t read_some(implementation_type& impl,
209 const MutableBufferSequence& buffers, boost::system::error_code& ec)
211 return service_impl_.read_some(impl, buffers, ec);
214 /// Start an asynchronous read.
215 template <typename MutableBufferSequence, typename ReadHandler>
216 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
217 void (boost::system::error_code, std::size_t))
218 async_read_some(implementation_type& impl,
219 const MutableBufferSequence& buffers,
220 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
222 async_completion<ReadHandler,
223 void (boost::system::error_code, std::size_t)> init(handler);
225 service_impl_.async_read_some(impl, buffers, init.completion_handler);
227 return init.result.get();
231 // Destroy all user-defined handler objects owned by the service.
234 service_impl_.shutdown();
237 // The platform-specific implementation.
238 service_impl_type service_impl_;
244 #include <boost/asio/detail/pop_options.hpp>
246 #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
247 // || defined(GENERATING_DOCUMENTATION)
249 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
251 #endif // BOOST_ASIO_SERIAL_PORT_SERVICE_HPP