]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/win_iocp_serial_port_service.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
11fdf7f2 | 5 | // Copyright (c) 2003-2018 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> | |
b32b8144 | 25 | #include <boost/asio/io_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 FG |
35 | class win_iocp_serial_port_service : |
36 | public 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. | |
46 | BOOST_ASIO_DECL win_iocp_serial_port_service( | |
b32b8144 | 47 | boost::asio::io_context& io_context); |
7c673cae FG |
48 | |
49 | // Destroy all user-defined handler objects owned by the service. | |
b32b8144 | 50 | BOOST_ASIO_DECL void shutdown(); |
7c673cae FG |
51 | |
52 | // Construct a new serial port implementation. | |
53 | void construct(implementation_type& impl) | |
54 | { | |
55 | handle_service_.construct(impl); | |
56 | } | |
57 | ||
58 | // Move-construct a new serial port implementation. | |
59 | void move_construct(implementation_type& impl, | |
60 | implementation_type& other_impl) | |
61 | { | |
62 | handle_service_.move_construct(impl, other_impl); | |
63 | } | |
64 | ||
65 | // Move-assign from another serial port implementation. | |
66 | void move_assign(implementation_type& impl, | |
67 | win_iocp_serial_port_service& other_service, | |
68 | implementation_type& other_impl) | |
69 | { | |
70 | handle_service_.move_assign(impl, | |
71 | other_service.handle_service_, other_impl); | |
72 | } | |
73 | ||
74 | // Destroy a serial port implementation. | |
75 | void destroy(implementation_type& impl) | |
76 | { | |
77 | handle_service_.destroy(impl); | |
78 | } | |
79 | ||
80 | // Open the serial port using the specified device name. | |
81 | BOOST_ASIO_DECL boost::system::error_code open(implementation_type& impl, | |
82 | const std::string& device, boost::system::error_code& ec); | |
83 | ||
84 | // Assign a native handle to a serial port implementation. | |
85 | boost::system::error_code assign(implementation_type& impl, | |
86 | const native_handle_type& handle, boost::system::error_code& ec) | |
87 | { | |
88 | return handle_service_.assign(impl, handle, ec); | |
89 | } | |
90 | ||
91 | // Determine whether the serial port is open. | |
92 | bool is_open(const implementation_type& impl) const | |
93 | { | |
94 | return handle_service_.is_open(impl); | |
95 | } | |
96 | ||
97 | // Destroy a serial port implementation. | |
98 | boost::system::error_code close(implementation_type& impl, | |
99 | boost::system::error_code& ec) | |
100 | { | |
101 | return handle_service_.close(impl, ec); | |
102 | } | |
103 | ||
104 | // Get the native serial port representation. | |
105 | native_handle_type native_handle(implementation_type& impl) | |
106 | { | |
107 | return handle_service_.native_handle(impl); | |
108 | } | |
109 | ||
110 | // Cancel all operations associated with the handle. | |
111 | boost::system::error_code cancel(implementation_type& impl, | |
112 | boost::system::error_code& ec) | |
113 | { | |
114 | return handle_service_.cancel(impl, ec); | |
115 | } | |
116 | ||
117 | // Set an option on the serial port. | |
118 | template <typename SettableSerialPortOption> | |
119 | boost::system::error_code set_option(implementation_type& impl, | |
120 | const SettableSerialPortOption& option, boost::system::error_code& ec) | |
121 | { | |
122 | return do_set_option(impl, | |
123 | &win_iocp_serial_port_service::store_option<SettableSerialPortOption>, | |
124 | &option, ec); | |
125 | } | |
126 | ||
127 | // Get an option from the serial port. | |
128 | template <typename GettableSerialPortOption> | |
129 | boost::system::error_code get_option(const implementation_type& impl, | |
130 | GettableSerialPortOption& option, boost::system::error_code& ec) const | |
131 | { | |
132 | return do_get_option(impl, | |
133 | &win_iocp_serial_port_service::load_option<GettableSerialPortOption>, | |
134 | &option, ec); | |
135 | } | |
136 | ||
137 | // Send a break sequence to the serial port. | |
138 | boost::system::error_code send_break(implementation_type&, | |
139 | boost::system::error_code& ec) | |
140 | { | |
141 | ec = boost::asio::error::operation_not_supported; | |
142 | return ec; | |
143 | } | |
144 | ||
145 | // Write the given data. Returns the number of bytes sent. | |
146 | template <typename ConstBufferSequence> | |
147 | size_t write_some(implementation_type& impl, | |
148 | const ConstBufferSequence& buffers, boost::system::error_code& ec) | |
149 | { | |
150 | return handle_service_.write_some(impl, buffers, ec); | |
151 | } | |
152 | ||
153 | // Start an asynchronous write. The data being written must be valid for the | |
154 | // lifetime of the asynchronous operation. | |
155 | template <typename ConstBufferSequence, typename Handler> | |
156 | void async_write_some(implementation_type& impl, | |
157 | const ConstBufferSequence& buffers, Handler& handler) | |
158 | { | |
159 | handle_service_.async_write_some(impl, buffers, handler); | |
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. | |
172 | template <typename MutableBufferSequence, typename Handler> | |
173 | void async_read_some(implementation_type& impl, | |
174 | const MutableBufferSequence& buffers, Handler& handler) | |
175 | { | |
176 | handle_service_.async_read_some(impl, buffers, handler); | |
177 | } | |
178 | ||
179 | private: | |
180 | // Function pointer type for storing a serial port option. | |
181 | typedef boost::system::error_code (*store_function_type)( | |
182 | const void*, ::DCB&, boost::system::error_code&); | |
183 | ||
184 | // Helper function template to store a serial port option. | |
185 | template <typename SettableSerialPortOption> | |
186 | static boost::system::error_code store_option(const void* option, | |
187 | ::DCB& storage, boost::system::error_code& ec) | |
188 | { | |
b32b8144 FG |
189 | static_cast<const SettableSerialPortOption*>(option)->store(storage, ec); |
190 | return ec; | |
7c673cae FG |
191 | } |
192 | ||
193 | // Helper function to set a serial port option. | |
194 | BOOST_ASIO_DECL boost::system::error_code do_set_option( | |
195 | implementation_type& impl, store_function_type store, | |
196 | const void* option, boost::system::error_code& ec); | |
197 | ||
198 | // Function pointer type for loading a serial port option. | |
199 | typedef boost::system::error_code (*load_function_type)( | |
200 | void*, const ::DCB&, boost::system::error_code&); | |
201 | ||
202 | // Helper function template to load a serial port option. | |
203 | template <typename GettableSerialPortOption> | |
204 | static boost::system::error_code load_option(void* option, | |
205 | const ::DCB& storage, boost::system::error_code& ec) | |
206 | { | |
b32b8144 FG |
207 | static_cast<GettableSerialPortOption*>(option)->load(storage, ec); |
208 | return ec; | |
7c673cae FG |
209 | } |
210 | ||
211 | // Helper function to get a serial port option. | |
212 | BOOST_ASIO_DECL boost::system::error_code do_get_option( | |
213 | const implementation_type& impl, load_function_type load, | |
214 | void* option, boost::system::error_code& ec) const; | |
215 | ||
216 | // The implementation used for initiating asynchronous operations. | |
217 | win_iocp_handle_service handle_service_; | |
218 | }; | |
219 | ||
220 | } // namespace detail | |
221 | } // namespace asio | |
222 | } // namespace boost | |
223 | ||
224 | #include <boost/asio/detail/pop_options.hpp> | |
225 | ||
226 | #if defined(BOOST_ASIO_HEADER_ONLY) | |
227 | # include <boost/asio/detail/impl/win_iocp_serial_port_service.ipp> | |
228 | #endif // defined(BOOST_ASIO_HEADER_ONLY) | |
229 | ||
230 | #endif // defined(BOOST_ASIO_HAS_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT) | |
231 | ||
232 | #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SERIAL_PORT_SERVICE_HPP |