]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/reactive_socket_service_base.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / asio / detail / reactive_socket_service_base.hpp
1 //
2 // detail/reactive_socket_service_base.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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_BASE_HPP
12 #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_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 && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
22
23 #include <boost/asio/buffer.hpp>
24 #include <boost/asio/error.hpp>
25 #include <boost/asio/execution_context.hpp>
26 #include <boost/asio/socket_base.hpp>
27 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
28 #include <boost/asio/detail/memory.hpp>
29 #include <boost/asio/detail/reactive_null_buffers_op.hpp>
30 #include <boost/asio/detail/reactive_socket_recv_op.hpp>
31 #include <boost/asio/detail/reactive_socket_recvmsg_op.hpp>
32 #include <boost/asio/detail/reactive_socket_send_op.hpp>
33 #include <boost/asio/detail/reactive_wait_op.hpp>
34 #include <boost/asio/detail/reactor.hpp>
35 #include <boost/asio/detail/reactor_op.hpp>
36 #include <boost/asio/detail/socket_holder.hpp>
37 #include <boost/asio/detail/socket_ops.hpp>
38 #include <boost/asio/detail/socket_types.hpp>
39
40 #include <boost/asio/detail/push_options.hpp>
41
42 namespace boost {
43 namespace asio {
44 namespace detail {
45
46 class reactive_socket_service_base
47 {
48 public:
49 // The native type of a socket.
50 typedef socket_type native_handle_type;
51
52 // The implementation type of the socket.
53 struct base_implementation_type
54 {
55 // The native socket representation.
56 socket_type socket_;
57
58 // The current state of the socket.
59 socket_ops::state_type state_;
60
61 // Per-descriptor data used by the reactor.
62 reactor::per_descriptor_data reactor_data_;
63 };
64
65 // Constructor.
66 BOOST_ASIO_DECL reactive_socket_service_base(execution_context& context);
67
68 // Destroy all user-defined handler objects owned by the service.
69 BOOST_ASIO_DECL void base_shutdown();
70
71 // Construct a new socket implementation.
72 BOOST_ASIO_DECL void construct(base_implementation_type& impl);
73
74 // Move-construct a new socket implementation.
75 BOOST_ASIO_DECL void base_move_construct(base_implementation_type& impl,
76 base_implementation_type& other_impl) BOOST_ASIO_NOEXCEPT;
77
78 // Move-assign from another socket implementation.
79 BOOST_ASIO_DECL void base_move_assign(base_implementation_type& impl,
80 reactive_socket_service_base& other_service,
81 base_implementation_type& other_impl);
82
83 // Destroy a socket implementation.
84 BOOST_ASIO_DECL void destroy(base_implementation_type& impl);
85
86 // Determine whether the socket is open.
87 bool is_open(const base_implementation_type& impl) const
88 {
89 return impl.socket_ != invalid_socket;
90 }
91
92 // Destroy a socket implementation.
93 BOOST_ASIO_DECL boost::system::error_code close(
94 base_implementation_type& impl, boost::system::error_code& ec);
95
96 // Release ownership of the socket.
97 BOOST_ASIO_DECL socket_type release(
98 base_implementation_type& impl, boost::system::error_code& ec);
99
100 // Get the native socket representation.
101 native_handle_type native_handle(base_implementation_type& impl)
102 {
103 return impl.socket_;
104 }
105
106 // Cancel all operations associated with the socket.
107 BOOST_ASIO_DECL boost::system::error_code cancel(
108 base_implementation_type& impl, boost::system::error_code& ec);
109
110 // Determine whether the socket is at the out-of-band data mark.
111 bool at_mark(const base_implementation_type& impl,
112 boost::system::error_code& ec) const
113 {
114 return socket_ops::sockatmark(impl.socket_, ec);
115 }
116
117 // Determine the number of bytes available for reading.
118 std::size_t available(const base_implementation_type& impl,
119 boost::system::error_code& ec) const
120 {
121 return socket_ops::available(impl.socket_, ec);
122 }
123
124 // Place the socket into the state where it will listen for new connections.
125 boost::system::error_code listen(base_implementation_type& impl,
126 int backlog, boost::system::error_code& ec)
127 {
128 socket_ops::listen(impl.socket_, backlog, ec);
129 return ec;
130 }
131
132 // Perform an IO control command on the socket.
133 template <typename IO_Control_Command>
134 boost::system::error_code io_control(base_implementation_type& impl,
135 IO_Control_Command& command, boost::system::error_code& ec)
136 {
137 socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
138 static_cast<ioctl_arg_type*>(command.data()), ec);
139 return ec;
140 }
141
142 // Gets the non-blocking mode of the socket.
143 bool non_blocking(const base_implementation_type& impl) const
144 {
145 return (impl.state_ & socket_ops::user_set_non_blocking) != 0;
146 }
147
148 // Sets the non-blocking mode of the socket.
149 boost::system::error_code non_blocking(base_implementation_type& impl,
150 bool mode, boost::system::error_code& ec)
151 {
152 socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec);
153 return ec;
154 }
155
156 // Gets the non-blocking mode of the native socket implementation.
157 bool native_non_blocking(const base_implementation_type& impl) const
158 {
159 return (impl.state_ & socket_ops::internal_non_blocking) != 0;
160 }
161
162 // Sets the non-blocking mode of the native socket implementation.
163 boost::system::error_code native_non_blocking(base_implementation_type& impl,
164 bool mode, boost::system::error_code& ec)
165 {
166 socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec);
167 return ec;
168 }
169
170 // Wait for the socket to become ready to read, ready to write, or to have
171 // pending error conditions.
172 boost::system::error_code wait(base_implementation_type& impl,
173 socket_base::wait_type w, boost::system::error_code& ec)
174 {
175 switch (w)
176 {
177 case socket_base::wait_read:
178 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
179 break;
180 case socket_base::wait_write:
181 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
182 break;
183 case socket_base::wait_error:
184 socket_ops::poll_error(impl.socket_, impl.state_, -1, ec);
185 break;
186 default:
187 ec = boost::asio::error::invalid_argument;
188 break;
189 }
190
191 return ec;
192 }
193
194 // Asynchronously wait for the socket to become ready to read, ready to
195 // write, or to have pending error conditions.
196 template <typename Handler, typename IoExecutor>
197 void async_wait(base_implementation_type& impl,
198 socket_base::wait_type w, Handler& handler, const IoExecutor& io_ex)
199 {
200 bool is_continuation =
201 boost_asio_handler_cont_helpers::is_continuation(handler);
202
203 // Allocate and construct an operation to wrap the handler.
204 typedef reactive_wait_op<Handler, IoExecutor> op;
205 typename op::ptr p = { boost::asio::detail::addressof(handler),
206 op::ptr::allocate(handler), 0 };
207 p.p = new (p.v) op(success_ec_, handler, io_ex);
208
209 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
210 &impl, impl.socket_, "async_wait"));
211
212 int op_type;
213 switch (w)
214 {
215 case socket_base::wait_read:
216 op_type = reactor::read_op;
217 break;
218 case socket_base::wait_write:
219 op_type = reactor::write_op;
220 break;
221 case socket_base::wait_error:
222 op_type = reactor::except_op;
223 break;
224 default:
225 p.p->ec_ = boost::asio::error::invalid_argument;
226 reactor_.post_immediate_completion(p.p, is_continuation);
227 p.v = p.p = 0;
228 return;
229 }
230
231 start_op(impl, op_type, p.p, is_continuation, false, false);
232 p.v = p.p = 0;
233 }
234
235 // Send the given data to the peer.
236 template <typename ConstBufferSequence>
237 size_t send(base_implementation_type& impl,
238 const ConstBufferSequence& buffers,
239 socket_base::message_flags flags, boost::system::error_code& ec)
240 {
241 typedef buffer_sequence_adapter<boost::asio::const_buffer,
242 ConstBufferSequence> bufs_type;
243
244 if (bufs_type::is_single_buffer)
245 {
246 return socket_ops::sync_send1(impl.socket_,
247 impl.state_, bufs_type::first(buffers).data(),
248 bufs_type::first(buffers).size(), flags, ec);
249 }
250 else
251 {
252 bufs_type bufs(buffers);
253 return socket_ops::sync_send(impl.socket_, impl.state_,
254 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
255 }
256 }
257
258 // Wait until data can be sent without blocking.
259 size_t send(base_implementation_type& impl, const null_buffers&,
260 socket_base::message_flags, boost::system::error_code& ec)
261 {
262 // Wait for socket to become ready.
263 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
264
265 return 0;
266 }
267
268 // Start an asynchronous send. The data being sent must be valid for the
269 // lifetime of the asynchronous operation.
270 template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
271 void async_send(base_implementation_type& impl,
272 const ConstBufferSequence& buffers, socket_base::message_flags flags,
273 Handler& handler, const IoExecutor& io_ex)
274 {
275 bool is_continuation =
276 boost_asio_handler_cont_helpers::is_continuation(handler);
277
278 // Allocate and construct an operation to wrap the handler.
279 typedef reactive_socket_send_op<
280 ConstBufferSequence, Handler, IoExecutor> op;
281 typename op::ptr p = { boost::asio::detail::addressof(handler),
282 op::ptr::allocate(handler), 0 };
283 p.p = new (p.v) op(success_ec_, impl.socket_,
284 impl.state_, buffers, flags, handler, io_ex);
285
286 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
287 &impl, impl.socket_, "async_send"));
288
289 start_op(impl, reactor::write_op, p.p, is_continuation, true,
290 ((impl.state_ & socket_ops::stream_oriented)
291 && buffer_sequence_adapter<boost::asio::const_buffer,
292 ConstBufferSequence>::all_empty(buffers)));
293 p.v = p.p = 0;
294 }
295
296 // Start an asynchronous wait until data can be sent without blocking.
297 template <typename Handler, typename IoExecutor>
298 void async_send(base_implementation_type& impl, const null_buffers&,
299 socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
300 {
301 bool is_continuation =
302 boost_asio_handler_cont_helpers::is_continuation(handler);
303
304 // Allocate and construct an operation to wrap the handler.
305 typedef reactive_null_buffers_op<Handler, IoExecutor> op;
306 typename op::ptr p = { boost::asio::detail::addressof(handler),
307 op::ptr::allocate(handler), 0 };
308 p.p = new (p.v) op(success_ec_, handler, io_ex);
309
310 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
311 &impl, impl.socket_, "async_send(null_buffers)"));
312
313 start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
314 p.v = p.p = 0;
315 }
316
317 // Receive some data from the peer. Returns the number of bytes received.
318 template <typename MutableBufferSequence>
319 size_t receive(base_implementation_type& impl,
320 const MutableBufferSequence& buffers,
321 socket_base::message_flags flags, boost::system::error_code& ec)
322 {
323 typedef buffer_sequence_adapter<boost::asio::mutable_buffer,
324 MutableBufferSequence> bufs_type;
325
326 if (bufs_type::is_single_buffer)
327 {
328 return socket_ops::sync_recv1(impl.socket_,
329 impl.state_, bufs_type::first(buffers).data(),
330 bufs_type::first(buffers).size(), flags, ec);
331 }
332 else
333 {
334 bufs_type bufs(buffers);
335 return socket_ops::sync_recv(impl.socket_, impl.state_,
336 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
337 }
338 }
339
340 // Wait until data can be received without blocking.
341 size_t receive(base_implementation_type& impl, const null_buffers&,
342 socket_base::message_flags, boost::system::error_code& ec)
343 {
344 // Wait for socket to become ready.
345 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
346
347 return 0;
348 }
349
350 // Start an asynchronous receive. The buffer for the data being received
351 // must be valid for the lifetime of the asynchronous operation.
352 template <typename MutableBufferSequence,
353 typename Handler, typename IoExecutor>
354 void async_receive(base_implementation_type& impl,
355 const MutableBufferSequence& buffers, socket_base::message_flags flags,
356 Handler& handler, const IoExecutor& io_ex)
357 {
358 bool is_continuation =
359 boost_asio_handler_cont_helpers::is_continuation(handler);
360
361 // Allocate and construct an operation to wrap the handler.
362 typedef reactive_socket_recv_op<
363 MutableBufferSequence, Handler, IoExecutor> op;
364 typename op::ptr p = { boost::asio::detail::addressof(handler),
365 op::ptr::allocate(handler), 0 };
366 p.p = new (p.v) op(success_ec_, impl.socket_,
367 impl.state_, buffers, flags, handler, io_ex);
368
369 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
370 &impl, impl.socket_, "async_receive"));
371
372 start_op(impl,
373 (flags & socket_base::message_out_of_band)
374 ? reactor::except_op : reactor::read_op,
375 p.p, is_continuation,
376 (flags & socket_base::message_out_of_band) == 0,
377 ((impl.state_ & socket_ops::stream_oriented)
378 && buffer_sequence_adapter<boost::asio::mutable_buffer,
379 MutableBufferSequence>::all_empty(buffers)));
380 p.v = p.p = 0;
381 }
382
383 // Wait until data can be received without blocking.
384 template <typename Handler, typename IoExecutor>
385 void async_receive(base_implementation_type& impl,
386 const null_buffers&, socket_base::message_flags flags,
387 Handler& handler, const IoExecutor& io_ex)
388 {
389 bool is_continuation =
390 boost_asio_handler_cont_helpers::is_continuation(handler);
391
392 // Allocate and construct an operation to wrap the handler.
393 typedef reactive_null_buffers_op<Handler, IoExecutor> op;
394 typename op::ptr p = { boost::asio::detail::addressof(handler),
395 op::ptr::allocate(handler), 0 };
396 p.p = new (p.v) op(success_ec_, handler, io_ex);
397
398 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
399 &impl, impl.socket_, "async_receive(null_buffers)"));
400
401 start_op(impl,
402 (flags & socket_base::message_out_of_band)
403 ? reactor::except_op : reactor::read_op,
404 p.p, is_continuation, false, false);
405 p.v = p.p = 0;
406 }
407
408 // Receive some data with associated flags. Returns the number of bytes
409 // received.
410 template <typename MutableBufferSequence>
411 size_t receive_with_flags(base_implementation_type& impl,
412 const MutableBufferSequence& buffers,
413 socket_base::message_flags in_flags,
414 socket_base::message_flags& out_flags, boost::system::error_code& ec)
415 {
416 buffer_sequence_adapter<boost::asio::mutable_buffer,
417 MutableBufferSequence> bufs(buffers);
418
419 return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
420 bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
421 }
422
423 // Wait until data can be received without blocking.
424 size_t receive_with_flags(base_implementation_type& impl,
425 const null_buffers&, socket_base::message_flags,
426 socket_base::message_flags& out_flags, boost::system::error_code& ec)
427 {
428 // Wait for socket to become ready.
429 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
430
431 // Clear out_flags, since we cannot give it any other sensible value when
432 // performing a null_buffers operation.
433 out_flags = 0;
434
435 return 0;
436 }
437
438 // Start an asynchronous receive. The buffer for the data being received
439 // must be valid for the lifetime of the asynchronous operation.
440 template <typename MutableBufferSequence,
441 typename Handler, typename IoExecutor>
442 void async_receive_with_flags(base_implementation_type& impl,
443 const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
444 socket_base::message_flags& out_flags, Handler& handler,
445 const IoExecutor& io_ex)
446 {
447 bool is_continuation =
448 boost_asio_handler_cont_helpers::is_continuation(handler);
449
450 // Allocate and construct an operation to wrap the handler.
451 typedef reactive_socket_recvmsg_op<
452 MutableBufferSequence, Handler, IoExecutor> op;
453 typename op::ptr p = { boost::asio::detail::addressof(handler),
454 op::ptr::allocate(handler), 0 };
455 p.p = new (p.v) op(success_ec_, impl.socket_,
456 buffers, in_flags, out_flags, handler, io_ex);
457
458 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
459 &impl, impl.socket_, "async_receive_with_flags"));
460
461 start_op(impl,
462 (in_flags & socket_base::message_out_of_band)
463 ? reactor::except_op : reactor::read_op,
464 p.p, is_continuation,
465 (in_flags & socket_base::message_out_of_band) == 0, false);
466 p.v = p.p = 0;
467 }
468
469 // Wait until data can be received without blocking.
470 template <typename Handler, typename IoExecutor>
471 void async_receive_with_flags(base_implementation_type& impl,
472 const null_buffers&, socket_base::message_flags in_flags,
473 socket_base::message_flags& out_flags, Handler& handler,
474 const IoExecutor& io_ex)
475 {
476 bool is_continuation =
477 boost_asio_handler_cont_helpers::is_continuation(handler);
478
479 // Allocate and construct an operation to wrap the handler.
480 typedef reactive_null_buffers_op<Handler, IoExecutor> op;
481 typename op::ptr p = { boost::asio::detail::addressof(handler),
482 op::ptr::allocate(handler), 0 };
483 p.p = new (p.v) op(success_ec_, handler, io_ex);
484
485 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
486 &impl, impl.socket_, "async_receive_with_flags(null_buffers)"));
487
488 // Clear out_flags, since we cannot give it any other sensible value when
489 // performing a null_buffers operation.
490 out_flags = 0;
491
492 start_op(impl,
493 (in_flags & socket_base::message_out_of_band)
494 ? reactor::except_op : reactor::read_op,
495 p.p, is_continuation, false, false);
496 p.v = p.p = 0;
497 }
498
499 protected:
500 // Open a new socket implementation.
501 BOOST_ASIO_DECL boost::system::error_code do_open(
502 base_implementation_type& impl, int af,
503 int type, int protocol, boost::system::error_code& ec);
504
505 // Assign a native socket to a socket implementation.
506 BOOST_ASIO_DECL boost::system::error_code do_assign(
507 base_implementation_type& impl, int type,
508 const native_handle_type& native_socket, boost::system::error_code& ec);
509
510 // Start the asynchronous read or write operation.
511 BOOST_ASIO_DECL void start_op(base_implementation_type& impl, int op_type,
512 reactor_op* op, bool is_continuation, bool is_non_blocking, bool noop);
513
514 // Start the asynchronous accept operation.
515 BOOST_ASIO_DECL void start_accept_op(base_implementation_type& impl,
516 reactor_op* op, bool is_continuation, bool peer_is_open);
517
518 // Start the asynchronous connect operation.
519 BOOST_ASIO_DECL void start_connect_op(base_implementation_type& impl,
520 reactor_op* op, bool is_continuation,
521 const socket_addr_type* addr, size_t addrlen);
522
523 // The selector that performs event demultiplexing for the service.
524 reactor& reactor_;
525
526 // Cached success value to avoid accessing category singleton.
527 const boost::system::error_code success_ec_;
528 };
529
530 } // namespace detail
531 } // namespace asio
532 } // namespace boost
533
534 #include <boost/asio/detail/pop_options.hpp>
535
536 #if defined(BOOST_ASIO_HEADER_ONLY)
537 # include <boost/asio/detail/impl/reactive_socket_service_base.ipp>
538 #endif // defined(BOOST_ASIO_HEADER_ONLY)
539
540 #endif // !defined(BOOST_ASIO_HAS_IOCP)
541 // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
542
543 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP