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/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; }); } 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_;