Files
boost_beast/include/boost/beast/_experimental/test/impl/stream.ipp
T

277 lines
6.0 KiB
C++
Raw Normal View History

2019-02-22 13:06:43 -08:00
//
// Copyright (c) 2016-2019 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_TEST_IMPL_STREAM_IPP
#define BOOST_BEAST_TEST_IMPL_STREAM_IPP
#include <boost/beast/_experimental/test/stream.hpp>
#include <boost/beast/core/bind_handler.hpp>
2019-03-05 08:25:54 -08:00
#include <boost/beast/core/buffer_traits.hpp>
2019-02-26 18:55:23 -08:00
#include <boost/make_shared.hpp>
2019-02-22 13:06:43 -08:00
#include <stdexcept>
2019-02-26 18:55:23 -08:00
#include <vector>
2019-02-22 13:06:43 -08:00
namespace boost {
namespace beast {
namespace test {
//------------------------------------------------------------------------------
2020-12-11 12:03:34 +01:00
template<class Executor>
void basic_stream<Executor>::initiate_read(
boost::shared_ptr<detail::stream_state> const& in_,
std::unique_ptr<detail::stream_read_op_base>&& op,
2019-02-23 02:30:08 +01:00
std::size_t buf_size)
{
std::unique_lock<std::mutex> lock(in_->m);
++in_->nread;
if(in_->op != nullptr)
2019-03-01 13:39:12 -08:00
BOOST_THROW_EXCEPTION(
std::logic_error{"in_->op != nullptr"});
2019-02-23 02:30:08 +01:00
// test failure
error_code ec;
if(in_->fc && in_->fc->fail(ec))
{
lock.unlock();
(*op)(ec);
return;
}
// A request to read 0 bytes from a stream is a no-op.
2019-03-05 08:47:02 -08:00
if(buf_size == 0 || buffer_bytes(in_->b.data()) > 0)
2019-02-23 02:30:08 +01:00
{
lock.unlock();
(*op)(ec);
return;
}
// deliver error
2020-12-11 12:03:34 +01:00
if(in_->code != detail::stream_status::ok)
2019-02-23 02:30:08 +01:00
{
lock.unlock();
(*op)(net::error::eof);
return;
}
// complete when bytes available or closed
in_->op = std::move(op);
}
2019-02-22 13:06:43 -08:00
//------------------------------------------------------------------------------
2019-02-23 02:30:08 +01:00
2020-12-11 12:03:34 +01:00
template<class Executor>
basic_stream<Executor>::
~basic_stream()
2019-02-22 13:06:43 -08:00
{
close();
2019-02-26 18:55:23 -08:00
in_->remove();
2019-02-22 13:06:43 -08:00
}
2020-12-11 12:03:34 +01:00
template<class Executor>
basic_stream<Executor>::
basic_stream(basic_stream&& other)
2019-02-22 13:06:43 -08:00
{
2020-12-11 12:03:34 +01:00
auto in = detail::stream_service::make_impl(
other.in_->exec, other.in_->fc);
2019-02-22 13:06:43 -08:00
in_ = std::move(other.in_);
out_ = std::move(other.out_);
other.in_ = in;
}
2020-12-11 12:03:34 +01:00
template<class Executor>
basic_stream<Executor>&
basic_stream<Executor>::
operator=(basic_stream&& other)
2019-02-22 13:06:43 -08:00
{
close();
2020-12-11 12:03:34 +01:00
auto in = detail::stream_service::make_impl(
other.in_->exec, other.in_->fc);
2019-02-26 18:55:23 -08:00
in_->remove();
2019-02-22 13:06:43 -08:00
in_ = std::move(other.in_);
out_ = std::move(other.out_);
other.in_ = in;
return *this;
}
//------------------------------------------------------------------------------
2020-12-11 12:03:34 +01:00
template<class Executor>
basic_stream<Executor>::
basic_stream(executor_type exec)
: in_(detail::stream_service::make_impl(std::move(exec), nullptr))
2019-02-22 13:06:43 -08:00
{
}
2020-12-11 12:03:34 +01:00
template<class Executor>
basic_stream<Executor>::
basic_stream(
2019-02-22 13:06:43 -08:00
net::io_context& ioc,
fail_count& fc)
2020-12-11 12:03:34 +01:00
: in_(detail::stream_service::make_impl(ioc.get_executor(), &fc))
2019-02-22 13:06:43 -08:00
{
}
2020-12-11 12:03:34 +01:00
template<class Executor>
basic_stream<Executor>::
basic_stream(
2019-02-22 13:06:43 -08:00
net::io_context& ioc,
string_view s)
2020-12-11 12:03:34 +01:00
: in_(detail::stream_service::make_impl(ioc.get_executor(), nullptr))
2019-02-22 13:06:43 -08:00
{
in_->b.commit(net::buffer_copy(
in_->b.prepare(s.size()),
net::buffer(s.data(), s.size())));
}
2020-12-11 12:03:34 +01:00
template<class Executor>
basic_stream<Executor>::
basic_stream(
2019-02-22 13:06:43 -08:00
net::io_context& ioc,
fail_count& fc,
string_view s)
2020-12-11 12:03:34 +01:00
: in_(detail::stream_service::make_impl(ioc.get_executor(), &fc))
2019-02-22 13:06:43 -08:00
{
in_->b.commit(net::buffer_copy(
in_->b.prepare(s.size()),
net::buffer(s.data(), s.size())));
}
2020-12-11 12:03:34 +01:00
template<class Executor>
2019-02-22 13:06:43 -08:00
void
2020-12-11 12:03:34 +01:00
basic_stream<Executor>::
connect(basic_stream& remote)
2019-02-22 13:06:43 -08:00
{
BOOST_ASSERT(! out_.lock());
BOOST_ASSERT(! remote.out_.lock());
2019-04-29 14:12:46 +02:00
std::lock(in_->m, remote.in_->m);
std::lock_guard<std::mutex> guard1{in_->m, std::adopt_lock};
std::lock_guard<std::mutex> guard2{remote.in_->m, std::adopt_lock};
2019-02-22 13:06:43 -08:00
out_ = remote.in_;
remote.out_ = in_;
2020-12-11 12:03:34 +01:00
in_->code = detail::stream_status::ok;
remote.in_->code = detail::stream_status::ok;
2019-02-22 13:06:43 -08:00
}
2020-12-11 12:03:34 +01:00
template<class Executor>
2019-02-22 13:06:43 -08:00
string_view
2020-12-11 12:03:34 +01:00
basic_stream<Executor>::
2019-02-22 13:06:43 -08:00
str() const
{
auto const bs = in_->b.data();
2019-03-05 08:47:02 -08:00
if(buffer_bytes(bs) == 0)
2019-02-22 13:06:43 -08:00
return {};
2019-06-09 19:25:17 +02:00
net::const_buffer const b = *net::buffer_sequence_begin(bs);
2019-02-22 13:06:43 -08:00
return {static_cast<char const*>(b.data()), b.size()};
}
2020-12-11 12:03:34 +01:00
template<class Executor>
2019-02-22 13:06:43 -08:00
void
2020-12-11 12:03:34 +01:00
basic_stream<Executor>::
2019-02-22 13:06:43 -08:00
append(string_view s)
{
std::lock_guard<std::mutex> lock{in_->m};
in_->b.commit(net::buffer_copy(
in_->b.prepare(s.size()),
net::buffer(s.data(), s.size())));
}
2020-12-11 12:03:34 +01:00
template<class Executor>
2019-02-22 13:06:43 -08:00
void
2020-12-11 12:03:34 +01:00
basic_stream<Executor>::
2019-02-22 13:06:43 -08:00
clear()
{
std::lock_guard<std::mutex> lock{in_->m};
in_->b.consume(in_->b.size());
}
2020-12-11 12:03:34 +01:00
template<class Executor>
2019-02-22 13:06:43 -08:00
void
2020-12-11 12:03:34 +01:00
basic_stream<Executor>::
2019-02-22 13:06:43 -08:00
close()
{
2019-02-26 18:55:23 -08:00
in_->cancel_read();
2019-02-22 13:06:43 -08:00
// disconnect
{
auto out = out_.lock();
out_.reset();
// notify peer
if(out)
{
std::lock_guard<std::mutex> lock(out->m);
2020-12-11 12:03:34 +01:00
if(out->code == detail::stream_status::ok)
2019-02-22 13:06:43 -08:00
{
2020-12-11 12:03:34 +01:00
out->code = detail::stream_status::eof;
2019-02-22 13:06:43 -08:00
out->notify_read();
}
}
}
}
2020-12-11 12:03:34 +01:00
template<class Executor>
2019-02-22 13:06:43 -08:00
void
2020-12-11 12:03:34 +01:00
basic_stream<Executor>::
2019-02-22 13:06:43 -08:00
close_remote()
{
std::lock_guard<std::mutex> lock{in_->m};
2020-12-11 12:03:34 +01:00
if(in_->code == detail::stream_status::ok)
2019-02-22 13:06:43 -08:00
{
2020-12-11 12:03:34 +01:00
in_->code = detail::stream_status::eof;
2019-02-22 13:06:43 -08:00
in_->notify_read();
}
}
2020-12-11 12:03:34 +01:00
template<class Executor>
2019-02-22 13:06:43 -08:00
void
teardown(
role_type,
2020-12-11 12:03:34 +01:00
basic_stream<Executor>& s,
2019-02-22 13:06:43 -08:00
boost::system::error_code& ec)
{
if( s.in_->fc &&
s.in_->fc->fail(ec))
return;
s.close();
if( s.in_->fc &&
s.in_->fc->fail(ec))
ec = net::error::eof;
else
ec = {};
}
//------------------------------------------------------------------------------
2020-12-11 12:03:34 +01:00
template<class Executor>
basic_stream<Executor>
connect(basic_stream<Executor>& to)
2019-02-22 13:06:43 -08:00
{
2020-12-11 12:03:34 +01:00
basic_stream<Executor> from(to.get_executor());
2019-02-22 13:06:43 -08:00
from.connect(to);
return from;
}
2020-12-11 12:03:34 +01:00
template<class Executor>
2019-02-22 13:06:43 -08:00
void
2020-12-11 12:03:34 +01:00
connect(basic_stream<Executor>& s1, basic_stream<Executor>& s2)
2019-02-22 13:06:43 -08:00
{
s1.connect(s2);
}
} // test
} // beast
} // boost
#endif