diff --git a/server_http.hpp b/server_http.hpp index 2f09ec1..8a1d4af 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -21,14 +21,14 @@ namespace SimpleWeb { boost::asio::streambuf streambuf; - std::shared_ptr socket; + socket_type &socket; - Response(boost::asio::io_service& io_service, std::shared_ptr socket, boost::asio::yield_context& yield): + Response(boost::asio::io_service& io_service, socket_type &socket, boost::asio::yield_context& yield): yield(yield), socket(socket), stream(&streambuf) {} void flush() { boost::system::error_code ec; - boost::asio::async_write(*socket, streambuf, yield[ec]); + boost::asio::async_write(socket, streambuf, yield[ec]); if(ec) throw std::runtime_error(ec.message()); @@ -77,10 +77,12 @@ namespace SimpleWeb { unsigned short remote_endpoint_port; private: - Request(): content(&streambuf) {} + Request(boost::asio::io_service &io_service): content(&streambuf), strand(io_service) {} boost::asio::streambuf streambuf; + boost::asio::strand strand; + void read_remote_endpoint_data(socket_type& socket) { try { remote_endpoint_address=socket.lowest_layer().remote_endpoint().address().to_string(); @@ -149,7 +151,6 @@ namespace SimpleWeb { boost::asio::io_service io_service; boost::asio::ip::tcp::endpoint endpoint; boost::asio::ip::tcp::acceptor acceptor; - boost::asio::strand strand; size_t num_threads; std::vector threads; @@ -157,7 +158,7 @@ namespace SimpleWeb { size_t timeout_content; ServerBase(unsigned short port, size_t num_threads, size_t timeout_request, size_t timeout_send_or_receive) : - endpoint(boost::asio::ip::tcp::v4(), port), acceptor(io_service, endpoint), strand(io_service), + endpoint(boost::asio::ip::tcp::v4(), port), acceptor(io_service, endpoint), num_threads(num_threads), timeout_request(timeout_request), timeout_content(timeout_send_or_receive) {} virtual void accept()=0; @@ -175,10 +176,10 @@ namespace SimpleWeb { return timer; } - std::shared_ptr set_timeout_on_socket_strand_wrapped(std::shared_ptr socket, size_t seconds) { + std::shared_ptr set_timeout_on_socket(std::shared_ptr socket, std::shared_ptr request, size_t seconds) { std::shared_ptr timer(new boost::asio::deadline_timer(io_service)); timer->expires_from_now(boost::posix_time::seconds(seconds)); - timer->async_wait(strand.wrap([socket](const boost::system::error_code& ec){ + timer->async_wait(request->strand.wrap([socket](const boost::system::error_code& ec){ if(!ec) { boost::system::error_code ec; socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); @@ -191,7 +192,7 @@ namespace SimpleWeb { void read_request_and_content(std::shared_ptr socket) { //Create new streambuf (Request::streambuf) for async_read_until() //shared_ptr is used to pass temporary objects to the asynchronous functions - std::shared_ptr request(new Request()); + std::shared_ptr request(new Request(io_service)); request->read_remote_endpoint_data(*socket); //Set timeout on the following boost::asio::async-read or write function @@ -294,10 +295,10 @@ namespace SimpleWeb { //Set timeout on the following boost::asio::async-read or write function std::shared_ptr timer; if(timeout_content>0) - timer=set_timeout_on_socket_strand_wrapped(socket, timeout_content); + timer=set_timeout_on_socket(socket, request, timeout_content); - boost::asio::spawn(strand, [this, &resource_function, socket, request, timer](boost::asio::yield_context yield) { - Response response(io_service, socket, yield); + boost::asio::spawn(request->strand, [this, &resource_function, socket, request, timer](boost::asio::yield_context yield) { + Response response(io_service, *socket, yield); try { resource_function(response, request); diff --git a/test/parse_test.cpp b/test/parse_test.cpp index ce84f00..5888a8d 100644 --- a/test/parse_test.cpp +++ b/test/parse_test.cpp @@ -15,7 +15,7 @@ public: void accept() {} bool parse_request_test() { - std::shared_ptr request(new Request()); + std::shared_ptr request(new Request(io_service)); stringstream ss; ss << "GET /test/ HTTP/1.1\r\n";