]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp11/multicast/receiver.cpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / libs / asio / example / cpp11 / multicast / receiver.cpp
1 //
2 // receiver.cpp
3 // ~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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
11 #include <array>
12 #include <iostream>
13 #include <string>
14 #include <boost/asio.hpp>
15
16 constexpr short multicast_port = 30001;
17
18 class receiver
19 {
20 public:
21 receiver(boost::asio::io_context& io_context,
22 const boost::asio::ip::address& listen_address,
23 const boost::asio::ip::address& multicast_address)
24 : socket_(io_context)
25 {
26 // Create the socket so that multiple may be bound to the same address.
27 boost::asio::ip::udp::endpoint listen_endpoint(
28 listen_address, multicast_port);
29 socket_.open(listen_endpoint.protocol());
30 socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
31 socket_.bind(listen_endpoint);
32
33 // Join the multicast group.
34 socket_.set_option(
35 boost::asio::ip::multicast::join_group(multicast_address));
36
37 do_receive();
38 }
39
40 private:
41 void do_receive()
42 {
43 socket_.async_receive_from(
44 boost::asio::buffer(data_), sender_endpoint_,
45 [this](boost::system::error_code ec, std::size_t length)
46 {
47 if (!ec)
48 {
49 std::cout.write(data_.data(), length);
50 std::cout << std::endl;
51
52 do_receive();
53 }
54 });
55 }
56
57 boost::asio::ip::udp::socket socket_;
58 boost::asio::ip::udp::endpoint sender_endpoint_;
59 std::array<char, 1024> data_;
60 };
61
62 int main(int argc, char* argv[])
63 {
64 try
65 {
66 if (argc != 3)
67 {
68 std::cerr << "Usage: receiver <listen_address> <multicast_address>\n";
69 std::cerr << " For IPv4, try:\n";
70 std::cerr << " receiver 0.0.0.0 239.255.0.1\n";
71 std::cerr << " For IPv6, try:\n";
72 std::cerr << " receiver 0::0 ff31::8000:1234\n";
73 return 1;
74 }
75
76 boost::asio::io_context io_context;
77 receiver r(io_context,
78 boost::asio::ip::make_address(argv[1]),
79 boost::asio::ip::make_address(argv[2]));
80 io_context.run();
81 }
82 catch (std::exception& e)
83 {
84 std::cerr << "Exception: " << e.what() << "\n";
85 }
86
87 return 0;
88 }