reactive_socket_service.hpp 18 KB

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