]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/win_iocp_serial_port_service.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
1e59de90 | 5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
6 | // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com) |
7 | // | |
8 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
9 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
10 | // | |
11 | ||
12 | #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_SERIAL_PORT_SERVICE_HPP | |
13 | #define BOOST_ASIO_DETAIL_WIN_IOCP_SERIAL_PORT_SERVICE_HPP | |
14 | ||
15 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
16 | # pragma once | |
17 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
18 | ||
19 | #include <boost/asio/detail/config.hpp> | |
20 | ||
21 | #if defined(BOOST_ASIO_HAS_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT) | |
22 | ||
23 | #include <string> | |
24 | #include <boost/asio/error.hpp> | |
92f5a8d4 | 25 | #include <boost/asio/execution_context.hpp> |
7c673cae FG |
26 | #include <boost/asio/detail/win_iocp_handle_service.hpp> |
27 | ||
28 | #include <boost/asio/detail/push_options.hpp> | |
29 | ||
30 | namespace boost { | |
31 | namespace asio { | |
32 | namespace detail { | |
33 | ||
34 | // Extend win_iocp_handle_service to provide serial port support. | |
b32b8144 | 35 | class win_iocp_serial_port_service : |
92f5a8d4 | 36 | public execution_context_service_base<win_iocp_serial_port_service> |
7c673cae FG |
37 | { |
38 | public: | |
39 | // The native type of a serial port. | |
40 | typedef win_iocp_handle_service::native_handle_type native_handle_type; | |
41 | ||
42 | // The implementation type of the serial port. | |
43 | typedef win_iocp_handle_service::implementation_type implementation_type; | |
44 | ||
45 | // Constructor. | |
92f5a8d4 | 46 | BOOST_ASIO_DECL win_iocp_serial_port_service(execution_context& context); |
7c673cae FG |
47 | |
48 | // Destroy all user-defined handler objects owned by the service. | |
b32b8144 | 49 | BOOST_ASIO_DECL void shutdown(); |
7c673cae FG |
50 | |
51 | // Construct a new serial port implementation. | |
52 | void construct(implementation_type& impl) | |
53 | { | |
54 | handle_service_.construct(impl); | |
55 | } | |
56 | ||
57 | // Move-construct a new serial port implementation. | |
58 | void move_construct(implementation_type& impl, | |
59 | implementation_type& other_impl) | |
60 | { | |
61 | handle_service_.move_construct(impl, other_impl); | |
62 | } | |
63 | ||
64 | // Move-assign from another serial port implementation. | |
65 | void move_assign(implementation_type& impl, | |
66 | win_iocp_serial_port_service& other_service, | |
67 | implementation_type& other_impl) | |
68 | { | |
69 | handle_service_.move_assign(impl, | |
70 | other_service.handle_service_, other_impl); | |
71 | } | |
72 | ||
73 | // Destroy a serial port implementation. | |
74 | void destroy(implementation_type& impl) | |
75 | { | |
76 | handle_service_.destroy(impl); | |
77 | } | |
78 | ||
79 | // Open the serial port using the specified device name. | |
80 | BOOST_ASIO_DECL boost::system::error_code open(implementation_type& impl, | |
81 | const std::string& device, boost::system::error_code& ec); | |
82 | ||
83 | // Assign a native handle to a serial port implementation. | |
84 | boost::system::error_code assign(implementation_type& impl, | |
85 | const native_handle_type& handle, boost::system::error_code& ec) | |
86 | { | |
87 | return handle_service_.assign(impl, handle, ec); | |
88 | } | |
89 | ||
90 | // Determine whether the serial port is open. | |
91 | bool is_open(const implementation_type& impl) const | |
92 | { | |
93 | return handle_service_.is_open(impl); | |
94 | } | |
95 | ||
96 | // Destroy a serial port implementation. | |
97 | boost::system::error_code close(implementation_type& impl, | |
98 | boost::system::error_code& ec) | |
99 | { | |
100 | return handle_service_.close(impl, ec); | |
101 | } | |
102 | ||
103 | // Get the native serial port representation. | |
104 | native_handle_type native_handle(implementation_type& impl) | |
105 | { | |
106 | return handle_service_.native_handle(impl); | |
107 | } | |
108 | ||
109 | // Cancel all operations associated with the handle. | |
110 | boost::system::error_code cancel(implementation_type& impl, | |
111 | boost::system::error_code& ec) | |
112 | { | |
113 | return handle_service_.cancel(impl, ec); | |
114 | } | |
115 | ||
116 | // Set an option on the serial port. | |
117 | template <typename SettableSerialPortOption> | |
118 | boost::system::error_code set_option(implementation_type& impl, | |
119 | const SettableSerialPortOption& option, boost::system::error_code& ec) | |
120 | { | |
121 | return do_set_option(impl, | |
122 | &win_iocp_serial_port_service::store_option<SettableSerialPortOption>, | |
123 | &option, ec); | |
124 | } | |
125 | ||
126 | // Get an option from the serial port. | |
127 | template <typename GettableSerialPortOption> | |
128 | boost::system::error_code get_option(const implementation_type& impl, | |
129 | GettableSerialPortOption& option, boost::system::error_code& ec) const | |
130 | { | |
131 | return do_get_option(impl, | |
132 | &win_iocp_serial_port_service::load_option<GettableSerialPortOption>, | |
133 | &option, ec); | |
134 | } | |
135 | ||
136 | // Send a break sequence to the serial port. | |
137 | boost::system::error_code send_break(implementation_type&, | |
138 | boost::system::error_code& ec) | |
139 | { | |
140 | ec = boost::asio::error::operation_not_supported; | |
141 | return ec; | |
142 | } | |
143 | ||
144 | // Write the given data. Returns the number of bytes sent. | |
145 | template <typename ConstBufferSequence> | |
146 | size_t write_some(implementation_type& impl, | |
147 | const ConstBufferSequence& buffers, boost::system::error_code& ec) | |
148 | { | |
149 | return handle_service_.write_some(impl, buffers, ec); | |
150 | } | |
151 | ||
152 | // Start an asynchronous write. The data being written must be valid for the | |
153 | // lifetime of the asynchronous operation. | |
92f5a8d4 | 154 | template <typename ConstBufferSequence, typename Handler, typename IoExecutor> |
7c673cae | 155 | void async_write_some(implementation_type& impl, |
92f5a8d4 TL |
156 | const ConstBufferSequence& buffers, |
157 | Handler& handler, const IoExecutor& io_ex) | |
7c673cae | 158 | { |
92f5a8d4 | 159 | handle_service_.async_write_some(impl, buffers, handler, io_ex); |
7c673cae FG |
160 | } |
161 | ||
162 | // Read some data. Returns the number of bytes received. | |
163 | template <typename MutableBufferSequence> | |
164 | size_t read_some(implementation_type& impl, | |
165 | const MutableBufferSequence& buffers, boost::system::error_code& ec) | |
166 | { | |
167 | return handle_service_.read_some(impl, buffers, ec); | |
168 | } | |
169 | ||
170 | // Start an asynchronous read. The buffer for the data being received must be | |
171 | // valid for the lifetime of the asynchronous operation. | |
92f5a8d4 TL |
172 | template <typename MutableBufferSequence, |
173 | typename Handler, typename IoExecutor> | |
7c673cae | 174 | void async_read_some(implementation_type& impl, |
92f5a8d4 TL |
175 | const MutableBufferSequence& buffers, |
176 | Handler& handler, const IoExecutor& io_ex) | |
7c673cae | 177 | { |
92f5a8d4 | 178 | handle_service_.async_read_some(impl, buffers, handler, io_ex); |
7c673cae FG |
179 | } |
180 | ||
181 | private: | |
182 | // Function pointer type for storing a serial port option. | |
183 | typedef boost::system::error_code (*store_function_type)( | |
184 | const void*, ::DCB&, boost::system::error_code&); | |
185 | ||
186 | // Helper function template to store a serial port option. | |
187 | template <typename SettableSerialPortOption> | |
188 | static boost::system::error_code store_option(const void* option, | |
189 | ::DCB& storage, boost::system::error_code& ec) | |
190 | { | |
b32b8144 FG |
191 | static_cast<const SettableSerialPortOption*>(option)->store(storage, ec); |
192 | return ec; | |
7c673cae FG |
193 | } |
194 | ||
195 | // Helper function to set a serial port option. | |
196 | BOOST_ASIO_DECL boost::system::error_code do_set_option( | |
197 | implementation_type& impl, store_function_type store, | |
198 | const void* option, boost::system::error_code& ec); | |
199 | ||
200 | // Function pointer type for loading a serial port option. | |
201 | typedef boost::system::error_code (*load_function_type)( | |
202 | void*, const ::DCB&, boost::system::error_code&); | |
203 | ||
204 | // Helper function template to load a serial port option. | |
205 | template <typename GettableSerialPortOption> | |
206 | static boost::system::error_code load_option(void* option, | |
207 | const ::DCB& storage, boost::system::error_code& ec) | |
208 | { | |
b32b8144 FG |
209 | static_cast<GettableSerialPortOption*>(option)->load(storage, ec); |
210 | return ec; | |
7c673cae FG |
211 | } |
212 | ||
213 | // Helper function to get a serial port option. | |
214 | BOOST_ASIO_DECL boost::system::error_code do_get_option( | |
215 | const implementation_type& impl, load_function_type load, | |
216 | void* option, boost::system::error_code& ec) const; | |
217 | ||
218 | // The implementation used for initiating asynchronous operations. | |
219 | win_iocp_handle_service handle_service_; | |
220 | }; | |
221 | ||
222 | } // namespace detail | |
223 | } // namespace asio | |
224 | } // namespace boost | |
225 | ||
226 | #include <boost/asio/detail/pop_options.hpp> | |
227 | ||
228 | #if defined(BOOST_ASIO_HEADER_ONLY) | |
229 | # include <boost/asio/detail/impl/win_iocp_serial_port_service.ipp> | |
230 | #endif // defined(BOOST_ASIO_HEADER_ONLY) | |
231 | ||
232 | #endif // defined(BOOST_ASIO_HAS_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT) | |
233 | ||
234 | #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SERIAL_PORT_SERVICE_HPP |