2 // detail/reactive_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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/io_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 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(boost::asio::io_context& io_context)
78 : service_base<reactive_socket_service<Protocol> >(io_context),
79 reactive_socket_service_base(io_context)
83 // Destroy all user-defined handler objects owned by the service.
86 this->base_shutdown();
89 // Move-construct a new socket implementation.
90 void move_construct(implementation_type& impl,
91 implementation_type& other_impl)
93 this->base_move_construct(impl, other_impl);
95 impl.protocol_ = other_impl.protocol_;
96 other_impl.protocol_ = endpoint_type().protocol();
99 // Move-assign from another socket implementation.
100 void move_assign(implementation_type& impl,
101 reactive_socket_service_base& other_service,
102 implementation_type& other_impl)
104 this->base_move_assign(impl, other_service, other_impl);
106 impl.protocol_ = other_impl.protocol_;
107 other_impl.protocol_ = endpoint_type().protocol();
110 // Move-construct a new socket implementation from another protocol type.
111 template <typename Protocol1>
112 void converting_move_construct(implementation_type& impl,
113 reactive_socket_service<Protocol1>&,
114 typename reactive_socket_service<
115 Protocol1>::implementation_type& other_impl)
117 this->base_move_construct(impl, other_impl);
119 impl.protocol_ = protocol_type(other_impl.protocol_);
120 other_impl.protocol_ = typename Protocol1::endpoint().protocol();
123 // Open a new socket implementation.
124 boost::system::error_code open(implementation_type& impl,
125 const protocol_type& protocol, boost::system::error_code& ec)
127 if (!do_open(impl, protocol.family(),
128 protocol.type(), protocol.protocol(), ec))
129 impl.protocol_ = protocol;
133 // Assign a native socket to a socket implementation.
134 boost::system::error_code assign(implementation_type& impl,
135 const protocol_type& protocol, const native_handle_type& native_socket,
136 boost::system::error_code& ec)
138 if (!do_assign(impl, protocol.type(), native_socket, ec))
139 impl.protocol_ = protocol;
143 // Get the native socket representation.
144 native_handle_type native_handle(implementation_type& impl)
149 // Bind the socket to the specified local endpoint.
150 boost::system::error_code bind(implementation_type& impl,
151 const endpoint_type& endpoint, boost::system::error_code& ec)
153 socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
157 // Set a socket option.
158 template <typename Option>
159 boost::system::error_code set_option(implementation_type& impl,
160 const Option& option, boost::system::error_code& ec)
162 socket_ops::setsockopt(impl.socket_, impl.state_,
163 option.level(impl.protocol_), option.name(impl.protocol_),
164 option.data(impl.protocol_), option.size(impl.protocol_), ec);
168 // Set a socket option.
169 template <typename Option>
170 boost::system::error_code get_option(const implementation_type& impl,
171 Option& option, boost::system::error_code& ec) const
173 std::size_t size = option.size(impl.protocol_);
174 socket_ops::getsockopt(impl.socket_, impl.state_,
175 option.level(impl.protocol_), option.name(impl.protocol_),
176 option.data(impl.protocol_), &size, ec);
178 option.resize(impl.protocol_, size);
182 // Get the local endpoint.
183 endpoint_type local_endpoint(const implementation_type& impl,
184 boost::system::error_code& ec) const
186 endpoint_type endpoint;
187 std::size_t addr_len = endpoint.capacity();
188 if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec))
189 return endpoint_type();
190 endpoint.resize(addr_len);
194 // Get the remote endpoint.
195 endpoint_type remote_endpoint(const implementation_type& impl,
196 boost::system::error_code& ec) const
198 endpoint_type endpoint;
199 std::size_t addr_len = endpoint.capacity();
200 if (socket_ops::getpeername(impl.socket_,
201 endpoint.data(), &addr_len, false, ec))
202 return endpoint_type();
203 endpoint.resize(addr_len);
207 // Disable sends or receives on the socket.
208 boost::system::error_code shutdown(base_implementation_type& impl,
209 socket_base::shutdown_type what, boost::system::error_code& ec)
211 socket_ops::shutdown(impl.socket_, what, ec);
215 // Send a datagram to the specified endpoint. Returns the number of bytes
217 template <typename ConstBufferSequence>
218 size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
219 const endpoint_type& destination, socket_base::message_flags flags,
220 boost::system::error_code& ec)
222 buffer_sequence_adapter<boost::asio::const_buffer,
223 ConstBufferSequence> bufs(buffers);
225 return socket_ops::sync_sendto(impl.socket_, impl.state_,
226 bufs.buffers(), bufs.count(), flags,
227 destination.data(), destination.size(), ec);
230 // Wait until data can be sent without blocking.
231 size_t send_to(implementation_type& impl, const null_buffers&,
232 const endpoint_type&, socket_base::message_flags,
233 boost::system::error_code& ec)
235 // Wait for socket to become ready.
236 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
241 // Start an asynchronous send. The data being sent must be valid for the
242 // lifetime of the asynchronous operation.
243 template <typename ConstBufferSequence, typename Handler>
244 void async_send_to(implementation_type& impl,
245 const ConstBufferSequence& buffers,
246 const endpoint_type& destination, socket_base::message_flags flags,
249 bool is_continuation =
250 boost_asio_handler_cont_helpers::is_continuation(handler);
252 // Allocate and construct an operation to wrap the handler.
253 typedef reactive_socket_sendto_op<ConstBufferSequence,
254 endpoint_type, Handler> op;
255 typename op::ptr p = { boost::asio::detail::addressof(handler),
256 op::ptr::allocate(handler), 0 };
257 p.p = new (p.v) op(impl.socket_, buffers, destination, flags, handler);
259 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
260 &impl, impl.socket_, "async_send_to"));
262 start_op(impl, reactor::write_op, p.p, is_continuation, true, false);
266 // Start an asynchronous wait until data can be sent without blocking.
267 template <typename Handler>
268 void async_send_to(implementation_type& impl, const null_buffers&,
269 const endpoint_type&, socket_base::message_flags, Handler& handler)
271 bool is_continuation =
272 boost_asio_handler_cont_helpers::is_continuation(handler);
274 // Allocate and construct an operation to wrap the handler.
275 typedef reactive_null_buffers_op<Handler> op;
276 typename op::ptr p = { boost::asio::detail::addressof(handler),
277 op::ptr::allocate(handler), 0 };
278 p.p = new (p.v) op(handler);
280 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
281 &impl, impl.socket_, "async_send_to(null_buffers)"));
283 start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
287 // Receive a datagram with the endpoint of the sender. Returns the number of
289 template <typename MutableBufferSequence>
290 size_t receive_from(implementation_type& impl,
291 const MutableBufferSequence& buffers,
292 endpoint_type& sender_endpoint, socket_base::message_flags flags,
293 boost::system::error_code& ec)
295 buffer_sequence_adapter<boost::asio::mutable_buffer,
296 MutableBufferSequence> bufs(buffers);
298 std::size_t addr_len = sender_endpoint.capacity();
299 std::size_t bytes_recvd = socket_ops::sync_recvfrom(
300 impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
301 flags, sender_endpoint.data(), &addr_len, ec);
304 sender_endpoint.resize(addr_len);
309 // Wait until data can be received without blocking.
310 size_t receive_from(implementation_type& impl, const null_buffers&,
311 endpoint_type& sender_endpoint, socket_base::message_flags,
312 boost::system::error_code& ec)
314 // Wait for socket to become ready.
315 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
317 // Reset endpoint since it can be given no sensible value at this time.
318 sender_endpoint = endpoint_type();
323 // Start an asynchronous receive. The buffer for the data being received and
324 // the sender_endpoint object must both be valid for the lifetime of the
325 // asynchronous operation.
326 template <typename MutableBufferSequence, typename Handler>
327 void async_receive_from(implementation_type& impl,
328 const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
329 socket_base::message_flags flags, Handler& handler)
331 bool is_continuation =
332 boost_asio_handler_cont_helpers::is_continuation(handler);
334 // Allocate and construct an operation to wrap the handler.
335 typedef reactive_socket_recvfrom_op<MutableBufferSequence,
336 endpoint_type, Handler> op;
337 typename op::ptr p = { boost::asio::detail::addressof(handler),
338 op::ptr::allocate(handler), 0 };
339 int protocol = impl.protocol_.type();
340 p.p = new (p.v) op(impl.socket_, protocol,
341 buffers, sender_endpoint, flags, handler);
343 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
344 &impl, impl.socket_, "async_receive_from"));
347 (flags & socket_base::message_out_of_band)
348 ? reactor::except_op : reactor::read_op,
349 p.p, is_continuation, true, false);
353 // Wait until data can be received without blocking.
354 template <typename Handler>
355 void async_receive_from(implementation_type& impl,
356 const null_buffers&, endpoint_type& sender_endpoint,
357 socket_base::message_flags flags, Handler& handler)
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_null_buffers_op<Handler> op;
364 typename op::ptr p = { boost::asio::detail::addressof(handler),
365 op::ptr::allocate(handler), 0 };
366 p.p = new (p.v) op(handler);
368 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
369 &impl, impl.socket_, "async_receive_from(null_buffers)"));
371 // Reset endpoint since it can be given no sensible value at this time.
372 sender_endpoint = endpoint_type();
375 (flags & socket_base::message_out_of_band)
376 ? reactor::except_op : reactor::read_op,
377 p.p, is_continuation, false, false);
381 // Accept a new connection.
382 template <typename Socket>
383 boost::system::error_code accept(implementation_type& impl,
384 Socket& peer, endpoint_type* peer_endpoint, boost::system::error_code& ec)
386 // We cannot accept a socket that is already open.
389 ec = boost::asio::error::already_open;
393 std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
394 socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
395 impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
396 peer_endpoint ? &addr_len : 0, ec));
398 // On success, assign new connection to peer socket object.
399 if (new_socket.get() != invalid_socket)
402 peer_endpoint->resize(addr_len);
403 peer.assign(impl.protocol_, new_socket.get(), ec);
405 new_socket.release();
411 #if defined(BOOST_ASIO_HAS_MOVE)
412 // Accept a new connection.
413 typename Protocol::socket accept(implementation_type& impl,
414 io_context* peer_io_context, endpoint_type* peer_endpoint,
415 boost::system::error_code& ec)
417 typename Protocol::socket peer(
418 peer_io_context ? *peer_io_context : io_context_);
420 std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
421 socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
422 impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
423 peer_endpoint ? &addr_len : 0, ec));
425 // On success, assign new connection to peer socket object.
426 if (new_socket.get() != invalid_socket)
429 peer_endpoint->resize(addr_len);
430 peer.assign(impl.protocol_, new_socket.get(), ec);
432 new_socket.release();
437 #endif // defined(BOOST_ASIO_HAS_MOVE)
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>
442 void async_accept(implementation_type& impl, Socket& peer,
443 endpoint_type* peer_endpoint, Handler& handler)
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> op;
450 typename op::ptr p = { boost::asio::detail::addressof(handler),
451 op::ptr::allocate(handler), 0 };
452 p.p = new (p.v) op(impl.socket_, impl.state_, peer,
453 impl.protocol_, peer_endpoint, handler);
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 Handler>
466 void async_accept(implementation_type& impl,
467 boost::asio::io_context* peer_io_context,
468 endpoint_type* peer_endpoint, Handler& handler)
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, Handler> op;
475 typename op::ptr p = { boost::asio::detail::addressof(handler),
476 op::ptr::allocate(handler), 0 };
477 p.p = new (p.v) op(peer_io_context ? *peer_io_context : io_context_,
478 impl.socket_, impl.state_, impl.protocol_, peer_endpoint, handler);
480 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
481 &impl, impl.socket_, "async_accept"));
483 start_accept_op(impl, p.p, is_continuation, false);
486 #endif // defined(BOOST_ASIO_HAS_MOVE)
488 // Connect the socket to the specified endpoint.
489 boost::system::error_code connect(implementation_type& impl,
490 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
492 socket_ops::sync_connect(impl.socket_,
493 peer_endpoint.data(), peer_endpoint.size(), ec);
497 // Start an asynchronous connect.
498 template <typename Handler>
499 void async_connect(implementation_type& impl,
500 const endpoint_type& peer_endpoint, Handler& handler)
502 bool is_continuation =
503 boost_asio_handler_cont_helpers::is_continuation(handler);
505 // Allocate and construct an operation to wrap the handler.
506 typedef reactive_socket_connect_op<Handler> op;
507 typename op::ptr p = { boost::asio::detail::addressof(handler),
508 op::ptr::allocate(handler), 0 };
509 p.p = new (p.v) op(impl.socket_, handler);
511 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
512 &impl, impl.socket_, "async_connect"));
514 start_connect_op(impl, p.p, is_continuation,
515 peer_endpoint.data(), peer_endpoint.size());
520 } // namespace detail
524 #include <boost/asio/detail/pop_options.hpp>
526 #endif // !defined(BOOST_ASIO_HAS_IOCP)
528 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP