From 7c02b5a58d796293426c7dc7e473ecc66d68977f Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 30 Mar 2012 16:00:39 +0200 Subject: [PATCH] udp_wrap: simplify AddressToJS() function --- src/udp_wrap.cc | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 8d491bb1ac5..5ed91253eec 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -63,10 +63,7 @@ namespace node { typedef ReqWrap SendWrap; -void AddressToJS(Handle info, - const sockaddr* addr, - int addrlen); - +Local AddressToJS(const sockaddr* addr); static Persistent address_symbol; static Persistent port_symbol; @@ -364,15 +361,13 @@ Handle UDPWrap::GetSockName(const Arguments& args) { reinterpret_cast(&address), &addrlen); - if (r == 0) { - Local sockname = Object::New(); - AddressToJS(sockname, reinterpret_cast(&address), addrlen); - return scope.Close(sockname); - } - else { + if (r) { SetErrno(uv_last_error(uv_default_loop())); return Null(); } + + const sockaddr* addr = reinterpret_cast(&address); + return scope.Close(AddressToJS(addr)); } @@ -431,34 +426,25 @@ void UDPWrap::OnRecv(uv_udp_t* handle, return; } - Local rinfo = Object::New(); - AddressToJS(rinfo, addr, sizeof(*addr)); - Local argv[] = { Local::New(wrap->object_), slab, Integer::NewFromUnsigned(buf.base - Buffer::Data(slab)), Integer::NewFromUnsigned(nread), - rinfo + AddressToJS(addr) }; MakeCallback(wrap->object_, "onmessage", ARRAY_SIZE(argv), argv); } -void AddressToJS(Handle info, - const sockaddr* addr, - int addrlen) { +Local AddressToJS(const sockaddr* addr) { + HandleScope scope; char ip[INET6_ADDRSTRLEN]; const sockaddr_in *a4; const sockaddr_in6 *a6; int port; - assert(addr != NULL); - - if (addrlen == 0) { - info->Set(address_symbol, String::Empty()); - return; - } + Local info = Object::New(); switch (addr->sa_family) { case AF_INET6: @@ -480,6 +466,8 @@ void AddressToJS(Handle info, default: info->Set(address_symbol, String::Empty()); } + + return scope.Close(info); }