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