Skip to content

Commit e7571c6

Browse files
committed
get_executor() works on initiation objects
This is required for the new `asio::cancel_at` and `asio::cancel_after` completion token adapters to work.
1 parent 1a2b85b commit e7571c6

File tree

16 files changed

+344
-158
lines changed

16 files changed

+344
-158
lines changed

include/boost/beast/_experimental/http/impl/icy_stream.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,20 @@ class read_op
167167

168168
struct run_read_op
169169
{
170+
icy_stream* self;
171+
172+
using executor_type = typename icy_stream::executor_type;
173+
174+
executor_type
175+
get_executor() const noexcept
176+
{
177+
return self->get_executor();
178+
}
179+
170180
template<class ReadHandler, class Buffers>
171181
void
172182
operator()(
173183
ReadHandler&& h,
174-
icy_stream* s,
175184
Buffers const& b)
176185
{
177186
// If you get an error on the following line it means
@@ -186,7 +195,7 @@ struct run_read_op
186195
read_op<
187196
Buffers,
188197
typename std::decay<ReadHandler>::type>(
189-
std::forward<ReadHandler>(h), *s, b);
198+
std::forward<ReadHandler>(h), *self, b);
190199
}
191200
};
192201

@@ -292,9 +301,8 @@ async_read_some(
292301
return net::async_initiate<
293302
ReadHandler,
294303
void(error_code, std::size_t)>(
295-
typename ops::run_read_op{},
304+
typename ops::run_read_op{this},
296305
handler,
297-
this,
298306
buffers);
299307
}
300308

