]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/reactive_socket_service.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / detail / reactive_socket_service.hpp
1 //
2 // detail/reactive_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_DETAIL_REACTIVE_SOCKET_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_REACTIVE_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_HAS_IOCP)
21
22 #include <boost/asio/buffer.hpp>
23 #include <boost/asio/error.hpp>
24 #include <boost/asio/io_context.hpp>
25 #include <boost/asio/socket_base.hpp>
26 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
27 #include <boost/asio/detail/memory.hpp>
28 #include <boost/asio/detail/noncopyable.hpp>
29 #include <boost/asio/detail/reactive_null_buffers_op.hpp>
30 #include <boost/asio/detail/reactive_socket_accept_op.hpp>
31 #include <boost/asio/detail/reactive_socket_connect_op.hpp>
32 #include <boost/asio/detail/reactive_socket_recvfrom_op.hpp>
33 #include <boost/asio/detail/reactive_socket_sendto_op.hpp>
34 #include <boost/asio/detail/reactive_socket_service_base.hpp>
35 #include <boost/asio/detail/reactor.hpp>
36 #include <boost/asio/detail/reactor_op.hpp>
37 #include <boost/asio/detail/socket_holder.hpp>
38 #include <boost/asio/detail/socket_ops.hpp>
39 #include <boost/asio/detail/socket_types.hpp>
40
41 #include <boost/asio/detail/push_options.hpp>
42
43 namespace boost {
44 namespace asio {
45 namespace detail {
46
47 template <typename Protocol>
48 class reactive_socket_service :
49 public service_base<reactive_socket_service<Protocol> >,
50 public reactive_socket_service_base
51 {
52 public:
53 // The protocol type.
54 typedef Protocol protocol_type;
55
56 // The endpoint type.
57 typedef typename Protocol::endpoint endpoint_type;
58
59 // The native type of a socket.
60 typedef socket_type native_handle_type;
61
62 // The implementation type of the socket.
63 struct implementation_type :
64 reactive_socket_service_base::base_implementation_type
65 {
66 // Default constructor.
67 implementation_type()
68 : protocol_(endpoint_type().protocol())
69 {
70 }
71
72 // The protocol associated with the socket.
73 protocol_type protocol_;
74 };
75
76 // Constructor.
77 reactive_socket_service(boost::asio::io_context& io_context)
78 : service_base<reactive_socket_service<Protocol> >(io_context),
79 reactive_socket_service_base(io_context)
80 {
81 }
82
83 // Destroy all user-defined handler objects owned by the service.
84 void shutdown()
85 {
86 this->base_shutdown();
87 }
88
89 // Move-construct a new socket implementation.
90 void move_construct(implementation_type& impl,
91 implementation_type& other_impl)
92 {
93 this->base_move_construct(impl, other_impl);
94
95 impl.protocol_ = other_impl.protocol_;
96 other_impl.protocol_ = endpoint_type().protocol();
97 }
98
99 // Move-assign from another socket implementation.
100 void move_assign(implementation_type& impl,
101 reactive_socket_service_base& other_service,
102 implementation_type& other_impl)
103 {
104 this->base_move_assign(impl, other_service, other_impl);
105
106 impl.protocol_ = other_impl.protocol_;
107 other_impl.protocol_ = endpoint_type().protocol();
108 }
109
110 // Move-construct a new socket implementation from another protocol type.
111 template <typename Protocol1>
112 void converting_move_construct(implementation_type& impl,
113 reactive_socket_service<Protocol1>&,
114 typename reactive_socket_service<
115 Protocol1>::implementation_type& other_impl)
116 {
117 this->base_move_construct(impl, other_impl);
118
119 impl.protocol_ = protocol_type(other_impl.protocol_);
120 other_impl.protocol_ = typename Protocol1::endpoint().protocol();
121 }
122
123 // Open a new socket implementation.
124 boost::system::error_code open(implementation_type& impl,
125 const protocol_type& protocol, boost::system::error_code& ec)
126 {
127 if (!do_open(impl, protocol.family(),
128 protocol.type(), protocol.protocol(), ec))
129 impl.protocol_ = protocol;
130 return ec;
131 }
132
133 // Assign a native socket to a socket implementation.
134 boost::system::error_code assign(implementation_type& impl,
135 const protocol_type& protocol, const native_handle_type& native_socket,
136 boost::system::error_code& ec)
137 {
138 if (!do_assign(impl, protocol.type(), native_socket, ec))
139 impl.protocol_ = protocol;
140 return ec;
141 }
142
143 // Get the native socket representation.
144 native_handle_type native_handle(implementation_type& impl)
145 {
146 return impl.socket_;
147 }
148
149 // Bind the socket to the specified local endpoint.
150 boost::system::error_code bind(implementation_type& impl,
151 const endpoint_type& endpoint, boost::system::error_code& ec)
152 {
153 socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
154 return ec;
155 }
156
157 // Set a socket option.
158 template <typename Option>
159 boost::system::error_code set_option(implementation_type& impl,
160 const Option& option, boost::system::error_code& ec)
161 {
162 socket_ops::setsockopt(impl.socket_, impl.state_,
163 option.level(impl.protocol_), option.name(impl.protocol_),
164 option.data(impl.protocol_), option.size(impl.protocol_), ec);
165 return ec;
166 }
167
168 // Set a socket option.
169 template <typename Option>
170 boost::system::error_code get_option(const implementation_type& impl,
171 Option& option, boost::system::error_code& ec) const
172 {
173 std::size_t size = option.size(impl.protocol_);
174 socket_ops::getsockopt(impl.socket_, impl.state_,
175 option.level(impl.protocol_), option.name(impl.protocol_),
176 option.data(impl.protocol_), &size, ec);
177 if (!ec)
178 option.resize(impl.protocol_, size);
179 return ec;
180 }
181
182 // Get the local endpoint.
183 endpoint_type local_endpoint(const implementation_type& impl,
184 boost::system::error_code& ec) const
185 {
186 endpoint_type endpoint;
187 std::size_t addr_len = endpoint.capacity();
188 if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec))
189 return endpoint_type();
190 endpoint.resize(addr_len);
191 return endpoint;
192 }
193
194 // Get the remote endpoint.
195 endpoint_type remote_endpoint(const implementation_type& impl,
196 boost::system::error_code& ec) const
197 {
198 endpoint_type endpoint;
199 std::size_t addr_len = endpoint.capacity();
200 if (socket_ops::getpeername(impl.socket_,
201 endpoint.data(), &addr_len, false, ec))
202 return endpoint_type();
203 endpoint.resize(addr_len);
204 return endpoint;
205 }
206
207 // Disable sends or receives on the socket.
208 boost::system::error_code shutdown(base_implementation_type& impl,
209 socket_base::shutdown_type what, boost::system::error_code& ec)
210 {
211 socket_ops::shutdown(impl.socket_, what, ec);
212 return ec;
213 }
214
215 // Send a datagram to the specified endpoint. Returns the number of bytes
216 // sent.
217 template <typename ConstBufferSequence>
218 size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
219 const endpoint_type& destination, socket_base::message_flags flags,
220 boost::system::error_code& ec)
221 {
222 buffer_sequence_adapter<boost::asio::const_buffer,
223 ConstBufferSequence> bufs(buffers);
224
225 return socket_ops::sync_sendto(impl.socket_, impl.state_,
226 bufs.buffers(), bufs.count(), flags,
227 destination.data(), destination.size(), ec);
228 }
229
230 // Wait until data can be sent without blocking.
231 size_t send_to(implementation_type& impl, const null_buffers&,
232 const endpoint_type&, socket_base::message_flags,
233 boost::system::error_code& ec)
234 {
235 // Wait for socket to become ready.
236 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
237
238 return 0;
239 }
240
241 // Start an asynchronous send. The data being sent must be valid for the
242 // lifetime of the asynchronous operation.
243 template <typename ConstBufferSequence, typename Handler>
244 void async_send_to(implementation_type& impl,
245 const ConstBufferSequence& buffers,
246 const endpoint_type& destination, socket_base::message_flags flags,
247 Handler& handler)
248 {
249 bool is_continuation =
250 boost_asio_handler_cont_helpers::is_continuation(handler);
251
252 // Allocate and construct an operation to wrap the handler.
253 typedef reactive_socket_sendto_op<ConstBufferSequence,
254 endpoint_type, Handler> op;
255 typename op::ptr p = { boost::asio::detail::addressof(handler),
256 op::ptr::allocate(handler), 0 };
257 p.p = new (p.v) op(impl.socket_, buffers, destination, flags, handler);
258
259 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
260 &impl, impl.socket_, "async_send_to"));
261
262 start_op(impl, reactor::write_op, p.p, is_continuation, true, false);
263 p.v = p.p = 0;
264 }
265
266 // Start an asynchronous wait until data can be sent without blocking.
267 template <typename Handler>
268 void async_send_to(implementation_type& impl, const null_buffers&,
269 const endpoint_type&, socket_base::message_flags, Handler& handler)
270 {
271 bool is_continuation =
272 boost_asio_handler_cont_helpers::is_continuation(handler);
273
274 // Allocate and construct an operation to wrap the handler.
275 typedef reactive_null_buffers_op<Handler> op;
276 typename op::ptr p = { boost::asio::detail::addressof(handler),
277 op::ptr::allocate(handler), 0 };
278 p.p = new (p.v) op(handler);
279
280 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
281 &impl, impl.socket_, "async_send_to(null_buffers)"));
282
283 start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
284 p.v = p.p = 0;
285 }
286
287 // Receive a datagram with the endpoint of the sender. Returns the number of
288 // bytes received.
289 template <typename MutableBufferSequence>
290 size_t receive_from(implementation_type& impl,
291 const MutableBufferSequence& buffers,
292 endpoint_type& sender_endpoint, socket_base::message_flags flags,
293 boost::system::error_code& ec)
294 {
295 buffer_sequence_adapter<boost::asio::mutable_buffer,
296 MutableBufferSequence> bufs(buffers);
297
298 std::size_t addr_len = sender_endpoint.capacity();
299 std::size_t bytes_recvd = socket_ops::sync_recvfrom(
300 impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
301 flags, sender_endpoint.data(), &addr_len, ec);
302
303 if (!ec)
304 sender_endpoint.resize(addr_len);
305
306 return bytes_recvd;
307 }
308
309 // Wait until data can be received without blocking.
310 size_t receive_from(implementation_type& impl, const null_buffers&,
311 endpoint_type& sender_endpoint, socket_base::message_flags,
312 boost::system::error_code& ec)
313 {
314 // Wait for socket to become ready.
315 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
316
317 // Reset endpoint since it can be given no sensible value at this time.
318 sender_endpoint = endpoint_type();
319
320 return 0;
321 }
322
323 // Start an asynchronous receive. The buffer for the data being received and
324 // the sender_endpoint object must both be valid for the lifetime of the
325 // asynchronous operation.
326 template <typename MutableBufferSequence, typename Handler>
327 void async_receive_from(implementation_type& impl,
328 const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
329 socket_base::message_flags flags, Handler& handler)
330 {
331 bool is_continuation =
332 boost_asio_handler_cont_helpers::is_continuation(handler);
333
334 // Allocate and construct an operation to wrap the handler.
335 typedef reactive_socket_recvfrom_op<MutableBufferSequence,
336 endpoint_type, Handler> op;
337 typename op::ptr p = { boost::asio::detail::addressof(handler),
338 op::ptr::allocate(handler), 0 };
339 int protocol = impl.protocol_.type();
340 p.p = new (p.v) op(impl.socket_, protocol,
341 buffers, sender_endpoint, flags, handler);
342
343 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
344 &impl, impl.socket_, "async_receive_from"));
345
346 start_op(impl,
347 (flags & socket_base::message_out_of_band)
348 ? reactor::except_op : reactor::read_op,
349 p.p, is_continuation, true, false);
350 p.v = p.p = 0;
351 }
352
353 // Wait until data can be received without blocking.
354 template <typename Handler>
355 void async_receive_from(implementation_type& impl,
356 const null_buffers&, endpoint_type& sender_endpoint,
357 socket_base::message_flags flags, Handler& handler)
358 {
359 bool is_continuation =
360 boost_asio_handler_cont_helpers::is_continuation(handler);
361
362 // Allocate and construct an operation to wrap the handler.
363 typedef reactive_null_buffers_op<Handler> op;
364 typename op::ptr p = { boost::asio::detail::addressof(handler),
365 op::ptr::allocate(handler), 0 };
366 p.p = new (p.v) op(handler);
367
368 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
369 &impl, impl.socket_, "async_receive_from(null_buffers)"));
370
371 // Reset endpoint since it can be given no sensible value at this time.
372 sender_endpoint = endpoint_type();
373
374 start_op(impl,
375 (flags & socket_base::message_out_of_band)
376 ? reactor::except_op : reactor::read_op,
377 p.p, is_continuation, false, false);
378 p.v = p.p = 0;
379 }
380
381 // Accept a new connection.
382 template <typename Socket>
383 boost::system::error_code accept(implementation_type& impl,
384 Socket& peer, endpoint_type* peer_endpoint, boost::system::error_code& ec)
385 {
386 // We cannot accept a socket that is already open.
387 if (peer.is_open())
388 {
389 ec = boost::asio::error::already_open;
390 return ec;
391 }
392
393 std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
394 socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
395 impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
396 peer_endpoint ? &addr_len : 0, ec));
397
398 // On success, assign new connection to peer socket object.
399 if (new_socket.get() != invalid_socket)
400 {
401 if (peer_endpoint)
402 peer_endpoint->resize(addr_len);
403 peer.assign(impl.protocol_, new_socket.get(), ec);
404 if (!ec)
405 new_socket.release();
406 }
407
408 return ec;
409 }
410
411 #if defined(BOOST_ASIO_HAS_MOVE)
412 // Accept a new connection.
413 typename Protocol::socket accept(implementation_type& impl,
414 io_context* peer_io_context, endpoint_type* peer_endpoint,
415 boost::system::error_code& ec)
416 {
417 typename Protocol::socket peer(
418 peer_io_context ? *peer_io_context : io_context_);
419
420 std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
421 socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
422 impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
423 peer_endpoint ? &addr_len : 0, ec));
424
425 // On success, assign new connection to peer socket object.
426 if (new_socket.get() != invalid_socket)
427 {
428 if (peer_endpoint)
429 peer_endpoint->resize(addr_len);
430 peer.assign(impl.protocol_, new_socket.get(), ec);
431 if (!ec)
432 new_socket.release();
433 }
434
435 return peer;
436 }
437 #endif // defined(BOOST_ASIO_HAS_MOVE)
438
439 // Start an asynchronous accept. The peer and peer_endpoint objects must be
440 // valid until the accept's handler is invoked.
441 template <typename Socket, typename Handler>
442 void async_accept(implementation_type& impl, Socket& peer,
443 endpoint_type* peer_endpoint, Handler& handler)
444 {
445 bool is_continuation =
446 boost_asio_handler_cont_helpers::is_continuation(handler);
447
448 // Allocate and construct an operation to wrap the handler.
449 typedef reactive_socket_accept_op<Socket, Protocol, Handler> op;
450 typename op::ptr p = { boost::asio::detail::addressof(handler),
451 op::ptr::allocate(handler), 0 };
452 p.p = new (p.v) op(impl.socket_, impl.state_, peer,
453 impl.protocol_, peer_endpoint, handler);
454
455 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
456 &impl, impl.socket_, "async_accept"));
457
458 start_accept_op(impl, p.p, is_continuation, peer.is_open());
459 p.v = p.p = 0;
460 }
461
462 #if defined(BOOST_ASIO_HAS_MOVE)
463 // Start an asynchronous accept. The peer_endpoint object must be valid until
464 // the accept's handler is invoked.
465 template <typename Handler>
466 void async_accept(implementation_type& impl,
467 boost::asio::io_context* peer_io_context,
468 endpoint_type* peer_endpoint, Handler& handler)
469 {
470 bool is_continuation =
471 boost_asio_handler_cont_helpers::is_continuation(handler);
472
473 // Allocate and construct an operation to wrap the handler.
474 typedef reactive_socket_move_accept_op<Protocol, Handler> op;
475 typename op::ptr p = { boost::asio::detail::addressof(handler),
476 op::ptr::allocate(handler), 0 };
477 p.p = new (p.v) op(peer_io_context ? *peer_io_context : io_context_,
478 impl.socket_, impl.state_, impl.protocol_, peer_endpoint, handler);
479
480 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
481 &impl, impl.socket_, "async_accept"));
482
483 start_accept_op(impl, p.p, is_continuation, false);
484 p.v = p.p = 0;
485 }
486 #endif // defined(BOOST_ASIO_HAS_MOVE)
487
488 // Connect the socket to the specified endpoint.
489 boost::system::error_code connect(implementation_type& impl,
490 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
491 {
492 socket_ops::sync_connect(impl.socket_,
493 peer_endpoint.data(), peer_endpoint.size(), ec);
494 return ec;
495 }
496
497 // Start an asynchronous connect.
498 template <typename Handler>
499 void async_connect(implementation_type& impl,
500 const endpoint_type& peer_endpoint, Handler& handler)
501 {
502 bool is_continuation =
503 boost_asio_handler_cont_helpers::is_continuation(handler);
504
505 // Allocate and construct an operation to wrap the handler.
506 typedef reactive_socket_connect_op<Handler> op;
507 typename op::ptr p = { boost::asio::detail::addressof(handler),
508 op::ptr::allocate(handler), 0 };
509 p.p = new (p.v) op(impl.socket_, handler);
510
511 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
512 &impl, impl.socket_, "async_connect"));
513
514 start_connect_op(impl, p.p, is_continuation,
515 peer_endpoint.data(), peer_endpoint.size());
516 p.v = p.p = 0;
517 }
518 };
519
520 } // namespace detail
521 } // namespace asio
522 } // namespace boost
523
524 #include <boost/asio/detail/pop_options.hpp>
525
526 #endif // !defined(BOOST_ASIO_HAS_IOCP)
527
528 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP