stream_socket_service.hpp 8.8 KB

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