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.

389 lines
12KB

  1. //
  2. // seq_packet_socket_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_SEQ_PACKET_SOCKET_SERVICE_HPP
  11. #define ASIO_SEQ_PACKET_SOCKET_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 <cstddef>
  17. #include "asio/async_result.hpp"
  18. #include "asio/detail/type_traits.hpp"
  19. #include "asio/error.hpp"
  20. #include "asio/io_context.hpp"
  21. #if defined(ASIO_WINDOWS_RUNTIME)
  22. # include "asio/detail/null_socket_service.hpp"
  23. #elif defined(ASIO_HAS_IOCP)
  24. # include "asio/detail/win_iocp_socket_service.hpp"
  25. #else
  26. # include "asio/detail/reactive_socket_service.hpp"
  27. #endif
  28. #include "asio/detail/push_options.hpp"
  29. namespace asio {
  30. /// Default service implementation for a sequenced packet socket.
  31. template <typename Protocol>
  32. class seq_packet_socket_service
  33. #if defined(GENERATING_DOCUMENTATION)
  34. : public asio::io_context::service
  35. #else
  36. : public asio::detail::service_base<
  37. seq_packet_socket_service<Protocol> >
  38. #endif
  39. {
  40. public:
  41. #if defined(GENERATING_DOCUMENTATION)
  42. /// The unique service identifier.
  43. static asio::io_context::id id;
  44. #endif
  45. /// The protocol type.
  46. typedef Protocol protocol_type;
  47. /// The endpoint type.
  48. typedef typename Protocol::endpoint endpoint_type;
  49. private:
  50. // The type of the platform-specific implementation.
  51. #if defined(ASIO_WINDOWS_RUNTIME)
  52. typedef detail::null_socket_service<Protocol> service_impl_type;
  53. #elif defined(ASIO_HAS_IOCP)
  54. typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
  55. #else
  56. typedef detail::reactive_socket_service<Protocol> service_impl_type;
  57. #endif
  58. public:
  59. /// The type of a sequenced packet socket implementation.
  60. #if defined(GENERATING_DOCUMENTATION)
  61. typedef implementation_defined implementation_type;
  62. #else
  63. typedef typename service_impl_type::implementation_type implementation_type;
  64. #endif
  65. /// The native socket type.
  66. #if defined(GENERATING_DOCUMENTATION)
  67. typedef implementation_defined native_handle_type;
  68. #else
  69. typedef typename service_impl_type::native_handle_type native_handle_type;
  70. #endif
  71. /// Construct a new sequenced packet socket service for the specified
  72. /// io_context.
  73. explicit seq_packet_socket_service(asio::io_context& io_context)
  74. : asio::detail::service_base<
  75. seq_packet_socket_service<Protocol> >(io_context),
  76. service_impl_(io_context)
  77. {
  78. }
  79. /// Construct a new sequenced packet socket implementation.
  80. void construct(implementation_type& impl)
  81. {
  82. service_impl_.construct(impl);
  83. }
  84. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  85. /// Move-construct a new sequenced packet socket implementation.
  86. void move_construct(implementation_type& impl,
  87. implementation_type& other_impl)
  88. {
  89. service_impl_.move_construct(impl, other_impl);
  90. }
  91. /// Move-assign from another sequenced packet socket implementation.
  92. void move_assign(implementation_type& impl,
  93. seq_packet_socket_service& other_service,
  94. implementation_type& other_impl)
  95. {
  96. service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
  97. }
  98. /// Move-construct a new sequenced packet socket implementation from another
  99. /// protocol type.
  100. template <typename Protocol1>
  101. void converting_move_construct(implementation_type& impl,
  102. typename seq_packet_socket_service<
  103. Protocol1>::implementation_type& other_impl,
  104. typename enable_if<is_convertible<
  105. Protocol1, Protocol>::value>::type* = 0)
  106. {
  107. service_impl_.template converting_move_construct<Protocol1>(
  108. impl, other_impl);
  109. }
  110. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  111. /// Destroy a sequenced packet socket implementation.
  112. void destroy(implementation_type& impl)
  113. {
  114. service_impl_.destroy(impl);
  115. }
  116. /// Open a sequenced packet socket.
  117. asio::error_code open(implementation_type& impl,
  118. const protocol_type& protocol, asio::error_code& ec)
  119. {
  120. if (protocol.type() == ASIO_OS_DEF(SOCK_SEQPACKET))
  121. service_impl_.open(impl, protocol, ec);
  122. else
  123. ec = asio::error::invalid_argument;
  124. return ec;
  125. }
  126. /// Assign an existing native socket to a sequenced packet socket.
  127. asio::error_code assign(implementation_type& impl,
  128. const protocol_type& protocol, const native_handle_type& native_socket,
  129. asio::error_code& ec)
  130. {
  131. return service_impl_.assign(impl, protocol, native_socket, ec);
  132. }
  133. /// Determine whether the socket is open.
  134. bool is_open(const implementation_type& impl) const
  135. {
  136. return service_impl_.is_open(impl);
  137. }
  138. /// Close a sequenced packet socket implementation.
  139. asio::error_code close(implementation_type& impl,
  140. asio::error_code& ec)
  141. {
  142. return service_impl_.close(impl, ec);
  143. }
  144. /// Get the native socket implementation.
  145. native_handle_type native_handle(implementation_type& impl)
  146. {
  147. return service_impl_.native_handle(impl);
  148. }
  149. /// Cancel all asynchronous operations associated with the socket.
  150. asio::error_code cancel(implementation_type& impl,
  151. asio::error_code& ec)
  152. {
  153. return service_impl_.cancel(impl, ec);
  154. }
  155. /// Determine whether the socket is at the out-of-band data mark.
  156. bool at_mark(const implementation_type& impl,
  157. asio::error_code& ec) const
  158. {
  159. return service_impl_.at_mark(impl, ec);
  160. }
  161. /// Determine the number of bytes available for reading.
  162. std::size_t available(const implementation_type& impl,
  163. asio::error_code& ec) const
  164. {
  165. return service_impl_.available(impl, ec);
  166. }
  167. /// Bind the sequenced packet socket to the specified local endpoint.
  168. asio::error_code bind(implementation_type& impl,
  169. const endpoint_type& endpoint, asio::error_code& ec)
  170. {
  171. return service_impl_.bind(impl, endpoint, ec);
  172. }
  173. /// Connect the sequenced packet socket to the specified endpoint.
  174. asio::error_code connect(implementation_type& impl,
  175. const endpoint_type& peer_endpoint, asio::error_code& ec)
  176. {
  177. return service_impl_.connect(impl, peer_endpoint, ec);
  178. }
  179. /// Start an asynchronous connect.
  180. template <typename ConnectHandler>
  181. ASIO_INITFN_RESULT_TYPE(ConnectHandler,
  182. void (asio::error_code))
  183. async_connect(implementation_type& impl,
  184. const endpoint_type& peer_endpoint,
  185. ASIO_MOVE_ARG(ConnectHandler) handler)
  186. {
  187. async_completion<ConnectHandler,
  188. void (asio::error_code)> init(handler);
  189. service_impl_.async_connect(impl, peer_endpoint, init.handler);
  190. return init.result.get();
  191. }
  192. /// Set a socket option.
  193. template <typename SettableSocketOption>
  194. asio::error_code set_option(implementation_type& impl,
  195. const SettableSocketOption& option, asio::error_code& ec)
  196. {
  197. return service_impl_.set_option(impl, option, ec);
  198. }
  199. /// Get a socket option.
  200. template <typename GettableSocketOption>
  201. asio::error_code get_option(const implementation_type& impl,
  202. GettableSocketOption& option, asio::error_code& ec) const
  203. {
  204. return service_impl_.get_option(impl, option, ec);
  205. }
  206. /// Perform an IO control command on the socket.
  207. template <typename IoControlCommand>
  208. asio::error_code io_control(implementation_type& impl,
  209. IoControlCommand& command, asio::error_code& ec)
  210. {
  211. return service_impl_.io_control(impl, command, ec);
  212. }
  213. /// Gets the non-blocking mode of the socket.
  214. bool non_blocking(const implementation_type& impl) const
  215. {
  216. return service_impl_.non_blocking(impl);
  217. }
  218. /// Sets the non-blocking mode of the socket.
  219. asio::error_code non_blocking(implementation_type& impl,
  220. bool mode, asio::error_code& ec)
  221. {
  222. return service_impl_.non_blocking(impl, mode, ec);
  223. }
  224. /// Gets the non-blocking mode of the native socket implementation.
  225. bool native_non_blocking(const implementation_type& impl) const
  226. {
  227. return service_impl_.native_non_blocking(impl);
  228. }
  229. /// Sets the non-blocking mode of the native socket implementation.
  230. asio::error_code native_non_blocking(implementation_type& impl,
  231. bool mode, asio::error_code& ec)
  232. {
  233. return service_impl_.native_non_blocking(impl, mode, ec);
  234. }
  235. /// Get the local endpoint.
  236. endpoint_type local_endpoint(const implementation_type& impl,
  237. asio::error_code& ec) const
  238. {
  239. return service_impl_.local_endpoint(impl, ec);
  240. }
  241. /// Get the remote endpoint.
  242. endpoint_type remote_endpoint(const implementation_type& impl,
  243. asio::error_code& ec) const
  244. {
  245. return service_impl_.remote_endpoint(impl, ec);
  246. }
  247. /// Disable sends or receives on the socket.
  248. asio::error_code shutdown(implementation_type& impl,
  249. socket_base::shutdown_type what, asio::error_code& ec)
  250. {
  251. return service_impl_.shutdown(impl, what, ec);
  252. }
  253. /// Wait for the socket to become ready to read, ready to write, or to have
  254. /// pending error conditions.
  255. asio::error_code wait(implementation_type& impl,
  256. socket_base::wait_type w, asio::error_code& ec)
  257. {
  258. return service_impl_.wait(impl, w, ec);
  259. }
  260. /// Asynchronously wait for the socket to become ready to read, ready to
  261. /// write, or to have pending error conditions.
  262. template <typename WaitHandler>
  263. ASIO_INITFN_RESULT_TYPE(WaitHandler,
  264. void (asio::error_code))
  265. async_wait(implementation_type& impl, socket_base::wait_type w,
  266. ASIO_MOVE_ARG(WaitHandler) handler)
  267. {
  268. async_completion<WaitHandler,
  269. void (asio::error_code)> init(handler);
  270. service_impl_.async_wait(impl, w, init.handler);
  271. return init.result.get();
  272. }
  273. /// Send the given data to the peer.
  274. template <typename ConstBufferSequence>
  275. std::size_t send(implementation_type& impl,
  276. const ConstBufferSequence& buffers,
  277. socket_base::message_flags flags, asio::error_code& ec)
  278. {
  279. return service_impl_.send(impl, buffers, flags, ec);
  280. }
  281. /// Start an asynchronous send.
  282. template <typename ConstBufferSequence, typename WriteHandler>
  283. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  284. void (asio::error_code, std::size_t))
  285. async_send(implementation_type& impl,
  286. const ConstBufferSequence& buffers,
  287. socket_base::message_flags flags,
  288. ASIO_MOVE_ARG(WriteHandler) handler)
  289. {
  290. async_completion<WriteHandler,
  291. void (asio::error_code, std::size_t)> init(handler);
  292. service_impl_.async_send(impl, buffers, flags, init.handler);
  293. return init.result.get();
  294. }
  295. /// Receive some data from the peer.
  296. template <typename MutableBufferSequence>
  297. std::size_t receive(implementation_type& impl,
  298. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  299. socket_base::message_flags& out_flags, asio::error_code& ec)
  300. {
  301. return service_impl_.receive_with_flags(impl,
  302. buffers, in_flags, out_flags, ec);
  303. }
  304. /// Start an asynchronous receive.
  305. template <typename MutableBufferSequence, typename ReadHandler>
  306. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  307. void (asio::error_code, std::size_t))
  308. async_receive(implementation_type& impl,
  309. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  310. socket_base::message_flags& out_flags,
  311. ASIO_MOVE_ARG(ReadHandler) handler)
  312. {
  313. async_completion<ReadHandler,
  314. void (asio::error_code, std::size_t)> init(handler);
  315. service_impl_.async_receive_with_flags(impl,
  316. buffers, in_flags, out_flags, init.handler);
  317. return init.result.get();
  318. }
  319. private:
  320. // Destroy all user-defined handler objects owned by the service.
  321. void shutdown()
  322. {
  323. service_impl_.shutdown();
  324. }
  325. // The platform-specific implementation.
  326. service_impl_type service_impl_;
  327. };
  328. } // namespace asio
  329. #include "asio/detail/pop_options.hpp"
  330. #endif // ASIO_SEQ_PACKET_SOCKET_SERVICE_HPP