// // 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_WRITE_OP_HPP #define BEAST_WEBSOCKET_IMPL_WRITE_OP_HPP #include #include #include #include #include #include #include namespace beast { namespace websocket { // write a message // template template class stream::write_op { using alloc_type = handler_alloc; struct data : op { stream& ws; consuming_buffers cb; Handler h; std::size_t remain; bool cont; int state = 0; template data(DeducedHandler&& h_, stream& ws_, Buffers const& bs) : ws(ws_) , cb(bs) , h(std::forward(h_)) , remain(boost::asio::buffer_size(cb)) , cont(boost_asio_handler_cont_helpers:: is_continuation(h)) { } }; std::shared_ptr d_; public: write_op(write_op&&) = default; write_op(write_op const&) = default; template explicit write_op(DeducedHandler&& h, stream& ws, Args&&... args) : d_(std::allocate_shared(alloc_type{h}, std::forward(h), ws, std::forward(args)...)) { (*this)(error_code{}, false); } void operator()(error_code ec, bool again = true); friend void* asio_handler_allocate( std::size_t size, write_op* op) { return boost_asio_handler_alloc_helpers:: allocate(size, op->d_->h); } friend void asio_handler_deallocate( void* p, std::size_t size, write_op* op) { return boost_asio_handler_alloc_helpers:: deallocate(p, size, op->d_->h); } friend bool asio_handler_is_continuation(write_op* op) { return op->d_->cont; } template friend void asio_handler_invoke(Function&& f, write_op* op) { return boost_asio_handler_invoke_helpers:: invoke(f, op->d_->h); } }; template template void stream:: write_op:: operator()(error_code ec, bool again) { auto& d = *d_; d.cont = d.cont || again; while(! ec && d.state != 99) { switch(d.state) { case 0: { auto const n = std::min( d.remain, d.ws.wr_frag_size_); d.remain -= n; auto const fin = d.remain <= 0; if(fin) d.state = 99; auto const pb = prepare_buffers(n, d.cb); d.cb.consume(n); d.ws.async_write_frame(fin, pb, std::move(*this)); return; } } } d.h(ec); } } // websocket } // beast #endif