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.

raw_socket_service.hpp 13KB

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