]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | // |
2 | // range_based_for.cpp | |
3 | // ~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
1e59de90 | 5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
11fdf7f2 TL |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
92f5a8d4 TL |
11 | #include <boost/asio/co_spawn.hpp> |
12 | #include <boost/asio/detached.hpp> | |
11fdf7f2 TL |
13 | #include <boost/asio/io_context.hpp> |
14 | #include <boost/asio/ip/tcp.hpp> | |
15 | #include <boost/asio/signal_set.hpp> | |
16 | #include <boost/asio/write.hpp> | |
17 | #include <cstdio> | |
18 | ||
19 | using boost::asio::ip::tcp; | |
92f5a8d4 TL |
20 | using boost::asio::awaitable; |
21 | using boost::asio::co_spawn; | |
22 | using boost::asio::detached; | |
23 | using boost::asio::use_awaitable; | |
11fdf7f2 TL |
24 | |
25 | class connection_iter | |
26 | { | |
27 | friend class connections; | |
28 | tcp::acceptor* acceptor_ = nullptr; | |
29 | tcp::socket socket_; | |
30 | ||
31 | connection_iter(tcp::acceptor& a, tcp::socket s) | |
32 | : acceptor_(&a), socket_(std::move(s)) {} | |
33 | ||
34 | public: | |
35 | tcp::socket operator*() | |
36 | { | |
37 | return std::move(socket_); | |
38 | } | |
39 | ||
40 | awaitable<void> operator++() | |
41 | { | |
92f5a8d4 | 42 | socket_ = co_await acceptor_->async_accept(use_awaitable); |
11fdf7f2 TL |
43 | } |
44 | ||
45 | bool operator==(const connection_iter&) const noexcept | |
46 | { | |
47 | return false; | |
48 | } | |
49 | ||
50 | bool operator!=(const connection_iter&) const noexcept | |
51 | { | |
52 | return true; | |
53 | } | |
54 | }; | |
55 | ||
56 | class connections | |
57 | { | |
58 | tcp::acceptor& acceptor_; | |
59 | ||
60 | public: | |
61 | explicit connections(tcp::acceptor& a) : acceptor_(a) {} | |
62 | ||
63 | awaitable<connection_iter> begin() | |
64 | { | |
92f5a8d4 | 65 | tcp::socket s = co_await acceptor_.async_accept(use_awaitable); |
11fdf7f2 TL |
66 | co_return connection_iter(acceptor_, std::move(s)); |
67 | } | |
68 | ||
69 | connection_iter end() | |
70 | { | |
71 | return connection_iter(acceptor_, | |
92f5a8d4 | 72 | tcp::socket(acceptor_.get_executor())); |
11fdf7f2 TL |
73 | } |
74 | }; | |
75 | ||
76 | awaitable<void> listener(tcp::acceptor acceptor) | |
77 | { | |
11fdf7f2 TL |
78 | for co_await (tcp::socket s : connections(acceptor)) |
79 | { | |
92f5a8d4 | 80 | co_await boost::asio::async_write(s, boost::asio::buffer("hello\r\n", 7), use_awaitable); |
11fdf7f2 TL |
81 | } |
82 | } | |
83 | ||
84 | int main() | |
85 | { | |
86 | try | |
87 | { | |
88 | boost::asio::io_context io_context(1); | |
89 | ||
90 | boost::asio::signal_set signals(io_context, SIGINT, SIGTERM); | |
91 | signals.async_wait([&](auto, auto){ io_context.stop(); }); | |
92 | ||
93 | tcp::acceptor acceptor(io_context, {tcp::v4(), 55555}); | |
20effc67 | 94 | co_spawn(io_context, listener(std::move(acceptor)), detached); |
11fdf7f2 TL |
95 | |
96 | io_context.run(); | |
97 | } | |
98 | catch (std::exception& e) | |
99 | { | |
100 | std::printf("Exception: %s\n", e.what()); | |
101 | } | |
102 | } |