datagram_socket_service.hpp 15 KB

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