reactive_socket_service_base.hpp 18 KB

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