Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / asio / detail / reactive_socket_service.hpp
1 //
2 // detail/reactive_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
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)
9 //
10
11 #ifndef BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19
20 #if !defined(BOOST_ASIO_HAS_IOCP)
21
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>
40
41 #include <boost/asio/detail/push_options.hpp>
42
43 namespace boost {
44 namespace asio {
45 namespace detail {
46
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
51 {
52 public:
53   // The protocol type.
54   typedef Protocol protocol_type;
55
56   // The endpoint type.
57   typedef typename Protocol::endpoint endpoint_type;
58
59   // The native type of a socket.
60   typedef socket_type native_handle_type;
61
62   // The implementation type of the socket.
63   struct implementation_type :
64     reactive_socket_service_base::base_implementation_type
65   {
66     // Default constructor.
67     implementation_type()
68       : protocol_(endpoint_type().protocol())
69     {
70     }
71
72     // The protocol associated with the socket.
73     protocol_type protocol_;
74   };
75
76   // Constructor.
77   reactive_socket_service(execution_context& context)
78     : execution_context_service_base<
79         reactive_socket_service<Protocol> >(context),
80       reactive_socket_service_base(context)
81   {
82   }
83
84   // Destroy all user-defined handler objects owned by the service.
85   void shutdown()
86   {
87     this->base_shutdown();
88   }
89
90   // Move-construct a new socket implementation.
91   void move_construct(implementation_type& impl,
92       implementation_type& other_impl) BOOST_ASIO_NOEXCEPT
93   {
94     this->base_move_construct(impl, other_impl);
95
96     impl.protocol_ = other_impl.protocol_;
97     other_impl.protocol_ = endpoint_type().protocol();
98   }
99
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)
104   {
105     this->base_move_assign(impl, other_service, other_impl);
106
107     impl.protocol_ = other_impl.protocol_;
108     other_impl.protocol_ = endpoint_type().protocol();
109   }
110
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)
117   {
118     this->base_move_construct(impl, other_impl);
119
120     impl.protocol_ = protocol_type(other_impl.protocol_);
121     other_impl.protocol_ = typename Protocol1::endpoint().protocol();
122   }
123
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)
127   {
128     if (!do_open(impl, protocol.family(),
129           protocol.type(), protocol.protocol(), ec))
130       impl.protocol_ = protocol;
131     return ec;
132   }
133
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)
138   {
139     if (!do_assign(impl, protocol.type(), native_socket, ec))
140       impl.protocol_ = protocol;
141     return ec;
142   }
143
144   // Get the native socket representation.
145   native_handle_type native_handle(implementation_type& impl)
146   {
147     return impl.socket_;
148   }
149
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)
153   {
154     socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
155     return ec;
156   }
157
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)
162   {
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);
166     return ec;
167   }
168
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
173   {
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);
178     if (!ec)
179       option.resize(impl.protocol_, size);
180     return ec;
181   }
182
183   // Get the local endpoint.
184   endpoint_type local_endpoint(const implementation_type& impl,
185       boost::system::error_code& ec) const
186   {
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);
192     return endpoint;
193   }
194
195   // Get the remote endpoint.
196   endpoint_type remote_endpoint(const implementation_type& impl,
197       boost::system::error_code& ec) const
198   {
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);
205     return endpoint;
206   }
207
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)
211   {
212     socket_ops::shutdown(impl.socket_, what, ec);
213     return ec;
214   }
215
216   // Send a datagram to the specified endpoint. Returns the number of bytes
217   // sent.
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)
222   {
223     buffer_sequence_adapter<boost::asio::const_buffer,
224         ConstBufferSequence> bufs(buffers);
225
226     return socket_ops::sync_sendto(impl.socket_, impl.state_,
227         bufs.buffers(), bufs.count(), flags,
228         destination.data(), destination.size(), ec);
229   }
230
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)
235   {
236     // Wait for socket to become ready.
237     socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
238
239     return 0;
240   }
241
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)
249   {
250     bool is_continuation =
251       boost_asio_handler_cont_helpers::is_continuation(handler);
252
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);
260
261     BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
262           &impl, impl.socket_, "async_send_to"));
263
264     start_op(impl, reactor::write_op, p.p, is_continuation, true, false);
265     p.v = p.p = 0;
266   }
267
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)
273   {
274     bool is_continuation =
275       boost_asio_handler_cont_helpers::is_continuation(handler);
276
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);
282
283     BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
284           &impl, impl.socket_, "async_send_to(null_buffers)"));
285
286     start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
287     p.v = p.p = 0;
288   }
289
290   // Receive a datagram with the endpoint of the sender. Returns the number of
291   // bytes received.
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)
297   {
298     buffer_sequence_adapter<boost::asio::mutable_buffer,
299         MutableBufferSequence> bufs(buffers);
300
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);
305
306     if (!ec)
307       sender_endpoint.resize(addr_len);
308
309     return bytes_recvd;
310   }
311
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)
316   {
317     // Wait for socket to become ready.
318     socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
319
320     // Reset endpoint since it can be given no sensible value at this time.
321     sender_endpoint = endpoint_type();
322
323     return 0;
324   }
325
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)
335   {
336     bool is_continuation =
337       boost_asio_handler_cont_helpers::is_continuation(handler);
338
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);
347
348     BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
349           &impl, impl.socket_, "async_receive_from"));
350
351     start_op(impl,
352         (flags & socket_base::message_out_of_band)
353           ? reactor::except_op : reactor::read_op,
354         p.p, is_continuation, true, false);
355     p.v = p.p = 0;
356   }
357
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)
363   {
364     bool is_continuation =
365       boost_asio_handler_cont_helpers::is_continuation(handler);
366
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);
372
373     BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
374           &impl, impl.socket_, "async_receive_from(null_buffers)"));
375
376     // Reset endpoint since it can be given no sensible value at this time.
377     sender_endpoint = endpoint_type();
378
379     start_op(impl,
380         (flags & socket_base::message_out_of_band)
381           ? reactor::except_op : reactor::read_op,
382         p.p, is_continuation, false, false);
383     p.v = p.p = 0;
384   }
385
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)
390   {
391     // We cannot accept a socket that is already open.
392     if (peer.is_open())
393     {
394       ec = boost::asio::error::already_open;
395       return ec;
396     }
397
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));
402
403     // On success, assign new connection to peer socket object.
404     if (new_socket.get() != invalid_socket)
405     {
406       if (peer_endpoint)
407         peer_endpoint->resize(addr_len);
408       peer.assign(impl.protocol_, new_socket.get(), ec);
409       if (!ec)
410         new_socket.release();
411     }
412
413     return ec;
414   }
415
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)
421   {
422     bool is_continuation =
423       boost_asio_handler_cont_helpers::is_continuation(handler);
424
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);
431
432     BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
433           &impl, impl.socket_, "async_accept"));
434
435     start_accept_op(impl, p.p, is_continuation, peer.is_open());
436     p.v = p.p = 0;
437   }
438
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)
446   {
447     bool is_continuation =
448       boost_asio_handler_cont_helpers::is_continuation(handler);
449
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);
457
458     BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
459           &impl, impl.socket_, "async_accept"));
460
461     start_accept_op(impl, p.p, is_continuation, false);
462     p.v = p.p = 0;
463   }
464 #endif // defined(BOOST_ASIO_HAS_MOVE)
465
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)
469   {
470     socket_ops::sync_connect(impl.socket_,
471         peer_endpoint.data(), peer_endpoint.size(), ec);
472     return ec;
473   }
474
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)
480   {
481     bool is_continuation =
482       boost_asio_handler_cont_helpers::is_continuation(handler);
483
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);
489
490     BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
491           &impl, impl.socket_, "async_connect"));
492
493     start_connect_op(impl, p.p, is_continuation,
494         peer_endpoint.data(), peer_endpoint.size());
495     p.v = p.p = 0;
496   }
497 };
498
499 } // namespace detail
500 } // namespace asio
501 } // namespace boost
502
503 #include <boost/asio/detail/pop_options.hpp>
504
505 #endif // !defined(BOOST_ASIO_HAS_IOCP)
506
507 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP