stream_socket_service.hpp 11 KB

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