2 // detail/reactive_socket_service_base.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 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/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>
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(execution_context& context);
68 // Destroy all user-defined handler objects owned by the service.
69 BOOST_ASIO_DECL void base_shutdown();
71 // Construct a new socket implementation.
72 BOOST_ASIO_DECL void construct(base_implementation_type& impl);
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;
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);
83 // Destroy a socket implementation.
84 BOOST_ASIO_DECL void destroy(base_implementation_type& impl);
86 // Determine whether the socket is open.
87 bool is_open(const base_implementation_type& impl) const
89 return impl.socket_ != invalid_socket;
92 // Destroy a socket implementation.
93 BOOST_ASIO_DECL boost::system::error_code close(
94 base_implementation_type& impl, boost::system::error_code& ec);
96 // Release ownership of the socket.
97 BOOST_ASIO_DECL socket_type release(
98 base_implementation_type& impl, boost::system::error_code& ec);
100 // Get the native socket representation.
101 native_handle_type native_handle(base_implementation_type& impl)
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);
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
114 return socket_ops::sockatmark(impl.socket_, ec);
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
121 return socket_ops::available(impl.socket_, ec);
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)
128 socket_ops::listen(impl.socket_, backlog, ec);
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)
137 socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
138 static_cast<ioctl_arg_type*>(command.data()), ec);
142 // Gets the non-blocking mode of the socket.
143 bool non_blocking(const base_implementation_type& impl) const
145 return (impl.state_ & socket_ops::user_set_non_blocking) != 0;
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)
152 socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec);
156 // Gets the non-blocking mode of the native socket implementation.
157 bool native_non_blocking(const base_implementation_type& impl) const
159 return (impl.state_ & socket_ops::internal_non_blocking) != 0;
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)
166 socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec);
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)
177 case socket_base::wait_read:
178 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
180 case socket_base::wait_write:
181 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
183 case socket_base::wait_error:
184 socket_ops::poll_error(impl.socket_, impl.state_, -1, ec);
187 ec = boost::asio::error::invalid_argument;
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)
200 bool is_continuation =
201 boost_asio_handler_cont_helpers::is_continuation(handler);
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(handler, io_ex);
209 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
210 &impl, impl.socket_, "async_wait"));
215 case socket_base::wait_read:
216 op_type = reactor::read_op;
218 case socket_base::wait_write:
219 op_type = reactor::write_op;
221 case socket_base::wait_error:
222 op_type = reactor::except_op;
225 p.p->ec_ = boost::asio::error::invalid_argument;
226 reactor_.post_immediate_completion(p.p, is_continuation);
231 start_op(impl, op_type, p.p, is_continuation, false, false);
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)
241 buffer_sequence_adapter<boost::asio::const_buffer,
242 ConstBufferSequence> bufs(buffers);
244 return socket_ops::sync_send(impl.socket_, impl.state_,
245 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
248 // Wait until data can be sent without blocking.
249 size_t send(base_implementation_type& impl, const null_buffers&,
250 socket_base::message_flags, boost::system::error_code& ec)
252 // Wait for socket to become ready.
253 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
258 // Start an asynchronous send. The data being sent must be valid for the
259 // lifetime of the asynchronous operation.
260 template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
261 void async_send(base_implementation_type& impl,
262 const ConstBufferSequence& buffers, socket_base::message_flags flags,
263 Handler& handler, const IoExecutor& io_ex)
265 bool is_continuation =
266 boost_asio_handler_cont_helpers::is_continuation(handler);
268 // Allocate and construct an operation to wrap the handler.
269 typedef reactive_socket_send_op<
270 ConstBufferSequence, Handler, IoExecutor> 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_,
274 buffers, flags, handler, io_ex);
276 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
277 &impl, impl.socket_, "async_send"));
279 start_op(impl, reactor::write_op, p.p, is_continuation, true,
280 ((impl.state_ & socket_ops::stream_oriented)
281 && buffer_sequence_adapter<boost::asio::const_buffer,
282 ConstBufferSequence>::all_empty(buffers)));
286 // Start an asynchronous wait until data can be sent without blocking.
287 template <typename Handler, typename IoExecutor>
288 void async_send(base_implementation_type& impl, const null_buffers&,
289 socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
291 bool is_continuation =
292 boost_asio_handler_cont_helpers::is_continuation(handler);
294 // Allocate and construct an operation to wrap the handler.
295 typedef reactive_null_buffers_op<Handler, IoExecutor> op;
296 typename op::ptr p = { boost::asio::detail::addressof(handler),
297 op::ptr::allocate(handler), 0 };
298 p.p = new (p.v) op(handler, io_ex);
300 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
301 &impl, impl.socket_, "async_send(null_buffers)"));
303 start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
307 // Receive some data from the peer. Returns the number of bytes received.
308 template <typename MutableBufferSequence>
309 size_t receive(base_implementation_type& impl,
310 const MutableBufferSequence& buffers,
311 socket_base::message_flags flags, boost::system::error_code& ec)
313 buffer_sequence_adapter<boost::asio::mutable_buffer,
314 MutableBufferSequence> bufs(buffers);
316 return socket_ops::sync_recv(impl.socket_, impl.state_,
317 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
320 // Wait until data can be received without blocking.
321 size_t receive(base_implementation_type& impl, const null_buffers&,
322 socket_base::message_flags, boost::system::error_code& ec)
324 // Wait for socket to become ready.
325 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
330 // Start an asynchronous receive. The buffer for the data being received
331 // must be valid for the lifetime of the asynchronous operation.
332 template <typename MutableBufferSequence,
333 typename Handler, typename IoExecutor>
334 void async_receive(base_implementation_type& impl,
335 const MutableBufferSequence& buffers, socket_base::message_flags flags,
336 Handler& handler, const IoExecutor& io_ex)
338 bool is_continuation =
339 boost_asio_handler_cont_helpers::is_continuation(handler);
341 // Allocate and construct an operation to wrap the handler.
342 typedef reactive_socket_recv_op<
343 MutableBufferSequence, Handler, IoExecutor> op;
344 typename op::ptr p = { boost::asio::detail::addressof(handler),
345 op::ptr::allocate(handler), 0 };
346 p.p = new (p.v) op(impl.socket_, impl.state_,
347 buffers, flags, handler, io_ex);
349 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
350 &impl, impl.socket_, "async_receive"));
353 (flags & socket_base::message_out_of_band)
354 ? reactor::except_op : reactor::read_op,
355 p.p, is_continuation,
356 (flags & socket_base::message_out_of_band) == 0,
357 ((impl.state_ & socket_ops::stream_oriented)
358 && buffer_sequence_adapter<boost::asio::mutable_buffer,
359 MutableBufferSequence>::all_empty(buffers)));
363 // Wait until data can be received without blocking.
364 template <typename Handler, typename IoExecutor>
365 void async_receive(base_implementation_type& impl,
366 const null_buffers&, socket_base::message_flags flags,
367 Handler& handler, const IoExecutor& io_ex)
369 bool is_continuation =
370 boost_asio_handler_cont_helpers::is_continuation(handler);
372 // Allocate and construct an operation to wrap the handler.
373 typedef reactive_null_buffers_op<Handler, IoExecutor> op;
374 typename op::ptr p = { boost::asio::detail::addressof(handler),
375 op::ptr::allocate(handler), 0 };
376 p.p = new (p.v) op(handler, io_ex);
378 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
379 &impl, impl.socket_, "async_receive(null_buffers)"));
382 (flags & socket_base::message_out_of_band)
383 ? reactor::except_op : reactor::read_op,
384 p.p, is_continuation, false, false);
388 // Receive some data with associated flags. Returns the number of bytes
390 template <typename MutableBufferSequence>
391 size_t receive_with_flags(base_implementation_type& impl,
392 const MutableBufferSequence& buffers,
393 socket_base::message_flags in_flags,
394 socket_base::message_flags& out_flags, boost::system::error_code& ec)
396 buffer_sequence_adapter<boost::asio::mutable_buffer,
397 MutableBufferSequence> bufs(buffers);
399 return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
400 bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
403 // Wait until data can be received without blocking.
404 size_t receive_with_flags(base_implementation_type& impl,
405 const null_buffers&, socket_base::message_flags,
406 socket_base::message_flags& out_flags, boost::system::error_code& ec)
408 // Wait for socket to become ready.
409 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
411 // Clear out_flags, since we cannot give it any other sensible value when
412 // performing a null_buffers operation.
418 // Start an asynchronous receive. The buffer for the data being received
419 // must be valid for the lifetime of the asynchronous operation.
420 template <typename MutableBufferSequence,
421 typename Handler, typename IoExecutor>
422 void async_receive_with_flags(base_implementation_type& impl,
423 const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
424 socket_base::message_flags& out_flags, Handler& handler,
425 const IoExecutor& io_ex)
427 bool is_continuation =
428 boost_asio_handler_cont_helpers::is_continuation(handler);
430 // Allocate and construct an operation to wrap the handler.
431 typedef reactive_socket_recvmsg_op<
432 MutableBufferSequence, Handler, IoExecutor> op;
433 typename op::ptr p = { boost::asio::detail::addressof(handler),
434 op::ptr::allocate(handler), 0 };
435 p.p = new (p.v) op(impl.socket_, buffers,
436 in_flags, out_flags, handler, io_ex);
438 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
439 &impl, impl.socket_, "async_receive_with_flags"));
442 (in_flags & socket_base::message_out_of_band)
443 ? reactor::except_op : reactor::read_op,
444 p.p, is_continuation,
445 (in_flags & socket_base::message_out_of_band) == 0, false);
449 // Wait until data can be received without blocking.
450 template <typename Handler, typename IoExecutor>
451 void async_receive_with_flags(base_implementation_type& impl,
452 const null_buffers&, socket_base::message_flags in_flags,
453 socket_base::message_flags& out_flags, Handler& handler,
454 const IoExecutor& io_ex)
456 bool is_continuation =
457 boost_asio_handler_cont_helpers::is_continuation(handler);
459 // Allocate and construct an operation to wrap the handler.
460 typedef reactive_null_buffers_op<Handler, IoExecutor> op;
461 typename op::ptr p = { boost::asio::detail::addressof(handler),
462 op::ptr::allocate(handler), 0 };
463 p.p = new (p.v) op(handler, io_ex);
465 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
466 &impl, impl.socket_, "async_receive_with_flags(null_buffers)"));
468 // Clear out_flags, since we cannot give it any other sensible value when
469 // performing a null_buffers operation.
473 (in_flags & socket_base::message_out_of_band)
474 ? reactor::except_op : reactor::read_op,
475 p.p, is_continuation, false, false);
480 // Open a new socket implementation.
481 BOOST_ASIO_DECL boost::system::error_code do_open(
482 base_implementation_type& impl, int af,
483 int type, int protocol, boost::system::error_code& ec);
485 // Assign a native socket to a socket implementation.
486 BOOST_ASIO_DECL boost::system::error_code do_assign(
487 base_implementation_type& impl, int type,
488 const native_handle_type& native_socket, boost::system::error_code& ec);
490 // Start the asynchronous read or write operation.
491 BOOST_ASIO_DECL void start_op(base_implementation_type& impl, int op_type,
492 reactor_op* op, bool is_continuation, bool is_non_blocking, bool noop);
494 // Start the asynchronous accept operation.
495 BOOST_ASIO_DECL void start_accept_op(base_implementation_type& impl,
496 reactor_op* op, bool is_continuation, bool peer_is_open);
498 // Start the asynchronous connect operation.
499 BOOST_ASIO_DECL void start_connect_op(base_implementation_type& impl,
500 reactor_op* op, bool is_continuation,
501 const socket_addr_type* addr, size_t addrlen);
503 // The selector that performs event demultiplexing for the service.
507 } // namespace detail
511 #include <boost/asio/detail/pop_options.hpp>
513 #if defined(BOOST_ASIO_HEADER_ONLY)
514 # include <boost/asio/detail/impl/reactive_socket_service_base.ipp>
515 #endif // defined(BOOST_ASIO_HEADER_ONLY)
517 #endif // !defined(BOOST_ASIO_HAS_IOCP)
518 // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
520 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP