mirror of
https://github.com/boostorg/beast.git
synced 2026-05-04 19:54:13 +02:00
Refactor stream operations and tests plus coverage
This commit is contained in:
@@ -114,6 +114,7 @@ struct fail_error_code : error_code
|
||||
class fail_counter
|
||||
{
|
||||
std::size_t n_;
|
||||
std::size_t i_ = 0;
|
||||
error_code ec_;
|
||||
|
||||
public:
|
||||
@@ -131,13 +132,20 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
/// Returns the fail index
|
||||
std::size_t
|
||||
count() const
|
||||
{
|
||||
return n_;
|
||||
}
|
||||
|
||||
/// Throw an exception on the Nth failure
|
||||
void
|
||||
fail()
|
||||
{
|
||||
if(n_ > 0)
|
||||
--n_;
|
||||
if(! n_)
|
||||
if(i_ < n_)
|
||||
++i_;
|
||||
if(i_ == n_)
|
||||
BOOST_THROW_EXCEPTION(system_error{ec_});
|
||||
}
|
||||
|
||||
@@ -145,9 +153,9 @@ public:
|
||||
bool
|
||||
fail(error_code& ec)
|
||||
{
|
||||
if(n_ > 0)
|
||||
--n_;
|
||||
if(! n_)
|
||||
if(i_ < n_)
|
||||
++i_;
|
||||
if(i_ == n_)
|
||||
{
|
||||
ec = ec_;
|
||||
return true;
|
||||
|
||||
@@ -78,6 +78,11 @@ class stream
|
||||
std::size_t write_max =
|
||||
(std::numeric_limits<std::size_t>::max)();
|
||||
|
||||
~state()
|
||||
{
|
||||
BOOST_ASSERT(! op);
|
||||
}
|
||||
|
||||
explicit
|
||||
state(
|
||||
boost::asio::io_service& ios_,
|
||||
@@ -87,11 +92,6 @@ class stream
|
||||
{
|
||||
}
|
||||
|
||||
~state()
|
||||
{
|
||||
BOOST_ASSERT(! op);
|
||||
}
|
||||
|
||||
void
|
||||
on_write()
|
||||
{
|
||||
@@ -119,6 +119,10 @@ public:
|
||||
/// Destructor
|
||||
~stream()
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lock{in_->m};
|
||||
in_->op.reset();
|
||||
}
|
||||
auto out = out_.lock();
|
||||
if(out)
|
||||
{
|
||||
@@ -612,16 +616,17 @@ teardown(
|
||||
stream& s,
|
||||
boost::system::error_code& ec)
|
||||
{
|
||||
if(s.in_->fc)
|
||||
{
|
||||
if(s.in_->fc->fail(ec))
|
||||
return;
|
||||
}
|
||||
if( s.in_->fc &&
|
||||
s.in_->fc->fail(ec))
|
||||
return;
|
||||
|
||||
s.close();
|
||||
|
||||
if( s.in_->fc &&
|
||||
s.in_->fc->fail(ec))
|
||||
ec = boost::asio::error::eof;
|
||||
else
|
||||
{
|
||||
s.close();
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
}
|
||||
|
||||
template<class TeardownHandler>
|
||||
@@ -633,10 +638,17 @@ async_teardown(
|
||||
TeardownHandler&& handler)
|
||||
{
|
||||
error_code ec;
|
||||
if(s.in_->fc && s.in_->fc->fail(ec))
|
||||
if( s.in_->fc &&
|
||||
s.in_->fc->fail(ec))
|
||||
return s.get_io_service().post(
|
||||
bind_handler(std::move(handler), ec));
|
||||
s.close();
|
||||
if( s.in_->fc &&
|
||||
s.in_->fc->fail(ec))
|
||||
ec = boost::asio::error::eof;
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
|
||||
s.get_io_service().post(
|
||||
bind_handler(std::move(handler), ec));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user