reactive_socket_service_base.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. //
  2. // detail/reactive_socket_service_base.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_BASE_HPP
  11. #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_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. && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  18. #include <boost/asio/buffer.hpp>
  19. #include <boost/asio/error.hpp>
  20. #include <boost/asio/execution_context.hpp>
  21. #include <boost/asio/socket_base.hpp>
  22. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  23. #include <boost/asio/detail/memory.hpp>
  24. #include <boost/asio/detail/reactive_null_buffers_op.hpp>
  25. #include <boost/asio/detail/reactive_socket_recv_op.hpp>
  26. #include <boost/asio/detail/reactive_socket_recvmsg_op.hpp>
  27. #include <boost/asio/detail/reactive_socket_send_op.hpp>
  28. #include <boost/asio/detail/reactive_wait_op.hpp>
  29. #include <boost/asio/detail/reactor.hpp>
  30. #include <boost/asio/detail/reactor_op.hpp>
  31. #include <boost/asio/detail/socket_holder.hpp>
  32. #include <boost/asio/detail/socket_ops.hpp>
  33. #include <boost/asio/detail/socket_types.hpp>
  34. #include <boost/asio/detail/push_options.hpp>
  35. namespace boost {
  36. namespace asio {
  37. namespace detail {
  38. class reactive_socket_service_base
  39. {
  40. public:
  41. // The native type of a socket.
  42. typedef socket_type native_handle_type;
  43. // The implementation type of the socket.
  44. struct base_implementation_type
  45. {
  46. // The native socket representation.
  47. socket_type socket_;
  48. // The current state of the socket.
  49. socket_ops::state_type state_;
  50. // Per-descriptor data used by the reactor.
  51. reactor::per_descriptor_data reactor_data_;
  52. };
  53. // Constructor.
  54. BOOST_ASIO_DECL reactive_socket_service_base(execution_context& context);
  55. // Destroy all user-defined handler objects owned by the service.
  56. BOOST_ASIO_DECL void base_shutdown();
  57. // Construct a new socket implementation.
  58. BOOST_ASIO_DECL void construct(base_implementation_type& impl);
  59. // Move-construct a new socket implementation.
  60. BOOST_ASIO_DECL void base_move_construct(base_implementation_type& impl,
  61. base_implementation_type& other_impl) BOOST_ASIO_NOEXCEPT;
  62. // Move-assign from another socket implementation.
  63. BOOST_ASIO_DECL void base_move_assign(base_implementation_type& impl,
  64. reactive_socket_service_base& other_service,
  65. base_implementation_type& other_impl);
  66. // Destroy a socket implementation.
  67. BOOST_ASIO_DECL void destroy(base_implementation_type& impl);
  68. // Determine whether the socket is open.
  69. bool is_open(const base_implementation_type& impl) const
  70. {
  71. return impl.socket_ != invalid_socket;
  72. }
  73. // Destroy a socket implementation.
  74. BOOST_ASIO_DECL boost::system::error_code close(
  75. base_implementation_type& impl, boost::system::error_code& ec);
  76. // Release ownership of the socket.
  77. BOOST_ASIO_DECL socket_type release(
  78. base_implementation_type& impl, boost::system::error_code& ec);
  79. // Get the native socket representation.
  80. native_handle_type native_handle(base_implementation_type& impl)
  81. {
  82. return impl.socket_;
  83. }
  84. // Cancel all operations associated with the socket.
  85. BOOST_ASIO_DECL boost::system::error_code cancel(
  86. base_implementation_type& impl, boost::system::error_code& ec);
  87. // Determine whether the socket is at the out-of-band data mark.
  88. bool at_mark(const base_implementation_type& impl,
  89. boost::system::error_code& ec) const
  90. {
  91. return socket_ops::sockatmark(impl.socket_, ec);
  92. }
  93. // Determine the number of bytes available for reading.
  94. std::size_t available(const base_implementation_type& impl,
  95. boost::system::error_code& ec) const
  96. {
  97. return socket_ops::available(impl.socket_, ec);
  98. }
  99. // Place the socket into the state where it will listen for new connections.
  100. boost::system::error_code listen(base_implementation_type& impl,
  101. int backlog, boost::system::error_code& ec)
  102. {
  103. socket_ops::listen(impl.socket_, backlog, ec);
  104. return ec;
  105. }
  106. // Perform an IO control command on the socket.
  107. template <typename IO_Control_Command>
  108. boost::system::error_code io_control(base_implementation_type& impl,
  109. IO_Control_Command& command, boost::system::error_code& ec)
  110. {
  111. socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
  112. static_cast<ioctl_arg_type*>(command.data()), ec);
  113. return ec;
  114. }
  115. // Gets the non-blocking mode of the socket.
  116. bool non_blocking(const base_implementation_type& impl) const
  117. {
  118. return (impl.state_ & socket_ops::user_set_non_blocking) != 0;
  119. }
  120. // Sets the non-blocking mode of the socket.
  121. boost::system::error_code non_blocking(base_implementation_type& impl,
  122. bool mode, boost::system::error_code& ec)
  123. {
  124. socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec);
  125. return ec;
  126. }
  127. // Gets the non-blocking mode of the native socket implementation.
  128. bool native_non_blocking(const base_implementation_type& impl) const
  129. {
  130. return (impl.state_ & socket_ops::internal_non_blocking) != 0;
  131. }
  132. // Sets the non-blocking mode of the native socket implementation.
  133. boost::system::error_code native_non_blocking(base_implementation_type& impl,
  134. bool mode, boost::system::error_code& ec)
  135. {
  136. socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec);
  137. return ec;
  138. }
  139. // Wait for the socket to become ready to read, ready to write, or to have
  140. // pending error conditions.
  141. boost::system::error_code wait(base_implementation_type& impl,
  142. socket_base::wait_type w, boost::system::error_code& ec)
  143. {
  144. switch (w)
  145. {
  146. case socket_base::wait_read:
  147. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  148. break;
  149. case socket_base::wait_write:
  150. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  151. break;
  152. case socket_base::wait_error:
  153. socket_ops::poll_error(impl.socket_, impl.state_, -1, ec);
  154. break;
  155. default:
  156. ec = boost::asio::error::invalid_argument;
  157. break;
  158. }
  159. return ec;
  160. }
  161. // Asynchronously wait for the socket to become ready to read, ready to
  162. // write, or to have pending error conditions.
  163. template <typename Handler, typename IoExecutor>
  164. void async_wait(base_implementation_type& impl,
  165. socket_base::wait_type w, Handler& handler, const IoExecutor& io_ex)
  166. {
  167. bool is_continuation =
  168. boost_asio_handler_cont_helpers::is_continuation(handler);
  169. // Allocate and construct an operation to wrap the handler.
  170. typedef reactive_wait_op<Handler, IoExecutor> op;
  171. typename op::ptr p = { boost::asio::detail::addressof(handler),
  172. op::ptr::allocate(handler), 0 };
  173. p.p = new (p.v) op(handler, io_ex);
  174. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  175. &impl, impl.socket_, "async_wait"));
  176. int op_type;
  177. switch (w)
  178. {
  179. case socket_base::wait_read:
  180. op_type = reactor::read_op;
  181. break;
  182. case socket_base::wait_write:
  183. op_type = reactor::write_op;
  184. break;
  185. case socket_base::wait_error:
  186. op_type = reactor::except_op;
  187. break;
  188. default:
  189. p.p->ec_ = boost::asio::error::invalid_argument;
  190. reactor_.post_immediate_completion(p.p, is_continuation);
  191. p.v = p.p = 0;
  192. return;
  193. }
  194. start_op(impl, op_type, p.p, is_continuation, false, false);
  195. p.v = p.p = 0;
  196. }
  197. // Send the given data to the peer.
  198. template <typename ConstBufferSequence>
  199. size_t send(base_implementation_type& impl,
  200. const ConstBufferSequence& buffers,
  201. socket_base::message_flags flags, boost::system::error_code& ec)
  202. {
  203. buffer_sequence_adapter<boost::asio::const_buffer,
  204. ConstBufferSequence> bufs(buffers);
  205. return socket_ops::sync_send(impl.socket_, impl.state_,
  206. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  207. }
  208. // Wait until data can be sent without blocking.
  209. size_t send(base_implementation_type& impl, const null_buffers&,
  210. socket_base::message_flags, boost::system::error_code& ec)
  211. {
  212. // Wait for socket to become ready.
  213. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  214. return 0;
  215. }
  216. // Start an asynchronous send. The data being sent must be valid for the
  217. // lifetime of the asynchronous operation.
  218. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  219. void async_send(base_implementation_type& impl,
  220. const ConstBufferSequence& buffers, socket_base::message_flags flags,
  221. Handler& handler, const IoExecutor& io_ex)
  222. {
  223. bool is_continuation =
  224. boost_asio_handler_cont_helpers::is_continuation(handler);
  225. // Allocate and construct an operation to wrap the handler.
  226. typedef reactive_socket_send_op<
  227. ConstBufferSequence, Handler, IoExecutor> op;
  228. typename op::ptr p = { boost::asio::detail::addressof(handler),
  229. op::ptr::allocate(handler), 0 };
  230. p.p = new (p.v) op(impl.socket_, impl.state_,
  231. buffers, flags, handler, io_ex);
  232. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  233. &impl, impl.socket_, "async_send"));
  234. start_op(impl, reactor::write_op, p.p, is_continuation, true,
  235. ((impl.state_ & socket_ops::stream_oriented)
  236. && buffer_sequence_adapter<boost::asio::const_buffer,
  237. ConstBufferSequence>::all_empty(buffers)));
  238. p.v = p.p = 0;
  239. }
  240. // Start an asynchronous wait until data can be sent without blocking.
  241. template <typename Handler, typename IoExecutor>
  242. void async_send(base_implementation_type& impl, const null_buffers&,
  243. socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
  244. {
  245. bool is_continuation =
  246. boost_asio_handler_cont_helpers::is_continuation(handler);
  247. // Allocate and construct an operation to wrap the handler.
  248. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  249. typename op::ptr p = { boost::asio::detail::addressof(handler),
  250. op::ptr::allocate(handler), 0 };
  251. p.p = new (p.v) op(handler, io_ex);
  252. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  253. &impl, impl.socket_, "async_send(null_buffers)"));
  254. start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
  255. p.v = p.p = 0;
  256. }
  257. // Receive some data from the peer. Returns the number of bytes received.
  258. template <typename MutableBufferSequence>
  259. size_t receive(base_implementation_type& impl,
  260. const MutableBufferSequence& buffers,
  261. socket_base::message_flags flags, boost::system::error_code& ec)
  262. {
  263. buffer_sequence_adapter<boost::asio::mutable_buffer,
  264. MutableBufferSequence> bufs(buffers);
  265. return socket_ops::sync_recv(impl.socket_, impl.state_,
  266. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  267. }
  268. // Wait until data can be received without blocking.
  269. size_t receive(base_implementation_type& impl, const null_buffers&,
  270. socket_base::message_flags, boost::system::error_code& ec)
  271. {
  272. // Wait for socket to become ready.
  273. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  274. return 0;
  275. }
  276. // Start an asynchronous receive. The buffer for the data being received
  277. // must be valid for the lifetime of the asynchronous operation.
  278. template <typename MutableBufferSequence,
  279. typename Handler, typename IoExecutor>
  280. void async_receive(base_implementation_type& impl,
  281. const MutableBufferSequence& buffers, socket_base::message_flags flags,
  282. Handler& handler, const IoExecutor& io_ex)
  283. {
  284. bool is_continuation =
  285. boost_asio_handler_cont_helpers::is_continuation(handler);
  286. // Allocate and construct an operation to wrap the handler.
  287. typedef reactive_socket_recv_op<
  288. MutableBufferSequence, Handler, IoExecutor> op;
  289. typename op::ptr p = { boost::asio::detail::addressof(handler),
  290. op::ptr::allocate(handler), 0 };
  291. p.p = new (p.v) op(impl.socket_, impl.state_,
  292. buffers, flags, handler, io_ex);
  293. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  294. &impl, impl.socket_, "async_receive"));
  295. start_op(impl,
  296. (flags & socket_base::message_out_of_band)
  297. ? reactor::except_op : reactor::read_op,
  298. p.p, is_continuation,
  299. (flags & socket_base::message_out_of_band) == 0,
  300. ((impl.state_ & socket_ops::stream_oriented)
  301. && buffer_sequence_adapter<boost::asio::mutable_buffer,
  302. MutableBufferSequence>::all_empty(buffers)));
  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(base_implementation_type& impl,
  308. const null_buffers&, 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(null_buffers)"));
  320. start_op(impl,
  321. (flags & socket_base::message_out_of_band)
  322. ? reactor::except_op : reactor::read_op,
  323. p.p, is_continuation, false, false);
  324. p.v = p.p = 0;
  325. }
  326. // Receive some data with associated flags. Returns the number of bytes
  327. // received.
  328. template <typename MutableBufferSequence>
  329. size_t receive_with_flags(base_implementation_type& impl,
  330. const MutableBufferSequence& buffers,
  331. socket_base::message_flags in_flags,
  332. socket_base::message_flags& out_flags, boost::system::error_code& ec)
  333. {
  334. buffer_sequence_adapter<boost::asio::mutable_buffer,
  335. MutableBufferSequence> bufs(buffers);
  336. return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
  337. bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
  338. }
  339. // Wait until data can be received without blocking.
  340. size_t receive_with_flags(base_implementation_type& impl,
  341. const null_buffers&, socket_base::message_flags,
  342. socket_base::message_flags& out_flags, boost::system::error_code& ec)
  343. {
  344. // Wait for socket to become ready.
  345. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  346. // Clear out_flags, since we cannot give it any other sensible value when
  347. // performing a null_buffers operation.
  348. out_flags = 0;
  349. return 0;
  350. }
  351. // Start an asynchronous receive. The buffer for the data being received
  352. // must be valid for the lifetime of the asynchronous operation.
  353. template <typename MutableBufferSequence,
  354. typename Handler, typename IoExecutor>
  355. void async_receive_with_flags(base_implementation_type& impl,
  356. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  357. socket_base::message_flags& out_flags, Handler& handler,
  358. 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_recvmsg_op<
  364. MutableBufferSequence, Handler, IoExecutor> op;
  365. typename op::ptr p = { boost::asio::detail::addressof(handler),
  366. op::ptr::allocate(handler), 0 };
  367. p.p = new (p.v) op(impl.socket_, buffers,
  368. in_flags, out_flags, handler, io_ex);
  369. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  370. &impl, impl.socket_, "async_receive_with_flags"));
  371. start_op(impl,
  372. (in_flags & socket_base::message_out_of_band)
  373. ? reactor::except_op : reactor::read_op,
  374. p.p, is_continuation,
  375. (in_flags & socket_base::message_out_of_band) == 0, false);
  376. p.v = p.p = 0;
  377. }
  378. // Wait until data can be received without blocking.
  379. template <typename Handler, typename IoExecutor>
  380. void async_receive_with_flags(base_implementation_type& impl,
  381. const null_buffers&, socket_base::message_flags in_flags,
  382. socket_base::message_flags& out_flags, Handler& handler,
  383. const IoExecutor& io_ex)
  384. {
  385. bool is_continuation =
  386. boost_asio_handler_cont_helpers::is_continuation(handler);
  387. // Allocate and construct an operation to wrap the handler.
  388. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  389. typename op::ptr p = { boost::asio::detail::addressof(handler),
  390. op::ptr::allocate(handler), 0 };
  391. p.p = new (p.v) op(handler, io_ex);
  392. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  393. &impl, impl.socket_, "async_receive_with_flags(null_buffers)"));
  394. // Clear out_flags, since we cannot give it any other sensible value when
  395. // performing a null_buffers operation.
  396. out_flags = 0;
  397. start_op(impl,
  398. (in_flags & socket_base::message_out_of_band)
  399. ? reactor::except_op : reactor::read_op,
  400. p.p, is_continuation, false, false);
  401. p.v = p.p = 0;
  402. }
  403. protected:
  404. // Open a new socket implementation.
  405. BOOST_ASIO_DECL boost::system::error_code do_open(
  406. base_implementation_type& impl, int af,
  407. int type, int protocol, boost::system::error_code& ec);
  408. // Assign a native socket to a socket implementation.
  409. BOOST_ASIO_DECL boost::system::error_code do_assign(
  410. base_implementation_type& impl, int type,
  411. const native_handle_type& native_socket, boost::system::error_code& ec);
  412. // Start the asynchronous read or write operation.
  413. BOOST_ASIO_DECL void start_op(base_implementation_type& impl, int op_type,
  414. reactor_op* op, bool is_continuation, bool is_non_blocking, bool noop);
  415. // Start the asynchronous accept operation.
  416. BOOST_ASIO_DECL void start_accept_op(base_implementation_type& impl,
  417. reactor_op* op, bool is_continuation, bool peer_is_open);
  418. // Start the asynchronous connect operation.
  419. BOOST_ASIO_DECL void start_connect_op(base_implementation_type& impl,
  420. reactor_op* op, bool is_continuation,
  421. const socket_addr_type* addr, size_t addrlen);
  422. // The selector that performs event demultiplexing for the service.
  423. reactor& reactor_;
  424. };
  425. } // namespace detail
  426. } // namespace asio
  427. } // namespace boost
  428. #include <boost/asio/detail/pop_options.hpp>
  429. #if defined(BOOST_ASIO_HEADER_ONLY)
  430. # include <boost/asio/detail/impl/reactive_socket_service_base.ipp>
  431. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  432. #endif // !defined(BOOST_ASIO_HAS_IOCP)
  433. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  434. #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP