hmbdc
simplify-high-performance-messaging-programming
Transport.hpp
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include "hmbdc/app/utils/EpollTask.hpp"
5 #include "hmbdc/app/Config.hpp"
6 #include "hmbdc/comm/inet/Misc.hpp"
7 #include "hmbdc/time/Timers.hpp"
8 #include "hmbdc/Exception.hpp"
9 
10 #include <memory>
11 #include <string>
12 
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/ip.h>
16 #include <arpa/inet.h>
17 
18 namespace hmbdc { namespace tips { namespace udpcast {
19 
20 using Config = hmbdc::app::Config;
22  EpollFd(Config const& cfg) {
23  fd = socket(AF_INET, SOCK_DGRAM, 0);
24  if(fd < 0) {
25  HMBDC_THROW(std::runtime_error, "failed to create socket");
26  }
27  auto iface =
28  comm::inet::getLocalIpMatchMask(cfg.getExt<std::string>("ifaceAddr"));
29  struct in_addr localInterface;
30  localInterface.s_addr = inet_addr(iface.c_str());
31  if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF
32  , (char *)&localInterface, sizeof(localInterface)) < 0) {
33  HMBDC_THROW(std::runtime_error, "failed to set ifaceAddr " << cfg.getExt<std::string>("ifaceAddr"));
34  }
35  }
36 };
37 
38 struct Transport : EpollFd {
39  using ptr = std::shared_ptr<Transport>;
40 
41  Transport(Config const& cfg)
42  : EpollFd(cfg)
43  , config_(cfg)
44  , mtu_(config_.getExt<size_t>("mtu")) {
45  mtu_ -= (8u + 20u); // 8bytes udp header and 20bytes ip header
46  cfg (hmbdcName_, "hmbdcName")
47  (schedPolicy_, "schedPolicy")
48  (schedPriority_, "schedPriority")
49  ;
50  }
51 
52  std::tuple<char const*, int> schedSpec() const {
53  return std::make_tuple(this->schedPolicy_.c_str(), this->schedPriority_);
54  }
55 
56  bool operator == (Transport const& other ) const {
57  return &config_ == &other.config_;
58  }
59 
60  bool operator < (Transport const& other ) const {
61  return &config_ < &other.config_;
62  }
63 
64 protected:
65  char const* hmbdcName() const {
66  return this->hmbdcName_.c_str();
67  }
68  std::string hmbdcName_;
69  std::string schedPolicy_;
70  int schedPriority_;
71  Config const config_;
72  size_t mtu_;
73 };
74 
75 }}}
T getExt(const path_type &param, bool throwIfMissing=true) const
get a value from the config
Definition: Config.hpp:238
Definition: Transport.hpp:38
class to hold an hmbdc configuration
Definition: Config.hpp:44
Definition: EpollTask.hpp:87
Definition: Transport.hpp:21
Definition: Base.hpp:12