Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

udp_wrap: simplify AddressToJS() function

  • Loading branch information...
bnoordhuis committed Mar 30, 2012
1 parent 1ab95a5 commit 7c02b5a58d796293426c7dc7e473ecc66d68977f
Showing with 11 additions and 23 deletions.
  1. +11 −23 src/udp_wrap.cc
@@ -63,10 +63,7 @@ namespace node {

typedef ReqWrap<uv_udp_send_t> SendWrap;

void AddressToJS(Handle<Object> info,
const sockaddr* addr,
int addrlen);

Local<Object> AddressToJS(const sockaddr* addr);

static Persistent<String> address_symbol;
static Persistent<String> port_symbol;
@@ -364,15 +361,13 @@ Handle<Value> UDPWrap::GetSockName(const Arguments& args) {
reinterpret_cast<sockaddr*>(&address),
&addrlen);

if (r == 0) {
Local<Object> sockname = Object::New();
AddressToJS(sockname, reinterpret_cast<sockaddr*>(&address), addrlen);
return scope.Close(sockname);
}
else {
if (r) {
SetErrno(uv_last_error(uv_default_loop()));
return Null();
}

const sockaddr* addr = reinterpret_cast<const sockaddr*>(&address);
return scope.Close(AddressToJS(addr));
}


@@ -431,34 +426,25 @@ void UDPWrap::OnRecv(uv_udp_t* handle,
return;
}

Local<Object> rinfo = Object::New();
AddressToJS(rinfo, addr, sizeof(*addr));

Local<Value> argv[] = {
Local<Object>::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<Object> info,
const sockaddr* addr,
int addrlen) {
Local<Object> 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<Object> info = Object::New();

switch (addr->sa_family) {
case AF_INET6:
@@ -480,6 +466,8 @@ void AddressToJS(Handle<Object> info,
default:
info->Set(address_symbol, String::Empty());
}

return scope.Close(info);
}


0 comments on commit 7c02b5a

Please sign in to comment.
You can’t perform that action at this time.