Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

socket_acceptor_service.hpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //
  2. // socket_acceptor_service.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_SOCKET_ACCEPTOR_SERVICE_HPP
  11. #define ASIO_SOCKET_ACCEPTOR_SERVICE_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. #include "asio/basic_socket.hpp"
  17. #include "asio/detail/type_traits.hpp"
  18. #include "asio/error.hpp"
  19. #include "asio/io_context.hpp"
  20. #if defined(ASIO_WINDOWS_RUNTIME)
  21. # include "asio/detail/null_socket_service.hpp"
  22. #elif defined(ASIO_HAS_IOCP)
  23. # include "asio/detail/win_iocp_socket_service.hpp"
  24. #else
  25. # include "asio/detail/reactive_socket_service.hpp"
  26. #endif
  27. #include "asio/detail/push_options.hpp"
  28. namespace asio {
  29. /// Default service implementation for a socket acceptor.
  30. template <typename Protocol>
  31. class socket_acceptor_service
  32. #if defined(GENERATING_DOCUMENTATION)
  33. : public asio::io_context::service
  34. #else
  35. : public asio::detail::service_base<socket_acceptor_service<Protocol> >
  36. #endif
  37. {
  38. public:
  39. #if defined(GENERATING_DOCUMENTATION)
  40. /// The unique service identifier.
  41. static asio::io_context::id id;
  42. #endif
  43. /// The protocol type.
  44. typedef Protocol protocol_type;
  45. /// The endpoint type.
  46. typedef typename protocol_type::endpoint endpoint_type;
  47. private:
  48. // The type of the platform-specific implementation.
  49. #if defined(ASIO_WINDOWS_RUNTIME)
  50. typedef detail::null_socket_service<Protocol> service_impl_type;
  51. #elif defined(ASIO_HAS_IOCP)
  52. typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
  53. #else
  54. typedef detail::reactive_socket_service<Protocol> service_impl_type;
  55. #endif
  56. public:
  57. /// The native type of the socket acceptor.
  58. #if defined(GENERATING_DOCUMENTATION)
  59. typedef implementation_defined implementation_type;
  60. #else
  61. typedef typename service_impl_type::implementation_type implementation_type;
  62. #endif
  63. /// The native acceptor type.
  64. #if defined(GENERATING_DOCUMENTATION)
  65. typedef implementation_defined native_handle_type;
  66. #else
  67. typedef typename service_impl_type::native_handle_type native_handle_type;
  68. #endif
  69. /// Construct a new socket acceptor service for the specified io_context.
  70. explicit socket_acceptor_service(asio::io_context& io_context)
  71. : asio::detail::service_base<
  72. socket_acceptor_service<Protocol> >(io_context),
  73. service_impl_(io_context)
  74. {
  75. }
  76. /// Construct a new socket acceptor implementation.
  77. void construct(implementation_type& impl)
  78. {
  79. service_impl_.construct(impl);
  80. }
  81. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  82. /// Move-construct a new socket acceptor implementation.
  83. void move_construct(implementation_type& impl,
  84. implementation_type& other_impl)
  85. {
  86. service_impl_.move_construct(impl, other_impl);
  87. }
  88. /// Move-assign from another socket acceptor implementation.
  89. void move_assign(implementation_type& impl,
  90. socket_acceptor_service& other_service,
  91. implementation_type& other_impl)
  92. {
  93. service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
  94. }
  95. /// Move-construct a new socket acceptor implementation from another protocol
  96. /// type.
  97. template <typename Protocol1>
  98. void converting_move_construct(implementation_type& impl,
  99. typename socket_acceptor_service<
  100. Protocol1>::implementation_type& other_impl,
  101. typename enable_if<is_convertible<
  102. Protocol1, Protocol>::value>::type* = 0)
  103. {
  104. service_impl_.template converting_move_construct<Protocol1>(
  105. impl, other_impl);
  106. }
  107. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  108. /// Destroy a socket acceptor implementation.
  109. void destroy(implementation_type& impl)
  110. {
  111. service_impl_.destroy(impl);
  112. }
  113. /// Open a new socket acceptor implementation.
  114. asio::error_code open(implementation_type& impl,
  115. const protocol_type& protocol, asio::error_code& ec)
  116. {
  117. return service_impl_.open(impl, protocol, ec);
  118. }
  119. /// Assign an existing native acceptor to a socket acceptor.
  120. asio::error_code assign(implementation_type& impl,
  121. const protocol_type& protocol, const native_handle_type& native_acceptor,
  122. asio::error_code& ec)
  123. {
  124. return service_impl_.assign(impl, protocol, native_acceptor, ec);
  125. }
  126. /// Determine whether the acceptor is open.
  127. bool is_open(const implementation_type& impl) const
  128. {
  129. return service_impl_.is_open(impl);
  130. }
  131. /// Cancel all asynchronous operations associated with the acceptor.
  132. asio::error_code cancel(implementation_type& impl,
  133. asio::error_code& ec)
  134. {
  135. return service_impl_.cancel(impl, ec);
  136. }
  137. /// Bind the socket acceptor to the specified local endpoint.
  138. asio::error_code bind(implementation_type& impl,
  139. const endpoint_type& endpoint, asio::error_code& ec)
  140. {
  141. return service_impl_.bind(impl, endpoint, ec);
  142. }
  143. /// Place the socket acceptor into the state where it will listen for new
  144. /// connections.
  145. asio::error_code listen(implementation_type& impl, int backlog,
  146. asio::error_code& ec)
  147. {
  148. return service_impl_.listen(impl, backlog, ec);
  149. }
  150. /// Close a socket acceptor implementation.
  151. asio::error_code close(implementation_type& impl,
  152. asio::error_code& ec)
  153. {
  154. return service_impl_.close(impl, ec);
  155. }
  156. /// Get the native acceptor implementation.
  157. native_handle_type native_handle(implementation_type& impl)
  158. {
  159. return service_impl_.native_handle(impl);
  160. }
  161. /// Set a socket option.
  162. template <typename SettableSocketOption>
  163. asio::error_code set_option(implementation_type& impl,
  164. const SettableSocketOption& option, asio::error_code& ec)
  165. {
  166. return service_impl_.set_option(impl, option, ec);
  167. }
  168. /// Get a socket option.
  169. template <typename GettableSocketOption>
  170. asio::error_code get_option(const implementation_type& impl,
  171. GettableSocketOption& option, asio::error_code& ec) const
  172. {
  173. return service_impl_.get_option(impl, option, ec);
  174. }
  175. /// Perform an IO control command on the socket.
  176. template <typename IoControlCommand>
  177. asio::error_code io_control(implementation_type& impl,
  178. IoControlCommand& command, asio::error_code& ec)
  179. {
  180. return service_impl_.io_control(impl, command, ec);
  181. }
  182. /// Gets the non-blocking mode of the acceptor.
  183. bool non_blocking(const implementation_type& impl) const
  184. {
  185. return service_impl_.non_blocking(impl);
  186. }
  187. /// Sets the non-blocking mode of the acceptor.
  188. asio::error_code non_blocking(implementation_type& impl,
  189. bool mode, asio::error_code& ec)
  190. {
  191. return service_impl_.non_blocking(impl, mode, ec);
  192. }
  193. /// Gets the non-blocking mode of the native acceptor implementation.
  194. bool native_non_blocking(const implementation_type& impl) const
  195. {
  196. return service_impl_.native_non_blocking(impl);
  197. }
  198. /// Sets the non-blocking mode of the native acceptor implementation.
  199. asio::error_code native_non_blocking(implementation_type& impl,
  200. bool mode, asio::error_code& ec)
  201. {
  202. return service_impl_.native_non_blocking(impl, mode, ec);
  203. }
  204. /// Get the local endpoint.
  205. endpoint_type local_endpoint(const implementation_type& impl,
  206. asio::error_code& ec) const
  207. {
  208. return service_impl_.local_endpoint(impl, ec);
  209. }
  210. /// Wait for the acceptor to become ready to read, ready to write, or to have
  211. /// pending error conditions.
  212. asio::error_code wait(implementation_type& impl,
  213. socket_base::wait_type w, asio::error_code& ec)
  214. {
  215. return service_impl_.wait(impl, w, ec);
  216. }
  217. /// Asynchronously wait for the acceptor to become ready to read, ready to
  218. /// write, or to have pending error conditions.
  219. template <typename WaitHandler>
  220. ASIO_INITFN_RESULT_TYPE(WaitHandler,
  221. void (asio::error_code))
  222. async_wait(implementation_type& impl, socket_base::wait_type w,
  223. ASIO_MOVE_ARG(WaitHandler) handler)
  224. {
  225. async_completion<WaitHandler,
  226. void (asio::error_code)> init(handler);
  227. service_impl_.async_wait(impl, w, init.handler);
  228. return init.result.get();
  229. }
  230. /// Accept a new connection.
  231. template <typename Protocol1, typename SocketService>
  232. asio::error_code accept(implementation_type& impl,
  233. basic_socket<Protocol1, SocketService>& peer,
  234. endpoint_type* peer_endpoint, asio::error_code& ec,
  235. typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
  236. {
  237. return service_impl_.accept(impl, peer, peer_endpoint, ec);
  238. }
  239. #if defined(ASIO_HAS_MOVE)
  240. /// Accept a new connection.
  241. typename Protocol::socket accept(implementation_type& impl,
  242. io_context* peer_io_context, endpoint_type* peer_endpoint,
  243. asio::error_code& ec)
  244. {
  245. return service_impl_.accept(impl, peer_io_context, peer_endpoint, ec);
  246. }
  247. #endif // defined(ASIO_HAS_MOVE)
  248. /// Start an asynchronous accept.
  249. template <typename Protocol1, typename SocketService, typename AcceptHandler>
  250. ASIO_INITFN_RESULT_TYPE(AcceptHandler,
  251. void (asio::error_code))
  252. async_accept(implementation_type& impl,
  253. basic_socket<Protocol1, SocketService>& peer,
  254. endpoint_type* peer_endpoint,
  255. ASIO_MOVE_ARG(AcceptHandler) handler,
  256. typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
  257. {
  258. async_completion<AcceptHandler,
  259. void (asio::error_code)> init(handler);
  260. service_impl_.async_accept(impl, peer, peer_endpoint, init.handler);
  261. return init.result.get();
  262. }
  263. #if defined(ASIO_HAS_MOVE)
  264. /// Start an asynchronous accept.
  265. template <typename MoveAcceptHandler>
  266. ASIO_INITFN_RESULT_TYPE(MoveAcceptHandler,
  267. void (asio::error_code, typename Protocol::socket))
  268. async_accept(implementation_type& impl,
  269. asio::io_context* peer_io_context, endpoint_type* peer_endpoint,
  270. ASIO_MOVE_ARG(MoveAcceptHandler) handler)
  271. {
  272. async_completion<MoveAcceptHandler,
  273. void (asio::error_code,
  274. typename Protocol::socket)> init(handler);
  275. service_impl_.async_accept(impl,
  276. peer_io_context, peer_endpoint, init.handler);
  277. return init.result.get();
  278. }
  279. #endif // defined(ASIO_HAS_MOVE)
  280. private:
  281. // Destroy all user-defined handler objects owned by the service.
  282. void shutdown()
  283. {
  284. service_impl_.shutdown();
  285. }
  286. // The platform-specific implementation.
  287. service_impl_type service_impl_;
  288. };
  289. } // namespace asio
  290. #include "asio/detail/pop_options.hpp"
  291. #endif // ASIO_SOCKET_ACCEPTOR_SERVICE_HPP