2 // detail/reactive_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 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_HPP
12 #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_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)
22 #include <boost/asio/buffer.hpp>
23 #include <boost/asio/error.hpp>
24 #include <boost/asio/execution_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>
41 #include <boost/asio/detail/push_options.hpp>
47 template <typename Protocol>
48 class reactive_socket_service :
49 public execution_context_service_base<reactive_socket_service<Protocol> >,
50 public reactive_socket_service_base
54 typedef Protocol protocol_type;
57 typedef typename Protocol::endpoint endpoint_type;
59 // The native type of a socket.
60 typedef socket_type native_handle_type;
62 // The implementation type of the socket.
63 struct implementation_type :
64 reactive_socket_service_base::base_implementation_type
66 // Default constructor.
68 : protocol_(endpoint_type().protocol())
72 // The protocol associated with the socket.
73 protocol_type protocol_;
77 reactive_socket_service(execution_context& context)
78 : execution_context_service_base<
79 reactive_socket_service<Protocol> >(context),
80 reactive_socket_service_base(context)
84 // Destroy all user-defined handler objects owned by the service.
87 this->base_shutdown();
90 // Move-construct a new socket implementation.
91 void move_construct(implementation_type& impl,
92 implementation_type& other_impl) BOOST_ASIO_NOEXCEPT
94 this->base_move_construct(impl, other_impl);
96 impl.protocol_ = other_impl.protocol_;
97 other_impl.protocol_ = endpoint_type().protocol();
100 // Move-assign from another socket implementation.
101 void move_assign(implementation_type& impl,
102 reactive_socket_service_base& other_service,
103 implementation_type& other_impl)
105 this->base_move_assign(impl, other_service, other_impl);
107 impl.protocol_ = other_impl.protocol_;
108 other_impl.protocol_ = endpoint_type().protocol();
111 // Move-construct a new socket implementation from another protocol type.
112 template <typename Protocol1>
113 void converting_move_construct(implementation_type& impl,
114 reactive_socket_service<Protocol1>&,
115 typename reactive_socket_service<
116 Protocol1>::implementation_type& other_impl)
118 this->base_move_construct(impl, other_impl);
120 impl.protocol_ = protocol_type(other_impl.protocol_);
121 other_impl.protocol_ = typename Protocol1::endpoint().protocol();
124 // Open a new socket implementation.
125 boost::system::error_code open(implementation_type& impl,
126 const protocol_type& protocol, boost::system::error_code& ec)
128 if (!do_open(impl, protocol.family(),
129 protocol.type(), protocol.protocol(), ec))
130 impl.protocol_ = protocol;
134 // Assign a native socket to a socket implementation.
135 boost::system::error_code assign(implementation_type& impl,
136 const protocol_type& protocol, const native_handle_type& native_socket,
137 boost::system::error_code& ec)
139 if (!do_assign(impl, protocol.type(), native_socket, ec))
140 impl.protocol_ = protocol;
144 // Get the native socket representation.
145 native_handle_type native_handle(implementation_type& impl)
150 // Bind the socket to the specified local endpoint.
151 boost::system::error_code bind(implementation_type& impl,
152 const endpoint_type& endpoint, boost::system::error_code& ec)
154 socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
158 // Set a socket option.
159 template <typename Option>
160 boost::system::error_code set_option(implementation_type& impl,
161 const Option& option, boost::system::error_code& ec)
163 socket_ops::setsockopt(impl.socket_, impl.state_,
164 option.level(impl.protocol_), option.name(impl.protocol_),
165 option.data(impl.protocol_), option.size(impl.protocol_), ec);
169 // Set a socket option.
170 template <typename Option>
171 boost::system::error_code get_option(const implementation_type& impl,
172 Option& option, boost::system::error_code& ec) const
174 std::size_t size = option.size(impl.protocol_);
175 socket_ops::getsockopt(impl.socket_, impl.state_,
176 option.level(impl.protocol_), option.name(impl.protocol_),
177 option.data(impl.protocol_), &size, ec);
179 option.resize(impl.protocol_, size);
183 // Get the local endpoint.
184 endpoint_type local_endpoint(const implementation_type& impl,
185 boost::system::error_code& ec) const
187 endpoint_type endpoint;
188 std::size_t addr_len = endpoint.capacity();
189 if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec))
190 return endpoint_type();
191 endpoint.resize(addr_len);
195 // Get the remote endpoint.
196 endpoint_type remote_endpoint(const implementation_type& impl,
197 boost::system::error_code& ec) const
199 endpoint_type endpoint;
200 std::size_t addr_len = endpoint.capacity();
201 if (socket_ops::getpeername(impl.socket_,
202 endpoint.data(), &addr_len, false, ec))
203 return endpoint_type();
204 endpoint.resize(addr_len);
208 // Disable sends or receives on the socket.
209 boost::system::error_code shutdown(base_implementation_type& impl,
210 socket_base::shutdown_type what, boost::system::error_code& ec)
212 socket_ops::shutdown(impl.socket_, what, ec);
216 // Send a datagram to the specified endpoint. Returns the number of bytes
218 template <typename ConstBufferSequence>
219 size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
220 const endpoint_type& destination, socket_base::message_flags flags,
221 boost::system::error_code& ec)
223 typedef buffer_sequence_adapter<boost::asio::const_buffer,
224 ConstBufferSequence> bufs_type;
226 if (bufs_type::is_single_buffer)
228 return socket_ops::sync_sendto1(impl.socket_, impl.state_,
229 bufs_type::first(buffers).data(),
230 bufs_type::first(buffers).size(), flags,
231 destination.data(), destination.size(), ec);
235 bufs_type bufs(buffers);
236 return socket_ops::sync_sendto(impl.socket_, impl.state_,
237 bufs.buffers(), bufs.count(), flags,
238 destination.data(), destination.size(), ec);
242 // Wait until data can be sent without blocking.
243 size_t send_to(implementation_type& impl, const null_buffers&,
244 const endpoint_type&, socket_base::message_flags,
245 boost::system::error_code& ec)
247 // Wait for socket to become ready.
248 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
253 // Start an asynchronous send. The data being sent must be valid for the
254 // lifetime of the asynchronous operation.
255 template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
256 void async_send_to(implementation_type& impl,
257 const ConstBufferSequence& buffers,
258 const endpoint_type& destination, socket_base::message_flags flags,
259 Handler& handler, const IoExecutor& io_ex)
261 bool is_continuation =
262 boost_asio_handler_cont_helpers::is_continuation(handler);
264 // Allocate and construct an operation to wrap the handler.
265 typedef reactive_socket_sendto_op<ConstBufferSequence,
266 endpoint_type, Handler, IoExecutor> op;
267 typename op::ptr p = { boost::asio::detail::addressof(handler),
268 op::ptr::allocate(handler), 0 };
269 p.p = new (p.v) op(success_ec_, impl.socket_,
270 buffers, destination, flags, handler, io_ex);
272 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
273 &impl, impl.socket_, "async_send_to"));
275 start_op(impl, reactor::write_op, p.p, is_continuation, true, false);
279 // Start an asynchronous wait until data can be sent without blocking.
280 template <typename Handler, typename IoExecutor>
281 void async_send_to(implementation_type& impl, const null_buffers&,
282 const endpoint_type&, socket_base::message_flags,
283 Handler& handler, const IoExecutor& io_ex)
285 bool is_continuation =
286 boost_asio_handler_cont_helpers::is_continuation(handler);
288 // Allocate and construct an operation to wrap the handler.
289 typedef reactive_null_buffers_op<Handler, IoExecutor> op;
290 typename op::ptr p = { boost::asio::detail::addressof(handler),
291 op::ptr::allocate(handler), 0 };
292 p.p = new (p.v) op(success_ec_, handler, io_ex);
294 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
295 &impl, impl.socket_, "async_send_to(null_buffers)"));
297 start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
301 // Receive a datagram with the endpoint of the sender. Returns the number of
303 template <typename MutableBufferSequence>
304 size_t receive_from(implementation_type& impl,
305 const MutableBufferSequence& buffers,
306 endpoint_type& sender_endpoint, socket_base::message_flags flags,
307 boost::system::error_code& ec)
309 typedef buffer_sequence_adapter<boost::asio::mutable_buffer,
310 MutableBufferSequence> bufs_type;
312 std::size_t addr_len = sender_endpoint.capacity();
313 std::size_t bytes_recvd;
314 if (bufs_type::is_single_buffer)
316 bytes_recvd = socket_ops::sync_recvfrom1(impl.socket_,
317 impl.state_, bufs_type::first(buffers).data(),
318 bufs_type::first(buffers).size(), flags,
319 sender_endpoint.data(), &addr_len, ec);
323 bufs_type bufs(buffers);
324 bytes_recvd = socket_ops::sync_recvfrom(
325 impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
326 flags, sender_endpoint.data(), &addr_len, ec);
330 sender_endpoint.resize(addr_len);
335 // Wait until data can be received without blocking.
336 size_t receive_from(implementation_type& impl, const null_buffers&,
337 endpoint_type& sender_endpoint, socket_base::message_flags,
338 boost::system::error_code& ec)
340 // Wait for socket to become ready.
341 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
343 // Reset endpoint since it can be given no sensible value at this time.
344 sender_endpoint = endpoint_type();
349 // Start an asynchronous receive. The buffer for the data being received and
350 // the sender_endpoint object must both be valid for the lifetime of the
351 // asynchronous operation.
352 template <typename MutableBufferSequence,
353 typename Handler, typename IoExecutor>
354 void async_receive_from(implementation_type& impl,
355 const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
356 socket_base::message_flags flags, Handler& handler,
357 const IoExecutor& io_ex)
359 bool is_continuation =
360 boost_asio_handler_cont_helpers::is_continuation(handler);
362 // Allocate and construct an operation to wrap the handler.
363 typedef reactive_socket_recvfrom_op<MutableBufferSequence,
364 endpoint_type, Handler, IoExecutor> op;
365 typename op::ptr p = { boost::asio::detail::addressof(handler),
366 op::ptr::allocate(handler), 0 };
367 int protocol = impl.protocol_.type();
368 p.p = new (p.v) op(success_ec_, impl.socket_, protocol,
369 buffers, sender_endpoint, flags, handler, io_ex);
371 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
372 &impl, impl.socket_, "async_receive_from"));
375 (flags & socket_base::message_out_of_band)
376 ? reactor::except_op : reactor::read_op,
377 p.p, is_continuation, true, false);
381 // Wait until data can be received without blocking.
382 template <typename Handler, typename IoExecutor>
383 void async_receive_from(implementation_type& impl, const null_buffers&,
384 endpoint_type& sender_endpoint, socket_base::message_flags flags,
385 Handler& handler, const IoExecutor& io_ex)
387 bool is_continuation =
388 boost_asio_handler_cont_helpers::is_continuation(handler);
390 // Allocate and construct an operation to wrap the handler.
391 typedef reactive_null_buffers_op<Handler, IoExecutor> op;
392 typename op::ptr p = { boost::asio::detail::addressof(handler),
393 op::ptr::allocate(handler), 0 };
394 p.p = new (p.v) op(success_ec_, handler, io_ex);
396 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
397 &impl, impl.socket_, "async_receive_from(null_buffers)"));
399 // Reset endpoint since it can be given no sensible value at this time.
400 sender_endpoint = endpoint_type();
403 (flags & socket_base::message_out_of_band)
404 ? reactor::except_op : reactor::read_op,
405 p.p, is_continuation, false, false);
409 // Accept a new connection.
410 template <typename Socket>
411 boost::system::error_code accept(implementation_type& impl,
412 Socket& peer, endpoint_type* peer_endpoint, boost::system::error_code& ec)
414 // We cannot accept a socket that is already open.
417 ec = boost::asio::error::already_open;
421 std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
422 socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
423 impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
424 peer_endpoint ? &addr_len : 0, ec));
426 // On success, assign new connection to peer socket object.
427 if (new_socket.get() != invalid_socket)
430 peer_endpoint->resize(addr_len);
431 peer.assign(impl.protocol_, new_socket.get(), ec);
433 new_socket.release();
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, typename IoExecutor>
442 void async_accept(implementation_type& impl, Socket& peer,
443 endpoint_type* peer_endpoint, Handler& handler, const IoExecutor& io_ex)
445 bool is_continuation =
446 boost_asio_handler_cont_helpers::is_continuation(handler);
448 // Allocate and construct an operation to wrap the handler.
449 typedef reactive_socket_accept_op<Socket, Protocol, Handler, IoExecutor> op;
450 typename op::ptr p = { boost::asio::detail::addressof(handler),
451 op::ptr::allocate(handler), 0 };
452 p.p = new (p.v) op(success_ec_, impl.socket_, impl.state_,
453 peer, impl.protocol_, peer_endpoint, handler, io_ex);
455 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
456 &impl, impl.socket_, "async_accept"));
458 start_accept_op(impl, p.p, is_continuation, peer.is_open());
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 PeerIoExecutor, typename Handler, typename IoExecutor>
466 void async_move_accept(implementation_type& impl,
467 const PeerIoExecutor& peer_io_ex, endpoint_type* peer_endpoint,
468 Handler& handler, const IoExecutor& io_ex)
470 bool is_continuation =
471 boost_asio_handler_cont_helpers::is_continuation(handler);
473 // Allocate and construct an operation to wrap the handler.
474 typedef reactive_socket_move_accept_op<Protocol,
475 PeerIoExecutor, Handler, IoExecutor> op;
476 typename op::ptr p = { boost::asio::detail::addressof(handler),
477 op::ptr::allocate(handler), 0 };
478 p.p = new (p.v) op(success_ec_, peer_io_ex, impl.socket_,
479 impl.state_, impl.protocol_, peer_endpoint, handler, io_ex);
481 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
482 &impl, impl.socket_, "async_accept"));
484 start_accept_op(impl, p.p, is_continuation, false);
487 #endif // defined(BOOST_ASIO_HAS_MOVE)
489 // Connect the socket to the specified endpoint.
490 boost::system::error_code connect(implementation_type& impl,
491 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
493 socket_ops::sync_connect(impl.socket_,
494 peer_endpoint.data(), peer_endpoint.size(), ec);
498 // Start an asynchronous connect.
499 template <typename Handler, typename IoExecutor>
500 void async_connect(implementation_type& impl,
501 const endpoint_type& peer_endpoint,
502 Handler& handler, const IoExecutor& io_ex)
504 bool is_continuation =
505 boost_asio_handler_cont_helpers::is_continuation(handler);
507 // Allocate and construct an operation to wrap the handler.
508 typedef reactive_socket_connect_op<Handler, IoExecutor> op;
509 typename op::ptr p = { boost::asio::detail::addressof(handler),
510 op::ptr::allocate(handler), 0 };
511 p.p = new (p.v) op(success_ec_, impl.socket_, handler, io_ex);
513 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
514 &impl, impl.socket_, "async_connect"));
516 start_connect_op(impl, p.p, is_continuation,
517 peer_endpoint.data(), peer_endpoint.size());
522 } // namespace detail
526 #include <boost/asio/detail/pop_options.hpp>
528 #endif // !defined(BOOST_ASIO_HAS_IOCP)
530 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP