reactive_socket_service.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. //
  2. // detail/reactive_socket_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. #ifndef BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if !defined(BOOST_ASIO_HAS_IOCP)
  17. #include <boost/asio/buffer.hpp>
  18. #include <boost/asio/error.hpp>
  19. #include <boost/asio/execution_context.hpp>
  20. #include <boost/asio/socket_base.hpp>
  21. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  22. #include <boost/asio/detail/memory.hpp>
  23. #include <boost/asio/detail/noncopyable.hpp>
  24. #include <boost/asio/detail/reactive_null_buffers_op.hpp>
  25. #include <boost/asio/detail/reactive_socket_accept_op.hpp>
  26. #include <boost/asio/detail/reactive_socket_connect_op.hpp>
  27. #include <boost/asio/detail/reactive_socket_recvfrom_op.hpp>
  28. #include <boost/asio/detail/reactive_socket_sendto_op.hpp>
  29. #include <boost/asio/detail/reactive_socket_service_base.hpp>
  30. #include <boost/asio/detail/reactor.hpp>
  31. #include <boost/asio/detail/reactor_op.hpp>
  32. #include <boost/asio/detail/socket_holder.hpp>
  33. #include <boost/asio/detail/socket_ops.hpp>
  34. #include <boost/asio/detail/socket_types.hpp>
  35. #include <boost/asio/detail/push_options.hpp>
  36. namespace boost {
  37. namespace asio {
  38. namespace detail {
  39. template <typename Protocol>
  40. class reactive_socket_service :
  41. public execution_context_service_base<reactive_socket_service<Protocol> >,
  42. public reactive_socket_service_base
  43. {
  44. public:
  45. // The protocol type.
  46. typedef Protocol protocol_type;
  47. // The endpoint type.
  48. typedef typename Protocol::endpoint endpoint_type;
  49. // The native type of a socket.
  50. typedef socket_type native_handle_type;
  51. // The implementation type of the socket.
  52. struct implementation_type :
  53. reactive_socket_service_base::base_implementation_type
  54. {
  55. // Default constructor.
  56. implementation_type()
  57. : protocol_(endpoint_type().protocol())
  58. {
  59. }
  60. // The protocol associated with the socket.
  61. protocol_type protocol_;
  62. };
  63. // Constructor.
  64. reactive_socket_service(execution_context& context)
  65. : execution_context_service_base<
  66. reactive_socket_service<Protocol> >(context),
  67. reactive_socket_service_base(context)
  68. {
  69. }
  70. // Destroy all user-defined handler objects owned by the service.
  71. void shutdown()
  72. {
  73. this->base_shutdown();
  74. }
  75. // Move-construct a new socket implementation.
  76. void move_construct(implementation_type& impl,
  77. implementation_type& other_impl) BOOST_ASIO_NOEXCEPT
  78. {
  79. this->base_move_construct(impl, other_impl);
  80. impl.protocol_ = other_impl.protocol_;
  81. other_impl.protocol_ = endpoint_type().protocol();
  82. }
  83. // Move-assign from another socket implementation.
  84. void move_assign(implementation_type& impl,
  85. reactive_socket_service_base& other_service,
  86. implementation_type& other_impl)
  87. {
  88. this->base_move_assign(impl, other_service, other_impl);
  89. impl.protocol_ = other_impl.protocol_;
  90. other_impl.protocol_ = endpoint_type().protocol();
  91. }
  92. // Move-construct a new socket implementation from another protocol type.
  93. template <typename Protocol1>
  94. void converting_move_construct(implementation_type& impl,
  95. reactive_socket_service<Protocol1>&,
  96. typename reactive_socket_service<
  97. Protocol1>::implementation_type& other_impl)
  98. {
  99. this->base_move_construct(impl, other_impl);
  100. impl.protocol_ = protocol_type(other_impl.protocol_);
  101. other_impl.protocol_ = typename Protocol1::endpoint().protocol();
  102. }
  103. // Open a new socket implementation.
  104. boost::system::error_code open(implementation_type& impl,
  105. const protocol_type& protocol, boost::system::error_code& ec)
  106. {
  107. if (!do_open(impl, protocol.family(),
  108. protocol.type(), protocol.protocol(), ec))
  109. impl.protocol_ = protocol;
  110. return ec;
  111. }
  112. // Assign a native socket to a socket implementation.
  113. boost::system::error_code assign(implementation_type& impl,
  114. const protocol_type& protocol, const native_handle_type& native_socket,
  115. boost::system::error_code& ec)
  116. {
  117. if (!do_assign(impl, protocol.type(), native_socket, ec))
  118. impl.protocol_ = protocol;
  119. return ec;
  120. }
  121. // Get the native socket representation.
  122. native_handle_type native_handle(implementation_type& impl)
  123. {
  124. return impl.socket_;
  125. }
  126. // Bind the socket to the specified local endpoint.
  127. boost::system::error_code bind(implementation_type& impl,
  128. const endpoint_type& endpoint, boost::system::error_code& ec)
  129. {
  130. socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
  131. return ec;
  132. }
  133. // Set a socket option.
  134. template <typename Option>
  135. boost::system::error_code set_option(implementation_type& impl,
  136. const Option& option, boost::system::error_code& ec)
  137. {
  138. socket_ops::setsockopt(impl.socket_, impl.state_,
  139. option.level(impl.protocol_), option.name(impl.protocol_),
  140. option.data(impl.protocol_), option.size(impl.protocol_), ec);
  141. return ec;
  142. }
  143. // Set a socket option.
  144. template <typename Option>
  145. boost::system::error_code get_option(const implementation_type& impl,
  146. Option& option, boost::system::error_code& ec) const
  147. {
  148. std::size_t size = option.size(impl.protocol_);
  149. socket_ops::getsockopt(impl.socket_, impl.state_,
  150. option.level(impl.protocol_), option.name(impl.protocol_),
  151. option.data(impl.protocol_), &size, ec);
  152. if (!ec)
  153. option.resize(impl.protocol_, size);
  154. return ec;
  155. }
  156. // Get the local endpoint.
  157. endpoint_type local_endpoint(const implementation_type& impl,
  158. boost::system::error_code& ec) const
  159. {
  160. endpoint_type endpoint;
  161. std::size_t addr_len = endpoint.capacity();
  162. if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec))
  163. return endpoint_type();
  164. endpoint.resize(addr_len);
  165. return endpoint;
  166. }
  167. // Get the remote endpoint.
  168. endpoint_type remote_endpoint(const implementation_type& impl,
  169. boost::system::error_code& ec) const
  170. {
  171. endpoint_type endpoint;
  172. std::size_t addr_len = endpoint.capacity();
  173. if (socket_ops::getpeername(impl.socket_,
  174. endpoint.data(), &addr_len, false, ec))
  175. return endpoint_type();
  176. endpoint.resize(addr_len);
  177. return endpoint;
  178. }
  179. // Disable sends or receives on the socket.
  180. boost::system::error_code shutdown(base_implementation_type& impl,
  181. socket_base::shutdown_type what, boost::system::error_code& ec)
  182. {
  183. socket_ops::shutdown(impl.socket_, what, ec);
  184. return ec;
  185. }
  186. // Send a datagram to the specified endpoint. Returns the number of bytes
  187. // sent.
  188. template <typename ConstBufferSequence>
  189. size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
  190. const endpoint_type& destination, socket_base::message_flags flags,
  191. boost::system::error_code& ec)
  192. {
  193. typedef buffer_sequence_adapter<boost::asio::const_buffer,
  194. ConstBufferSequence> bufs_type;
  195. if (bufs_type::is_single_buffer)
  196. {
  197. return socket_ops::sync_sendto1(impl.socket_, impl.state_,
  198. bufs_type::first(buffers).data(),
  199. bufs_type::first(buffers).size(), flags,
  200. destination.data(), destination.size(), ec);
  201. }
  202. else
  203. {
  204. bufs_type bufs(buffers);
  205. return socket_ops::sync_sendto(impl.socket_, impl.state_,
  206. bufs.buffers(), bufs.count(), flags,
  207. destination.data(), destination.size(), ec);
  208. }
  209. }
  210. // Wait until data can be sent without blocking.
  211. size_t send_to(implementation_type& impl, const null_buffers&,
  212. const endpoint_type&, socket_base::message_flags,
  213. boost::system::error_code& ec)
  214. {
  215. // Wait for socket to become ready.
  216. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  217. return 0;
  218. }
  219. // Start an asynchronous send. The data being sent must be valid for the
  220. // lifetime of the asynchronous operation.
  221. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  222. void async_send_to(implementation_type& impl,
  223. const ConstBufferSequence& buffers,
  224. const endpoint_type& destination, socket_base::message_flags flags,
  225. Handler& handler, const IoExecutor& io_ex)
  226. {
  227. bool is_continuation =
  228. boost_asio_handler_cont_helpers::is_continuation(handler);
  229. // Allocate and construct an operation to wrap the handler.
  230. typedef reactive_socket_sendto_op<ConstBufferSequence,
  231. endpoint_type, Handler, IoExecutor> op;
  232. typename op::ptr p = { boost::asio::detail::addressof(handler),
  233. op::ptr::allocate(handler), 0 };
  234. p.p = new (p.v) op(success_ec_, impl.socket_,
  235. buffers, destination, flags, handler, io_ex);
  236. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  237. &impl, impl.socket_, "async_send_to"));
  238. start_op(impl, reactor::write_op, p.p, is_continuation, true, false);
  239. p.v = p.p = 0;
  240. }
  241. // Start an asynchronous wait until data can be sent without blocking.
  242. template <typename Handler, typename IoExecutor>
  243. void async_send_to(implementation_type& impl, const null_buffers&,
  244. const endpoint_type&, socket_base::message_flags,
  245. Handler& handler, const IoExecutor& io_ex)
  246. {
  247. bool is_continuation =
  248. boost_asio_handler_cont_helpers::is_continuation(handler);
  249. // Allocate and construct an operation to wrap the handler.
  250. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  251. typename op::ptr p = { boost::asio::detail::addressof(handler),
  252. op::ptr::allocate(handler), 0 };
  253. p.p = new (p.v) op(success_ec_, handler, io_ex);
  254. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  255. &impl, impl.socket_, "async_send_to(null_buffers)"));
  256. start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
  257. p.v = p.p = 0;
  258. }
  259. // Receive a datagram with the endpoint of the sender. Returns the number of
  260. // bytes received.
  261. template <typename MutableBufferSequence>
  262. size_t receive_from(implementation_type& impl,
  263. const MutableBufferSequence& buffers,
  264. endpoint_type& sender_endpoint, socket_base::message_flags flags,
  265. boost::system::error_code& ec)
  266. {
  267. typedef buffer_sequence_adapter<boost::asio::mutable_buffer,
  268. MutableBufferSequence> bufs_type;
  269. std::size_t addr_len = sender_endpoint.capacity();
  270. std::size_t bytes_recvd;
  271. if (bufs_type::is_single_buffer)
  272. {
  273. bytes_recvd = socket_ops::sync_recvfrom1(impl.socket_,
  274. impl.state_, bufs_type::first(buffers).data(),
  275. bufs_type::first(buffers).size(), flags,
  276. sender_endpoint.data(), &addr_len, ec);
  277. }
  278. else
  279. {
  280. bufs_type bufs(buffers);
  281. bytes_recvd = socket_ops::sync_recvfrom(
  282. impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
  283. flags, sender_endpoint.data(), &addr_len, ec);
  284. }
  285. if (!ec)
  286. sender_endpoint.resize(addr_len);
  287. return bytes_recvd;
  288. }
  289. // Wait until data can be received without blocking.
  290. size_t receive_from(implementation_type& impl, const null_buffers&,
  291. endpoint_type& sender_endpoint, socket_base::message_flags,
  292. boost::system::error_code& ec)
  293. {
  294. // Wait for socket to become ready.
  295. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  296. // Reset endpoint since it can be given no sensible value at this time.
  297. sender_endpoint = endpoint_type();
  298. return 0;
  299. }
  300. // Start an asynchronous receive. The buffer for the data being received and
  301. // the sender_endpoint object must both be valid for the lifetime of the
  302. // asynchronous operation.
  303. template <typename MutableBufferSequence,
  304. typename Handler, typename IoExecutor>
  305. void async_receive_from(implementation_type& impl,
  306. const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
  307. socket_base::message_flags flags, Handler& handler,
  308. const IoExecutor& io_ex)
  309. {
  310. bool is_continuation =
  311. boost_asio_handler_cont_helpers::is_continuation(handler);
  312. // Allocate and construct an operation to wrap the handler.
  313. typedef reactive_socket_recvfrom_op<MutableBufferSequence,
  314. endpoint_type, Handler, IoExecutor> op;
  315. typename op::ptr p = { boost::asio::detail::addressof(handler),
  316. op::ptr::allocate(handler), 0 };
  317. int protocol = impl.protocol_.type();
  318. p.p = new (p.v) op(success_ec_, impl.socket_, protocol,
  319. buffers, sender_endpoint, flags, handler, io_ex);
  320. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  321. &impl, impl.socket_, "async_receive_from"));
  322. start_op(impl,
  323. (flags & socket_base::message_out_of_band)
  324. ? reactor::except_op : reactor::read_op,
  325. p.p, is_continuation, true, false);
  326. p.v = p.p = 0;
  327. }
  328. // Wait until data can be received without blocking.
  329. template <typename Handler, typename IoExecutor>
  330. void async_receive_from(implementation_type& impl, const null_buffers&,
  331. endpoint_type& sender_endpoint, socket_base::message_flags flags,
  332. Handler& handler, const IoExecutor& io_ex)
  333. {
  334. bool is_continuation =
  335. boost_asio_handler_cont_helpers::is_continuation(handler);
  336. // Allocate and construct an operation to wrap the handler.
  337. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  338. typename op::ptr p = { boost::asio::detail::addressof(handler),
  339. op::ptr::allocate(handler), 0 };
  340. p.p = new (p.v) op(success_ec_, handler, io_ex);
  341. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  342. &impl, impl.socket_, "async_receive_from(null_buffers)"));
  343. // Reset endpoint since it can be given no sensible value at this time.
  344. sender_endpoint = endpoint_type();
  345. start_op(impl,
  346. (flags & socket_base::message_out_of_band)
  347. ? reactor::except_op : reactor::read_op,
  348. p.p, is_continuation, false, false);
  349. p.v = p.p = 0;
  350. }
  351. // Accept a new connection.
  352. template <typename Socket>
  353. boost::system::error_code accept(implementation_type& impl,
  354. Socket& peer, endpoint_type* peer_endpoint, boost::system::error_code& ec)
  355. {
  356. // We cannot accept a socket that is already open.
  357. if (peer.is_open())
  358. {
  359. ec = boost::asio::error::already_open;
  360. return ec;
  361. }
  362. std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
  363. socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
  364. impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
  365. peer_endpoint ? &addr_len : 0, ec));
  366. // On success, assign new connection to peer socket object.
  367. if (new_socket.get() != invalid_socket)
  368. {
  369. if (peer_endpoint)
  370. peer_endpoint->resize(addr_len);
  371. peer.assign(impl.protocol_, new_socket.get(), ec);
  372. if (!ec)
  373. new_socket.release();
  374. }
  375. return ec;
  376. }
  377. // Start an asynchronous accept. The peer and peer_endpoint objects must be
  378. // valid until the accept's handler is invoked.
  379. template <typename Socket, typename Handler, typename IoExecutor>
  380. void async_accept(implementation_type& impl, Socket& peer,
  381. endpoint_type* peer_endpoint, Handler& handler, const IoExecutor& io_ex)
  382. {
  383. bool is_continuation =
  384. boost_asio_handler_cont_helpers::is_continuation(handler);
  385. // Allocate and construct an operation to wrap the handler.
  386. typedef reactive_socket_accept_op<Socket, Protocol, Handler, IoExecutor> op;
  387. typename op::ptr p = { boost::asio::detail::addressof(handler),
  388. op::ptr::allocate(handler), 0 };
  389. p.p = new (p.v) op(success_ec_, impl.socket_, impl.state_,
  390. peer, impl.protocol_, peer_endpoint, handler, io_ex);
  391. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  392. &impl, impl.socket_, "async_accept"));
  393. start_accept_op(impl, p.p, is_continuation, peer.is_open());
  394. p.v = p.p = 0;
  395. }
  396. #if defined(BOOST_ASIO_HAS_MOVE)
  397. // Start an asynchronous accept. The peer_endpoint object must be valid until
  398. // the accept's handler is invoked.
  399. template <typename PeerIoExecutor, typename Handler, typename IoExecutor>
  400. void async_move_accept(implementation_type& impl,
  401. const PeerIoExecutor& peer_io_ex, endpoint_type* peer_endpoint,
  402. Handler& handler, const IoExecutor& io_ex)
  403. {
  404. bool is_continuation =
  405. boost_asio_handler_cont_helpers::is_continuation(handler);
  406. // Allocate and construct an operation to wrap the handler.
  407. typedef reactive_socket_move_accept_op<Protocol,
  408. PeerIoExecutor, Handler, IoExecutor> op;
  409. typename op::ptr p = { boost::asio::detail::addressof(handler),
  410. op::ptr::allocate(handler), 0 };
  411. p.p = new (p.v) op(success_ec_, peer_io_ex, impl.socket_,
  412. impl.state_, impl.protocol_, peer_endpoint, handler, io_ex);
  413. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  414. &impl, impl.socket_, "async_accept"));
  415. start_accept_op(impl, p.p, is_continuation, false);
  416. p.v = p.p = 0;
  417. }
  418. #endif // defined(BOOST_ASIO_HAS_MOVE)
  419. // Connect the socket to the specified endpoint.
  420. boost::system::error_code connect(implementation_type& impl,
  421. const endpoint_type& peer_endpoint, boost::system::error_code& ec)
  422. {
  423. socket_ops::sync_connect(impl.socket_,
  424. peer_endpoint.data(), peer_endpoint.size(), ec);
  425. return ec;
  426. }
  427. // Start an asynchronous connect.
  428. template <typename Handler, typename IoExecutor>
  429. void async_connect(implementation_type& impl,
  430. const endpoint_type& peer_endpoint,
  431. Handler& handler, const IoExecutor& io_ex)
  432. {
  433. bool is_continuation =
  434. boost_asio_handler_cont_helpers::is_continuation(handler);
  435. // Allocate and construct an operation to wrap the handler.
  436. typedef reactive_socket_connect_op<Handler, IoExecutor> op;
  437. typename op::ptr p = { boost::asio::detail::addressof(handler),
  438. op::ptr::allocate(handler), 0 };
  439. p.p = new (p.v) op(success_ec_, impl.socket_, handler, io_ex);
  440. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  441. &impl, impl.socket_, "async_connect"));
  442. start_connect_op(impl, p.p, is_continuation,
  443. peer_endpoint.data(), peer_endpoint.size());
  444. p.v = p.p = 0;
  445. }
  446. };
  447. } // namespace detail
  448. } // namespace asio
  449. } // namespace boost
  450. #include <boost/asio/detail/pop_options.hpp>
  451. #endif // !defined(BOOST_ASIO_HAS_IOCP)
  452. #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP