Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions uv/SocketAddr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ Description: https://github.com/wlgq2/uv-cpp

#include "include/SocketAddr.hpp"
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <stdexcept>
#include <cstdlib>


using namespace uv;

Expand Down Expand Up @@ -112,4 +118,23 @@ uint16_t uv::SocketAddr::GetIpAndPort(const sockaddr_storage* addr, std::string&
}
}

std::pair<std::string, int> 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.");
}
}



13 changes: 12 additions & 1 deletion uv/Udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char*>(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)
{
Expand All @@ -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;
});
}
Expand Down
2 changes: 1 addition & 1 deletion uv/include/SocketAddr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, int> ExtractIpAndPort(const std::string& str);
private:
std::string ip_;
unsigned short port_;
Expand Down