reactive_socket_service.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. #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. buffer_sequence_adapter<boost::asio::const_buffer,
  194. ConstBufferSequence> bufs(buffers);
  195. return socket_ops::sync_sendto(impl.socket_, impl.state_,
  196. bufs.buffers(), bufs.count(), flags,
  197. destination.data(), destination.size(), ec);
  198. }
  199. // Wait until data can be sent without blocking.
  200. size_t send_to(implementation_type& impl, const null_buffers&,
  201. const endpoint_type&, socket_base::message_flags,
  202. boost::system::error_code& ec)
  203. {
  204. // Wait for socket to become ready.
  205. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  206. return 0;
  207. }
  208. // Start an asynchronous send. The data being sent must be valid for the
  209. // lifetime of the asynchronous operation.
  210. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  211. void async_send_to(implementation_type& impl,
  212. const ConstBufferSequence& buffers,
  213. const endpoint_type& destination, socket_base::message_flags flags,
  214. Handler& handler, const IoExecutor& io_ex)
  215. {
  216. bool is_continuation =
  217. boost_asio_handler_cont_helpers::is_continuation(handler);
  218. // Allocate and construct an operation to wrap the handler.
  219. typedef reactive_socket_sendto_op<ConstBufferSequence,
  220. endpoint_type, Handler, IoExecutor> op;
  221. typename op::ptr p = { boost::asio::detail::addressof(handler),
  222. op::ptr::allocate(handler), 0 };
  223. p.p = new (p.v) op(impl.socket_, buffers,
  224. destination, flags, handler, io_ex);
  225. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  226. &impl, impl.socket_, "async_send_to"));
  227. start_op(impl, reactor::write_op, p.p, is_continuation, true, false);
  228. p.v = p.p = 0;
  229. }
  230. // Start an asynchronous wait until data can be sent without blocking.
  231. template <typename Handler, typename IoExecutor>
  232. void async_send_to(implementation_type& impl, const null_buffers&,
  233. const endpoint_type&, socket_base::message_flags,
  234. Handler& handler, const IoExecutor& io_ex)
  235. {
  236. bool is_continuation =
  237. boost_asio_handler_cont_helpers::is_continuation(handler);
  238. // Allocate and construct an operation to wrap the handler.
  239. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  240. typename op::ptr p = { boost::asio::detail::addressof(handler),
  241. op::ptr::allocate(handler), 0 };
  242. p.p = new (p.v) op(handler, io_ex);
  243. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  244. &impl, impl.socket_, "async_send_to(null_buffers)"));
  245. start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
  246. p.v = p.p = 0;
  247. }
  248. // Receive a datagram with the endpoint of the sender. Returns the number of
  249. // bytes received.
  250. template <typename MutableBufferSequence>
  251. size_t receive_from(implementation_type& impl,
  252. const MutableBufferSequence& buffers,
  253. endpoint_type& sender_endpoint, socket_base::message_flags flags,
  254. boost::system::error_code& ec)
  255. {
  256. buffer_sequence_adapter<boost::asio::mutable_buffer,
  257. MutableBufferSequence> bufs(buffers);
  258. std::size_t addr_len = sender_endpoint.capacity();
  259. std::size_t bytes_recvd = socket_ops::sync_recvfrom(
  260. impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
  261. flags, sender_endpoint.data(), &addr_len, ec);
  262. if (!ec)
  263. sender_endpoint.resize(addr_len);
  264. return bytes_recvd;
  265. }
  266. // Wait until data can be received without blocking.
  267. size_t receive_from(implementation_type& impl, const null_buffers&,
  268. endpoint_type& sender_endpoint, socket_base::message_flags,
  269. boost::system::error_code& ec)
  270. {
  271. // Wait for socket to become ready.
  272. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  273. // Reset endpoint since it can be given no sensible value at this time.
  274. sender_endpoint = endpoint_type();
  275. return 0;
  276. }
  277. // Start an asynchronous receive. The buffer for the data being received and
  278. // the sender_endpoint object must both be valid for the lifetime of the
  279. // asynchronous operation.
  280. template <typename MutableBufferSequence,
  281. typename Handler, typename IoExecutor>
  282. void async_receive_from(implementation_type& impl,
  283. const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
  284. socket_base::message_flags flags, Handler& handler,
  285. const IoExecutor& io_ex)
  286. {
  287. bool is_continuation =
  288. boost_asio_handler_cont_helpers::is_continuation(handler);
  289. // Allocate and construct an operation to wrap the handler.
  290. typedef reactive_socket_recvfrom_op<MutableBufferSequence,
  291. endpoint_type, Handler, IoExecutor> op;
  292. typename op::ptr p = { boost::asio::detail::addressof(handler),
  293. op::ptr::allocate(handler), 0 };
  294. int protocol = impl.protocol_.type();
  295. p.p = new (p.v) op(impl.socket_, protocol, buffers,
  296. sender_endpoint, flags, handler, io_ex);
  297. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  298. &impl, impl.socket_, "async_receive_from"));
  299. start_op(impl,
  300. (flags & socket_base::message_out_of_band)
  301. ? reactor::except_op : reactor::read_op,
  302. p.p, is_continuation, true, false);
  303. p.v = p.p = 0;
  304. }
  305. // Wait until data can be received without blocking.
  306. template <typename Handler, typename IoExecutor>
  307. void async_receive_from(implementation_type& impl, const null_buffers&,
  308. endpoint_type& sender_endpoint, socket_base::message_flags flags,
  309. Handler& handler, const IoExecutor& io_ex)
  310. {
  311. bool is_continuation =
  312. boost_asio_handler_cont_helpers::is_continuation(handler);
  313. // Allocate and construct an operation to wrap the handler.
  314. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  315. typename op::ptr p = { boost::asio::detail::addressof(handler),
  316. op::ptr::allocate(handler), 0 };
  317. p.p = new (p.v) op(handler, io_ex);
  318. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  319. &impl, impl.socket_, "async_receive_from(null_buffers)"));
  320. // Reset endpoint since it can be given no sensible value at this time.
  321. sender_endpoint = endpoint_type();
  322. start_op(impl,
  323. (flags & socket_base::message_out_of_band)
  324. ? reactor::except_op : reactor::read_op,
  325. p.p, is_continuation, false, false);
  326. p.v = p.p = 0;
  327. }
  328. // Accept a new connection.
  329. template <typename Socket>
  330. boost::system::error_code accept(implementation_type& impl,
  331. Socket& peer, endpoint_type* peer_endpoint, boost::system::error_code& ec)
  332. {
  333. // We cannot accept a socket that is already open.
  334. if (peer.is_open())
  335. {
  336. ec = boost::asio::error::already_open;
  337. return ec;
  338. }
  339. std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
  340. socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
  341. impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
  342. peer_endpoint ? &addr_len : 0, ec));
  343. // On success, assign new connection to peer socket object.
  344. if (new_socket.get() != invalid_socket)
  345. {
  346. if (peer_endpoint)
  347. peer_endpoint->resize(addr_len);
  348. peer.assign(impl.protocol_, new_socket.get(), ec);
  349. if (!ec)
  350. new_socket.release();
  351. }
  352. return ec;
  353. }
  354. // Start an asynchronous accept. The peer and peer_endpoint objects must be
  355. // valid until the accept's handler is invoked.
  356. template <typename Socket, typename Handler, typename IoExecutor>
  357. void async_accept(implementation_type& impl, Socket& peer,
  358. endpoint_type* peer_endpoint, Handler& handler, const IoExecutor& io_ex)
  359. {
  360. bool is_continuation =
  361. boost_asio_handler_cont_helpers::is_continuation(handler);
  362. // Allocate and construct an operation to wrap the handler.
  363. typedef reactive_socket_accept_op<Socket, Protocol, Handler, IoExecutor> op;
  364. typename op::ptr p = { boost::asio::detail::addressof(handler),
  365. op::ptr::allocate(handler), 0 };
  366. p.p = new (p.v) op(impl.socket_, impl.state_, peer,
  367. impl.protocol_, peer_endpoint, handler, io_ex);
  368. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  369. &impl, impl.socket_, "async_accept"));
  370. start_accept_op(impl, p.p, is_continuation, peer.is_open());
  371. p.v = p.p = 0;
  372. }
  373. #if defined(BOOST_ASIO_HAS_MOVE)
  374. // Start an asynchronous accept. The peer_endpoint object must be valid until
  375. // the accept's handler is invoked.
  376. template <typename PeerIoExecutor, typename Handler, typename IoExecutor>
  377. void async_move_accept(implementation_type& impl,
  378. const PeerIoExecutor& peer_io_ex, endpoint_type* peer_endpoint,
  379. Handler& handler, const IoExecutor& io_ex)
  380. {
  381. bool is_continuation =
  382. boost_asio_handler_cont_helpers::is_continuation(handler);
  383. // Allocate and construct an operation to wrap the handler.
  384. typedef reactive_socket_move_accept_op<Protocol,
  385. PeerIoExecutor, Handler, IoExecutor> op;
  386. typename op::ptr p = { boost::asio::detail::addressof(handler),
  387. op::ptr::allocate(handler), 0 };
  388. p.p = new (p.v) op(peer_io_ex, impl.socket_, impl.state_,
  389. impl.protocol_, peer_endpoint, handler, io_ex);
  390. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  391. &impl, impl.socket_, "async_accept"));
  392. start_accept_op(impl, p.p, is_continuation, false);
  393. p.v = p.p = 0;
  394. }
  395. #endif // defined(BOOST_ASIO_HAS_MOVE)
  396. // Connect the socket to the specified endpoint.
  397. boost::system::error_code connect(implementation_type& impl,
  398. const endpoint_type& peer_endpoint, boost::system::error_code& ec)
  399. {
  400. socket_ops::sync_connect(impl.socket_,
  401. peer_endpoint.data(), peer_endpoint.size(), ec);
  402. return ec;
  403. }
  404. // Start an asynchronous connect.
  405. template <typename Handler, typename IoExecutor>
  406. void async_connect(implementation_type& impl,
  407. const endpoint_type& peer_endpoint,
  408. Handler& handler, const IoExecutor& io_ex)
  409. {
  410. bool is_continuation =
  411. boost_asio_handler_cont_helpers::is_continuation(handler);
  412. // Allocate and construct an operation to wrap the handler.
  413. typedef reactive_socket_connect_op<Handler, IoExecutor> op;
  414. typename op::ptr p = { boost::asio::detail::addressof(handler),
  415. op::ptr::allocate(handler), 0 };
  416. p.p = new (p.v) op(impl.socket_, handler, io_ex);
  417. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  418. &impl, impl.socket_, "async_connect"));
  419. start_connect_op(impl, p.p, is_continuation,
  420. peer_endpoint.data(), peer_endpoint.size());
  421. p.v = p.p = 0;
  422. }
  423. };
  424. } // namespace detail
  425. } // namespace asio
  426. } // namespace boost
  427. #include <boost/asio/detail/pop_options.hpp>
  428. #endif // !defined(BOOST_ASIO_HAS_IOCP)
  429. #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP