39 # include <dsn/service_api_c.h> 40 # include <dsn/utility/autoref_ptr.h> 41 # include <unordered_map> 42 # include <unordered_set> 48 #ifdef DSN_USE_THRIFT_SERIALIZATION 49 # include <thrift/protocol/TProtocol.h> 58 class rpc_group_address;
59 class rpc_uri_address;
69 void assign_ipv4(uint32_t ip, uint16_t port);
70 void assign_ipv4(
const char* host, uint16_t port);
71 void assign_ipv4_local_address(
const char* card_interface, uint16_t port);
72 void assign_uri(dsn_uri_t uri);
73 void assign_group(dsn_group_t g);
80 const char* to_string()
const;
81 std::string to_std_string()
const;
82 bool from_string_ipv4(
const char* s);
86 uint32_t ip()
const {
return (uint32_t)_addr.u.
v4.ip; }
87 uint16_t port()
const {
return (uint16_t)_addr.u.
v4.port; }
88 rpc_group_address* group_address()
const {
return (rpc_group_address*)(uintptr_t)_addr.u.
group.group; }
89 dsn_group_t group_handle()
const {
return (dsn_group_t)(uintptr_t)_addr.u.
group.group; }
90 rpc_uri_address* uri_address()
const {
return (rpc_uri_address*)(uintptr_t)_addr.u.uri.uri; }
91 dsn_uri_t uri_handle()
const {
return (dsn_group_t)(uintptr_t)_addr.u.uri.uri; }
92 bool is_invalid()
const {
return _addr.u.
v4.type == HOST_TYPE_INVALID; }
93 void set_invalid() { clear(); }
99 #ifdef DSN_USE_THRIFT_SERIALIZATION 100 uint32_t read(::apache::thrift::protocol::TProtocol* iprot);
101 uint32_t write(::apache::thrift::protocol::TProtocol* oprot)
const;
118 std::string _url_host;
122 inline rpc_address::rpc_address(uint32_t ip, uint16_t port)
124 assign_ipv4(ip, port);
127 "make sure rpc_address does not add new payload to dsn_address_t to keep it sizeof(uint64_t)");
130 inline rpc_address::rpc_address(
const char* host, uint16_t port)
132 assign_ipv4(host, port);
135 inline void rpc_address::assign_ipv4(uint32_t ip, uint16_t port)
140 _addr.u.v4.port = port;
143 inline void rpc_address::assign_ipv4(
const char* host, uint16_t port)
148 _addr.u.v4.port = port;
151 inline void rpc_address::assign_ipv4_local_address(
const char* network_interface, uint16_t port)
156 _addr.u.v4.port = port;
159 inline void rpc_address::assign_uri(dsn_uri_t uri)
163 _addr.u.uri.uri = (uint64_t)uri;
166 inline void rpc_address::assign_group(dsn_group_t g)
170 _addr.u.group.group = (uint64_t)g;
173 inline rpc_address::rpc_address()
176 _addr.u.v4.type = HOST_TYPE_INVALID;
179 inline rpc_address::rpc_address(
const rpc_address& addr)
197 if (_addr.u.v4.type != r.type())
200 switch (_addr.u.v4.type)
203 return _addr.u.v4.ip == r.ip() && _addr.u.v4.port == r.port();
205 return strcmp(to_string(), r.to_string()) == 0;
207 return _addr.u.group.group == r.c_addr().u.
group.group;
215 return !(*
this == r);
220 if (_addr.u.v4.type != r.type())
221 return _addr.u.v4.type < r.type();
223 switch (_addr.u.v4.type)
226 return _addr.u.v4.ip < r.ip() || (_addr.u.v4.ip == r.ip() && _addr.u.v4.port < r.port());
228 return strcmp(to_string(), r.to_string()) < 0;
230 return _addr.u.group.group < r.c_addr().u.
group.group;
236 inline void rpc_address::clear()
241 inline const char* rpc_address::to_string()
const 246 inline std::string rpc_address::to_std_string()
const 248 return std::string(to_string());
251 inline bool rpc_address::from_string_ipv4(
const char* s)
253 std::string str = std::string(s);
254 auto pos = str.find_last_of(
':');
255 if (pos == std::string::npos)
259 auto host = str.substr(0, pos);
260 auto port = atoi(str.substr(pos + 1).c_str());
261 assign_ipv4(host.c_str(), (uint16_t)port);
266 inline url_host_address::url_host_address(
const char* url_or_host_port)
268 std::string s(url_or_host_port);
269 auto sp = s.find(
':');
270 if (sp != std::string::npos)
272 uint16_t port = (uint16_t)atoi(s.substr(sp + 1).c_str());
275 _url_host = std::string(url_or_host_port);
281 assign_ipv4(s.c_str(), port);
293 size_t operator()(const ::dsn::rpc_address &ep)
const 298 return std::hash<uint32_t>()(ep.ip()) ^ std::hash<uint16_t>()(ep.port());
300 return std::hash<std::string>()(std::string(ep.to_string()));
302 return std::hash<void*>()(ep.group_address());
rpc address, which is always encoded into a 64-bit integer
Definition: api_layer1.h:471
4 bytes for IPv4
Definition: api_layer1.h:465
DSN_API uint32_t dsn_ipv4_local(const char *network_interface)
get local ipv4 according to the given network interface name
struct dsn_address_t::u_t::@2 v4
HOST_TYPE_IPV4
unsigned long long group
dsn_group_t
Definition: api_layer1.h:486
DSN_API const char * dsn_address_to_string(dsn_address_t addr)
dump a RPC address to a meaningful string for logging purpose
reference to an address group object
Definition: api_layer1.h:466
Definition: address.h:110
DSN_API dsn_uri_t dsn_uri_build(const char *url)
build URI address from a string URL, must be destroyed later using dsn_uri_destroy ...
DSN_API uint32_t dsn_ipv4_from_host(const char *name)
translate from hostname to ipv4 in host machine order
universal resource identifier as a string
Definition: api_layer1.h:467
dsn_host_type_t
rpc address host type
Definition: api_layer1.h:462