udp_wrap: simplify AddressToJS() function

pull/24503/head
Ben Noordhuis 2012-03-30 16:00:39 +02:00
parent 1ab95a536a
commit 7c02b5a58d
1 changed files with 11 additions and 23 deletions

View File

@ -63,10 +63,7 @@ namespace node {
typedef ReqWrap<uv_udp_send_t> SendWrap; typedef ReqWrap<uv_udp_send_t> SendWrap;
void AddressToJS(Handle<Object> info, Local<Object> AddressToJS(const sockaddr* addr);
const sockaddr* addr,
int addrlen);
static Persistent<String> address_symbol; static Persistent<String> address_symbol;
static Persistent<String> port_symbol; static Persistent<String> port_symbol;
@ -364,15 +361,13 @@ Handle<Value> UDPWrap::GetSockName(const Arguments& args) {
reinterpret_cast<sockaddr*>(&address), reinterpret_cast<sockaddr*>(&address),
&addrlen); &addrlen);
if (r == 0) { if (r) {
Local<Object> sockname = Object::New();
AddressToJS(sockname, reinterpret_cast<sockaddr*>(&address), addrlen);
return scope.Close(sockname);
}
else {
SetErrno(uv_last_error(uv_default_loop())); SetErrno(uv_last_error(uv_default_loop()));
return Null(); 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; return;
} }
Local<Object> rinfo = Object::New();
AddressToJS(rinfo, addr, sizeof(*addr));
Local<Value> argv[] = { Local<Value> argv[] = {
Local<Object>::New(wrap->object_), Local<Object>::New(wrap->object_),
slab, slab,
Integer::NewFromUnsigned(buf.base - Buffer::Data(slab)), Integer::NewFromUnsigned(buf.base - Buffer::Data(slab)),
Integer::NewFromUnsigned(nread), Integer::NewFromUnsigned(nread),
rinfo AddressToJS(addr)
}; };
MakeCallback(wrap->object_, "onmessage", ARRAY_SIZE(argv), argv); MakeCallback(wrap->object_, "onmessage", ARRAY_SIZE(argv), argv);
} }
void AddressToJS(Handle<Object> info, Local<Object> AddressToJS(const sockaddr* addr) {
const sockaddr* addr, HandleScope scope;
int addrlen) {
char ip[INET6_ADDRSTRLEN]; char ip[INET6_ADDRSTRLEN];
const sockaddr_in *a4; const sockaddr_in *a4;
const sockaddr_in6 *a6; const sockaddr_in6 *a6;
int port; int port;
assert(addr != NULL); Local<Object> info = Object::New();
if (addrlen == 0) {
info->Set(address_symbol, String::Empty());
return;
}
switch (addr->sa_family) { switch (addr->sa_family) {
case AF_INET6: case AF_INET6:
@ -480,6 +466,8 @@ void AddressToJS(Handle<Object> info,
default: default:
info->Set(address_symbol, String::Empty()); info->Set(address_symbol, String::Empty());
} }
return scope.Close(info);
} }