forked from boostorg/beast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_base.cpp
More file actions
762 lines (665 loc) · 22 KB
/
Copy pathasync_base.cpp
File metadata and controls
762 lines (665 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
//
// 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
//
// Test that header file is self-contained.
#include <boost/beast/core/async_base.hpp>
#include "test_handler.hpp"
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/beast/_experimental/test/handler.hpp>
#include <boost/beast/_experimental/test/stream.hpp>
#include <boost/beast/core/error.hpp>
#include <boost/asio/async_result.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/system_executor.hpp>
#include <boost/asio/write.hpp>
#include <boost/core/ignore_unused.hpp>
#include <stdexcept>
//------------------------------------------------------------------------------
namespace boost {
namespace beast {
namespace {
struct ex1_type
{
void* context() { return nullptr; }
void on_work_started() {}
void on_work_finished() {}
template<class F> void dispatch(F&&) {}
template<class F> void post(F&&) {}
template<class F> void defer(F&&) {}
};
struct no_alloc
{
};
struct nested_alloc
{
struct allocator_type
{
};
};
struct intrusive_alloc
{
struct allocator_type
{
};
};
struct no_ex
{
using executor_type = net::system_executor;
};
struct nested_ex
{
struct executor_type
{
};
};
struct intrusive_ex
{
struct executor_type
{
};
};
template<class E, class A>
struct handler;
template<>
struct handler<no_ex, no_alloc>
{
};
template<>
struct handler<no_ex, nested_alloc>
: nested_alloc
{
};
template<>
struct handler<no_ex, intrusive_alloc>
{
};
template<>
struct handler<nested_ex, no_alloc>
: nested_ex
{
};
template<>
struct handler<intrusive_ex, no_alloc>
{
};
} // (anon)
} // beast
} // boost
namespace boost {
namespace asio {
template<class Allocator>
struct associated_allocator<
boost::beast::handler<
boost::beast::no_ex,
boost::beast::intrusive_alloc>,
Allocator>
{
using type =
boost::beast::intrusive_alloc::allocator_type;
static type get(
boost::beast::handler<
boost::beast::no_ex,
boost::beast::intrusive_alloc> const& h,
Allocator const& a = Allocator()) noexcept
{
return type{};
}
};
template<class Executor>
struct associated_executor<
boost::beast::handler<
boost::beast::intrusive_ex,
boost::beast::no_alloc>,
Executor>
{
using type =
boost::beast::intrusive_ex::executor_type;
static type get(
boost::beast::handler<
boost::beast::intrusive_ex,
boost::beast::no_alloc> const& h,
Executor const& a = Executor()) noexcept
{
return type{};
}
};
} // asio
} // boost
//------------------------------------------------------------------------------
namespace boost {
namespace beast {
class async_base_test : public beast::unit_test::suite
{
public:
// no associated allocator
BOOST_STATIC_ASSERT(
std::is_same<
std::allocator<void>,
net::associated_allocator_t<
async_base<
handler<no_ex, no_alloc>,
net::io_context::executor_type>
>>::value);
BOOST_STATIC_ASSERT(
std::is_same<
std::allocator<int>,
net::associated_allocator_t<
async_base<
handler<no_ex, no_alloc>,
net::io_context::executor_type,
std::allocator<int>>
>>::value);
BOOST_STATIC_ASSERT(
std::is_same<
std::allocator<void>,
net::associated_allocator_t<
async_base<
handler<no_ex, no_alloc>,
net::io_context::executor_type>,
std::allocator<int> // ignored
>>::value);
BOOST_STATIC_ASSERT(
std::is_same<
std::allocator<int>,
net::associated_allocator_t<
async_base<
handler<no_ex, no_alloc>,
net::io_context::executor_type,
std::allocator<int>>,
std::allocator<double> // ignored
>>::value);
// nested associated allocator
BOOST_STATIC_ASSERT(
std::is_same<
nested_alloc::allocator_type,
net::associated_allocator_t<
async_base<
handler<no_ex, nested_alloc>,
net::io_context::executor_type>
>>::value);
BOOST_STATIC_ASSERT(
std::is_same<
nested_alloc::allocator_type,
net::associated_allocator_t<
async_base<
handler<no_ex, nested_alloc>,
net::io_context::executor_type,
std::allocator<int>> // ignored
>>::value);
BOOST_STATIC_ASSERT(
std::is_same<
nested_alloc::allocator_type,
net::associated_allocator_t<
async_base<
handler<no_ex, nested_alloc>,
net::io_context::executor_type>,
std::allocator<int> // ignored
>>::value);
BOOST_STATIC_ASSERT(
std::is_same<
nested_alloc::allocator_type,
net::associated_allocator_t<
async_base<
handler<no_ex, nested_alloc>,
net::io_context::executor_type,
std::allocator<int>>, // ignored
std::allocator<int> // ignored
>>::value);
// intrusive associated allocator
BOOST_STATIC_ASSERT(
std::is_same<
intrusive_alloc::allocator_type,
net::associated_allocator_t<
async_base<
handler<no_ex, intrusive_alloc>,
net::io_context::executor_type>
>>::value);
BOOST_STATIC_ASSERT(
std::is_same<
intrusive_alloc::allocator_type,
net::associated_allocator_t<
async_base<
handler<no_ex, intrusive_alloc>,
net::io_context::executor_type,
std::allocator<int>> // ignored
>>::value);
BOOST_STATIC_ASSERT(
std::is_same<
intrusive_alloc::allocator_type,
net::associated_allocator_t<
async_base<
handler<no_ex, intrusive_alloc>,
net::io_context::executor_type>,
std::allocator<int> // ignored
>>::value);
BOOST_STATIC_ASSERT(
std::is_same<
intrusive_alloc::allocator_type,
net::associated_allocator_t<
async_base<
handler<no_ex, intrusive_alloc>,
net::io_context::executor_type,
std::allocator<int>>, // ignored
std::allocator<int> // ignored
>>::value);
// no associated executor
BOOST_STATIC_ASSERT(
std::is_same<
ex1_type,
net::associated_executor_t<
async_base<
handler<no_ex, no_alloc>,
ex1_type>
>>::value);
BOOST_STATIC_ASSERT(
std::is_same<
ex1_type,
net::associated_executor_t<
async_base<
handler<no_ex, no_alloc>,
ex1_type>,
net::system_executor // ignored
>>::value);
// nested associated executor
BOOST_STATIC_ASSERT(
std::is_same<
nested_ex::executor_type,
net::associated_executor_t<
async_base<
handler<nested_ex, no_alloc>,
ex1_type>
>>::value);
BOOST_STATIC_ASSERT(
std::is_same<
nested_ex::executor_type,
net::associated_executor_t<
async_base<
handler<nested_ex, no_alloc>,
ex1_type>,
net::system_executor // ignored
>>::value);
// intrusive associated executor
BOOST_STATIC_ASSERT(
std::is_same<
intrusive_ex::executor_type,
net::associated_executor_t<
async_base<
handler<intrusive_ex, no_alloc>,
ex1_type>
>>::value);
BOOST_STATIC_ASSERT(
std::is_same<
intrusive_ex::executor_type,
net::associated_executor_t<
async_base<
handler<intrusive_ex, no_alloc>,
ex1_type>,
net::system_executor // ignored
>>::value);
struct final_handler
{
bool& invoked;
void
operator()()
{
invoked = true;
}
};
void
testBase()
{
// get_allocator
{
simple_allocator alloc;
simple_allocator alloc2;
async_base<
move_only_handler,
simple_executor,
simple_allocator> op(
move_only_handler{}, {}, alloc);
BEAST_EXPECT(op.get_allocator() == alloc);
BEAST_EXPECT(op.get_allocator() != alloc2);
}
// get_executor
{
simple_executor ex;
simple_executor ex2;
async_base<
move_only_handler,
simple_executor> op(
move_only_handler{}, ex);
BEAST_EXPECT(op.get_executor() == ex);
BEAST_EXPECT(op.get_executor() != ex2);
}
// move construction
{
async_base<
move_only_handler,
simple_executor> op(
move_only_handler{}, {});
auto op2 = std::move(op);
}
// observers
{
bool b = false;
async_base<
legacy_handler,
simple_executor> op(
legacy_handler{b}, {});
BEAST_EXPECT(! op.handler().hook_invoked);
b = true;
BEAST_EXPECT(op.handler().hook_invoked);
b = false;
BEAST_EXPECT(! op.release_handler().hook_invoked);
}
// invocation
{
net::io_context ioc;
async_base<
test::handler,
net::io_context::executor_type> op(
test::any_handler(), ioc.get_executor());
op.complete(true);
}
{
net::io_context ioc;
async_base<
test::handler,
net::io_context::executor_type> op(
test::any_handler(), ioc.get_executor());
op.complete(false);
ioc.run();
}
{
async_base<
test::handler,
simple_executor> op(
test::any_handler(), {});
op.complete_now();
}
// legacy hooks
legacy_handler::test(
[](legacy_handler h)
{
return async_base<
legacy_handler,
simple_executor>(
std::move(h), {});
});
}
void
testStableBase()
{
// get_allocator
{
simple_allocator alloc;
simple_allocator alloc2;
stable_async_base<
move_only_handler,
simple_executor,
simple_allocator> op(
move_only_handler{}, {}, alloc);
BEAST_EXPECT(op.get_allocator() == alloc);
BEAST_EXPECT(op.get_allocator() != alloc2);
}
// get_executor
{
simple_executor ex;
simple_executor ex2;
stable_async_base<
move_only_handler,
simple_executor> op(
move_only_handler{}, ex);
BEAST_EXPECT(op.get_executor() == ex);
BEAST_EXPECT(op.get_executor() != ex2);
}
// move construction
{
stable_async_base<
move_only_handler,
simple_executor> op(
move_only_handler{}, {});
auto op2 = std::move(op);
}
// invocation
{
net::io_context ioc;
stable_async_base<
test::handler,
net::io_context::executor_type> op(
test::any_handler(), ioc.get_executor());
op.complete(true);
}
{
net::io_context ioc1;
net::io_context ioc2;
auto h = net::bind_executor(ioc2, test::any_handler());
stable_async_base<
decltype(h),
net::io_context::executor_type> op(
std::move(h),
ioc1.get_executor());
op.complete(false);
BEAST_EXPECT(ioc1.run() == 0);
BEAST_EXPECT(ioc2.run() == 1);
}
{
stable_async_base<
test::handler,
simple_executor> op(
test::any_handler(), {});
op.complete_now();
}
// legacy hooks
legacy_handler::test(
[](legacy_handler h)
{
return stable_async_base<
legacy_handler,
simple_executor>(
std::move(h), {});
});
// allocate_stable
{
bool destroyed = false;
{
struct data
{
bool& destroyed;
~data()
{
destroyed = true;
}
};
stable_async_base<
move_only_handler,
simple_executor> op(
move_only_handler{}, {});
BEAST_EXPECT(! destroyed);
auto& d = allocate_stable<data>(op, destroyed);
BEAST_EXPECT(! d.destroyed);
}
BEAST_EXPECT(destroyed);
}
{
struct throwing_data
{
throwing_data()
{
BOOST_THROW_EXCEPTION(
std::exception{});
}
};
stable_async_base<
move_only_handler,
simple_executor> op(
move_only_handler{}, {});
try
{
allocate_stable<throwing_data>(op);
fail();
}
catch(std::exception const&)
{
pass();
}
}
}
//--------------------------------------------------------------------------
// Asynchronously read into a buffer until the buffer is full, or an error occurs
template<class AsyncReadStream, class ReadHandler>
typename net::async_result<ReadHandler, void(error_code, std::size_t)>::return_type
async_read(AsyncReadStream& stream, net::mutable_buffer buffer, ReadHandler&& handler)
{
using handler_type = BOOST_ASIO_HANDLER_TYPE(ReadHandler, void(error_code, std::size_t));
using base_type = async_base<handler_type, typename AsyncReadStream::executor_type>;
struct op : base_type
{
AsyncReadStream& stream_;
net::mutable_buffer buffer_;
std::size_t total_bytes_transferred_;
op(
AsyncReadStream& stream,
net::mutable_buffer buffer,
handler_type& handler)
: base_type(std::move(handler), stream.get_executor())
, stream_(stream)
, buffer_(buffer)
, total_bytes_transferred_(0)
{
(*this)({}, 0, false); // start the operation
}
void operator()(error_code ec, std::size_t bytes_transferred, bool is_continuation = true)
{
// Adjust the count of bytes and advance our buffer
total_bytes_transferred_ += bytes_transferred;
buffer_ = buffer_ + bytes_transferred;
// Keep reading until buffer is full or an error occurs
if(! ec && buffer_.size() > 0)
return stream_.async_read_some(buffer_, std::move(*this));
// Call the completion handler with the result. If `is_continuation` is
// false, which happens on the first time through this function, then
// `net::post` will be used to call the completion handler, otherwise
// the completion handler will be invoked directly.
this->complete(is_continuation, ec, total_bytes_transferred_);
}
};
net::async_completion<ReadHandler, void(error_code, std::size_t)> init{handler};
op(stream, buffer, init.completion_handler);
return init.result.get();
}
// Asynchronously send a message multiple times, once per second
template <class AsyncWriteStream, class T, class WriteHandler>
auto async_write_messages(
AsyncWriteStream& stream,
T const& message,
std::size_t repeat_count,
WriteHandler&& handler) ->
typename net::async_result<
typename std::decay<WriteHandler>::type,
void(error_code)>::return_type
{
using handler_type = typename net::async_completion<WriteHandler, void(error_code)>::completion_handler_type;
using base_type = stable_async_base<handler_type, typename AsyncWriteStream::executor_type>;
struct op : base_type, boost::asio::coroutine
{
// This object must have a stable address
struct temporary_data
{
// Although std::string is in theory movable, most implementations
// use a "small buffer optimization" which means that we might
// be submitting a buffer to the write operation and then
// moving the string, invalidating the buffer. To prevent
// undefined behavior we store the string object itself at
// a stable location.
std::string const message;
net::steady_timer timer;
temporary_data(std::string message_, net::io_context& ctx)
: message(std::move(message_))
, timer(ctx)
{
}
};
AsyncWriteStream& stream_;
std::size_t repeats_;
temporary_data& data_;
op(AsyncWriteStream& stream, std::size_t repeats, std::string message, handler_type& handler)
: base_type(std::move(handler), stream.get_executor())
, stream_(stream)
, repeats_(repeats)
, data_(allocate_stable<temporary_data>(*this, std::move(message), stream.get_executor().context()))
{
(*this)(); // start the operation
}
// Including this file provides the keywords for macro-based coroutines
#include <boost/asio/yield.hpp>
void operator()(error_code ec = {}, std::size_t = 0)
{
reenter(*this)
{
// If repeats starts at 0 then we must complete immediately. But
// we can't call the final handler from inside the initiating
// function, so we post our intermediate handler first. We use
// net::async_write with an empty buffer instead of calling
// net::post to avoid an extra function template instantiation, to
// keep compile times lower and make the resulting executable smaller.
yield net::async_write(stream_, net::const_buffer{}, std::move(*this));
while(! ec && repeats_-- > 0)
{
// Send the string. We construct a `const_buffer` here to guarantee
// that we do not create an additional function template instantation
// of net::async_write, since we already instantiated it above for
// net::const_buffer.
yield net::async_write(stream_,
net::const_buffer(net::buffer(data_.message)), std::move(*this));
if(ec)
break;
// Set the timer and wait
data_.timer.expires_after(std::chrono::seconds(1));
yield data_.timer.async_wait(std::move(*this));
}
}
// The base class destroys the temporary data automatically,
// before invoking the final completion handler
this->complete_now(ec);
}
// Including this file undefines the macros for the coroutines
#include <boost/asio/unyield.hpp>
};
net::async_completion<WriteHandler, void(error_code)> completion(handler);
std::ostringstream os;
os << message;
op(stream, repeat_count, os.str(), completion.completion_handler);
return completion.result.get();
}
void
testJavadocs()
{
struct handler
{
void operator()(error_code = {}, std::size_t = 0)
{
}
};
BEAST_EXPECT((&async_base_test::async_read<test::stream, handler>));
BEAST_EXPECT((&async_base_test::async_write_messages<test::stream, std::string, handler>));
}
//--------------------------------------------------------------------------
void
run() override
{
testBase();
testStableBase();
testJavadocs();
}
};
BEAST_DEFINE_TESTSUITE(beast,core,async_base);
} // beast
} // boost