// // Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef BEAST_WEBSOCKET_IMPL_ACCEPT_OP_HPP #define BEAST_WEBSOCKET_IMPL_ACCEPT_OP_HPP #include #include #include #include #include #include #include #include namespace beast { namespace websocket { // read and respond to an upgrade request // template template class stream::accept_op { using alloc_type = handler_alloc; struct data { stream& ws; http::request req; Handler h; bool cont; int state = 0; template data(DeducedHandler&& h_, stream& ws_, Buffers const& buffers) : ws(ws_) , h(std::forward(h_)) , cont(boost_asio_handler_cont_helpers:: is_continuation(h)) { using boost::asio::buffer_copy; using boost::asio::buffer_size; ws.stream_.buffer().commit(buffer_copy( ws.stream_.buffer().prepare( buffer_size(buffers)), buffers)); } }; std::shared_ptr d_; public: accept_op(accept_op&&) = default; accept_op(accept_op const&) = default; template accept_op(DeducedHandler&& h, stream& ws, Args&&... args) : d_(std::allocate_shared(alloc_type{h}, std::forward(h), ws, std::forward(args)...)) { (*this)(error_code{}, 0, false); } void operator()(error_code const& ec) { (*this)(ec, 0); } void operator()(error_code const& ec, std::size_t bytes_transferred, bool again = true); friend auto asio_handler_allocate( std::size_t size, accept_op* op) { return boost_asio_handler_alloc_helpers:: allocate(size, op->d_->h); } friend auto asio_handler_deallocate( void* p, std::size_t size, accept_op* op) { return boost_asio_handler_alloc_helpers:: deallocate(p, size, op->d_->h); } friend auto asio_handler_is_continuation(accept_op* op) { return op->d_->cont; } template friend auto asio_handler_invoke(Function&& f, accept_op* op) { return boost_asio_handler_invoke_helpers:: invoke(f, op->d_->h); } }; template template void stream::accept_op:: operator()(error_code const& ec, std::size_t bytes_transferred, bool again) { auto& d = *d_; d.cont = d.cont || again; while(! ec && d.state != 99) { switch(d.state) { case 0: // read message d.state = 1; http::async_read(d.ws.next_layer_, d.ws.stream_.buffer(), d.req, std::move(*this)); return; // got message case 1: // respond to request response_op{ std::move(d.h), d.ws, d.req, true}; return; } } d.h(ec); } } // websocket } // beast #endif