From 07cd29212ba65fd002a57004c82ec9a51ae57871 Mon Sep 17 00:00:00 2001 From: Eliauk Date: Fri, 13 Dec 2024 14:21:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0socketAdd=20tostr=20=E5=90=8E?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=20=E5=8F=8D=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uv/SocketAddr.cpp | 25 +++++++++++++++++++++++++ uv/include/SocketAddr.hpp | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/uv/SocketAddr.cpp b/uv/SocketAddr.cpp index ef18ce8..ef2efe8 100755 --- a/uv/SocketAddr.cpp +++ b/uv/SocketAddr.cpp @@ -10,6 +10,12 @@ Description: https://github.com/wlgq2/uv-cpp #include "include/SocketAddr.hpp" #include +#include +#include +#include +#include +#include + using namespace uv; @@ -112,4 +118,23 @@ uint16_t uv::SocketAddr::GetIpAndPort(const sockaddr_storage* addr, std::string& } } +std::pair uv::SocketAddr::ExtractIpAndPort(const std::string& str) +{ + size_t delim_pos = str.find(':'); + if (delim_pos == std::string::npos) { + throw std::invalid_argument("Invalid format: missing ':' delimiter."); + } + + std::string ip = str.substr(0, delim_pos); + std::string port_str = str.substr(delim_pos + 1); + + try { + int port = std::stoi(port_str); + return std::make_pair(ip, port); + } catch (const std::exception& e) { + throw std::invalid_argument("Invalid port: cannot convert to an integer."); + } +} + + diff --git a/uv/include/SocketAddr.hpp b/uv/include/SocketAddr.hpp index fe910fb..c820d16 100755 --- a/uv/include/SocketAddr.hpp +++ b/uv/include/SocketAddr.hpp @@ -40,7 +40,7 @@ class SocketAddr static void AddrToStr(uv_tcp_t* client, std::string& addrStr, IPV ipv = Ipv4); static uint16_t GetIpAndPort(const sockaddr_storage* addr, std::string& out, IPV ipv = Ipv4); - + std::pair ExtractIpAndPort(const std::string& str); private: std::string ip_; unsigned short port_;