2 // detail/reactive_socket_service.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_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 buffer_sequence_adapter<boost::asio::const_buffer,
224 ConstBufferSequence> bufs(buffers);
226 return socket_ops::sync_sendto(impl.socket_, impl.state_,
227 bufs.buffers(), bufs.count(), flags,
228 destination.data(), destination.size(), ec);
231 // Wait until data can be sent without blocking.
232 size_t send_to(implementation_type& impl, const null_buffers&,
233 const endpoint_type&, socket_base::message_flags,
234 boost::system::error_code& ec)
236 // Wait for socket to become ready.
237 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
242 // Start an asynchronous send. The data being sent must be valid for the
243 // lifetime of the asynchronous operation.
244 template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
245 void async_send_to(implementation_type& impl,
246 const ConstBufferSequence& buffers,
247 const endpoint_type& destination, socket_base::message_flags flags,
248 Handler& handler, const IoExecutor& io_ex)
250 bool is_continuation =
251 boost_asio_handler_cont_helpers::is_continuation(handler);
253 // Allocate and construct an operation to wrap the handler.
254 typedef reactive_socket_sendto_op<ConstBufferSequence,
255 endpoint_type, Handler, IoExecutor> op;
256 typename op::ptr p = { boost::asio::detail::addressof(handler),
257 op::ptr::allocate(handler), 0 };
258 p.p = new (p.v) op(impl.socket_, buffers,
259 destination, flags, handler, io_ex);
261 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
262 &impl, impl.socket_, "async_send_to"));
264 start_op(impl, reactor::write_op, p.p, is_continuation, true, false);
268 // Start an asynchronous wait until data can be sent without blocking.
269 template <typename Handler, typename IoExecutor>
270 void async_send_to(implementation_type& impl, const null_buffers&,
271 const endpoint_type&, socket_base::message_flags,
272 Handler& handler, const IoExecutor& io_ex)
274 bool is_continuation =
275 boost_asio_handler_cont_helpers::is_continuation(handler);
277 // Allocate and construct an operation to wrap the handler.
278 typedef reactive_null_buffers_op<Handler, IoExecutor> op;
279 typename op::ptr p = { boost::asio::detail::addressof(handler),
280 op::ptr::allocate(handler), 0 };
281 p.p = new (p.v) op(handler, io_ex);
283 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
284 &impl, impl.socket_, "async_send_to(null_buffers)"));
286 start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
290 // Receive a datagram with the endpoint of the sender. Returns the number of
292 template <typename MutableBufferSequence>
293 size_t receive_from(implementation_type& impl,
294 const MutableBufferSequence& buffers,
295 endpoint_type& sender_endpoint, socket_base::message_flags flags,
296 boost::system::error_code& ec)
298 buffer_sequence_adapter<boost::asio::mutable_buffer,
299 MutableBufferSequence> bufs(buffers);
301 std::size_t addr_len = sender_endpoint.capacity();
302 std::size_t bytes_recvd = socket_ops::sync_recvfrom(
303 impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
304 flags, sender_endpoint.data(), &addr_len, ec);
307 sender_endpoint.resize(addr_len);
312 // Wait until data can be received without blocking.
313 size_t receive_from(implementation_type& impl, const null_buffers&,
314 endpoint_type& sender_endpoint, socket_base::message_flags,
315 boost::system::error_code& ec)
317 // Wait for socket to become ready.
318 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
320 // Reset endpoint since it can be given no sensible value at this time.
321 sender_endpoint = endpoint_type();
326 // Start an asynchronous receive. The buffer for the data being received and
327 // the sender_endpoint object must both be valid for the lifetime of the
328 // asynchronous operation.
329 template <typename MutableBufferSequence,
330 typename Handler, typename IoExecutor>
331 void async_receive_from(implementation_type& impl,
332 const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
333 socket_base::message_flags flags, Handler& handler,
334 const IoExecutor& io_ex)
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_recvfrom_op<MutableBufferSequence,
341 endpoint_type, Handler, IoExecutor> op;
342 typename op::ptr p = { boost::asio::detail::addressof(handler),
343 op::ptr::allocate(handler), 0 };
344 int protocol = impl.protocol_.type();
345 p.p = new (p.v) op(impl.socket_, protocol, buffers,
346 sender_endpoint, flags, handler, io_ex);
348 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
349 &impl, impl.socket_, "async_receive_from"));
352 (flags & socket_base::message_out_of_band)
353 ? reactor::except_op : reactor::read_op,
354 p.p, is_continuation, true, false);
358 // Wait until data can be received without blocking.
359 template <typename Handler, typename IoExecutor>
360 void async_receive_from(implementation_type& impl, const null_buffers&,
361 endpoint_type& sender_endpoint, socket_base::message_flags flags,
362 Handler& handler, const IoExecutor& io_ex)
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, IoExecutor> 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, io_ex);
373 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
374 &impl, impl.socket_, "async_receive_from(null_buffers)"));
376 // Reset endpoint since it can be given no sensible value at this time.
377 sender_endpoint = endpoint_type();
380 (flags & socket_base::message_out_of_band)
381 ? reactor::except_op : reactor::read_op,
382 p.p, is_continuation, false, false);
386 // Accept a new connection.
387 template <typename Socket>
388 boost::system::error_code accept(implementation_type& impl,
389 Socket& peer, endpoint_type* peer_endpoint, boost::system::error_code& ec)
391 // We cannot accept a socket that is already open.
394 ec = boost::asio::error::already_open;
398 std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
399 socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
400 impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
401 peer_endpoint ? &addr_len : 0, ec));
403 // On success, assign new connection to peer socket object.
404 if (new_socket.get() != invalid_socket)
407 peer_endpoint->resize(addr_len);
408 peer.assign(impl.protocol_, new_socket.get(), ec);
410 new_socket.release();
416 // Start an asynchronous accept. The peer and peer_endpoint objects must be
417 // valid until the accept's handler is invoked.
418 template <typename Socket, typename Handler, typename IoExecutor>
419 void async_accept(implementation_type& impl, Socket& peer,
420 endpoint_type* peer_endpoint, Handler& handler, const IoExecutor& io_ex)
422 bool is_continuation =
423 boost_asio_handler_cont_helpers::is_continuation(handler);
425 // Allocate and construct an operation to wrap the handler.
426 typedef reactive_socket_accept_op<Socket, Protocol, Handler, IoExecutor> op;
427 typename op::ptr p = { boost::asio::detail::addressof(handler),
428 op::ptr::allocate(handler), 0 };
429 p.p = new (p.v) op(impl.socket_, impl.state_, peer,
430 impl.protocol_, peer_endpoint, handler, io_ex);
432 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
433 &impl, impl.socket_, "async_accept"));
435 start_accept_op(impl, p.p, is_continuation, peer.is_open());
439 #if defined(BOOST_ASIO_HAS_MOVE)
440 // Start an asynchronous accept. The peer_endpoint object must be valid until
441 // the accept's handler is invoked.
442 template <typename PeerIoExecutor, typename Handler, typename IoExecutor>
443 void async_move_accept(implementation_type& impl,
444 const PeerIoExecutor& peer_io_ex, endpoint_type* peer_endpoint,
445 Handler& handler, const IoExecutor& io_ex)
447 bool is_continuation =
448 boost_asio_handler_cont_helpers::is_continuation(handler);
450 // Allocate and construct an operation to wrap the handler.
451 typedef reactive_socket_move_accept_op<Protocol,
452 PeerIoExecutor, Handler, IoExecutor> op;
453 typename op::ptr p = { boost::asio::detail::addressof(handler),
454 op::ptr::allocate(handler), 0 };
455 p.p = new (p.v) op(peer_io_ex, impl.socket_, impl.state_,
456 impl.protocol_, peer_endpoint, handler, io_ex);
458 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
459 &impl, impl.socket_, "async_accept"));
461 start_accept_op(impl, p.p, is_continuation, false);
464 #endif // defined(BOOST_ASIO_HAS_MOVE)
466 // Connect the socket to the specified endpoint.
467 boost::system::error_code connect(implementation_type& impl,
468 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
470 socket_ops::sync_connect(impl.socket_,
471 peer_endpoint.data(), peer_endpoint.size(), ec);
475 // Start an asynchronous connect.
476 template <typename Handler, typename IoExecutor>
477 void async_connect(implementation_type& impl,
478 const endpoint_type& peer_endpoint,
479 Handler& handler, const IoExecutor& io_ex)
481 bool is_continuation =
482 boost_asio_handler_cont_helpers::is_continuation(handler);
484 // Allocate and construct an operation to wrap the handler.
485 typedef reactive_socket_connect_op<Handler, IoExecutor> op;
486 typename op::ptr p = { boost::asio::detail::addressof(handler),
487 op::ptr::allocate(handler), 0 };
488 p.p = new (p.v) op(impl.socket_, handler, io_ex);
490 BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
491 &impl, impl.socket_, "async_connect"));
493 start_connect_op(impl, p.p, is_continuation,
494 peer_endpoint.data(), peer_endpoint.size());
499 } // namespace detail
503 #include <boost/asio/detail/pop_options.hpp>
505 #endif // !defined(BOOST_ASIO_HAS_IOCP)
507 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP