]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/raw_socket_service.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / raw_socket_service.hpp
1 //
2 // raw_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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
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>
19
20 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
21
22 #include <cstddef>
23 #include <boost/asio/async_result.hpp>
24 #include <boost/asio/detail/type_traits.hpp>
25 #include <boost/asio/error.hpp>
26 #include <boost/asio/io_context.hpp>
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)
45 : public boost::asio::io_context::service
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.
53 static boost::asio::io_context::id id;
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
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
87 /// Construct a new raw socket service for the specified io_context.
88 explicit raw_socket_service(boost::asio::io_context& io_context)
89 : boost::asio::detail::service_base<
90 raw_socket_service<Protocol> >(io_context),
91 service_impl_(io_context)
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
117 // All socket services have access to each other's implementations.
118 template <typename Protocol1> friend class raw_socket_service;
119
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,
124 raw_socket_service<Protocol1>& other_service,
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>(
131 impl, other_service.service_impl_, other_impl);
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.
142 BOOST_ASIO_SYNC_OP_VOID open(implementation_type& impl,
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;
149 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
150 }
151
152 /// Assign an existing native socket to a raw socket.
153 BOOST_ASIO_SYNC_OP_VOID assign(implementation_type& impl,
154 const protocol_type& protocol, const native_handle_type& native_socket,
155 boost::system::error_code& ec)
156 {
157 service_impl_.assign(impl, protocol, native_socket, ec);
158 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
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.
168 BOOST_ASIO_SYNC_OP_VOID close(implementation_type& impl,
169 boost::system::error_code& ec)
170 {
171 service_impl_.close(impl, ec);
172 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
173 }
174
175 /// Release ownership of the underlying socket.
176 native_handle_type release(implementation_type& impl,
177 boost::system::error_code& ec)
178 {
179 return service_impl_.release(impl, ec);
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.
189 BOOST_ASIO_SYNC_OP_VOID cancel(implementation_type& impl,
190 boost::system::error_code& ec)
191 {
192 service_impl_.cancel(impl, ec);
193 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
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.
211 BOOST_ASIO_SYNC_OP_VOID bind(implementation_type& impl,
212 const endpoint_type& endpoint, boost::system::error_code& ec)
213 {
214 service_impl_.bind(impl, endpoint, ec);
215 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
216 }
217
218 /// Connect the raw socket to the specified endpoint.
219 BOOST_ASIO_SYNC_OP_VOID connect(implementation_type& impl,
220 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
221 {
222 service_impl_.connect(impl, peer_endpoint, ec);
223 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
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 {
234 async_completion<ConnectHandler,
235 void (boost::system::error_code)> init(handler);
236
237 service_impl_.async_connect(impl, peer_endpoint, init.completion_handler);
238
239 return init.result.get();
240 }
241
242 /// Set a socket option.
243 template <typename SettableSocketOption>
244 BOOST_ASIO_SYNC_OP_VOID set_option(implementation_type& impl,
245 const SettableSocketOption& option, boost::system::error_code& ec)
246 {
247 service_impl_.set_option(impl, option, ec);
248 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
249 }
250
251 /// Get a socket option.
252 template <typename GettableSocketOption>
253 BOOST_ASIO_SYNC_OP_VOID get_option(const implementation_type& impl,
254 GettableSocketOption& option, boost::system::error_code& ec) const
255 {
256 service_impl_.get_option(impl, option, ec);
257 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
258 }
259
260 /// Perform an IO control command on the socket.
261 template <typename IoControlCommand>
262 BOOST_ASIO_SYNC_OP_VOID io_control(implementation_type& impl,
263 IoControlCommand& command, boost::system::error_code& ec)
264 {
265 service_impl_.io_control(impl, command, ec);
266 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
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.
276 BOOST_ASIO_SYNC_OP_VOID non_blocking(implementation_type& impl,
277 bool mode, boost::system::error_code& ec)
278 {
279 service_impl_.non_blocking(impl, mode, ec);
280 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
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.
290 BOOST_ASIO_SYNC_OP_VOID native_non_blocking(implementation_type& impl,
291 bool mode, boost::system::error_code& ec)
292 {
293 service_impl_.native_non_blocking(impl, mode, ec);
294 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
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.
312 BOOST_ASIO_SYNC_OP_VOID shutdown(implementation_type& impl,
313 socket_base::shutdown_type what, boost::system::error_code& ec)
314 {
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();
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 {
361 async_completion<WriteHandler,
362 void (boost::system::error_code, std::size_t)> init(handler);
363
364 service_impl_.async_send(impl, buffers, flags, init.completion_handler);
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 {
387 async_completion<WriteHandler,
388 void (boost::system::error_code, std::size_t)> init(handler);
389
390 service_impl_.async_send_to(impl, buffers,
391 destination, flags, init.completion_handler);
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 {
414 async_completion<ReadHandler,
415 void (boost::system::error_code, std::size_t)> init(handler);
416
417 service_impl_.async_receive(impl, buffers, flags, init.completion_handler);
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 {
441 async_completion<ReadHandler,
442 void (boost::system::error_code, std::size_t)> init(handler);
443
444 service_impl_.async_receive_from(impl, buffers,
445 sender_endpoint, flags, init.completion_handler);
446
447 return init.result.get();
448 }
449
450 private:
451 // Destroy all user-defined handler objects owned by the service.
452 void shutdown()
453 {
454 service_impl_.shutdown();
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
466 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
467
468 #endif // BOOST_ASIO_RAW_SOCKET_SERVICE_HPP