2 // detail/reactive_socket_service_base.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP
12 #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if !defined(BOOST_ASIO_HAS_IOCP) \
21 && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
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>
40 #include <boost/asio/detail/push_options.hpp>
46 class reactive_socket_service_base
49 // The native type of a socket.
50 typedef socket_type native_handle_type;
52 // The implementation type of the socket.
53 struct base_implementation_type
55 // The native socket representation.
58 // The current state of the socket.
59 socket_ops::state_type state_;
61 // Per-descriptor data used by the reactor.
62 reactor::per_descriptor_data reactor_data_;
66 BOOST_ASIO_DECL reactive_socket_service_base(
67 boost::asio::io_context& io_context);
69 // Destroy all user-defined handler objects owned by the service.
70 BOOST_ASIO_DECL void base_shutdown();
72 // Construct a new socket implementation.
73 BOOST_ASIO_DECL void construct(base_implementation_type& impl);
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);
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);
84 // Destroy a socket implementation.
85 BOOST_ASIO_DECL void destroy(base_implementation_type& impl);
87 // Determine whether the socket is open.
88 bool is_open(const base_implementation_type& impl) const
90 return impl.socket_ != invalid_socket;
93 // Destroy a socket implementation.
94 BOOST_ASIO_DECL boost::system::error_code close(
95 base_implementation_type& impl, boost::system::error_code& ec);
97 // Release ownership of the socket.
98 BOOST_ASIO_DECL socket_type release(
99 base_implementation_type& impl, boost::system::error_code& ec);
101 // Get the native socket representation.
102 native_handle_type native_handle(base_implementation_type& impl)
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);
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
115 return socket_ops::sockatmark(impl.socket_, ec);
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
122 return socket_ops::available(impl.socket_, ec);
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)
129 socket_ops::listen(impl.socket_, backlog, ec);
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)
138 socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
139 static_cast<ioctl_arg_type*>(command.data()), ec);
143 // Gets the non-blocking mode of the socket.
144 bool non_blocking(const base_implementation_type& impl) const
146 return (impl.state_ & socket_ops::user_set_non_blocking) != 0;
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)
153 socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec);
157 // Gets the non-blocking mode of the native socket implementation.
158 bool native_non_blocking(const base_implementation_type& impl) const
160 return (impl.state_ & socket_ops::internal_non_blocking) != 0;
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)
167 socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec);
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)
178 case socket_base::wait_read:
179 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
181 case socket_base::wait_write:
182 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
184 case socket_base::wait_error:
185 socket_ops::poll_error(impl.socket_, impl.state_, -1, ec);
188 ec = boost::asio::error::invalid_argument;
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)
201 bool is_continuation =
202 boost_asio_handler_cont_helpers::is_continuation(handler);
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);
210 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
211 &impl, impl.socket_, "async_wait"));
216 case socket_base::wait_read:
217 op_type = reactor::read_op;
219 case socket_base::wait_write:
220 op_type = reactor::write_op;
222 case socket_base::wait_error:
223 op_type = reactor::except_op;
226 p.p->ec_ = boost::asio::error::invalid_argument;
227 reactor_.post_immediate_completion(p.p, is_continuation);
232 start_op(impl, op_type, p.p, is_continuation, false, false);
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)
242 buffer_sequence_adapter<boost::asio::const_buffer,
243 ConstBufferSequence> bufs(buffers);
245 return socket_ops::sync_send(impl.socket_, impl.state_,
246 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
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)
253 // Wait for socket to become ready.
254 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
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)
266 bool is_continuation =
267 boost_asio_handler_cont_helpers::is_continuation(handler);
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);
275 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
276 &impl, impl.socket_, "async_send"));
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)));
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)
290 bool is_continuation =
291 boost_asio_handler_cont_helpers::is_continuation(handler);
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);
299 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
300 &impl, impl.socket_, "async_send(null_buffers)"));
302 start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
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)
312 buffer_sequence_adapter<boost::asio::mutable_buffer,
313 MutableBufferSequence> bufs(buffers);
315 return socket_ops::sync_recv(impl.socket_, impl.state_,
316 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
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)
323 // Wait for socket to become ready.
324 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
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)
336 bool is_continuation =
337 boost_asio_handler_cont_helpers::is_continuation(handler);
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);
345 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
346 &impl, impl.socket_, "async_receive"));
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)));
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)
364 bool is_continuation =
365 boost_asio_handler_cont_helpers::is_continuation(handler);
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);
373 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
374 &impl, impl.socket_, "async_receive(null_buffers)"));
377 (flags & socket_base::message_out_of_band)
378 ? reactor::except_op : reactor::read_op,
379 p.p, is_continuation, false, false);
383 // Receive some data with associated flags. Returns the number of bytes
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)
391 buffer_sequence_adapter<boost::asio::mutable_buffer,
392 MutableBufferSequence> bufs(buffers);
394 return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
395 bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
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)
403 // Wait for socket to become ready.
404 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
406 // Clear out_flags, since we cannot give it any other sensible value when
407 // performing a null_buffers operation.
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)
420 bool is_continuation =
421 boost_asio_handler_cont_helpers::is_continuation(handler);
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);
429 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
430 &impl, impl.socket_, "async_receive_with_flags"));
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);
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)
446 bool is_continuation =
447 boost_asio_handler_cont_helpers::is_continuation(handler);
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);
455 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
456 &impl, impl.socket_, "async_receive_with_flags(null_buffers)"));
458 // Clear out_flags, since we cannot give it any other sensible value when
459 // performing a null_buffers operation.
463 (in_flags & socket_base::message_out_of_band)
464 ? reactor::except_op : reactor::read_op,
465 p.p, is_continuation, false, false);
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);
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);
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);
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);
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);
493 // The io_context that owns this socket service.
494 io_context& io_context_;
496 // The selector that performs event demultiplexing for the service.
500 } // namespace detail
504 #include <boost/asio/detail/pop_options.hpp>
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)
510 #endif // !defined(BOOST_ASIO_HAS_IOCP)
511 // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
513 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP