ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

131 lines
3.4 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #ifndef CPPCMS_CGI_ACCEPTOR_H
  20. #define CPPCMS_CGI_ACCEPTOR_H
  21. #include "cgi_api.h"
  22. #include "service.h"
  23. #include "service_impl.h"
  24. #include "http_context.h"
  25. #include "config.h"
  26. #include <booster/aio/socket.h>
  27. #include <booster/aio/endpoint.h>
  28. namespace io = booster::aio;
  29. namespace cppcms {
  30. namespace impl {
  31. namespace cgi {
  32. template<class ServerAPI>
  33. class socket_acceptor : public acceptor {
  34. public:
  35. socket_acceptor(cppcms::service &srv,std::string ip,int port,int backlog) :
  36. srv_(srv),
  37. acceptor_(srv_.get_io_service()),
  38. stopped_(false),
  39. tcp_(true)
  40. {
  41. io::endpoint ep(ip,port);
  42. acceptor_.open(ep.family(),io::sock_stream);
  43. acceptor_.set_option(io::socket::reuse_address,true);
  44. acceptor_.bind(ep);
  45. acceptor_.listen(backlog);
  46. }
  47. #if !defined(CPPCMS_WIN32)
  48. socket_acceptor(cppcms::service &srv,int backlog) :
  49. srv_(srv),
  50. acceptor_(srv_.get_io_service()),
  51. stopped_(false),
  52. tcp_(false)
  53. {
  54. acceptor_.attach(0);
  55. acceptor_.listen(backlog);
  56. }
  57. socket_acceptor(cppcms::service &srv,std::string path,int backlog) :
  58. srv_(srv),
  59. acceptor_(srv_.get_io_service()),
  60. stopped_(false),
  61. tcp_(false)
  62. {
  63. io::endpoint ep(path);
  64. acceptor_.open(io::pf_unix,io::sock_stream);
  65. acceptor_.set_option(io::socket::reuse_address,true);
  66. ::unlink(path.c_str());
  67. acceptor_.bind(ep);
  68. acceptor_.listen(backlog);
  69. }
  70. #endif
  71. struct accept_binder {
  72. socket_acceptor *self;
  73. accept_binder(socket_acceptor *s=0) : self(s) {}
  74. void operator()(booster::system::error_code const &e)
  75. {
  76. self->on_accept(e);
  77. }
  78. };
  79. virtual void async_accept()
  80. {
  81. if(stopped_)
  82. return;
  83. ServerAPI *api=new ServerAPI(srv_);
  84. api_=api;
  85. asio_socket_ = &api->socket_;
  86. acceptor_.async_accept(*asio_socket_,accept_binder(this));
  87. }
  88. virtual void stop()
  89. {
  90. stopped_=true;
  91. acceptor_.cancel();
  92. }
  93. private:
  94. void on_accept(booster::system::error_code const &e)
  95. {
  96. if(!e) {
  97. if(tcp_)
  98. asio_socket_->set_option(io::socket::tcp_no_delay,true);
  99. intrusive_ptr< ::cppcms::http::context> cnt(new ::cppcms::http::context(api_));
  100. api_=0;
  101. cnt->run();
  102. }
  103. async_accept();
  104. }
  105. cppcms::service &srv_;
  106. intrusive_ptr<connection> api_;
  107. booster::aio::socket *asio_socket_,acceptor_;
  108. bool stopped_;
  109. bool tcp_;
  110. };
  111. } // cgi
  112. } // impl
  113. } // cppcms
  114. #endif