PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/boost/cgi/basic_request_acceptor.hpp

http://github.com/darrengarvey/cgi
C++ Header | 297 lines | 232 code | 39 blank | 26 comment | 2 complexity | 09a25d85d99b75e38780190a88cc7cb9 MD5 | raw file
  1. // -- basic_request_acceptor.hpp --
  2. //
  3. // Copyright (c) Darren Garvey 2007.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. ////////////////////////////////////////////////////////////////
  9. #ifndef CGI_BASIC_REQUEST_ACCEPTOR_HPP_INCLUDED__
  10. #define CGI_BASIC_REQUEST_ACCEPTOR_HPP_INCLUDED__
  11. #include <boost/noncopyable.hpp>
  12. #include <boost/asio/ip/tcp.hpp>
  13. #include <boost/system/error_code.hpp>
  14. #include <boost/asio/basic_io_object.hpp>
  15. #include <boost/asio/ip/basic_endpoint.hpp>
  16. #include <boost/asio/ip/tcp.hpp>
  17. ///////////////////////////////////////////////////////////
  18. #include "boost/cgi/detail/throw_error.hpp"
  19. #include "boost/cgi/common/basic_protocol_service.hpp"
  20. BOOST_CGI_NAMESPACE_BEGIN
  21. namespace common {
  22. /// The interface class for any *BOOST_CGI_NAMESPACE::acceptor.
  23. template<typename Protocol>
  24. class basic_request_acceptor
  25. //: public boost::asio::basic_io_object<RequestAcceptorService>
  26. : public boost::asio::basic_io_object<
  27. typename protocol_traits<Protocol>::acceptor_service
  28. >
  29. //, private boost::noncopyable
  30. {
  31. public:
  32. // typedef impl_type;
  33. typedef Protocol protocol_type;
  34. typedef protocol_traits<protocol_type> traits;
  35. typedef typename traits::acceptor_service service_type;
  36. typedef typename traits::endpoint_type endpoint_type;
  37. typedef typename traits::native_type native_type;
  38. typedef typename traits::protocol_service_type protocol_service_type;
  39. typedef typename
  40. service_type::accept_handler_type accept_handler_type;
  41. template<typename IoServiceProvider>
  42. explicit basic_request_acceptor(
  43. common::basic_protocol_service<protocol_type, IoServiceProvider>& ps)
  44. : boost::asio::basic_io_object<service_type>(ps.get_io_service())
  45. {
  46. this->service.set_protocol_service(this->implementation, ps);
  47. boost::system::error_code ec;
  48. if (this->service.default_init(this->implementation, ec)) {
  49. detail::throw_error(ec);
  50. }
  51. }
  52. template<typename IoServiceProvider, typename InternetProtocol>
  53. explicit basic_request_acceptor(
  54. common::basic_protocol_service<protocol_type, IoServiceProvider>& ps,
  55. const boost::asio::ip::basic_endpoint<InternetProtocol>& endpoint,
  56. bool reuse_addr = true)
  57. : boost::asio::basic_io_object<service_type>(ps.get_io_service())
  58. {
  59. this->service.set_protocol_service(this->implementation, ps);
  60. boost::system::error_code ec;
  61. open(endpoint.protocol(), ec);
  62. detail::throw_error(ec);
  63. if (reuse_addr)
  64. {
  65. set_option(boost::asio::socket_base::reuse_address(true), ec);
  66. detail::throw_error(ec);
  67. }
  68. bind(endpoint, ec);
  69. detail::throw_error(ec);
  70. listen(boost::asio::socket_base::max_connections, ec);
  71. detail::throw_error(ec);
  72. }
  73. template<typename IoServiceProvider, typename InternetProtocol>
  74. explicit basic_request_acceptor(
  75. common::basic_protocol_service<protocol_type, IoServiceProvider>& ps,
  76. const InternetProtocol& ip,
  77. const native_type& native_acceptor)
  78. : boost::asio::basic_io_object<service_type>(ps.get_io_service())
  79. {
  80. this->service.set_protocol_service(this->implementation, ps);
  81. boost::system::error_code ec;
  82. this->service.assign(this->implementation, ip, native_acceptor, ec);
  83. detail::throw_error(ec);
  84. }
  85. ~basic_request_acceptor()
  86. {
  87. }
  88. protocol_service_type& protocol_service() const
  89. {
  90. return this->service.protocol_service(this->implementation);
  91. }
  92. /// Check if the acceptor is open
  93. bool is_open()
  94. {
  95. return this->service.is_open(this->implementation);
  96. }
  97. /// Open the acceptor
  98. template<typename InternetProtocol>
  99. void open(const InternetProtocol& protocol)
  100. {
  101. boost::system::error_code ec;
  102. this->service.open(this->implementation, protocol, ec);
  103. detail::throw_error(ec);
  104. }
  105. /// Open the acceptor
  106. template<typename InternetProtocol>
  107. boost::system::error_code
  108. open(const InternetProtocol& protocol, boost::system::error_code& ec)
  109. {
  110. return this->service.open(this->implementation, protocol, ec);
  111. }
  112. /// Set the acceptor to listen
  113. void listen(int backlog = boost::asio::socket_base::max_connections)
  114. {
  115. boost::system::error_code ec;
  116. this->service.listen(this->implementation, backlog, ec);
  117. detail::throw_error(ec);
  118. }
  119. /// Set the acceptor to listen
  120. boost::system::error_code
  121. listen(int backlog, boost::system::error_code& ec)
  122. {
  123. return this->service.listen(this->implementation, backlog, ec);
  124. }
  125. template<typename Endpoint>
  126. void bind(Endpoint& ep)
  127. {
  128. boost::system::error_code ec;
  129. this->service.bind(this->implementation, ep, ec);
  130. detail::throw_error(ec);
  131. this->implementation.endpoint_ = ep;
  132. }
  133. template<typename Endpoint>
  134. boost::system::error_code
  135. bind(Endpoint& ep, boost::system::error_code& ec)
  136. {
  137. ec = this->service.bind(this->implementation, ep, ec);
  138. this->implementation.endpoint_ = ep;
  139. return ec;
  140. }
  141. /// Set socket option
  142. template<typename SettableSocketOption>
  143. void set_option(const SettableSocketOption& option)
  144. {
  145. boost::system::error_code ec;
  146. this->service.set_option(this->implementation, option, ec);
  147. detail::throw_error(ec);
  148. }
  149. /// Set socket option
  150. template<typename SettableSocketOption>
  151. boost::system::error_code
  152. set_option(const SettableSocketOption& option, boost::system::error_code& ec)
  153. {
  154. return this->service.set_option(this->implementation, option, ec);
  155. }
  156. /// Cancel all asynchronous operations associated with the acceptor.
  157. boost::system::error_code
  158. cancel()
  159. {
  160. return this->service.cancel(this->implementation);
  161. }
  162. /// Close the acceptor
  163. void close()
  164. {
  165. boost::system::error_code ec;
  166. this->service.close(this->implementation, ec);
  167. detail::throw_error(ec);
  168. }
  169. /// Close the acceptor
  170. boost::system::error_code
  171. close(boost::system::error_code& ec)
  172. {
  173. return this->service.close(this->implementation, ec);
  174. }
  175. template<typename InternetProtocol>
  176. void assign(InternetProtocol protocol, const native_type& native_acceptor)
  177. {
  178. boost::system::error_code ec;
  179. this->service.assign(this->implementation, protocol, native_acceptor, ec);
  180. detail::throw_error(ec);
  181. }
  182. template<typename InternetProtocol>
  183. boost::system::error_code
  184. assign(InternetProtocol protocol, const native_type& native_acceptor
  185. , boost::system::error_code& ec)
  186. {
  187. return this->service.assign(this->implementation, protocol
  188. , native_acceptor, ec);
  189. }
  190. /// Accept one request and handle it with `handler`.
  191. int accept(accept_handler_type handler)
  192. {
  193. boost::system::error_code ec;
  194. int status = this->service.accept(this->implementation, handler, &this->implementation.endpoint_, ec);
  195. detail::throw_error(ec);
  196. return status;
  197. }
  198. int accept(accept_handler_type handler, boost::system::error_code& ec)
  199. {
  200. return this->service.accept(this->implementation, handler, &this->implementation.endpoint_, ec);
  201. }
  202. void async_accept(accept_handler_type handler)
  203. {
  204. this->service.async_accept(this->implementation, handler);
  205. }
  206. template<typename CommonGatewayRequest>
  207. void accept(CommonGatewayRequest& request)
  208. {
  209. boost::system::error_code ec;
  210. this->service.accept(this->implementation, request, &this->implementation.endpoint_, ec);
  211. detail::throw_error(ec);
  212. }
  213. /// Accept one request
  214. template<typename CommonGatewayRequest>
  215. boost::system::error_code
  216. accept(CommonGatewayRequest& request, boost::system::error_code& ec)
  217. {
  218. return this->service.accept(this->implementation, request, &this->implementation.endpoint_, ec);
  219. }
  220. template<typename CommonGatewayRequest>
  221. boost::system::error_code
  222. accept(CommonGatewayRequest& request, endpoint_type& ep
  223. , boost::system::error_code& ec)
  224. {
  225. return this->service.accept(this->implementation, request, &ep, ec);
  226. }
  227. /// Asynchronously accept one request
  228. template<typename CommonGatewayRequest, typename Handler>
  229. void async_accept(CommonGatewayRequest& request, Handler handler)
  230. {
  231. this->service.async_accept(this->implementation, request, handler);
  232. }
  233. endpoint_type
  234. local_endpoint()
  235. {
  236. boost::system::error_code ec;
  237. endpoint_type ep(this->service.local_endpoint(this->implementation, ec));
  238. detail::throw_error(ec);
  239. return ep;
  240. }
  241. endpoint_type
  242. local_endpoint(boost::system::error_code& ec) const
  243. {
  244. return this->service.local_endpoint(this->implementation, ec);
  245. }
  246. native_type
  247. native()
  248. {
  249. return this->service.native(this->implementation);
  250. }
  251. bool is_cgi()
  252. {
  253. return this->service.is_cgi(this->implementation);
  254. }
  255. };
  256. } // namespace common
  257. BOOST_CGI_NAMESPACE_END
  258. #endif // CGI_BASIC_REQUEST_ACCEPTOR_HPP_INCLUDED__