JayBeams  0.1
Another project to have fun coding.
make_socket_udp_common.hpp
Go to the documentation of this file.
1 #ifndef jb_itch5_make_socket_udp_common_hpp
2 #define jb_itch5_make_socket_udp_common_hpp
3 
5 
6 #include <boost/asio/ip/multicast.hpp>
7 #include <boost/asio/ip/udp.hpp>
8 #include <boost/asio/ip/unicast.hpp>
9 
10 namespace jb {
11 namespace itch5 {
12 
13 /**
14  * Configure a UDP socket.
15  *
16  * @tparam socket_t the type of socket to create. The main reason to
17  * change this type is for dependency injection during testing.
18  *
19  * @param s the socket to configure.
20  * @param cfg the socket configuration.
21  */
22 template <class socket_t = boost::asio::ip::udp::socket>
23 void make_socket_udp_common(socket_t& s, udp_config_common const& cfg) {
24  // Some type aliases to shorten the code inside the function ...
25  using socket_base = boost::asio::socket_base;
26 
27  if (cfg.debug()) {
28  s.set_option(socket_base::debug(cfg.debug()));
29  }
30  s.set_option(socket_base::do_not_route(cfg.do_not_route()));
31  if (cfg.linger()) {
32  s.set_option(socket_base::linger(cfg.linger(), cfg.linger_seconds()));
33  }
34  if (cfg.receive_buffer_size() != -1) {
35  s.set_option(socket_base::receive_buffer_size(cfg.receive_buffer_size()));
36  }
37  if (cfg.receive_low_watermark() != -1) {
38  s.set_option(
39  socket_base::receive_low_watermark(cfg.receive_low_watermark()));
40  }
41  if (cfg.send_buffer_size() != -1) {
42  s.set_option(socket_base::send_buffer_size(cfg.send_buffer_size()));
43  }
44  if (cfg.send_low_watermark() != -1) {
45  s.set_option(socket_base::send_low_watermark(cfg.send_low_watermark()));
46  }
47 }
48 
49 } // namespace itch5
50 } // namespace jb
51 
52 #endif // jb_itch5_make_socket_udp_common_hpp
jb::config_attribute< udp_config_common, int > linger_seconds
jb::config_attribute< udp_config_common, int > send_low_watermark
jb::config_attribute< udp_config_common, bool > linger
jb::config_attribute< udp_config_common, int > receive_buffer_size
jb::config_attribute< udp_config_common, int > receive_low_watermark
jb::config_attribute< udp_config_common, bool > debug
void make_socket_udp_common(socket_t &s, udp_config_common const &cfg)
Configure a UDP socket.
jb::config_attribute< udp_config_common, bool > do_not_route
Common configuration parameters for both UDP senders and receivers.
jb::config_attribute< udp_config_common, int > send_buffer_size
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7