stream_socket_service.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // stream_socket_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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/error.hpp"
  18. #include "asio/io_service.hpp"
  19. #if defined(ASIO_HAS_IOCP)
  20. # include "asio/detail/win_iocp_socket_service.hpp"
  21. #else
  22. # include "asio/detail/reactive_socket_service.hpp"
  23. #endif
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. /// Default service implementation for a stream socket.
  27. template <typename Protocol>
  28. class stream_socket_service
  29. #if defined(GENERATING_DOCUMENTATION)
  30. : public asio::io_service::service
  31. #else
  32. : public asio::detail::service_base<stream_socket_service<Protocol> >
  33. #endif
  34. {
  35. public:
  36. #if defined(GENERATING_DOCUMENTATION)
  37. /// The unique service identifier.
  38. static asio::io_service::id id;
  39. #endif
  40. /// The protocol type.
  41. typedef Protocol protocol_type;
  42. /// The endpoint type.
  43. typedef typename Protocol::endpoint endpoint_type;
  44. private:
  45. // The type of the platform-specific implementation.
  46. #if defined(ASIO_HAS_IOCP)
  47. typedef detail::win_iocp_socket_service<Protocol> service_impl_type;
  48. #else
  49. typedef detail::reactive_socket_service<Protocol> service_impl_type;
  50. #endif
  51. public:
  52. /// The type of a stream socket implementation.
  53. #if defined(GENERATING_DOCUMENTATION)
  54. typedef implementation_defined implementation_type;
  55. #else
  56. typedef typename service_impl_type::implementation_type implementation_type;
  57. #endif
  58. /// The native socket type.
  59. #if defined(GENERATING_DOCUMENTATION)
  60. typedef implementation_defined native_type;
  61. #else
  62. typedef typename service_impl_type::native_type native_type;
  63. #endif
  64. /// Construct a new stream socket service for the specified io_service.
  65. explicit stream_socket_service(asio::io_service& io_service)
  66. : asio::detail::service_base<
  67. stream_socket_service<Protocol> >(io_service),
  68. service_impl_(io_service)
  69. {
  70. }
  71. /// Destroy all user-defined handler objects owned by the service.
  72. void shutdown_service()
  73. {
  74. service_impl_.shutdown_service();
  75. }
  76. /// Construct a new stream socket implementation.
  77. void construct(implementation_type& impl)
  78. {
  79. service_impl_.construct(impl);
  80. }
  81. /// Destroy a stream socket implementation.
  82. void destroy(implementation_type& impl)
  83. {
  84. service_impl_.destroy(impl);
  85. }
  86. /// Open a stream socket.
  87. asio::error_code open(implementation_type& impl,
  88. const protocol_type& protocol, asio::error_code& ec)
  89. {
  90. if (protocol.type() == SOCK_STREAM)
  91. service_impl_.open(impl, protocol, ec);
  92. else
  93. ec = asio::error::invalid_argument;
  94. return ec;
  95. }
  96. /// Assign an existing native socket to a stream socket.
  97. asio::error_code assign(implementation_type& impl,
  98. const protocol_type& protocol, const native_type& native_socket,
  99. asio::error_code& ec)
  100. {
  101. return service_impl_.assign(impl, protocol, native_socket, ec);
  102. }
  103. /// Determine whether the socket is open.
  104. bool is_open(const implementation_type& impl) const
  105. {
  106. return service_impl_.is_open(impl);
  107. }
  108. /// Close a stream socket implementation.
  109. asio::error_code close(implementation_type& impl,
  110. asio::error_code& ec)
  111. {
  112. return service_impl_.close(impl, ec);
  113. }
  114. /// Get the native socket implementation.
  115. native_type native(implementation_type& impl)
  116. {
  117. return service_impl_.native(impl);
  118. }
  119. /// Cancel all asynchronous operations associated with the socket.
  120. asio::error_code cancel(implementation_type& impl,
  121. asio::error_code& ec)
  122. {
  123. return service_impl_.cancel(impl, ec);
  124. }
  125. /// Determine whether the socket is at the out-of-band data mark.
  126. bool at_mark(const implementation_type& impl,
  127. asio::error_code& ec) const
  128. {
  129. return service_impl_.at_mark(impl, ec);
  130. }
  131. /// Determine the number of bytes available for reading.
  132. std::size_t available(const implementation_type& impl,
  133. asio::error_code& ec) const
  134. {
  135. return service_impl_.available(impl, ec);
  136. }
  137. /// Bind the stream socket to the specified local endpoint.
  138. asio::error_code bind(implementation_type& impl,
  139. const endpoint_type& endpoint, asio::error_code& ec)
  140. {
  141. return service_impl_.bind(impl, endpoint, ec);
  142. }
  143. /// Connect the stream socket to the specified endpoint.
  144. asio::error_code connect(implementation_type& impl,
  145. const endpoint_type& peer_endpoint, asio::error_code& ec)
  146. {
  147. return service_impl_.connect(impl, peer_endpoint, ec);
  148. }
  149. /// Start an asynchronous connect.
  150. template <typename ConnectHandler>
  151. void async_connect(implementation_type& impl,
  152. const endpoint_type& peer_endpoint, ConnectHandler handler)
  153. {
  154. service_impl_.async_connect(impl, peer_endpoint, handler);
  155. }
  156. /// Set a socket option.
  157. template <typename SettableSocketOption>
  158. asio::error_code set_option(implementation_type& impl,
  159. const SettableSocketOption& option, asio::error_code& ec)
  160. {
  161. return service_impl_.set_option(impl, option, ec);
  162. }
  163. /// Get a socket option.
  164. template <typename GettableSocketOption>
  165. asio::error_code get_option(const implementation_type& impl,
  166. GettableSocketOption& option, asio::error_code& ec) const
  167. {
  168. return service_impl_.get_option(impl, option, ec);
  169. }
  170. /// Perform an IO control command on the socket.
  171. template <typename IoControlCommand>
  172. asio::error_code io_control(implementation_type& impl,
  173. IoControlCommand& command, asio::error_code& ec)
  174. {
  175. return service_impl_.io_control(impl, command, ec);
  176. }
  177. /// Get the local endpoint.
  178. endpoint_type local_endpoint(const implementation_type& impl,
  179. asio::error_code& ec) const
  180. {
  181. return service_impl_.local_endpoint(impl, ec);
  182. }
  183. /// Get the remote endpoint.
  184. endpoint_type remote_endpoint(const implementation_type& impl,
  185. asio::error_code& ec) const
  186. {
  187. return service_impl_.remote_endpoint(impl, ec);
  188. }
  189. /// Disable sends or receives on the socket.
  190. asio::error_code shutdown(implementation_type& impl,
  191. socket_base::shutdown_type what, asio::error_code& ec)
  192. {
  193. return service_impl_.shutdown(impl, what, ec);
  194. }
  195. /// Send the given data to the peer.
  196. template <typename ConstBufferSequence>
  197. std::size_t send(implementation_type& impl,
  198. const ConstBufferSequence& buffers,
  199. socket_base::message_flags flags, asio::error_code& ec)
  200. {
  201. return service_impl_.send(impl, buffers, flags, ec);
  202. }
  203. /// Start an asynchronous send.
  204. template <typename ConstBufferSequence, typename WriteHandler>
  205. void async_send(implementation_type& impl,
  206. const ConstBufferSequence& buffers,
  207. socket_base::message_flags flags, WriteHandler handler)
  208. {
  209. service_impl_.async_send(impl, buffers, flags, handler);
  210. }
  211. /// Receive some data from the peer.
  212. template <typename MutableBufferSequence>
  213. std::size_t receive(implementation_type& impl,
  214. const MutableBufferSequence& buffers,
  215. socket_base::message_flags flags, asio::error_code& ec)
  216. {
  217. return service_impl_.receive(impl, buffers, flags, ec);
  218. }
  219. /// Start an asynchronous receive.
  220. template <typename MutableBufferSequence, typename ReadHandler>
  221. void async_receive(implementation_type& impl,
  222. const MutableBufferSequence& buffers,
  223. socket_base::message_flags flags, ReadHandler handler)
  224. {
  225. service_impl_.async_receive(impl, buffers, flags, handler);
  226. }
  227. private:
  228. // The platform-specific implementation.
  229. service_impl_type service_impl_;
  230. };
  231. } // namespace asio
  232. #include "asio/detail/pop_options.hpp"
  233. #endif // ASIO_STREAM_SOCKET_SERVICE_HPP