acelyc111 commented on code in PR #1897:
URL: 
https://github.com/apache/incubator-pegasus/pull/1897#discussion_r1497087124


##########
src/runtime/rpc/rpc_address.cpp:
##########
@@ -68,23 +68,65 @@ error_s rpc_address::GetAddrInfo(const std::string 
&hostname, const addrinfo &hi
     return error_s::ok();
 }
 
+bool extract_host_port(const std::string_view &host_port, std::string_view 
&host, uint16_t &port)
+{
+    // Check the port field is present.
+    const auto pos = host_port.find_last_of(':');
+    if (dsn_unlikely(pos == std::string::npos)) {
+        LOG_ERROR("bad format, should be in the form of <ip|host>:port, but 
got '{}'", host_port);
+        return false;
+    }
+
+    // Check port.
+    const auto port_str = host_port.substr(pos + 1);
+    if (dsn_unlikely(!dsn::buf2uint16(port_str, port))) {
+        LOG_ERROR("bad port, should be an uint16_t integer, but got '{}'", 
port_str);
+        return false;
+    }
+
+    host = host_port.substr(0, pos);
+    return true;
+}
+
 const rpc_address rpc_address::s_invalid_address;
 
+rpc_address::rpc_address(uint32_t ip, uint16_t port)
+{
+    _addr.v4.type = HOST_TYPE_IPV4;
+    _addr.v4.ip = ip;
+    _addr.v4.port = port;
+    static_assert(sizeof(rpc_address) == sizeof(uint64_t),
+                  "make sure rpc_address does not add new payload to "
+                  "rpc_address to keep it sizeof(uint64_t)");
+}
+
+rpc_address::rpc_address(const struct sockaddr_in &addr)
+{
+    _addr.v4.type = HOST_TYPE_IPV4;
+    _addr.v4.ip = static_cast<uint32_t>(ntohl(addr.sin_addr.s_addr));
+    _addr.v4.port = ntohs(addr.sin_port);
+}
+
+rpc_address::rpc_address(const rpc_address &another) { *this = another; }
+
+rpc_address::~rpc_address() { set_invalid(); }
+
 /*static*/
-uint32_t rpc_address::ipv4_from_host(const char *name)
+error_s rpc_address::ipv4_from_host(const char *hostname, uint32_t *ip)

Review Comment:
   Some other static functions, e.g., ipv4_from_network_interface(), 
is_docker_netcard() are the same, we can do this refactor in another pr, this 
patch is big enough.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to