diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/boost/libs/asio/example/cpp11/multicast | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/asio/example/cpp11/multicast')
3 files changed, 211 insertions, 0 deletions
diff --git a/src/boost/libs/asio/example/cpp11/multicast/Jamfile.v2 b/src/boost/libs/asio/example/cpp11/multicast/Jamfile.v2 new file mode 100644 index 000000000..dbfe4f47b --- /dev/null +++ b/src/boost/libs/asio/example/cpp11/multicast/Jamfile.v2 @@ -0,0 +1,32 @@ +# +# Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff 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) +# + +lib socket ; # SOLARIS +lib nsl ; # SOLARIS +lib ws2_32 ; # NT +lib mswsock ; # NT +lib ipv6 ; # HPUX +lib network ; # HAIKU + +project + : requirements + <library>/boost/system//boost_system + <define>BOOST_ALL_NO_LIB=1 + <threading>multi + <target-os>solaris:<library>socket + <target-os>solaris:<library>nsl + <target-os>windows:<define>_WIN32_WINNT=0x0501 + <target-os>windows,<toolset>gcc:<library>ws2_32 + <target-os>windows,<toolset>gcc:<library>mswsock + <target-os>windows,<toolset>gcc-cygwin:<define>__USE_W32_SOCKETS + <target-os>hpux,<toolset>gcc:<define>_XOPEN_SOURCE_EXTENDED + <target-os>hpux:<library>ipv6 + <target-os>haiku:<library>network + ; + +exe receiver : receiver.cpp ; +exe sender : sender.cpp ; diff --git a/src/boost/libs/asio/example/cpp11/multicast/receiver.cpp b/src/boost/libs/asio/example/cpp11/multicast/receiver.cpp new file mode 100644 index 000000000..7148288b8 --- /dev/null +++ b/src/boost/libs/asio/example/cpp11/multicast/receiver.cpp @@ -0,0 +1,88 @@ +// +// receiver.cpp +// ~~~~~~~~~~~~ +// +// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff 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) +// + +#include <array> +#include <iostream> +#include <string> +#include <boost/asio.hpp> + +constexpr short multicast_port = 30001; + +class receiver +{ +public: + receiver(boost::asio::io_context& io_context, + const boost::asio::ip::address& listen_address, + const boost::asio::ip::address& multicast_address) + : socket_(io_context) + { + // Create the socket so that multiple may be bound to the same address. + boost::asio::ip::udp::endpoint listen_endpoint( + listen_address, multicast_port); + socket_.open(listen_endpoint.protocol()); + socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true)); + socket_.bind(listen_endpoint); + + // Join the multicast group. + socket_.set_option( + boost::asio::ip::multicast::join_group(multicast_address)); + + do_receive(); + } + +private: + void do_receive() + { + socket_.async_receive_from( + boost::asio::buffer(data_), sender_endpoint_, + [this](boost::system::error_code ec, std::size_t length) + { + if (!ec) + { + std::cout.write(data_.data(), length); + std::cout << std::endl; + + do_receive(); + } + }); + } + + boost::asio::ip::udp::socket socket_; + boost::asio::ip::udp::endpoint sender_endpoint_; + std::array<char, 1024> data_; +}; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 3) + { + std::cerr << "Usage: receiver <listen_address> <multicast_address>\n"; + std::cerr << " For IPv4, try:\n"; + std::cerr << " receiver 0.0.0.0 239.255.0.1\n"; + std::cerr << " For IPv6, try:\n"; + std::cerr << " receiver 0::0 ff31::8000:1234\n"; + return 1; + } + + boost::asio::io_context io_context; + receiver r(io_context, + boost::asio::ip::make_address(argv[1]), + boost::asio::ip::make_address(argv[2])); + io_context.run(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} diff --git a/src/boost/libs/asio/example/cpp11/multicast/sender.cpp b/src/boost/libs/asio/example/cpp11/multicast/sender.cpp new file mode 100644 index 000000000..92312a71a --- /dev/null +++ b/src/boost/libs/asio/example/cpp11/multicast/sender.cpp @@ -0,0 +1,91 @@ +// +// sender.cpp +// ~~~~~~~~~~ +// +// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff 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) +// + +#include <iostream> +#include <sstream> +#include <string> +#include <boost/asio.hpp> + +constexpr short multicast_port = 30001; +constexpr int max_message_count = 10; + +class sender +{ +public: + sender(boost::asio::io_context& io_context, + const boost::asio::ip::address& multicast_address) + : endpoint_(multicast_address, multicast_port), + socket_(io_context, endpoint_.protocol()), + timer_(io_context), + message_count_(0) + { + do_send(); + } + +private: + void do_send() + { + std::ostringstream os; + os << "Message " << message_count_++; + message_ = os.str(); + + socket_.async_send_to( + boost::asio::buffer(message_), endpoint_, + [this](boost::system::error_code ec, std::size_t /*length*/) + { + if (!ec && message_count_ < max_message_count) + do_timeout(); + }); + } + + void do_timeout() + { + timer_.expires_after(std::chrono::seconds(1)); + timer_.async_wait( + [this](boost::system::error_code ec) + { + if (!ec) + do_send(); + }); + } + +private: + boost::asio::ip::udp::endpoint endpoint_; + boost::asio::ip::udp::socket socket_; + boost::asio::steady_timer timer_; + int message_count_; + std::string message_; +}; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: sender <multicast_address>\n"; + std::cerr << " For IPv4, try:\n"; + std::cerr << " sender 239.255.0.1\n"; + std::cerr << " For IPv6, try:\n"; + std::cerr << " sender ff31::8000:1234\n"; + return 1; + } + + boost::asio::io_context io_context; + sender s(io_context, boost::asio::ip::make_address(argv[1])); + io_context.run(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} |