stream_socket_service.hpp 12 KB

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