include/boost/beast/_experimental/test/impl/stream.hpp

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,27 @@ namespace boost {
2727
namespace beast {
2828
namespace test {
2929

30-
//------------------------------------------------------------------------------
30+
namespace detail
31+
{
32+
template<class To>
33+
struct extract_executor_op
34+
{
35+
To operator()(net::any_io_executor& ex) const
36+
{
37+
assert(ex.template target<To>());
38+
return *ex.template target<To>();
39+
}
40+
};
41+
42+
template<>
43+
struct extract_executor_op<net::any_io_executor>
44+
{
45+
net::any_io_executor operator()(net::any_io_executor& ex) const
46+
{
47+
return ex;
48+
}
49+
};
50+
} // detail
3151

3252
template<class Executor>
3353
template<class Handler, class Buffers>
@@ -163,13 +183,22 @@ class basic_stream<Executor>::read_op : public detail::stream_read_op_base
163183
template<class Executor>
164184
struct basic_stream<Executor>::run_read_op
165185
{
186+
boost::shared_ptr<detail::stream_state> const& in;
187+
188+
using executor_type = typename basic_stream::executor_type;
189+
190+
executor_type
191+
get_executor() const noexcept
192+
{
193+
return detail::extract_executor_op<Executor>()(in->exec);
194+
}
195+
166196
template<
167197
class ReadHandler,
168198
class MutableBufferSequence>
169199
void
170200
operator()(
171201
ReadHandler&& h,
172-
boost::shared_ptr<detail::stream_state> const& in,
173202
MutableBufferSequence const& buffers)
174203
{
175204
// If you get an error on the following line it means
@@ -197,13 +226,22 @@ struct basic_stream<Executor>::run_read_op
197226
template<class Executor>
198227
struct basic_stream<Executor>::run_write_op
199228
{
229+
boost::shared_ptr<detail::stream_state> const& in_;
230+
231+
using executor_type = typename basic_stream::executor_type;
232+
233+
executor_type
234+
get_executor() const noexcept
235+
{
236+
return detail::extract_executor_op<Executor>()(in_->exec);
237+
}
238+
200239
template<
201240
class WriteHandler,
202241
class ConstBufferSequence>
203242
void
204243
operator()(
205244
WriteHandler&& h,
206-
boost::shared_ptr<detail::stream_state> in_,
207245
boost::weak_ptr<detail::stream_state> out_,
208246
ConstBufferSequence const& buffers)
209247
{
@@ -336,9 +374,8 @@ async_read_some(
336374
return net::async_initiate<
337375
ReadHandler,
338376
void(error_code, std::size_t)>(
339-
run_read_op{},
377+
run_read_op{in_},
340378
handler,
341-
in_,
342379
buffers);
343380
}
344381

@@ -420,9 +457,8 @@ async_write_some(
420457
return net::async_initiate<
421458
WriteHandler,
422459
void(error_code, std::size_t)>(
423-
run_write_op{},
460+
run_write_op{in_},
424461
handler,
425-
in_,
426462
out_,
427463
buffers);
428464
}
@@ -467,28 +503,6 @@ connect(stream& to, Arg1&& arg1, ArgN&&... argn)
467503
return from;
468504
}
469505

470-
namespace detail
471-
{
472-
template<class To>
473-
struct extract_executor_op
474-
{
475-
To operator()(net::any_io_executor& ex) const
476-
{
477-
assert(ex.template target<To>());
478-
return *ex.template target<To>();
479-
}
480-
};
481-
482-
template<>
483-
struct extract_executor_op<net::any_io_executor>
484-
{
485-
net::any_io_executor operator()(net::any_io_executor& ex) const
486-
{
487-
return ex;
488-
}
489-
};
490-
}
491-
492506
template<class Executor>
493507
auto basic_stream<Executor>::get_executor() noexcept -> executor_type
494508
{

include/boost/beast/core/detail/impl/read.hpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,26 @@ class read_op
104104

105105
//------------------------------------------------------------------------------
106106

107+
template <typename AsyncReadStream>
107108
struct run_read_op
108109
{
110+
AsyncReadStream* stream;
111+
112+
using executor_type = typename AsyncReadStream::executor_type;
113+
114+
executor_type
115+
get_executor() const noexcept
116+
{
117+
return stream->get_executor();
118+
}
119+
109120
template<
110-
class AsyncReadStream,
111121
class DynamicBuffer,
112122
class Condition,
113123
class ReadHandler>
114124
void
115125
operator()(
116126
ReadHandler&& h,
117-
AsyncReadStream* s,
118127
DynamicBuffer* b,
119128
Condition&& c)
120129
{
@@ -133,7 +142,7 @@ struct run_read_op
133142
typename std::decay<Condition>::type,
134143
typename std::decay<ReadHandler>::type>(
135144
std::forward<ReadHandler>(h),
136-
*s,
145+
*stream,
137146
*b,
138147
std::forward<Condition>(c));
139148
}
@@ -234,9 +243,8 @@ async_read(
234243
return net::async_initiate<
235244
ReadHandler,
236245
void(error_code, std::size_t)>(
237-
typename dynamic_read_ops::run_read_op{},
246+
typename dynamic_read_ops::run_read_op<AsyncReadStream>{&stream},
238247
handler,
239-
&stream,
240248
&buffer,
241249
std::forward<CompletionCondition>(cond));
242250
}

include/boost/beast/core/detect_ssl.hpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ class detect_ssl_op;
348348
// authors of composed operations need to write it this way to get the
349349
// very best performance, for example when using Coroutines TS (`co_await`).
350350

351+
template <typename AsyncReadStream>
351352
struct run_detect_ssl_op
352353
{
353354
// The implementation of `net::async_initiate` captures the
@@ -361,20 +362,29 @@ struct run_detect_ssl_op
361362
// token into the "real handler" which must have the correct
362363
// signature, in this case `void(error_code, boost::tri_bool)`.
363364

365+
AsyncReadStream* stream;
366+
367+
using executor_type = typename AsyncReadStream::executor_type;
368+
369+
executor_type
370+
get_executor() const noexcept
371+
{
372+
return stream->get_executor();
373+
}
374+
364375
template<
365376
class DetectHandler,
366-
class AsyncReadStream,
367377
class DynamicBuffer>
368-
void operator()(
378+
void
379+
operator()(
369380
DetectHandler&& h,
370-
AsyncReadStream* s, // references are passed as pointers
371381
DynamicBuffer* b)
372382
{
373383
detect_ssl_op<
374384
typename std::decay<DetectHandler>::type,
375385
AsyncReadStream,
376386
DynamicBuffer>(
377-
std::forward<DetectHandler>(h), *s, *b);
387+
std::forward<DetectHandler>(h), *stream, *b);
378388
}
379389
};
380390

@@ -423,9 +433,8 @@ async_detect_ssl(
423433
return net::async_initiate<
424434
CompletionToken,
425435
void(error_code, bool)>(
426-
detail::run_detect_ssl_op{},
436+
detail::run_detect_ssl_op<AsyncReadStream>{&stream},
427437
token,
428-
&stream, // pass the reference by pointer
429438
&buffer);
430439
}
431440

0 commit comments

Comments
 (0)