reactive_socket_service_base.ipp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //
  2. // detail/reactive_socket_service_base.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_IMPL_REACTIVE_SOCKET_SERVICE_BASE_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_REACTIVE_SOCKET_SERVICE_BASE_IPP
  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. && !defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  19. #include <boost/asio/detail/reactive_socket_service_base.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. reactive_socket_service_base::reactive_socket_service_base(
  25. execution_context& context)
  26. : reactor_(use_service<reactor>(context))
  27. {
  28. reactor_.init_task();
  29. }
  30. void reactive_socket_service_base::base_shutdown()
  31. {
  32. }
  33. void reactive_socket_service_base::construct(
  34. reactive_socket_service_base::base_implementation_type& impl)
  35. {
  36. impl.socket_ = invalid_socket;
  37. impl.state_ = 0;
  38. impl.reactor_data_ = reactor::per_descriptor_data();
  39. }
  40. void reactive_socket_service_base::base_move_construct(
  41. reactive_socket_service_base::base_implementation_type& impl,
  42. reactive_socket_service_base::base_implementation_type& other_impl)
  43. noexcept
  44. {
  45. impl.socket_ = other_impl.socket_;
  46. other_impl.socket_ = invalid_socket;
  47. impl.state_ = other_impl.state_;
  48. other_impl.state_ = 0;
  49. reactor_.move_descriptor(impl.socket_,
  50. impl.reactor_data_, other_impl.reactor_data_);
  51. }
  52. void reactive_socket_service_base::base_move_assign(
  53. reactive_socket_service_base::base_implementation_type& impl,
  54. reactive_socket_service_base& other_service,
  55. reactive_socket_service_base::base_implementation_type& other_impl)
  56. {
  57. destroy(impl);
  58. impl.socket_ = other_impl.socket_;
  59. other_impl.socket_ = invalid_socket;
  60. impl.state_ = other_impl.state_;
  61. other_impl.state_ = 0;
  62. other_service.reactor_.move_descriptor(impl.socket_,
  63. impl.reactor_data_, other_impl.reactor_data_);
  64. }
  65. void reactive_socket_service_base::destroy(
  66. reactive_socket_service_base::base_implementation_type& impl)
  67. {
  68. if (impl.socket_ != invalid_socket)
  69. {
  70. BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
  71. "socket", &impl, impl.socket_, "close"));
  72. reactor_.deregister_descriptor(impl.socket_, impl.reactor_data_,
  73. (impl.state_ & socket_ops::possible_dup) == 0);
  74. boost::system::error_code ignored_ec;
  75. socket_ops::close(impl.socket_, impl.state_, true, ignored_ec);
  76. reactor_.cleanup_descriptor_data(impl.reactor_data_);
  77. }
  78. }
  79. boost::system::error_code reactive_socket_service_base::close(
  80. reactive_socket_service_base::base_implementation_type& impl,
  81. boost::system::error_code& ec)
  82. {
  83. if (is_open(impl))
  84. {
  85. BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
  86. "socket", &impl, impl.socket_, "close"));
  87. reactor_.deregister_descriptor(impl.socket_, impl.reactor_data_,
  88. (impl.state_ & socket_ops::possible_dup) == 0);
  89. socket_ops::close(impl.socket_, impl.state_, false, ec);
  90. reactor_.cleanup_descriptor_data(impl.reactor_data_);
  91. }
  92. else
  93. {
  94. ec = boost::system::error_code();
  95. }
  96. // The descriptor is closed by the OS even if close() returns an error.
  97. //
  98. // (Actually, POSIX says the state of the descriptor is unspecified. On
  99. // Linux the descriptor is apparently closed anyway; e.g. see
  100. // http://lkml.org/lkml/2005/9/10/129
  101. // We'll just have to assume that other OSes follow the same behaviour. The
  102. // known exception is when Windows's closesocket() function fails with
  103. // WSAEWOULDBLOCK, but this case is handled inside socket_ops::close().
  104. construct(impl);
  105. return ec;
  106. }
  107. socket_type reactive_socket_service_base::release(
  108. reactive_socket_service_base::base_implementation_type& impl,
  109. boost::system::error_code& ec)
  110. {
  111. if (!is_open(impl))
  112. {
  113. ec = boost::asio::error::bad_descriptor;
  114. return invalid_socket;
  115. }
  116. BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
  117. "socket", &impl, impl.socket_, "release"));
  118. reactor_.deregister_descriptor(impl.socket_, impl.reactor_data_, false);
  119. reactor_.cleanup_descriptor_data(impl.reactor_data_);
  120. socket_type sock = impl.socket_;
  121. construct(impl);
  122. ec = boost::system::error_code();
  123. return sock;
  124. }
  125. boost::system::error_code reactive_socket_service_base::cancel(
  126. reactive_socket_service_base::base_implementation_type& impl,
  127. boost::system::error_code& ec)
  128. {
  129. if (!is_open(impl))
  130. {
  131. ec = boost::asio::error::bad_descriptor;
  132. return ec;
  133. }
  134. BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
  135. "socket", &impl, impl.socket_, "cancel"));
  136. reactor_.cancel_ops(impl.socket_, impl.reactor_data_);
  137. ec = boost::system::error_code();
  138. return ec;
  139. }
  140. boost::system::error_code reactive_socket_service_base::do_open(
  141. reactive_socket_service_base::base_implementation_type& impl,
  142. int af, int type, int protocol, boost::system::error_code& ec)
  143. {
  144. if (is_open(impl))
  145. {
  146. ec = boost::asio::error::already_open;
  147. return ec;
  148. }
  149. socket_holder sock(socket_ops::socket(af, type, protocol, ec));
  150. if (sock.get() == invalid_socket)
  151. return ec;
  152. if (int err = reactor_.register_descriptor(sock.get(), impl.reactor_data_))
  153. {
  154. ec = boost::system::error_code(err,
  155. boost::asio::error::get_system_category());
  156. return ec;
  157. }
  158. impl.socket_ = sock.release();
  159. switch (type)
  160. {
  161. case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
  162. case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
  163. default: impl.state_ = 0; break;
  164. }
  165. ec = boost::system::error_code();
  166. return ec;
  167. }
  168. boost::system::error_code reactive_socket_service_base::do_assign(
  169. reactive_socket_service_base::base_implementation_type& impl, int type,
  170. const reactive_socket_service_base::native_handle_type& native_socket,
  171. boost::system::error_code& ec)
  172. {
  173. if (is_open(impl))
  174. {
  175. ec = boost::asio::error::already_open;
  176. return ec;
  177. }
  178. if (int err = reactor_.register_descriptor(
  179. native_socket, impl.reactor_data_))
  180. {
  181. ec = boost::system::error_code(err,
  182. boost::asio::error::get_system_category());
  183. return ec;
  184. }
  185. impl.socket_ = native_socket;
  186. switch (type)
  187. {
  188. case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
  189. case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
  190. default: impl.state_ = 0; break;
  191. }
  192. impl.state_ |= socket_ops::possible_dup;
  193. ec = boost::system::error_code();
  194. return ec;
  195. }
  196. void reactive_socket_service_base::do_start_op(
  197. reactive_socket_service_base::base_implementation_type& impl,
  198. int op_type, reactor_op* op, bool is_continuation,
  199. bool allow_speculative, bool noop, bool needs_non_blocking,
  200. void (*on_immediate)(operation* op, bool, const void*),
  201. const void* immediate_arg)
  202. {
  203. if (!noop)
  204. {
  205. if ((impl.state_ & socket_ops::non_blocking)
  206. || !needs_non_blocking
  207. || socket_ops::set_internal_non_blocking(
  208. impl.socket_, impl.state_, true, op->ec_))
  209. {
  210. reactor_.start_op(op_type, impl.socket_, impl.reactor_data_, op,
  211. is_continuation, allow_speculative, on_immediate, immediate_arg);
  212. return;
  213. }
  214. }
  215. on_immediate(op, is_continuation, immediate_arg);
  216. }
  217. void reactive_socket_service_base::do_start_accept_op(
  218. reactive_socket_service_base::base_implementation_type& impl,
  219. reactor_op* op, bool is_continuation, bool peer_is_open,
  220. void (*on_immediate)(operation* op, bool, const void*),
  221. const void* immediate_arg)
  222. {
  223. if (!peer_is_open)
  224. {
  225. do_start_op(impl, reactor::read_op, op, is_continuation,
  226. true, false, true, on_immediate, immediate_arg);
  227. }
  228. else
  229. {
  230. op->ec_ = boost::asio::error::already_open;
  231. on_immediate(op, is_continuation, immediate_arg);
  232. }
  233. }
  234. void reactive_socket_service_base::do_start_connect_op(
  235. reactive_socket_service_base::base_implementation_type& impl,
  236. reactor_op* op, bool is_continuation, const void* addr, size_t addrlen,
  237. void (*on_immediate)(operation* op, bool, const void*),
  238. const void* immediate_arg)
  239. {
  240. if ((impl.state_ & socket_ops::non_blocking)
  241. || socket_ops::set_internal_non_blocking(
  242. impl.socket_, impl.state_, true, op->ec_))
  243. {
  244. if (socket_ops::connect(impl.socket_, addr, addrlen, op->ec_) != 0)
  245. {
  246. if (op->ec_ == boost::asio::error::in_progress
  247. || op->ec_ == boost::asio::error::would_block)
  248. {
  249. op->ec_ = boost::system::error_code();
  250. reactor_.start_op(reactor::connect_op, impl.socket_, impl.reactor_data_,
  251. op, is_continuation, false, on_immediate, immediate_arg);
  252. return;
  253. }
  254. }
  255. }
  256. on_immediate(op, is_continuation, immediate_arg);
  257. }
  258. } // namespace detail
  259. } // namespace asio
  260. } // namespace boost
  261. #include <boost/asio/detail/pop_options.hpp>
  262. #endif // !defined(BOOST_ASIO_HAS_IOCP)
  263. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  264. // && !defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  265. #endif // BOOST_ASIO_DETAIL_IMPL_REACTIVE_SOCKET_SERVICE_BASE_IPP