JayBeams  0.1
Another project to have fun coding.
make_socket_udp_recv.hpp
Go to the documentation of this file.
1 #ifndef jb_itch5_make_socket_udp_recv_hpp
2 #define jb_itch5_make_socket_udp_recv_hpp
3 
6 
7 #include <boost/asio/ip/multicast.hpp>
8 #include <boost/asio/ip/udp.hpp>
9 
10 #include <string>
11 
12 namespace jb {
13 namespace itch5 {
14 
15 namespace detail {
16 /// Configure a socket to receive UDP packets
17 template <class socket_t>
18 void setup_socket_udp_recv(socket_t& socket, udp_receiver_config const& cfg) {
19  auto r_address = boost::asio::ip::address::from_string(cfg.address());
20 
21  boost::asio::ip::address local_address;
22 
23  // Automatically configure the best listening address ...
24  if (cfg.local_address() != "") {
25  // ... the user specified a listening address, use that ...
26  local_address = boost::asio::ip::address::from_string(cfg.local_address());
27  } else if (r_address.is_multicast()) {
28  // ... pick a default based on the protocol for the listening
29  // group ...
30  if (r_address.is_v6()) {
31  local_address = boost::asio::ip::address_v6();
32  } else {
33  local_address = boost::asio::ip::address_v4();
34  }
35  } else {
36  // ... whatever the receive address is, it must be a unicast
37  // address, use it as the local address of the socket. Notice
38  // that it can be ADDRANY, such as ::1 ...
39  local_address = r_address;
40  }
41 
42  // ... the rest if fairly mechanical stuff ..
43  boost::asio::ip::udp::endpoint endpoint(local_address, cfg.port());
44  socket.open(endpoint.protocol());
45  socket.set_option(
46  boost::asio::ip::udp::socket::reuse_address(cfg.reuse_address()));
47  socket.bind(endpoint);
48  if (r_address.is_multicast()) {
49  socket.set_option(boost::asio::ip::multicast::join_group(r_address));
50  socket.set_option(boost::asio::ip::multicast::enable_loopback(true));
51  }
52 }
53 } // namespace detail
54 
55 /**
56  * Create a socket given the configuration parameters.
57  *
58  * This is a function to create (open) sockets to receive UDP
59  * messages, either unicast or multicast and either IPv4 or IPv6.
60  *
61  * @tparam socket_t the type of socket to create. The main reason to
62  * change this type is for dependency injection during testing.
63  *
64  * @param io the Boost.ASIO io_service object that the socket is
65  * managed by.
66  *
67  * @param cfg the configuration parameters for the socket.
68  *
69  */
70 template <class socket_t = boost::asio::ip::udp::socket>
72  boost::asio::io_service& io, udp_receiver_config const& cfg) {
73  socket_t socket(io);
74  detail::setup_socket_udp_recv(socket, cfg);
75  make_socket_udp_common(socket, cfg);
76  return socket;
77 }
78 
79 } // namespace itch5
80 } // namespace jb
81 
82 #endif // jb_itch5_make_socket_udp_recv_hpp
socket_t make_socket_udp_recv(boost::asio::io_service &io, udp_receiver_config const &cfg)
Create a socket given the configuration parameters.
jb::config_attribute< udp_receiver_config, std::string > address
jb::config_attribute< udp_receiver_config, std::string > local_address
jb::config_attribute< udp_receiver_config, int > port
jb::config_attribute< udp_config_common, bool > reuse_address
void make_socket_udp_common(socket_t &s, udp_config_common const &cfg)
Configure a UDP socket.
A configuration object for UDP receivers.
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7
void setup_socket_udp_recv(socket_t &socket, udp_receiver_config const &cfg)
Configure a socket to receive UDP packets.