reactive_socket_service_base.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. //
  2. // detail/reactive_socket_service_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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_service.hpp>
  21. #include <boost/asio/socket_base.hpp>
  22. #include <boost/asio/detail/addressof.hpp>
  23. #include <boost/asio/detail/buffer_sequence_adapter.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/reactor.hpp>
  29. #include <boost/asio/detail/reactor_op.hpp>
  30. #include <boost/asio/detail/socket_holder.hpp>
  31. #include <boost/asio/detail/socket_ops.hpp>
  32. #include <boost/asio/detail/socket_types.hpp>
  33. #include <boost/asio/detail/push_options.hpp>
  34. namespace boost {
  35. namespace asio {
  36. namespace detail {
  37. class reactive_socket_service_base
  38. {
  39. public:
  40. // The native type of a socket.
  41. typedef socket_type native_handle_type;
  42. // The implementation type of the socket.
  43. struct base_implementation_type
  44. {
  45. // The native socket representation.
  46. socket_type socket_;
  47. // The current state of the socket.
  48. socket_ops::state_type state_;
  49. // Per-descriptor data used by the reactor.
  50. reactor::per_descriptor_data reactor_data_;
  51. };
  52. // Constructor.
  53. BOOST_ASIO_DECL reactive_socket_service_base(
  54. boost::asio::io_service& io_service);
  55. // Destroy all user-defined handler objects owned by the service.
  56. BOOST_ASIO_DECL void shutdown_service();
  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);
  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. // Get the native socket representation.
  77. native_handle_type native_handle(base_implementation_type& impl)
  78. {
  79. return impl.socket_;
  80. }
  81. // Cancel all operations associated with the socket.
  82. BOOST_ASIO_DECL boost::system::error_code cancel(
  83. base_implementation_type& impl, boost::system::error_code& ec);
  84. // Determine whether the socket is at the out-of-band data mark.
  85. bool at_mark(const base_implementation_type& impl,
  86. boost::system::error_code& ec) const
  87. {
  88. return socket_ops::sockatmark(impl.socket_, ec);
  89. }
  90. // Determine the number of bytes available for reading.
  91. std::size_t available(const base_implementation_type& impl,
  92. boost::system::error_code& ec) const
  93. {
  94. return socket_ops::available(impl.socket_, ec);
  95. }
  96. // Place the socket into the state where it will listen for new connections.
  97. boost::system::error_code listen(base_implementation_type& impl,
  98. int backlog, boost::system::error_code& ec)
  99. {
  100. socket_ops::listen(impl.socket_, backlog, ec);
  101. return ec;
  102. }
  103. // Perform an IO control command on the socket.
  104. template <typename IO_Control_Command>
  105. boost::system::error_code io_control(base_implementation_type& impl,
  106. IO_Control_Command& command, boost::system::error_code& ec)
  107. {
  108. socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
  109. static_cast<ioctl_arg_type*>(command.data()), ec);
  110. return ec;
  111. }
  112. // Gets the non-blocking mode of the socket.
  113. bool non_blocking(const base_implementation_type& impl) const
  114. {
  115. return (impl.state_ & socket_ops::user_set_non_blocking) != 0;
  116. }
  117. // Sets the non-blocking mode of the socket.
  118. boost::system::error_code non_blocking(base_implementation_type& impl,
  119. bool mode, boost::system::error_code& ec)
  120. {
  121. socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec);
  122. return ec;
  123. }
  124. // Gets the non-blocking mode of the native socket implementation.
  125. bool native_non_blocking(const base_implementation_type& impl) const
  126. {
  127. return (impl.state_ & socket_ops::internal_non_blocking) != 0;
  128. }
  129. // Sets the non-blocking mode of the native socket implementation.
  130. boost::system::error_code native_non_blocking(base_implementation_type& impl,
  131. bool mode, boost::system::error_code& ec)
  132. {
  133. socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec);
  134. return ec;
  135. }
  136. // Disable sends or receives on the socket.
  137. boost::system::error_code shutdown(base_implementation_type& impl,
  138. socket_base::shutdown_type what, boost::system::error_code& ec)
  139. {
  140. socket_ops::shutdown(impl.socket_, what, ec);
  141. return ec;
  142. }
  143. // Send the given data to the peer.
  144. template <typename ConstBufferSequence>
  145. size_t send(base_implementation_type& impl,
  146. const ConstBufferSequence& buffers,
  147. socket_base::message_flags flags, boost::system::error_code& ec)
  148. {
  149. buffer_sequence_adapter<boost::asio::const_buffer,
  150. ConstBufferSequence> bufs(buffers);
  151. return socket_ops::sync_send(impl.socket_, impl.state_,
  152. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  153. }
  154. // Wait until data can be sent without blocking.
  155. size_t send(base_implementation_type& impl, const null_buffers&,
  156. socket_base::message_flags, boost::system::error_code& ec)
  157. {
  158. // Wait for socket to become ready.
  159. socket_ops::poll_write(impl.socket_, impl.state_, ec);
  160. return 0;
  161. }
  162. // Start an asynchronous send. The data being sent must be valid for the
  163. // lifetime of the asynchronous operation.
  164. template <typename ConstBufferSequence, typename Handler>
  165. void async_send(base_implementation_type& impl,
  166. const ConstBufferSequence& buffers,
  167. socket_base::message_flags flags, Handler& handler)
  168. {
  169. bool is_continuation =
  170. boost_asio_handler_cont_helpers::is_continuation(handler);
  171. // Allocate and construct an operation to wrap the handler.
  172. typedef reactive_socket_send_op<ConstBufferSequence, Handler> op;
  173. typename op::ptr p = { boost::asio::detail::addressof(handler),
  174. boost_asio_handler_alloc_helpers::allocate(
  175. sizeof(op), handler), 0 };
  176. p.p = new (p.v) op(impl.socket_, buffers, flags, handler);
  177. BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send"));
  178. start_op(impl, reactor::write_op, p.p, is_continuation, true,
  179. ((impl.state_ & socket_ops::stream_oriented)
  180. && buffer_sequence_adapter<boost::asio::const_buffer,
  181. ConstBufferSequence>::all_empty(buffers)));
  182. p.v = p.p = 0;
  183. }
  184. // Start an asynchronous wait until data can be sent without blocking.
  185. template <typename Handler>
  186. void async_send(base_implementation_type& impl, const null_buffers&,
  187. socket_base::message_flags, Handler& handler)
  188. {
  189. bool is_continuation =
  190. boost_asio_handler_cont_helpers::is_continuation(handler);
  191. // Allocate and construct an operation to wrap the handler.
  192. typedef reactive_null_buffers_op<Handler> op;
  193. typename op::ptr p = { boost::asio::detail::addressof(handler),
  194. boost_asio_handler_alloc_helpers::allocate(
  195. sizeof(op), handler), 0 };
  196. p.p = new (p.v) op(handler);
  197. BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
  198. &impl, "async_send(null_buffers)"));
  199. start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
  200. p.v = p.p = 0;
  201. }
  202. // Receive some data from the peer. Returns the number of bytes received.
  203. template <typename MutableBufferSequence>
  204. size_t receive(base_implementation_type& impl,
  205. const MutableBufferSequence& buffers,
  206. socket_base::message_flags flags, boost::system::error_code& ec)
  207. {
  208. buffer_sequence_adapter<boost::asio::mutable_buffer,
  209. MutableBufferSequence> bufs(buffers);
  210. return socket_ops::sync_recv(impl.socket_, impl.state_,
  211. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  212. }
  213. // Wait until data can be received without blocking.
  214. size_t receive(base_implementation_type& impl, const null_buffers&,
  215. socket_base::message_flags, boost::system::error_code& ec)
  216. {
  217. // Wait for socket to become ready.
  218. socket_ops::poll_read(impl.socket_, impl.state_, ec);
  219. return 0;
  220. }
  221. // Start an asynchronous receive. The buffer for the data being received
  222. // must be valid for the lifetime of the asynchronous operation.
  223. template <typename MutableBufferSequence, typename Handler>
  224. void async_receive(base_implementation_type& impl,
  225. const MutableBufferSequence& buffers,
  226. socket_base::message_flags flags, Handler& handler)
  227. {
  228. bool is_continuation =
  229. boost_asio_handler_cont_helpers::is_continuation(handler);
  230. // Allocate and construct an operation to wrap the handler.
  231. typedef reactive_socket_recv_op<MutableBufferSequence, Handler> op;
  232. typename op::ptr p = { boost::asio::detail::addressof(handler),
  233. boost_asio_handler_alloc_helpers::allocate(
  234. sizeof(op), handler), 0 };
  235. p.p = new (p.v) op(impl.socket_, impl.state_, buffers, flags, handler);
  236. BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_receive"));
  237. start_op(impl,
  238. (flags & socket_base::message_out_of_band)
  239. ? reactor::except_op : reactor::read_op,
  240. p.p, is_continuation,
  241. (flags & socket_base::message_out_of_band) == 0,
  242. ((impl.state_ & socket_ops::stream_oriented)
  243. && buffer_sequence_adapter<boost::asio::mutable_buffer,
  244. MutableBufferSequence>::all_empty(buffers)));
  245. p.v = p.p = 0;
  246. }
  247. // Wait until data can be received without blocking.
  248. template <typename Handler>
  249. void async_receive(base_implementation_type& impl, const null_buffers&,
  250. socket_base::message_flags flags, Handler& handler)
  251. {
  252. bool is_continuation =
  253. boost_asio_handler_cont_helpers::is_continuation(handler);
  254. // Allocate and construct an operation to wrap the handler.
  255. typedef reactive_null_buffers_op<Handler> op;
  256. typename op::ptr p = { boost::asio::detail::addressof(handler),
  257. boost_asio_handler_alloc_helpers::allocate(
  258. sizeof(op), handler), 0 };
  259. p.p = new (p.v) op(handler);
  260. BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
  261. &impl, "async_receive(null_buffers)"));
  262. start_op(impl,
  263. (flags & socket_base::message_out_of_band)
  264. ? reactor::except_op : reactor::read_op,
  265. p.p, is_continuation, false, false);
  266. p.v = p.p = 0;
  267. }
  268. // Receive some data with associated flags. Returns the number of bytes
  269. // received.
  270. template <typename MutableBufferSequence>
  271. size_t receive_with_flags(base_implementation_type& impl,
  272. const MutableBufferSequence& buffers,
  273. socket_base::message_flags in_flags,
  274. socket_base::message_flags& out_flags, boost::system::error_code& ec)
  275. {
  276. buffer_sequence_adapter<boost::asio::mutable_buffer,
  277. MutableBufferSequence> bufs(buffers);
  278. return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
  279. bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
  280. }
  281. // Wait until data can be received without blocking.
  282. size_t receive_with_flags(base_implementation_type& impl,
  283. const null_buffers&, socket_base::message_flags,
  284. socket_base::message_flags& out_flags, boost::system::error_code& ec)
  285. {
  286. // Wait for socket to become ready.
  287. socket_ops::poll_read(impl.socket_, impl.state_, ec);
  288. // Clear out_flags, since we cannot give it any other sensible value when
  289. // performing a null_buffers operation.
  290. out_flags = 0;
  291. return 0;
  292. }
  293. // Start an asynchronous receive. The buffer for the data being received
  294. // must be valid for the lifetime of the asynchronous operation.
  295. template <typename MutableBufferSequence, typename Handler>
  296. void async_receive_with_flags(base_implementation_type& impl,
  297. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  298. socket_base::message_flags& out_flags, Handler& handler)
  299. {
  300. bool is_continuation =
  301. boost_asio_handler_cont_helpers::is_continuation(handler);
  302. // Allocate and construct an operation to wrap the handler.
  303. typedef reactive_socket_recvmsg_op<MutableBufferSequence, Handler> op;
  304. typename op::ptr p = { boost::asio::detail::addressof(handler),
  305. boost_asio_handler_alloc_helpers::allocate(
  306. sizeof(op), handler), 0 };
  307. p.p = new (p.v) op(impl.socket_, buffers, in_flags, out_flags, handler);
  308. BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
  309. &impl, "async_receive_with_flags"));
  310. start_op(impl,
  311. (in_flags & socket_base::message_out_of_band)
  312. ? reactor::except_op : reactor::read_op,
  313. p.p, is_continuation,
  314. (in_flags & socket_base::message_out_of_band) == 0, false);
  315. p.v = p.p = 0;
  316. }
  317. // Wait until data can be received without blocking.
  318. template <typename Handler>
  319. void async_receive_with_flags(base_implementation_type& impl,
  320. const null_buffers&, socket_base::message_flags in_flags,
  321. socket_base::message_flags& out_flags, Handler& handler)
  322. {
  323. bool is_continuation =
  324. boost_asio_handler_cont_helpers::is_continuation(handler);
  325. // Allocate and construct an operation to wrap the handler.
  326. typedef reactive_null_buffers_op<Handler> op;
  327. typename op::ptr p = { boost::asio::detail::addressof(handler),
  328. boost_asio_handler_alloc_helpers::allocate(
  329. sizeof(op), handler), 0 };
  330. p.p = new (p.v) op(handler);
  331. BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl,
  332. "async_receive_with_flags(null_buffers)"));
  333. // Clear out_flags, since we cannot give it any other sensible value when
  334. // performing a null_buffers operation.
  335. out_flags = 0;
  336. start_op(impl,
  337. (in_flags & socket_base::message_out_of_band)
  338. ? reactor::except_op : reactor::read_op,
  339. p.p, is_continuation, false, false);
  340. p.v = p.p = 0;
  341. }
  342. protected:
  343. // Open a new socket implementation.
  344. BOOST_ASIO_DECL boost::system::error_code do_open(
  345. base_implementation_type& impl, int af,
  346. int type, int protocol, boost::system::error_code& ec);
  347. // Assign a native socket to a socket implementation.
  348. BOOST_ASIO_DECL boost::system::error_code do_assign(
  349. base_implementation_type& impl, int type,
  350. const native_handle_type& native_socket, boost::system::error_code& ec);
  351. // Start the asynchronous read or write operation.
  352. BOOST_ASIO_DECL void start_op(base_implementation_type& impl, int op_type,
  353. reactor_op* op, bool is_continuation, bool is_non_blocking, bool noop);
  354. // Start the asynchronous accept operation.
  355. BOOST_ASIO_DECL void start_accept_op(base_implementation_type& impl,
  356. reactor_op* op, bool is_continuation, bool peer_is_open);
  357. // Start the asynchronous connect operation.
  358. BOOST_ASIO_DECL void start_connect_op(base_implementation_type& impl,
  359. reactor_op* op, bool is_continuation,
  360. const socket_addr_type* addr, size_t addrlen);
  361. // The selector that performs event demultiplexing for the service.
  362. reactor& reactor_;
  363. };
  364. } // namespace detail
  365. } // namespace asio
  366. } // namespace boost
  367. #include <boost/asio/detail/pop_options.hpp>
  368. #if defined(BOOST_ASIO_HEADER_ONLY)
  369. # include <boost/asio/detail/impl/reactive_socket_service_base.ipp>
  370. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  371. #endif // !defined(BOOST_ASIO_HAS_IOCP)
  372. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  373. #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP