]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // raw_socket_service.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
11fdf7f2 | 5 | // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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 | ||
11 | #ifndef BOOST_ASIO_RAW_SOCKET_SERVICE_HPP | |
12 | #define BOOST_ASIO_RAW_SOCKET_SERVICE_HPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
b32b8144 FG |
19 | |
20 | #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) | |
21 | ||
7c673cae FG |
22 | #include <cstddef> |
23 | #include <boost/asio/async_result.hpp> | |
24 | #include <boost/asio/detail/type_traits.hpp> | |
25 | #include <boost/asio/error.hpp> | |
b32b8144 | 26 | #include <boost/asio/io_context.hpp> |
7c673cae FG |
27 | |
28 | #if defined(BOOST_ASIO_WINDOWS_RUNTIME) | |
29 | # include <boost/asio/detail/null_socket_service.hpp> | |
30 | #elif defined(BOOST_ASIO_HAS_IOCP) | |
31 | # include <boost/asio/detail/win_iocp_socket_service.hpp> | |
32 | #else | |
33 | # include <boost/asio/detail/reactive_socket_service.hpp> | |
34 | #endif | |
35 | ||
36 | #include <boost/asio/detail/push_options.hpp> | |
37 | ||
38 | namespace boost { | |
39 | namespace asio { | |
40 | ||
41 | /// Default service implementation for a raw socket. | |
42 | template <typename Protocol> | |
43 | class raw_socket_service | |
44 | #if defined(GENERATING_DOCUMENTATION) | |
b32b8144 | 45 | : public boost::asio::io_context::service |
7c673cae FG |
46 | #else |
47 | : public boost::asio::detail::service_base<raw_socket_service<Protocol> > | |
48 | #endif | |
49 | { | |
50 | public: | |
51 | #if defined(GENERATING_DOCUMENTATION) | |
52 | /// The unique service identifier. | |
b32b8144 | 53 | static boost::asio::io_context::id id; |
7c673cae FG |
54 | #endif |
55 | ||
56 | /// The protocol type. | |
57 | typedef Protocol protocol_type; | |
58 | ||
59 | /// The endpoint type. | |
60 | typedef typename Protocol::endpoint endpoint_type; | |
61 | ||
62 | private: | |
63 | // The type of the platform-specific implementation. | |
64 | #if defined(BOOST_ASIO_WINDOWS_RUNTIME) | |
65 | typedef detail::null_socket_service<Protocol> service_impl_type; | |
66 | #elif defined(BOOST_ASIO_HAS_IOCP) | |
67 | typedef detail::win_iocp_socket_service<Protocol> service_impl_type; | |
68 | #else | |
69 | typedef detail::reactive_socket_service<Protocol> service_impl_type; | |
70 | #endif | |
71 | ||
72 | public: | |
73 | /// The type of a raw socket. | |
74 | #if defined(GENERATING_DOCUMENTATION) | |
75 | typedef implementation_defined implementation_type; | |
76 | #else | |
77 | typedef typename service_impl_type::implementation_type implementation_type; | |
78 | #endif | |
79 | ||
7c673cae FG |
80 | /// The native socket type. |
81 | #if defined(GENERATING_DOCUMENTATION) | |
82 | typedef implementation_defined native_handle_type; | |
83 | #else | |
84 | typedef typename service_impl_type::native_handle_type native_handle_type; | |
85 | #endif | |
86 | ||
b32b8144 FG |
87 | /// Construct a new raw socket service for the specified io_context. |
88 | explicit raw_socket_service(boost::asio::io_context& io_context) | |
7c673cae | 89 | : boost::asio::detail::service_base< |
b32b8144 FG |
90 | raw_socket_service<Protocol> >(io_context), |
91 | service_impl_(io_context) | |
7c673cae FG |
92 | { |
93 | } | |
94 | ||
95 | /// Construct a new raw socket implementation. | |
96 | void construct(implementation_type& impl) | |
97 | { | |
98 | service_impl_.construct(impl); | |
99 | } | |
100 | ||
101 | #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
102 | /// Move-construct a new raw socket implementation. | |
103 | void move_construct(implementation_type& impl, | |
104 | implementation_type& other_impl) | |
105 | { | |
106 | service_impl_.move_construct(impl, other_impl); | |
107 | } | |
108 | ||
109 | /// Move-assign from another raw socket implementation. | |
110 | void move_assign(implementation_type& impl, | |
111 | raw_socket_service& other_service, | |
112 | implementation_type& other_impl) | |
113 | { | |
114 | service_impl_.move_assign(impl, other_service.service_impl_, other_impl); | |
115 | } | |
116 | ||
b32b8144 FG |
117 | // All socket services have access to each other's implementations. |
118 | template <typename Protocol1> friend class raw_socket_service; | |
119 | ||
7c673cae FG |
120 | /// Move-construct a new raw socket implementation from another protocol |
121 | /// type. | |
122 | template <typename Protocol1> | |
123 | void converting_move_construct(implementation_type& impl, | |
b32b8144 | 124 | raw_socket_service<Protocol1>& other_service, |
7c673cae FG |
125 | typename raw_socket_service< |
126 | Protocol1>::implementation_type& other_impl, | |
127 | typename enable_if<is_convertible< | |
128 | Protocol1, Protocol>::value>::type* = 0) | |
129 | { | |
130 | service_impl_.template converting_move_construct<Protocol1>( | |
b32b8144 | 131 | impl, other_service.service_impl_, other_impl); |
7c673cae FG |
132 | } |
133 | #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
134 | ||
135 | /// Destroy a raw socket implementation. | |
136 | void destroy(implementation_type& impl) | |
137 | { | |
138 | service_impl_.destroy(impl); | |
139 | } | |
140 | ||
141 | // Open a new raw socket implementation. | |
b32b8144 | 142 | BOOST_ASIO_SYNC_OP_VOID open(implementation_type& impl, |
7c673cae FG |
143 | const protocol_type& protocol, boost::system::error_code& ec) |
144 | { | |
145 | if (protocol.type() == BOOST_ASIO_OS_DEF(SOCK_RAW)) | |
146 | service_impl_.open(impl, protocol, ec); | |
147 | else | |
148 | ec = boost::asio::error::invalid_argument; | |
b32b8144 | 149 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); |
7c673cae FG |
150 | } |
151 | ||
152 | /// Assign an existing native socket to a raw socket. | |
b32b8144 | 153 | BOOST_ASIO_SYNC_OP_VOID assign(implementation_type& impl, |
7c673cae FG |
154 | const protocol_type& protocol, const native_handle_type& native_socket, |
155 | boost::system::error_code& ec) | |
156 | { | |
b32b8144 FG |
157 | service_impl_.assign(impl, protocol, native_socket, ec); |
158 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
159 | } |
160 | ||
161 | /// Determine whether the socket is open. | |
162 | bool is_open(const implementation_type& impl) const | |
163 | { | |
164 | return service_impl_.is_open(impl); | |
165 | } | |
166 | ||
167 | /// Close a raw socket implementation. | |
b32b8144 | 168 | BOOST_ASIO_SYNC_OP_VOID close(implementation_type& impl, |
7c673cae FG |
169 | boost::system::error_code& ec) |
170 | { | |
b32b8144 FG |
171 | service_impl_.close(impl, ec); |
172 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
173 | } |
174 | ||
b32b8144 FG |
175 | /// Release ownership of the underlying socket. |
176 | native_handle_type release(implementation_type& impl, | |
177 | boost::system::error_code& ec) | |
7c673cae | 178 | { |
b32b8144 | 179 | return service_impl_.release(impl, ec); |
7c673cae FG |
180 | } |
181 | ||
182 | /// Get the native socket implementation. | |
183 | native_handle_type native_handle(implementation_type& impl) | |
184 | { | |
185 | return service_impl_.native_handle(impl); | |
186 | } | |
187 | ||
188 | /// Cancel all asynchronous operations associated with the socket. | |
b32b8144 | 189 | BOOST_ASIO_SYNC_OP_VOID cancel(implementation_type& impl, |
7c673cae FG |
190 | boost::system::error_code& ec) |
191 | { | |
b32b8144 FG |
192 | service_impl_.cancel(impl, ec); |
193 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
194 | } |
195 | ||
196 | /// Determine whether the socket is at the out-of-band data mark. | |
197 | bool at_mark(const implementation_type& impl, | |
198 | boost::system::error_code& ec) const | |
199 | { | |
200 | return service_impl_.at_mark(impl, ec); | |
201 | } | |
202 | ||
203 | /// Determine the number of bytes available for reading. | |
204 | std::size_t available(const implementation_type& impl, | |
205 | boost::system::error_code& ec) const | |
206 | { | |
207 | return service_impl_.available(impl, ec); | |
208 | } | |
209 | ||
210 | // Bind the raw socket to the specified local endpoint. | |
b32b8144 | 211 | BOOST_ASIO_SYNC_OP_VOID bind(implementation_type& impl, |
7c673cae FG |
212 | const endpoint_type& endpoint, boost::system::error_code& ec) |
213 | { | |
b32b8144 FG |
214 | service_impl_.bind(impl, endpoint, ec); |
215 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
216 | } |
217 | ||
218 | /// Connect the raw socket to the specified endpoint. | |
b32b8144 | 219 | BOOST_ASIO_SYNC_OP_VOID connect(implementation_type& impl, |
7c673cae FG |
220 | const endpoint_type& peer_endpoint, boost::system::error_code& ec) |
221 | { | |
b32b8144 FG |
222 | service_impl_.connect(impl, peer_endpoint, ec); |
223 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
224 | } |
225 | ||
226 | /// Start an asynchronous connect. | |
227 | template <typename ConnectHandler> | |
228 | BOOST_ASIO_INITFN_RESULT_TYPE(ConnectHandler, | |
229 | void (boost::system::error_code)) | |
230 | async_connect(implementation_type& impl, | |
231 | const endpoint_type& peer_endpoint, | |
232 | BOOST_ASIO_MOVE_ARG(ConnectHandler) handler) | |
233 | { | |
b32b8144 FG |
234 | async_completion<ConnectHandler, |
235 | void (boost::system::error_code)> init(handler); | |
7c673cae | 236 | |
b32b8144 | 237 | service_impl_.async_connect(impl, peer_endpoint, init.completion_handler); |
7c673cae FG |
238 | |
239 | return init.result.get(); | |
240 | } | |
241 | ||
242 | /// Set a socket option. | |
243 | template <typename SettableSocketOption> | |
b32b8144 | 244 | BOOST_ASIO_SYNC_OP_VOID set_option(implementation_type& impl, |
7c673cae FG |
245 | const SettableSocketOption& option, boost::system::error_code& ec) |
246 | { | |
b32b8144 FG |
247 | service_impl_.set_option(impl, option, ec); |
248 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
249 | } |
250 | ||
251 | /// Get a socket option. | |
252 | template <typename GettableSocketOption> | |
b32b8144 | 253 | BOOST_ASIO_SYNC_OP_VOID get_option(const implementation_type& impl, |
7c673cae FG |
254 | GettableSocketOption& option, boost::system::error_code& ec) const |
255 | { | |
b32b8144 FG |
256 | service_impl_.get_option(impl, option, ec); |
257 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
258 | } |
259 | ||
260 | /// Perform an IO control command on the socket. | |
261 | template <typename IoControlCommand> | |
b32b8144 | 262 | BOOST_ASIO_SYNC_OP_VOID io_control(implementation_type& impl, |
7c673cae FG |
263 | IoControlCommand& command, boost::system::error_code& ec) |
264 | { | |
b32b8144 FG |
265 | service_impl_.io_control(impl, command, ec); |
266 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
267 | } |
268 | ||
269 | /// Gets the non-blocking mode of the socket. | |
270 | bool non_blocking(const implementation_type& impl) const | |
271 | { | |
272 | return service_impl_.non_blocking(impl); | |
273 | } | |
274 | ||
275 | /// Sets the non-blocking mode of the socket. | |
b32b8144 | 276 | BOOST_ASIO_SYNC_OP_VOID non_blocking(implementation_type& impl, |
7c673cae FG |
277 | bool mode, boost::system::error_code& ec) |
278 | { | |
b32b8144 FG |
279 | service_impl_.non_blocking(impl, mode, ec); |
280 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
281 | } |
282 | ||
283 | /// Gets the non-blocking mode of the native socket implementation. | |
284 | bool native_non_blocking(const implementation_type& impl) const | |
285 | { | |
286 | return service_impl_.native_non_blocking(impl); | |
287 | } | |
288 | ||
289 | /// Sets the non-blocking mode of the native socket implementation. | |
b32b8144 | 290 | BOOST_ASIO_SYNC_OP_VOID native_non_blocking(implementation_type& impl, |
7c673cae FG |
291 | bool mode, boost::system::error_code& ec) |
292 | { | |
b32b8144 FG |
293 | service_impl_.native_non_blocking(impl, mode, ec); |
294 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
295 | } |
296 | ||
297 | /// Get the local endpoint. | |
298 | endpoint_type local_endpoint(const implementation_type& impl, | |
299 | boost::system::error_code& ec) const | |
300 | { | |
301 | return service_impl_.local_endpoint(impl, ec); | |
302 | } | |
303 | ||
304 | /// Get the remote endpoint. | |
305 | endpoint_type remote_endpoint(const implementation_type& impl, | |
306 | boost::system::error_code& ec) const | |
307 | { | |
308 | return service_impl_.remote_endpoint(impl, ec); | |
309 | } | |
310 | ||
311 | /// Disable sends or receives on the socket. | |
b32b8144 | 312 | BOOST_ASIO_SYNC_OP_VOID shutdown(implementation_type& impl, |
7c673cae FG |
313 | socket_base::shutdown_type what, boost::system::error_code& ec) |
314 | { | |
b32b8144 FG |
315 | service_impl_.shutdown(impl, what, ec); |
316 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
317 | } | |
318 | ||
319 | /// Wait for the socket to become ready to read, ready to write, or to have | |
320 | /// pending error conditions. | |
321 | BOOST_ASIO_SYNC_OP_VOID wait(implementation_type& impl, | |
322 | socket_base::wait_type w, boost::system::error_code& ec) | |
323 | { | |
324 | service_impl_.wait(impl, w, ec); | |
325 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
326 | } | |
327 | ||
328 | /// Asynchronously wait for the socket to become ready to read, ready to | |
329 | /// write, or to have pending error conditions. | |
330 | template <typename WaitHandler> | |
331 | BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler, | |
332 | void (boost::system::error_code)) | |
333 | async_wait(implementation_type& impl, socket_base::wait_type w, | |
334 | BOOST_ASIO_MOVE_ARG(WaitHandler) handler) | |
335 | { | |
336 | async_completion<WaitHandler, | |
337 | void (boost::system::error_code)> init(handler); | |
338 | ||
339 | service_impl_.async_wait(impl, w, init.completion_handler); | |
340 | ||
341 | return init.result.get(); | |
7c673cae FG |
342 | } |
343 | ||
344 | /// Send the given data to the peer. | |
345 | template <typename ConstBufferSequence> | |
346 | std::size_t send(implementation_type& impl, | |
347 | const ConstBufferSequence& buffers, | |
348 | socket_base::message_flags flags, boost::system::error_code& ec) | |
349 | { | |
350 | return service_impl_.send(impl, buffers, flags, ec); | |
351 | } | |
352 | ||
353 | /// Start an asynchronous send. | |
354 | template <typename ConstBufferSequence, typename WriteHandler> | |
355 | BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, | |
356 | void (boost::system::error_code, std::size_t)) | |
357 | async_send(implementation_type& impl, const ConstBufferSequence& buffers, | |
358 | socket_base::message_flags flags, | |
359 | BOOST_ASIO_MOVE_ARG(WriteHandler) handler) | |
360 | { | |
b32b8144 FG |
361 | async_completion<WriteHandler, |
362 | void (boost::system::error_code, std::size_t)> init(handler); | |
7c673cae | 363 | |
b32b8144 | 364 | service_impl_.async_send(impl, buffers, flags, init.completion_handler); |
7c673cae FG |
365 | |
366 | return init.result.get(); | |
367 | } | |
368 | ||
369 | /// Send raw data to the specified endpoint. | |
370 | template <typename ConstBufferSequence> | |
371 | std::size_t send_to(implementation_type& impl, | |
372 | const ConstBufferSequence& buffers, const endpoint_type& destination, | |
373 | socket_base::message_flags flags, boost::system::error_code& ec) | |
374 | { | |
375 | return service_impl_.send_to(impl, buffers, destination, flags, ec); | |
376 | } | |
377 | ||
378 | /// Start an asynchronous send. | |
379 | template <typename ConstBufferSequence, typename WriteHandler> | |
380 | BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, | |
381 | void (boost::system::error_code, std::size_t)) | |
382 | async_send_to(implementation_type& impl, | |
383 | const ConstBufferSequence& buffers, const endpoint_type& destination, | |
384 | socket_base::message_flags flags, | |
385 | BOOST_ASIO_MOVE_ARG(WriteHandler) handler) | |
386 | { | |
b32b8144 FG |
387 | async_completion<WriteHandler, |
388 | void (boost::system::error_code, std::size_t)> init(handler); | |
7c673cae FG |
389 | |
390 | service_impl_.async_send_to(impl, buffers, | |
b32b8144 | 391 | destination, flags, init.completion_handler); |
7c673cae FG |
392 | |
393 | return init.result.get(); | |
394 | } | |
395 | ||
396 | /// Receive some data from the peer. | |
397 | template <typename MutableBufferSequence> | |
398 | std::size_t receive(implementation_type& impl, | |
399 | const MutableBufferSequence& buffers, | |
400 | socket_base::message_flags flags, boost::system::error_code& ec) | |
401 | { | |
402 | return service_impl_.receive(impl, buffers, flags, ec); | |
403 | } | |
404 | ||
405 | /// Start an asynchronous receive. | |
406 | template <typename MutableBufferSequence, typename ReadHandler> | |
407 | BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, | |
408 | void (boost::system::error_code, std::size_t)) | |
409 | async_receive(implementation_type& impl, | |
410 | const MutableBufferSequence& buffers, | |
411 | socket_base::message_flags flags, | |
412 | BOOST_ASIO_MOVE_ARG(ReadHandler) handler) | |
413 | { | |
b32b8144 FG |
414 | async_completion<ReadHandler, |
415 | void (boost::system::error_code, std::size_t)> init(handler); | |
7c673cae | 416 | |
b32b8144 | 417 | service_impl_.async_receive(impl, buffers, flags, init.completion_handler); |
7c673cae FG |
418 | |
419 | return init.result.get(); | |
420 | } | |
421 | ||
422 | /// Receive raw data with the endpoint of the sender. | |
423 | template <typename MutableBufferSequence> | |
424 | std::size_t receive_from(implementation_type& impl, | |
425 | const MutableBufferSequence& buffers, endpoint_type& sender_endpoint, | |
426 | socket_base::message_flags flags, boost::system::error_code& ec) | |
427 | { | |
428 | return service_impl_.receive_from(impl, buffers, sender_endpoint, flags, | |
429 | ec); | |
430 | } | |
431 | ||
432 | /// Start an asynchronous receive that will get the endpoint of the sender. | |
433 | template <typename MutableBufferSequence, typename ReadHandler> | |
434 | BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, | |
435 | void (boost::system::error_code, std::size_t)) | |
436 | async_receive_from(implementation_type& impl, | |
437 | const MutableBufferSequence& buffers, endpoint_type& sender_endpoint, | |
438 | socket_base::message_flags flags, | |
439 | BOOST_ASIO_MOVE_ARG(ReadHandler) handler) | |
440 | { | |
b32b8144 FG |
441 | async_completion<ReadHandler, |
442 | void (boost::system::error_code, std::size_t)> init(handler); | |
7c673cae FG |
443 | |
444 | service_impl_.async_receive_from(impl, buffers, | |
b32b8144 | 445 | sender_endpoint, flags, init.completion_handler); |
7c673cae FG |
446 | |
447 | return init.result.get(); | |
448 | } | |
449 | ||
450 | private: | |
451 | // Destroy all user-defined handler objects owned by the service. | |
b32b8144 | 452 | void shutdown() |
7c673cae | 453 | { |
b32b8144 | 454 | service_impl_.shutdown(); |
7c673cae FG |
455 | } |
456 | ||
457 | // The platform-specific implementation. | |
458 | service_impl_type service_impl_; | |
459 | }; | |
460 | ||
461 | } // namespace asio | |
462 | } // namespace boost | |
463 | ||
464 | #include <boost/asio/detail/pop_options.hpp> | |
465 | ||
b32b8144 FG |
466 | #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) |
467 | ||
7c673cae | 468 | #endif // BOOST_ASIO_RAW_SOCKET_SERVICE_HPP |