Simplify buffered_read_stream composed op

fix #358
This commit is contained in:
Vinnie Falco
2017-06-20 18:58:13 -07:00
parent 9ca2ca7db0
commit 6d489b4658
2 changed files with 52 additions and 66 deletions

View File

@ -1,3 +1,9 @@
Version 64:
* Simplify buffered_read_stream composed op
--------------------------------------------------------------------------------
Version 63: Version 63:
* Use std::to_string instead of lexical_cast * Use std::to_string instead of lexical_cast

View File

@ -16,6 +16,7 @@
#include <boost/asio/handler_continuation_hook.hpp> #include <boost/asio/handler_continuation_hook.hpp>
#include <boost/asio/handler_invoke_hook.hpp> #include <boost/asio/handler_invoke_hook.hpp>
#include <boost/throw_exception.hpp> #include <boost/throw_exception.hpp>
#include <boost/config.hpp>
namespace beast { namespace beast {
@ -24,22 +25,10 @@ template<class MutableBufferSequence, class Handler>
class buffered_read_stream< class buffered_read_stream<
Stream, DynamicBuffer>::read_some_op Stream, DynamicBuffer>::read_some_op
{ {
// VFALCO What about bool cont for is_continuation? int step_ = 0;
struct data buffered_read_stream& s_;
{ MutableBufferSequence b_;
buffered_read_stream& srs; Handler h_;
MutableBufferSequence bs;
int state = 0;
data(Handler&, buffered_read_stream& srs_,
MutableBufferSequence const& bs_)
: srs(srs_)
, bs(bs_)
{
}
};
handler_ptr<data, Handler> d_;
public: public:
read_some_op(read_some_op&&) = default; read_some_op(read_some_op&&) = default;
@ -47,11 +36,12 @@ public:
template<class DeducedHandler, class... Args> template<class DeducedHandler, class... Args>
read_some_op(DeducedHandler&& h, read_some_op(DeducedHandler&& h,
buffered_read_stream& srs, Args&&... args) buffered_read_stream& s,
: d_(std::forward<DeducedHandler>(h), MutableBufferSequence const& b)
srs, std::forward<Args>(args)...) : s_(s)
, b_(b)
, h_(std::forward<DeducedHandler>(h))
{ {
(*this)(error_code{}, 0);
} }
void void
@ -64,7 +54,7 @@ public:
{ {
using boost::asio::asio_handler_allocate; using boost::asio::asio_handler_allocate;
return asio_handler_allocate( return asio_handler_allocate(
size, std::addressof(op->d_.handler())); size, std::addressof(op->h_));
} }
friend friend
@ -73,7 +63,7 @@ public:
{ {
using boost::asio::asio_handler_deallocate; using boost::asio::asio_handler_deallocate;
asio_handler_deallocate( asio_handler_deallocate(
p, size, std::addressof(op->d_.handler())); p, size, std::addressof(op->h_));
} }
friend friend
@ -81,7 +71,7 @@ public:
{ {
using boost::asio::asio_handler_is_continuation; using boost::asio::asio_handler_is_continuation;
return asio_handler_is_continuation( return asio_handler_is_continuation(
std::addressof(op->d_.handler())); std::addressof(op->h_));
} }
template<class Function> template<class Function>
@ -89,8 +79,7 @@ public:
void asio_handler_invoke(Function&& f, read_some_op* op) void asio_handler_invoke(Function&& f, read_some_op* op)
{ {
using boost::asio::asio_handler_invoke; using boost::asio::asio_handler_invoke;
asio_handler_invoke( asio_handler_invoke(f, std::addressof(op->h_));
f, std::addressof(op->d_.handler()));
} }
}; };
@ -101,56 +90,46 @@ buffered_read_stream<Stream, DynamicBuffer>::
read_some_op<MutableBufferSequence, Handler>::operator()( read_some_op<MutableBufferSequence, Handler>::operator()(
error_code const& ec, std::size_t bytes_transferred) error_code const& ec, std::size_t bytes_transferred)
{ {
auto& d = *d_; switch(step_)
while(! ec && d.state != 99)
{ {
switch(d.state) case 0:
if(s_.sb_.size() == 0)
{ {
case 0: if(s_.capacity_ == 0)
if(d.srs.sb_.size() == 0)
{ {
d.state = // read (unbuffered)
d.srs.capacity_ > 0 ? 2 : 1; step_ = 1;
break; return s_.next_layer_.async_read_some(
b_, std::move(*this));
} }
d.state = 4;
d.srs.get_io_service().post(
bind_handler(std::move(*this), ec, 0));
return;
case 1:
// read (unbuffered)
d.state = 99;
d.srs.next_layer_.async_read_some(
d.bs, std::move(*this));
return;
case 2:
// read // read
d.state = 3; step_ = 2;
d.srs.next_layer_.async_read_some( return s_.next_layer_.async_read_some(
d.srs.sb_.prepare(d.srs.capacity_), s_.sb_.prepare(s_.capacity_),
std::move(*this)); std::move(*this));
return;
// got data
case 3:
d.state = 4;
d.srs.sb_.commit(bytes_transferred);
break;
// copy
case 4:
bytes_transferred =
boost::asio::buffer_copy(
d.bs, d.srs.sb_.data());
d.srs.sb_.consume(bytes_transferred);
// call handler
d.state = 99;
break;
} }
step_ = 3;
s_.get_io_service().post(
bind_handler(std::move(*this), ec, 0));
return;
case 1:
// upcall
break;
case 2:
s_.sb_.commit(bytes_transferred);
BOOST_FALLTHROUGH;
case 3:
bytes_transferred =
boost::asio::buffer_copy(b_, s_.sb_.data());
s_.sb_.consume(bytes_transferred);
break;
} }
d_.invoke(ec, bytes_transferred); h_(ec, bytes_transferred);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -252,7 +231,8 @@ async_read_some(MutableBufferSequence const& buffers,
void(error_code, std::size_t)> init{handler}; void(error_code, std::size_t)> init{handler};
read_some_op<MutableBufferSequence, handler_type< read_some_op<MutableBufferSequence, handler_type<
ReadHandler, void(error_code, std::size_t)>>{ ReadHandler, void(error_code, std::size_t)>>{
init.completion_handler, *this, buffers}; init.completion_handler, *this, buffers}(
error_code{}, 0);
return init.result.get(); return init.result.get();
} }