hmbdc
simplify-high-performance-messaging-programming
Endpoint.hpp
1 #pragma once
2 
3 #include "hmbdc/Exception.hpp"
4 
5 #include <string>
6 #include <iostream>
7 #include <stdexcept>
8 
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <inttypes.h>
12 #include <string.h>
13 
14 namespace hmbdc { namespace comm { namespace inet {
15 struct Endpoint {
16  sockaddr_in v = {0};
17  Endpoint(){}
18  explicit Endpoint(std::string const& ipPort) {
19  char ip[64];
20  uint16_t port;
21  auto s = ipPort;
22  std::replace(std::begin(s), std::end(s), ':', ' ');
23 
24  if (2 != sscanf(s.c_str(), "%s %" SCNu16, ip, &port)) {
25  HMBDC_THROW(std::runtime_error, "incorrect address " << ipPort);
26  }
27  memset(&v, 0, sizeof(v));
28  v.sin_family = AF_INET;
29  v.sin_addr.s_addr = inet_addr(ip);
30  v.sin_port = htons(port);
31  }
32 
33 
34  Endpoint(std::string const& ip, uint16_t port) {
35  memset(&v, 0, sizeof(v));
36  v.sin_family = AF_INET;
37  v.sin_addr.s_addr = inet_addr(ip.c_str());
38  v.sin_port = htons(port);
39  }
40 
41  bool operator == (Endpoint const& other) const {
42  return memcmp(&v, &other.v, sizeof(v)) == 0;
43  }
44  friend
45  std::istream& operator >> (std::istream& is, Endpoint& ep) {
46  std::string ipPort;
47  is >> ipPort
48  ;
49  if (is) {
50  ep = Endpoint(ipPort);
51  }
52  return is;
53  }
54 };
55 }}}
56 
Definition: Endpoint.hpp:15
Definition: Base.hpp:12