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  using namespace std;
24  fd = socket(AF_INET, SOCK_DGRAM, 0);
25  if(fd < 0) {
26  HMBDC_THROW(runtime_error, "failed to create socket");
27  }
28  auto iface =
29  comm::inet::getLocalIpMatchMask(cfg.getExt<string>("ifaceAddr"));
30  struct in_addr localInterface;
31  localInterface.s_addr = inet_addr(iface.c_str());
32  if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF
33  , (char *)&localInterface, sizeof(localInterface)) < 0) {
34  HMBDC_THROW(runtime_error, "failed to set ifaceAddr " << cfg.getExt<string>("ifaceAddr"));
35  }
36  }
37 };
38 
39 struct Transport : EpollFd {
40  using ptr = std::shared_ptr<Transport>;
41 
42  Transport(Config const& cfg)
43  : EpollFd(cfg)
44  , config_(cfg)
45  , mtu_(config_.getExt<size_t>("mtu")) {
46  mtu_ -= (8u + 20u); // 8bytes udp header and 20bytes ip header
47  cfg (hmbdcName_, "hmbdcName")
48  (schedPolicy_, "schedPolicy")
49  (schedPriority_, "schedPriority")
50  ;
51  }
52 
53  std::tuple<char const*, int> schedSpec() const {
54  return std::make_tuple(this->schedPolicy_.c_str(), this->schedPriority_);
55  }
56 
57  bool operator == (Transport const& other ) const {
58  return &config_ == &other.config_;
59  }
60 
61  bool operator < (Transport const& other ) const {
62  return &config_ < &other.config_;
63  }
64 
65 protected:
66  char const* hmbdcName() const {
67  return this->hmbdcName_.c_str();
68  }
69  std::string hmbdcName_;
70  std::string schedPolicy_;
71  int schedPriority_;
72  Config const config_;
73  size_t mtu_;
74 };
75 
76 }}}
T getExt(const path_type &param, bool throwIfMissing=true) const
get a value from the config
Definition: Config.hpp:238
Definition: Transport.hpp:39
class to hold an hmbdc configuration
Definition: Config.hpp:45
Definition: TypedString.hpp:84
Definition: EpollTask.hpp:87
Definition: Transport.hpp:21
Definition: Base.hpp:12