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