From 07cd29212ba65fd002a57004c82ec9a51ae57871 Mon Sep 17 00:00:00 2001 From: Eliauk Date: Fri, 13 Dec 2024 14:21:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0socketAdd=20tostr=20?= =?UTF-8?q?=E5=90=8E=E5=AD=97=E7=AC=A6=E4=B8=B2=20=E5=8F=8D=E8=A7=A3?= =?UTF-8?q?=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_; From bb9dd91c00c8340269a0ea62fb348cac68641533 Mon Sep 17 00:00:00 2001 From: chujiangke Date: Mon, 24 Feb 2025 18:50:37 +0800 Subject: [PATCH 2/2] Update Udp.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 异步释放 --- uv/Udp.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/uv/Udp.cpp b/uv/Udp.cpp index 5f4689c..b656254 100755 --- a/uv/Udp.cpp +++ b/uv/Udp.cpp @@ -49,7 +49,13 @@ int Udp::bindAndRead(SocketAddr& addr) int Udp::send(SocketAddr& to, const char* buf, unsigned size) { uv_udp_send_t* sendHandle = new uv_udp_send_t(); - const uv_buf_t uvbuf = uv_buf_init(const_cast(buf), size); + + char* sendBuf = new char[size]; + memcpy(sendBuf, buf, size); + const uv_buf_t uvbuf = uv_buf_init(sendBuf, size); + + sendHandle->data = sendBuf; + return ::uv_udp_send(sendHandle, handle_, &uvbuf, 1, to.Addr(), [](uv_udp_send_t* handle, int status) { @@ -59,6 +65,11 @@ int Udp::send(SocketAddr& to, const char* buf, unsigned size) info += EventLoop::GetErrorMessage(status); uv::LogWriter::Instance()->error(info); } + if(handle->data) + { + delete[] (char*)handle->data; + } + delete handle; }); }