Skip to content

Instantly share code, notes, and snippets.

@madmongo1
Created December 11, 2020 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madmongo1/a47e4f78d7b60952a5b0dad47e4a563a to your computer and use it in GitHub Desktop.
Save madmongo1/a47e4f78d7b60952a5b0dad47e4a563a to your computer and use it in GitHub Desktop.
first_steps.patch
diff --git a/include/boost/beast/_experimental/test/impl/stream.hpp b/include/boost/beast/_experimental/test/impl/stream.hpp
index f7eac6afd..9f8dc63cd 100644
--- a/include/boost/beast/_experimental/test/impl/stream.hpp
+++ b/include/boost/beast/_experimental/test/impl/stream.hpp
@@ -27,7 +27,8 @@ namespace test {
//------------------------------------------------------------------------------
-struct stream::service_impl
+template<class Executor>
+struct basic_stream<Executor>::service_impl
{
std::mutex m_;
std::vector<state*> v_;
@@ -37,7 +38,8 @@ struct stream::service_impl
remove(state& impl);
};
-class stream::service
+template<class Executor>
+struct basic_stream<Executor>::service
: public beast::detail::service_base<service>
{
boost::shared_ptr<service_impl> sp_;
@@ -55,18 +57,19 @@ public:
static
auto
make_impl(
- net::io_context& ctx,
+ executor_type exec,
test::fail_count* fc) ->
boost::shared_ptr<state>;
};
//------------------------------------------------------------------------------
+template<class Executor>
template<class Handler, class Buffers>
-class stream::read_op : public stream::read_op_base
+class basic_stream<Executor>::read_op : public stream::read_op_base
{
using ex1_type =
- net::io_context::executor_type;
+ executor_type;
using ex2_type
= net::associated_executor_t<Handler, ex1_type>;
@@ -95,11 +98,11 @@ class stream::read_op : public stream::read_op_base
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
, wg2_(net::prefer(
net::get_associated_executor(
- h_, s->ioc.get_executor()),
+ h_, s->exec),
net::execution::outstanding_work.tracked))
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
, wg2_(net::get_associated_executor(
- h_, s->ioc.get_executor()))
+ h_, s->exec))
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
{
}
@@ -165,10 +168,10 @@ public:
Buffers const& b)
: fn_(std::forward<Handler_>(h), s, b)
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
- , wg1_(net::prefer(s->ioc.get_executor(),
+ , wg1_(net::prefer(s->exec,
net::execution::outstanding_work.tracked))
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
- , wg1_(s->ioc.get_executor())
+ , wg1_(s->exec)
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
{
}
@@ -187,7 +190,8 @@ public:
}
};
-struct stream::run_read_op
+template<class Executor>
+struct basic_stream<Executor>::run_read_op
{
template<
class ReadHandler,
@@ -220,7 +224,8 @@ struct stream::run_read_op
}
};
-struct stream::run_write_op
+template<class Executor>
+struct basic_stream<Executor>::run_write_op
{
template<
class WriteHandler,
@@ -245,7 +250,7 @@ struct stream::run_write_op
auto const upcall = [&](error_code ec, std::size_t n)
{
net::post(
- in_->ioc.get_executor(),
+ in_->exec,
beast::bind_front_handler(std::move(h), ec, n));
};
@@ -281,9 +286,10 @@ struct stream::run_write_op
//------------------------------------------------------------------------------
+template<class Executor>
template<class MutableBufferSequence>
std::size_t
-stream::
+basic_stream<Executor>::
read_some(MutableBufferSequence const& buffers)
{
static_assert(net::is_mutable_buffer_sequence<
@@ -296,9 +302,10 @@ read_some(MutableBufferSequence const& buffers)
return n;
}
+template<class Executor>
template<class MutableBufferSequence>
std::size_t
-stream::
+basic_stream<Executor>::
read_some(MutableBufferSequence const& buffers,
error_code& ec)
{
@@ -345,9 +352,10 @@ read_some(MutableBufferSequence const& buffers,
return 0;
}
+template<class Executor>
template<class MutableBufferSequence, BOOST_BEAST_ASYNC_TPARAM2 ReadHandler>
BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
-stream::
+basic_stream<Executor>::
async_read_some(
MutableBufferSequence const& buffers,
ReadHandler&& handler)
@@ -365,9 +373,10 @@ async_read_some(
buffers);
}
+template<class Executor>
template<class ConstBufferSequence>
std::size_t
-stream::
+basic_stream<Executor>::
write_some(ConstBufferSequence const& buffers)
{
static_assert(net::is_const_buffer_sequence<
@@ -381,9 +390,10 @@ write_some(ConstBufferSequence const& buffers)
return bytes_transferred;
}
+template<class Executor>
template<class ConstBufferSequence>
std::size_t
-stream::
+basic_stream<Executor>::
write_some(
ConstBufferSequence const& buffers, error_code& ec)
{
@@ -425,9 +435,10 @@ write_some(
return n;
}
+template<class Executor>
template<class ConstBufferSequence, BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
-stream::
+basic_stream<Executor>::
async_write_some(
ConstBufferSequence const& buffers,
WriteHandler&& handler)
@@ -448,11 +459,11 @@ async_write_some(
//------------------------------------------------------------------------------
-template<class TeardownHandler>
+template<class Executor, class TeardownHandler>
void
async_teardown(
role_type,
- stream& s,
+ basic_stream<Executor>& s,
TeardownHandler&& handler)
{
error_code ec;
@@ -477,8 +488,8 @@ async_teardown(
//------------------------------------------------------------------------------
-template<class Arg1, class... ArgN>
-stream
+template<class Executor, class Arg1, class... ArgN>
+basic_stream<Executor>
connect(stream& to, Arg1&& arg1, ArgN&&... argn)
{
stream from{
diff --git a/include/boost/beast/_experimental/test/impl/stream.ipp b/include/boost/beast/_experimental/test/impl/stream.ipp
index ec544ce15..c1ee83f34 100644
--- a/include/boost/beast/_experimental/test/impl/stream.ipp
+++ b/include/boost/beast/_experimental/test/impl/stream.ipp
@@ -23,7 +23,8 @@ namespace test {
//------------------------------------------------------------------------------
-stream::
+template<class Executor>
+basic_stream<Executor>::
service::
service(net::execution_context& ctx)
: beast::detail::service_base<service>(ctx)
@@ -31,8 +32,9 @@ service(net::execution_context& ctx)
{
}
+template<class Executor>
void
-stream::
+basic_stream<Executor>::
service::
shutdown()
{
@@ -47,23 +49,28 @@ shutdown()
}
}
+template<class Executor>
auto
-stream::
+basic_stream<Executor>::
service::
make_impl(
- net::io_context& ctx,
+ executor_type exec,
test::fail_count* fc) ->
boost::shared_ptr<state>
{
+ auto& ctx = net::query(
+ exec,
+ net::execution::context);
auto& svc = net::use_service<service>(ctx);
- auto sp = boost::make_shared<state>(ctx, svc.sp_, fc);
+ auto sp = boost::make_shared<state>(exec, svc.sp_, fc);
std::lock_guard<std::mutex> g(svc.sp_->m_);
svc.sp_->v_.push_back(sp.get());
return sp;
}
+template<class Executor>
void
-stream::
+basic_stream<Executor>::
service_impl::
remove(state& impl)
{
@@ -76,9 +83,10 @@ remove(state& impl)
//------------------------------------------------------------------------------
-void stream::initiate_read(
+template<class Executor>
+void basic_stream<Executor>::initiate_read(
boost::shared_ptr<state> const& in_,
- std::unique_ptr<stream::read_op_base>&& op,
+ std::unique_ptr<basic_stream::read_op_base>&& op,
std::size_t buf_size)
{
std::unique_lock<std::mutex> lock(in_->m);
@@ -117,19 +125,21 @@ void stream::initiate_read(
in_->op = std::move(op);
}
-stream::
+template<class Executor>
+basic_stream<Executor>::
state::
state(
- net::io_context& ioc_,
+ executor_type exec_,
boost::weak_ptr<service_impl> wp_,
fail_count* fc_)
- : ioc(ioc_)
+ : exec(std::move(exec_))
, wp(std::move(wp_))
, fc(fc_)
{
}
-stream::
+template<class Executor>
+basic_stream<Executor>::
state::
~state()
{
@@ -138,8 +148,9 @@ state::
(*op)(net::error::operation_aborted);
}
+template<class Executor>
void
-stream::
+basic_stream<Executor>::
state::
remove() noexcept
{
@@ -152,8 +163,9 @@ remove() noexcept
sp->remove(*this);
}
+template<class Executor>
void
-stream::
+basic_stream<Executor>::
state::
notify_read()
{
@@ -168,8 +180,9 @@ notify_read()
}
}
+template<class Executor>
void
-stream::
+basic_stream<Executor>::
state::
cancel_read()
{
@@ -185,30 +198,34 @@ cancel_read()
//------------------------------------------------------------------------------
-stream::
-~stream()
+template<class Executor>
+basic_stream<Executor>::
+~basic_stream()
{
close();
in_->remove();
}
-stream::
-stream(stream&& other)
+template<class Executor>
+basic_stream<Executor>::
+basic_stream(basic_stream&& other)
{
auto in = service::make_impl(
- other.in_->ioc, other.in_->fc);
+ other.in_->exec, other.in_->fc);
in_ = std::move(other.in_);
out_ = std::move(other.out_);
other.in_ = in;
}
-stream&
-stream::
-operator=(stream&& other)
+
+template<class Executor>
+basic_stream<Executor>&
+basic_stream<Executor>::
+operator=(basic_stream&& other)
{
close();
auto in = service::make_impl(
- other.in_->ioc, other.in_->fc);
+ other.in_->exec, other.in_->fc);
in_->remove();
in_ = std::move(other.in_);
out_ = std::move(other.out_);
@@ -218,46 +235,58 @@ operator=(stream&& other)
//------------------------------------------------------------------------------
-stream::
-stream(net::io_context& ioc)
- : in_(service::make_impl(ioc, nullptr))
+template<class Executor>
+basic_stream<Executor>::
+basic_stream(net::io_context& ioc)
+ : basic_stream(ioc.get_executor())
+{
+}
+
+template<class Executor>
+basic_stream<Executor>::
+basic_stream(executor_type exec)
+ : in_(service::make_impl(std::move(exec), nullptr))
{
}
-stream::
-stream(
+template<class Executor>
+basic_stream<Executor>::
+basic_stream(
net::io_context& ioc,
fail_count& fc)
- : in_(service::make_impl(ioc, &fc))
+ : in_(service::make_impl(ioc.get_executor(), &fc))
{
}
-stream::
-stream(
+template<class Executor>
+basic_stream<Executor>::
+basic_stream(
net::io_context& ioc,
string_view s)
- : in_(service::make_impl(ioc, nullptr))
+ : in_(service::make_impl(ioc.get_executor(), nullptr))
{
in_->b.commit(net::buffer_copy(
in_->b.prepare(s.size()),
net::buffer(s.data(), s.size())));
}
-stream::
-stream(
+template<class Executor>
+basic_stream<Executor>::
+basic_stream(
net::io_context& ioc,
fail_count& fc,
string_view s)
- : in_(service::make_impl(ioc, &fc))
+ : in_(service::make_impl(ioc.get_executor(), &fc))
{
in_->b.commit(net::buffer_copy(
in_->b.prepare(s.size()),
net::buffer(s.data(), s.size())));
}
+template<class Executor>
void
-stream::
-connect(stream& remote)
+basic_stream<Executor>::
+connect(basic_stream& remote)
{
BOOST_ASSERT(! out_.lock());
BOOST_ASSERT(! remote.out_.lock());
@@ -270,8 +299,9 @@ connect(stream& remote)
remote.in_->code = status::ok;
}
+template<class Executor>
string_view
-stream::
+basic_stream<Executor>::
str() const
{
auto const bs = in_->b.data();
@@ -281,8 +311,9 @@ str() const
return {static_cast<char const*>(b.data()), b.size()};
}
+template<class Executor>
void
-stream::
+basic_stream<Executor>::
append(string_view s)
{
std::lock_guard<std::mutex> lock{in_->m};
@@ -291,16 +322,18 @@ append(string_view s)
net::buffer(s.data(), s.size())));
}
+template<class Executor>
void
-stream::
+basic_stream<Executor>::
clear()
{
std::lock_guard<std::mutex> lock{in_->m};
in_->b.consume(in_->b.size());
}
+template<class Executor>
void
-stream::
+basic_stream<Executor>::
close()
{
in_->cancel_read();
@@ -323,8 +356,9 @@ close()
}
}
+template<class Executor>
void
-stream::
+basic_stream<Executor>::
close_remote()
{
std::lock_guard<std::mutex> lock{in_->m};
@@ -335,10 +369,11 @@ close_remote()
}
}
+template<class Executor>
void
teardown(
role_type,
- stream& s,
+ basic_stream<Executor>& s,
boost::system::error_code& ec)
{
if( s.in_->fc &&
@@ -356,20 +391,18 @@ teardown(
//------------------------------------------------------------------------------
-stream
-connect(stream& to)
+template<class Executor>
+basic_stream<Executor>
+connect(basic_stream<Executor>& to)
{
-#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
- stream from{net::query(to.get_executor(), net::execution::context)};
-#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
- stream from{to.get_executor().context()};
-#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
+ basic_stream<Executor> from(to.get_executor());
from.connect(to);
return from;
}
+template<class Executor>
void
-connect(stream& s1, stream& s2)
+connect(basic_stream<Executor>& s1, basic_stream<Executor>& s2)
{
s1.connect(s2);
}
diff --git a/include/boost/beast/_experimental/test/stream.hpp b/include/boost/beast/_experimental/test/stream.hpp
index 6b725ba1a..e248c51dd 100644
--- a/include/boost/beast/_experimental/test/stream.hpp
+++ b/include/boost/beast/_experimental/test/stream.hpp
@@ -20,6 +20,7 @@
#include <boost/asio/buffer.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/executor_work_guard.hpp>
+#include <boost/asio/any_io_executor.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/post.hpp>
#include <boost/assert.hpp>
@@ -106,8 +107,32 @@ namespace test {
@li <em>AsyncReadStream</em>
@li <em>AsyncWriteStream</em>
*/
-class stream
+template<class Executor = net::any_io_executor>
+class basic_stream;
+
+template<class Executor>
+void
+teardown(
+ role_type,
+ basic_stream<Executor>& s,
+ boost::system::error_code& ec);
+
+template<class Executor, class TeardownHandler>
+void
+async_teardown(
+ role_type role,
+ basic_stream<Executor>& s,
+ TeardownHandler&& handler);
+
+template<class Executor>
+class basic_stream
{
+public:
+ /// The type of the executor associated with the object.
+ using executor_type =
+ Executor;
+
+private:
struct state;
boost::shared_ptr<state> in_;
@@ -130,9 +155,9 @@ class stream
struct state
{
- friend class stream;
+ friend class basic_stream;
- net::io_context& ioc;
+ executor_type exec;
boost::weak_ptr<service_impl> wp;
std::mutex m;
flat_buffer b;
@@ -151,7 +176,7 @@ class stream
BOOST_BEAST_DECL
state(
- net::io_context& ioc_,
+ executor_type exec_,
boost::weak_ptr<service_impl> wp_,
fail_count* fc_);
@@ -192,7 +217,7 @@ class stream
template<class>
friend class boost::asio::ssl::stream;
// DEPRECATED
- using lowest_layer_type = stream;
+ using lowest_layer_type = basic_stream;
// DEPRECATED
lowest_layer_type&
lowest_layer() noexcept
@@ -221,7 +246,7 @@ public:
when performing any reads or writes.
*/
BOOST_BEAST_DECL
- ~stream();
+ ~basic_stream();
/** Move Constructor
@@ -229,7 +254,7 @@ public:
results in undefined behavior.
*/
BOOST_BEAST_DECL
- stream(stream&& other);
+ basic_stream(basic_stream&& other);
/** Move Assignment
@@ -237,8 +262,8 @@ public:
results in undefined behavior.
*/
BOOST_BEAST_DECL
- stream&
- operator=(stream&& other);
+ basic_stream&
+ operator=(basic_stream&& other);
/** Construct a stream
@@ -249,7 +274,18 @@ public:
*/
BOOST_BEAST_DECL
explicit
- stream(net::io_context& ioc);
+ basic_stream(net::io_context& ioc);
+
+ /** Construct a stream
+
+ The stream will be created in a disconnected state.
+
+ @param exec The `executor` object that the stream will use to
+ dispatch handlers for any asynchronous operations.
+ */
+ BOOST_BEAST_DECL
+ explicit
+ basic_stream(executor_type exec);
/** Construct a stream
@@ -264,7 +300,7 @@ public:
a simulated failure error will be raised.
*/
BOOST_BEAST_DECL
- stream(
+ basic_stream(
net::io_context& ioc,
fail_count& fc);
@@ -279,7 +315,7 @@ public:
including the null terminator.
*/
BOOST_BEAST_DECL
- stream(
+ basic_stream(
net::io_context& ioc,
string_view s);
@@ -299,7 +335,7 @@ public:
including the null terminator.
*/
BOOST_BEAST_DECL
- stream(
+ basic_stream(
net::io_context& ioc,
fail_count& fc,
string_view s);
@@ -307,17 +343,13 @@ public:
/// Establish a connection
BOOST_BEAST_DECL
void
- connect(stream& remote);
-
- /// The type of the executor associated with the object.
- using executor_type =
- net::io_context::executor_type;
+ connect(basic_stream& remote);
/// Return the executor associated with the object.
executor_type
get_executor() noexcept
{
- return in_->ioc.get_executor();
+ return in_->exec;
};
/// Set the maximum number of bytes returned by read_some
@@ -563,28 +595,26 @@ public:
#if ! BOOST_BEAST_DOXYGEN
friend
- BOOST_BEAST_DECL
void
- teardown(
+ teardown<>(
role_type,
- stream& s,
+ basic_stream& s,
boost::system::error_code& ec);
- template<class TeardownHandler>
+ template<class Ex2, class TeardownHandler>
friend
- BOOST_BEAST_DECL
void
async_teardown(
role_type role,
- stream& s,
+ basic_stream<Ex2>& s,
TeardownHandler&& handler);
#endif
};
#if ! BOOST_BEAST_DOXYGEN
-inline
+template<class Executor>
void
-beast_close_socket(stream& s)
+beast_close_socket(basic_stream<Executor>& s)
{
s.close();
}
@@ -599,31 +629,34 @@ beast_close_socket(stream& s)
@return The new, connected stream.
*/
+template<class Executor>
template<class... Args>
-stream
-connect(stream& to, Args&&... args);
+bascic_stream
+connect(basic_stream& to, Args&&... args);
#else
-BOOST_BEAST_DECL
-stream
-connect(stream& to);
+template<class Executor>
+basic_stream<Executor>
+connect(basic_stream<Executor>& to);
-BOOST_BEAST_DECL
+template<class Executor>
void
-connect(stream& s1, stream& s2);
+connect(basic_stream<Executor>& s1, basic_stream<Executor>& s2);
-template<class Arg1, class... ArgN>
-stream
-connect(stream& to, Arg1&& arg1, ArgN&&... argn);
+template<class Executor, class Arg1, class... ArgN>
+basic_stream<Executor>
+connect(basic_stream<Executor>& to, Arg1&& arg1, ArgN&&... argn);
#endif
+using stream = basic_stream<>;
+
} // test
} // beast
} // boost
#include <boost/beast/_experimental/test/impl/stream.hpp>
-#ifdef BOOST_BEAST_HEADER_ONLY
+//#ifdef BOOST_BEAST_HEADER_ONLY
#include <boost/beast/_experimental/test/impl/stream.ipp>
-#endif
+//#endif
#endif
diff --git a/test/beast/core/async_base.cpp b/test/beast/core/async_base.cpp
index 2125d4e77..84193690e 100644
--- a/test/beast/core/async_base.cpp
+++ b/test/beast/core/async_base.cpp
@@ -716,9 +716,9 @@ public:
net::steady_timer timer;
- temporary_data(std::string message_, net::io_context& ctx)
+ temporary_data(std::string message_, net::any_io_executor ex)
: message(std::move(message_))
- , timer(ctx)
+ , timer(std::move(ex))
{
}
};
@@ -733,7 +733,7 @@ public:
, repeats_(repeats)
, data_(allocate_stable<temporary_data>(*this,
std::move(message),
- net::query(stream.get_executor(), net::execution::context)))
+ stream.get_executor()))
{
(*this)(); // start the operation
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment