src/examples/cpp03/multicast/sender.cpp | src/examples/cpp11/multicast/sender.cpp |
⋮ | ⋮ |
1 | // | 1 | // |
2 | //·sender.cpp | 2 | //·sender.cpp |
3 | //·~~~~~~~~~~ | 3 | //·~~~~~~~~~~ |
4 | // | 4 | // |
5 | //·Copyright·(c)·2003-2023·Christopher·M.·Kohlhoff·(chris·at·kohlhoff·dot·com) | 5 | //·Copyright·(c)·2003-2023·Christopher·M.·Kohlhoff·(chris·at·kohlhoff·dot·com) |
6 | // | 6 | // |
7 | //·Distributed·under·the·Boost·Software·License,·Version·1.0.·(See·accompanying | 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) | 8 | //·file·LICENSE_1_0.txt·or·copy·at·http://www.boost.org/LICENSE_1_0.txt) |
9 | // | 9 | // |
10 | | 10 | |
11 | #include·<iostream> | 11 | #include·<iostream> |
12 | #include·<sstream> | 12 | #include·<sstream> |
13 | #include·<string> | 13 | #include·<string> |
14 | #include·"asio.hpp" | 14 | #include·"asio.hpp" |
15 | #include·"boost/bind/bind.hpp" | |
16 | | 15 | |
17 | const·short·multicast_port·=·30001; | 16 | constexpr·short·multicast_port·=·30001; |
18 | const·int·max_message_count·=·10; | 17 | constexpr·int·max_message_count·=·10; |
19 | | 18 | |
20 | class·sender | 19 | class·sender |
21 | { | 20 | { |
22 | public: | 21 | public: |
23 | ··sender(asio::io_context&·io_context, | 22 | ··sender(asio::io_context&·io_context, |
24 | ······const·asio::ip::address&·multicast_address) | 23 | ······const·asio::ip::address&·multicast_address) |
25 | ····:·endpoint_(multicast_address,·multicast_port), | 24 | ····:·endpoint_(multicast_address,·multicast_port), |
26 | ······socket_(io_context,·endpoint_.protocol()), | 25 | ······socket_(io_context,·endpoint_.protocol()), |
27 | ······timer_(io_context), | 26 | ······timer_(io_context), |
28 | ······message_count_(0) | 27 | ······message_count_(0) |
29 | ··{ | 28 | ··{ |
| 29 | ····do_send(); |
| 30 | ··} |
| 31 | |
| 32 | private: |
| 33 | ··void·do_send() |
| 34 | ··{ |
30 | ····std::ostringstream·os; | 35 | ····std::ostringstream·os; |
31 | ····os·<<·"Message·"·<<·message_count_++; | 36 | ····os·<<·"Message·"·<<·message_count_++; |
32 | ····message_·=·os.str(); | 37 | ····message_·=·os.str(); |
33 | | 38 | |
34 | ····socket_.async_send_to( | 39 | ····socket_.async_send_to( |
35 | ········asio::buffer(message_),·endpoint_, | 40 | ········asio::buffer(message_),·endpoint_, |
36 | ········boost::bind(&sender::handle_send_to,·this, | 41 | ········[this](std::error_code·ec,·std::size_t·/*length*/) |
37 | ··········asio::placeholders::error)); | 42 | ········{ |
| 43 | ··········if·(!ec·&&·message_count_·<·max_message_count) |
| 44 | ············do_timeout(); |
| 45 | ········}); |
38 | ··} | 46 | ··} |
39 | | 47 | |
40 | ··void·handle_send_to(const·asio::error_code&·error) | 48 | ··void·do_timeout() |
41 | ··{ | 49 | ··{ |
42 | ····if·(!error·&&·message_count_·<·max_message_count) | 50 | ····timer_.expires_after(std::chrono::seconds(1)); |
43 | ····{ | 51 | ····timer_.async_wait( |
44 | ······timer_.expires_after(asio::chrono::seconds(1)); | 52 | ········[this](std::error_code·ec) |
45 | ······timer_.async_wait( | 53 | ········{ |
46 | ··········boost::bind(&sender::handle_timeout,·this, | 54 | ··········if·(!ec) |
47 | ············asio::placeholders::error)); | 55 | ············do_send(); |
48 | ····} | 56 | ········}); |
49 | ··} | 57 | ··} |
50 | | 58 | |
51 | ··void·handle_timeout(const·asio::error_code&·error) | |
52 | ··{ | |
53 | ····if·(!error) | |
54 | ····{ | |
55 | ······std::ostringstream·os; | |
56 | ······os·<<·"Message·"·<<·message_count_++; | |
57 | ······message_·=·os.str(); | |
58 | | |
59 | ······socket_.async_send_to( | |
60 | ··········asio::buffer(message_),·endpoint_, | |
61 | ··········boost::bind(&sender::handle_send_to,·this, | |
62 | ············asio::placeholders::error)); | |
63 | ····} | |
64 | ··} | |
65 | | |
66 | private: | 59 | private: |
67 | ··asio::ip::udp::endpoint·endpoint_; | 60 | ··asio::ip::udp::endpoint·endpoint_; |
68 | ··asio::ip::udp::socket·socket_; | 61 | ··asio::ip::udp::socket·socket_; |
69 | ··asio::steady_timer·timer_; | 62 | ··asio::steady_timer·timer_; |
70 | ··int·message_count_; | 63 | ··int·message_count_; |
71 | ··std::string·message_; | 64 | ··std::string·message_; |
72 | }; | 65 | }; |
73 | | 66 | |
74 | int·main(int·argc,·char*·argv[]) | 67 | int·main(int·argc,·char*·argv[]) |
75 | { | 68 | { |
76 | ··try | 69 | ··try |
77 | ··{ | 70 | ··{ |
78 | ····if·(argc·!=·2) | 71 | ····if·(argc·!=·2) |
79 | ····{ | 72 | ····{ |
80 | ······std::cerr·<<·"Usage:·sender·<multicast_address>\n"; | 73 | ······std::cerr·<<·"Usage:·sender·<multicast_address>\n"; |
81 | ······std::cerr·<<·"··For·IPv4,·try:\n"; | 74 | ······std::cerr·<<·"··For·IPv4,·try:\n"; |
82 | ······std::cerr·<<·"····sender·239.255.0.1\n"; | 75 | ······std::cerr·<<·"····sender·239.255.0.1\n"; |
83 | ······std::cerr·<<·"··For·IPv6,·try:\n"; | 76 | ······std::cerr·<<·"··For·IPv6,·try:\n"; |
84 | ······std::cerr·<<·"····sender·ff31::8000:1234\n"; | 77 | ······std::cerr·<<·"····sender·ff31::8000:1234\n"; |
85 | ······return·1; | 78 | ······return·1; |
86 | ····} | 79 | ····} |
87 | | 80 | |
88 | ····asio::io_context·io_context; | 81 | ····asio::io_context·io_context; |
89 | ····sender·s(io_context,·asio::ip::make_address(argv[1])); | 82 | ····sender·s(io_context,·asio::ip::make_address(argv[1])); |
90 | ····io_context.run(); | 83 | ····io_context.run(); |
91 | ··} | 84 | ··} |
92 | ··catch·(std::exception&·e) | 85 | ··catch·(std::exception&·e) |
93 | ··{ | 86 | ··{ |
94 | ····std::cerr·<<·"Exception:·"·<<·e.what()·<<·"\n"; | 87 | ····std::cerr·<<·"Exception:·"·<<·e.what()·<<·"\n"; |
95 | ··} | 88 | ··} |
96 | | 89 | |
97 | ··return·0; | 90 | ··return·0; |
98 | } | 91 | } |