Skip to content

Commit

Permalink
src: return a shared_ptr<SocketAddress>
Browse files Browse the repository at this point in the history
Signed-off-by: James M Snell <jasnell@gmail.com>
  • Loading branch information
jasnell committed May 17, 2021
1 parent 2c54868 commit e913ced
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
25 changes: 8 additions & 17 deletions src/node_sockaddr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,6 @@ using v8::Object;
using v8::Uint32;
using v8::Value;

namespace {
template <typename T, typename F>
SocketAddress FromUVHandle(F fn, const T& handle) {
SocketAddress addr;
int len = sizeof(sockaddr_storage);
if (fn(&handle, addr.storage(), &len) == 0)
CHECK_EQ(static_cast<size_t>(len), addr.length());
else
addr.storage()->sa_family = 0;
return addr;
}
} // namespace

bool SocketAddress::ToSockAddr(
int32_t family,
const char* host,
Expand Down Expand Up @@ -96,19 +83,23 @@ size_t SocketAddress::Hash::operator()(const SocketAddress& addr) const {
return hash;
}

SocketAddress SocketAddress::FromSockName(const uv_tcp_t& handle) {
std::shared_ptr<SocketAddress> SocketAddress::FromSockName(
const uv_tcp_t& handle) {
return FromUVHandle(uv_tcp_getsockname, handle);
}

SocketAddress SocketAddress::FromSockName(const uv_udp_t& handle) {
std::shared_ptr<SocketAddress> SocketAddress::FromSockName(
const uv_udp_t& handle) {
return FromUVHandle(uv_udp_getsockname, handle);
}

SocketAddress SocketAddress::FromPeerName(const uv_tcp_t& handle) {
std::shared_ptr<SocketAddress> SocketAddress::FromPeerName(
const uv_tcp_t& handle) {
return FromUVHandle(uv_tcp_getpeername, handle);
}

SocketAddress SocketAddress::FromPeerName(const uv_udp_t& handle) {
std::shared_ptr<SocketAddress> SocketAddress::FromPeerName(
const uv_udp_t& handle) {
return FromUVHandle(uv_udp_getpeername, handle);
}

Expand Down
19 changes: 15 additions & 4 deletions src/node_sockaddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ class SocketAddress : public MemoryRetainer {
inline void Update(uint8_t* data, size_t len);
inline void Update(const sockaddr* data, size_t len);

static SocketAddress FromSockName(const uv_udp_t& handle);
static SocketAddress FromSockName(const uv_tcp_t& handle);
static SocketAddress FromPeerName(const uv_udp_t& handle);
static SocketAddress FromPeerName(const uv_tcp_t& handle);
static std::shared_ptr<SocketAddress> FromSockName(const uv_udp_t& handle);
static std::shared_ptr<SocketAddress> FromSockName(const uv_tcp_t& handle);
static std::shared_ptr<SocketAddress> FromPeerName(const uv_udp_t& handle);
static std::shared_ptr<SocketAddress> FromPeerName(const uv_tcp_t& handle);

inline v8::Local<v8::Object> ToJS(
Environment* env,
Expand All @@ -146,6 +146,17 @@ class SocketAddress : public MemoryRetainer {

private:
sockaddr_storage address_;

template <typename T, typename F>
static std::shared_ptr<SocketAddress> FromUVHandle(F fn, const T& handle) {
std::shared_ptr<SocketAddress> addr = std::make_shared<SocketAddress>();
int len = sizeof(sockaddr_storage);
if (fn(&handle, addr->storage(), &len) == 0) {
CHECK_EQ(static_cast<size_t>(len), addr->length());
return addr;
}
return std::shared_ptr<SocketAddress>();
}
};

class SocketAddressBase : public BaseObject {
Expand Down
6 changes: 4 additions & 2 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,13 @@ AsyncWrap* UDPWrap::GetAsyncWrap() {
}

SocketAddress UDPWrap::GetPeerName() {
return SocketAddress::FromPeerName(handle_);
std::shared_ptr<SocketAddress> addr = SocketAddress::FromPeerName(handle_);
return addr ? *(addr.get()) : SocketAddress();
}

SocketAddress UDPWrap::GetSockName() {
return SocketAddress::FromSockName(handle_);
std::shared_ptr<SocketAddress> addr = SocketAddress::FromSockName(handle_);
return addr ? *(addr.get()) : SocketAddress();
}

void UDPWrapBase::RecvStart(const FunctionCallbackInfo<Value>& args) {
Expand Down

0 comments on commit e913ced

Please sign in to comment.