seq_packet_socket_service.hpp 13 KB

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