reactive_socket_service_base.hpp 15 KB

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