Use async_initiate

This commit is contained in:
Vinnie Falco committed 2019-02-21 06:39:35 -08:00
1 parent e2d3f741b8
commit d08cc82e15
26 files changed
+1216 -590

No files matched your search

@@ -95,8 +95,7 @@ class buffered_read_stream
net::is_dynamic_buffer<DynamicBuffer>::value,
"DynamicBuffer type requirements not met");
template<class Buffers, class Handler>
class read_some_op;
struct ops;
DynamicBuffer buffer_;
std::size_t capacity_ = 0;
+130 -95
View File
@@ -26,6 +26,9 @@ static std::size_t constexpr default_max_stack_buffer = 16384;
//------------------------------------------------------------------------------
struct dynamic_read_ops
{
// read into a dynamic buffer until the
// condition is met or an error occurs
template<
@@ -46,19 +49,19 @@ class read_op
public:
read_op(read_op&&) = default;
template<class Handler_>
template<class Handler_, class Condition_>
read_op(
Handler_&& h,
Stream& s,
DynamicBuffer& b,
Condition cond,
Handler_&& h)
Condition_&& cond)
: async_op_base<Handler,
beast::executor_type<Stream>>(
std::forward<Handler_>(h),
s.get_executor())
, s_(s)
, b_(b)
, cond_(cond)
, cond_(std::forward<Condition_>(cond))
{
(*this)({}, 0, false);
}
@@ -100,16 +103,16 @@ public:
// EXPERIMENTAL
// optimized non-blocking read algorithm
template<
class Protocol,
class Protocol, class Executor,
class DynamicBuffer,
class Condition,
class Handler>
class read_non_blocking_op
: public net::coroutine
, public async_op_base<Handler,
beast::executor_type<net::basic_stream_socket<Protocol>>>
, public async_op_base<Handler, Executor>
{
net::basic_stream_socket<Protocol>& s_;
net::basic_stream_socket<
Protocol, Executor>& s_;
DynamicBuffer& b_;
Condition cond_;
std::size_t limit_;
@@ -119,18 +122,18 @@ public:
read_non_blocking_op(read_non_blocking_op&&) = default;
read_non_blocking_op(read_non_blocking_op const&) = delete;
template<class Handler_>
template<class Handler_, class Condition_>
read_non_blocking_op(
net::basic_stream_socket<Protocol>& s,
Handler_&& h,
net::basic_stream_socket<
Protocol, Executor>& s,
DynamicBuffer& b,
Condition cond,
Handler_&& h)
: async_op_base<Handler, beast::executor_type<
net::basic_stream_socket<Protocol>>>(
s.get_executor(), std::forward<Handler_>(h))
Condition cond)
: async_op_base<Handler, Executor>(
s.get_executor(), std::forward<Handler_>(h))
, s_(s)
, b_(b)
, cond_(cond)
, cond_(std::forward<Condition_>(cond))
{
(*this)({}, false);
}
@@ -198,92 +201,91 @@ public:
#endif
//------------------------------------------------------------------------------
template<
class AsyncReadStream,
class DynamicBuffer,
class CompletionCondition,
class ReadHandler,
class>
BOOST_ASIO_INITFN_RESULT_TYPE(
ReadHandler, void(error_code, std::size_t))
async_read(
AsyncReadStream& stream,
DynamicBuffer& buffer,
CompletionCondition cond,
ReadHandler&& handler)
struct run_read_op
{
static_assert(is_async_read_stream<AsyncReadStream>::value,
"AsyncReadStream type requirements not met");
static_assert(
net::is_dynamic_buffer<DynamicBuffer>::value,
"DynamicBuffer type requirements not met");
static_assert(
detail::is_invocable<CompletionCondition,
void(error_code&, std::size_t, DynamicBuffer&)>::value,
"CompletionCondition type requirements not met");
BOOST_BEAST_HANDLER_INIT(
ReadHandler, void(error_code, std::size_t));
detail::read_op<
AsyncReadStream,
DynamicBuffer,
CompletionCondition,
BOOST_ASIO_HANDLER_TYPE(
ReadHandler, void(error_code, std::size_t))>(
stream, buffer, std::move(cond),
std::move(init.completion_handler));
return init.result.get();
}
template<
class AsyncReadStream,
class DynamicBuffer,
class Condition,
class ReadHandler>
void
operator()(
ReadHandler&& h,
AsyncReadStream& s,
DynamicBuffer& b,
Condition&& c)
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for the handler.
#ifdef BOOST_BEAST_ENABLE_NON_BLOCKING
template<
class Protocol,
class DynamicBuffer,
class CompletionCondition,
class ReadHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(
ReadHandler, void(error_code, std::size_t))
async_read(
net::basic_stream_socket<Protocol>& socket,
DynamicBuffer& buffer,
CompletionCondition cond,
ReadHandler&& handler)
{
static_assert(
net::is_dynamic_buffer<DynamicBuffer>::value,
"DynamicBuffer type requirements not met");
static_assert(
detail::is_invocable<CompletionCondition,
void(error_code&, std::size_t, DynamicBuffer&)>::value,
"CompletionCondition type requirements not met");
BOOST_BEAST_HANDLER_INIT(
ReadHandler, void(error_code, std::size_t));
if(socket.non_blocking())
{
detail::read_non_blocking_op<
Protocol,
static_assert(
beast::detail::is_invocable<ReadHandler,
void(error_code, std::size_t)>::value,
"ReadHandler type requirements not met");
read_op<
AsyncReadStream,
DynamicBuffer,
CompletionCondition,
BOOST_ASIO_HANDLER_TYPE(
ReadHandler, void(error_code, std::size_t))>(
socket, buffer, std::move(cond),
std::move(init.completion_handler));
typename std::decay<Condition>::type,
typename std::decay<ReadHandler>::type>(
std::forward<ReadHandler>(h),
s,
b,
std::forward<Condition>(c));
}
else
#if BOOST_BEAST_ENABLE_NON_BLOCKING
template<
class Protocol, class Executor,
class DynamicBuffer,
class Condition,
class ReadHandler>
void
operator()(
net::basic_stream_socket<Protocol, Executor>& s,
DynamicBuffer& b,
Condition&& c,
ReadHandler&& h)
{
detail::read_op<
decltype(socket),
DynamicBuffer,
CompletionCondition,
BOOST_ASIO_HANDLER_TYPE(
ReadHandler, void(error_code, std::size_t))>(
socket, buffer, std::move(cond),
std::move(init.completion_handler));
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for the handler.
static_assert(
beast::detail::is_invocable<ReadHandler,
void(error_code, std::size_t)>::value,
"ReadHandler type requirements not met");
if(s.non_blocking())
{
read_non_blocking_op<
Protocol, Executor,
DynamicBuffer,
typename std::decay<Condition>::type,
typename std::decay<ReadHandler>::type>(
std::forward<ReadHandler>(h),
s,
b,
std::forward<Condition>(c));
}
else
{
read_op<
net::basic_stream_socket<Protocol, Executor>,
DynamicBuffer,
typename std::decay<Condition>::type,
typename std::decay<ReadHandler>::type>(
std::forward<ReadHandler>(h),
s,
b,
std::forward<Condition>(c));
}
}
return init.result.get();
}
#endif
};
};
//------------------------------------------------------------------------------
@@ -444,6 +446,39 @@ read(
}
#endif
template<
class AsyncReadStream,
class DynamicBuffer,
class CompletionCondition,
class ReadHandler,
class>
BOOST_ASIO_INITFN_RESULT_TYPE(
ReadHandler, void(error_code, std::size_t))
async_read(
AsyncReadStream& stream,
DynamicBuffer& buffer,
CompletionCondition&& cond,
ReadHandler&& handler)
{
static_assert(is_async_read_stream<AsyncReadStream>::value,
"AsyncReadStream type requirements not met");
static_assert(
net::is_dynamic_buffer<DynamicBuffer>::value,
"DynamicBuffer type requirements not met");
static_assert(
detail::is_invocable<CompletionCondition,
void(error_code&, std::size_t, DynamicBuffer&)>::value,
"CompletionCondition type requirements not met");
return net::async_initiate<
ReadHandler,
void(error_code, std::size_t)>(
typename dynamic_read_ops::run_read_op{},
handler,
stream,
buffer,
std::forward<CompletionCondition>(cond));
}
} // detail
} // beast
} // boost
+1 -1
View File
@@ -235,7 +235,7 @@ BOOST_ASIO_INITFN_RESULT_TYPE(
async_read(
AsyncReadStream& stream,
DynamicBuffer& buffer,
CompletionCondition completion_condition,
CompletionCondition&& completion_condition,
ReadHandler&& handler);
} // detail
@@ -124,20 +124,6 @@ struct is_contiguous_container<T, E, void_t<
>::type>>: std::true_type
{};
//------------------------------------------------------------------------------
/* If this static assert goes off, it means that the completion
handler you provided to an asynchronous initiating function did
not have the right signature. Check the parameter types for your
completion handler and make sure they match the list of types
expected by the initiating function,
*/
#define BOOST_BEAST_HANDLER_INIT(type, sig) \
static_assert(::boost::beast::detail::is_invocable< \
BOOST_ASIO_HANDLER_TYPE(type, sig), sig>::value, \
"CompletionHandler type requirements not met"); \
::boost::beast::net::async_completion<type, sig> init{handler}
} // detail
} // beast
} // boost
+2 -2
View File
@@ -90,13 +90,13 @@ class flat_stream
: private detail::flat_stream_base
#endif
{
template<class> class write_op;
NextLayer stream_;
flat_buffer buffer_;
BOOST_STATIC_ASSERT(has_get_executor<NextLayer>::value);
struct ops;
template<class ConstBufferSequence>
std::size_t
stack_write_some(
+16 -7
View File
@@ -539,16 +539,17 @@ struct run_read_op
operator()(
ReadHandler&& h,
basic_stream& s,
Buffers const& b
)
Buffers const& b)
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for a ReadHandler.
// requirements for the handler.
static_assert(
detail::is_invocable<ReadHandler,
void(error_code, std::size_t)>::value,
"ReadHandler type requirements not met");
transfer_op<
true,
Buffers,
@@ -568,11 +569,13 @@ struct run_write_op
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for a WriteHandler.
// requirements for the handler.
static_assert(
detail::is_invocable<WriteHandler,
void(error_code, std::size_t)>::value,
"WriteHandler type requirements not met");
transfer_op<
false,
Buffers,
@@ -592,11 +595,13 @@ struct run_connect_op
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for a ConnectHandler.
// requirements for the handler.
static_assert(
detail::is_invocable<ConnectHandler,
void(error_code)>::value,
"ConnectHandler type requirements not met");
connect_op<typename std::decay<ConnectHandler>::type>(
std::forward<ConnectHandler>(h), s, ep);
}
@@ -617,11 +622,13 @@ struct run_connect_range_op
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for a RangeConnectHandler.
// requirements for the handler.
static_assert(
detail::is_invocable<RangeConnectHandler,
void(error_code, typename Protocol::endpoint)>::value,
"RangeConnectHandler type requirements not met");
connect_op<typename std::decay<RangeConnectHandler>::type>(
std::forward<RangeConnectHandler>(h), s, eps, cond);
}
@@ -642,11 +649,13 @@ struct run_connect_iter_op
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for a IteratorConnectHandler.
// requirements for the handler.
static_assert(
detail::is_invocable<IteratorConnectHandler,
void(error_code, Iterator)>::value,
"IteratorConnectHandler type requirements not met");
connect_op<typename std::decay<IteratorConnectHandler>::type>(
std::forward<IteratorConnectHandler>(h), s, begin, end, cond);
}
@@ -22,23 +22,26 @@
namespace boost {
namespace beast {
template<class Stream, class DynamicBuffer>
struct buffered_read_stream<Stream, DynamicBuffer>::ops
{
template<class MutableBufferSequence, class Handler>
class buffered_read_stream<
Stream, DynamicBuffer>::read_some_op
: public async_op_base<
Handler, beast::executor_type<buffered_read_stream>>
class read_op
: public async_op_base<Handler,
beast::executor_type<buffered_read_stream>>
{
buffered_read_stream& s_;
MutableBufferSequence b_;
int step_ = 0;
public:
read_some_op(read_some_op&&) = default;
read_some_op(read_some_op const&) = delete;
read_op(read_op&&) = default;
read_op(read_op const&) = delete;
template<class Handler_>
read_some_op(
read_op(
Handler_&& h,
buffered_read_stream& s,
MutableBufferSequence const& b)
@@ -56,6 +59,7 @@ public:
error_code ec,
std::size_t bytes_transferred)
{
// VFALCO TODO Rewrite this using reenter/yield
switch(step_)
{
case 0:
@@ -99,6 +103,35 @@ public:
}
};
struct run_read_op
{
template<class ReadHandler, class Buffers>
void
operator()(
ReadHandler&& h,
buffered_read_stream& s,
Buffers const& b)
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for the handler.
static_assert(
beast::detail::is_invocable<ReadHandler,
void(error_code, std::size_t)>::value,
"ReadHandler type requirements not met");
read_op<
Buffers,
typename std::decay<ReadHandler>::type>(
std::forward<ReadHandler>(h),
s,
b);
}
};
};
//------------------------------------------------------------------------------
template<class Stream, class DynamicBuffer>
@@ -198,12 +231,13 @@ async_read_some(
if(buffer_.size() == 0 && capacity_ == 0)
return next_layer_.async_read_some(buffers,
std::forward<ReadHandler>(handler));
BOOST_BEAST_HANDLER_INIT(
ReadHandler, void(error_code, std::size_t));
read_some_op<MutableBufferSequence, BOOST_ASIO_HANDLER_TYPE(
ReadHandler, void(error_code, std::size_t))>(
std::move(init.completion_handler), *this, buffers);
return init.result.get();
return net::async_initiate<
ReadHandler,
void(error_code, std::size_t)>(
typename ops::run_read_op{},
handler,
*this,
buffers);
}
} // beast
+41 -9
View File
@@ -23,8 +23,11 @@ namespace boost {
namespace beast {
template<class NextLayer>
struct flat_stream<NextLayer>::ops
{
template<class Handler>
class flat_stream<NextLayer>::write_op
class write_op
: public async_op_base<Handler,
beast::executor_type<flat_stream>>
, public net::coroutine
@@ -34,9 +37,9 @@ public:
class ConstBufferSequence,
class Handler_>
write_op(
Handler_&& h,
flat_stream<NextLayer>& s,
ConstBufferSequence const& b,
Handler_&& h)
ConstBufferSequence const& b)
: async_op_base<Handler,
beast::executor_type<flat_stream>>(
std::forward<Handler_>(h),
@@ -72,6 +75,34 @@ public:
}
};
struct run_write_op
{
template<class WriteHandler, class Buffers>
void
operator()(
WriteHandler&& h,
flat_stream& s,
Buffers const& b)
{
// If you get an error on the following line it means
// that your handler does not meet the documented type
// requirements for the handler.
static_assert(
beast::detail::is_invocable<WriteHandler,
void(error_code, std::size_t)>::value,
"WriteHandler type requirements not met");
write_op<
typename std::decay<WriteHandler>::type>(
std::forward<WriteHandler>(h),
s,
b);
}
};
};
//------------------------------------------------------------------------------
template<class NextLayer>
@@ -212,12 +243,13 @@ async_write_some(
static_assert(net::is_const_buffer_sequence<
ConstBufferSequence>::value,
"ConstBufferSequence type requirements not met");
BOOST_BEAST_HANDLER_INIT(
WriteHandler, void(error_code, std::size_t));
write_op<BOOST_ASIO_HANDLER_TYPE(
WriteHandler, void(error_code, std::size_t))>(
*this, buffers, std::move(init.completion_handler));
return init.result.get();
return net::async_initiate<
WriteHandler,
void(error_code, std::size_t)>(
typename ops::run_write_op{},
handler,
*this,
buffers);
}
template<class NextLayer>