Navigation Menu

Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
dgram: bring back setTTL()
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Jan 23, 2012
1 parent 46e86aa commit 2775c0e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 43 deletions.
10 changes: 9 additions & 1 deletion lib/dgram.js
Expand Up @@ -230,7 +230,15 @@ Socket.prototype.setBroadcast = function(arg) {


Socket.prototype.setTTL = function(arg) {
throw new Error('not yet implemented');
if (typeof arg !== 'number') {
throw new TypeError('Argument must be a number');
}

if (this._handle.setTTL(arg)) {
throw errnoException(errno, 'setTTL');
}

return arg;
};


Expand Down
59 changes: 17 additions & 42 deletions src/udp_wrap.cc
Expand Up @@ -96,6 +96,7 @@ class UDPWrap: public HandleWrap {
static Handle<Value> SetMulticastTTL(const Arguments& args);
static Handle<Value> SetMulticastLoopback(const Arguments& args);
static Handle<Value> SetBroadcast(const Arguments& args);
static Handle<Value> SetTTL(const Arguments& args);

private:
static inline char* NewSlab(v8::Handle<v8::Object> global, v8::Handle<v8::Object> wrap_obj);
Expand Down Expand Up @@ -159,6 +160,7 @@ void UDPWrap::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(t, "setMulticastTTL", SetMulticastTTL);
NODE_SET_PROTOTYPE_METHOD(t, "setMulticastLoopback", SetMulticastLoopback);
NODE_SET_PROTOTYPE_METHOD(t, "setBroadcast", SetBroadcast);
NODE_SET_PROTOTYPE_METHOD(t, "setTTL", SetTTL);

target->Set(String::NewSymbol("UDP"),
Persistent<FunctionTemplate>::New(t)->GetFunction());
Expand Down Expand Up @@ -215,20 +217,25 @@ Handle<Value> UDPWrap::Bind6(const Arguments& args) {
return DoBind(args, AF_INET6);
}

Handle<Value> UDPWrap::SetBroadcast(const Arguments& args) {
HandleScope scope;
UNWRAP

assert(args.Length() == 1);
#define X(name, fn) \
Handle<Value> UDPWrap::name(const Arguments& args) { \
HandleScope scope; \
UNWRAP \
assert(args.Length() == 1); \
int flag = args[0]->Int32Value(); \
int r = fn(&wrap->handle_, flag); \
if (r) SetErrno(uv_last_error(uv_default_loop())); \
return scope.Close(Integer::New(r)); \
}

int on = args[0]->Uint32Value();
int r = uv_udp_set_broadcast(&wrap->handle_, on);
X(SetTTL, uv_udp_set_ttl)
X(SetBroadcast, uv_udp_set_broadcast)
X(SetMulticastTTL, uv_udp_set_multicast_ttl)
X(SetMulticastLoopback, uv_udp_set_multicast_loop)

if (r)
SetErrno(uv_last_error(uv_default_loop()));
#undef X

return scope.Close(Integer::New(r));
}

Handle<Value> UDPWrap::SetMembership(const Arguments& args,
uv_membership membership) {
Expand Down Expand Up @@ -265,38 +272,6 @@ Handle<Value> UDPWrap::DropMembership(const Arguments& args) {
}


Handle<Value> UDPWrap::SetMulticastTTL(const Arguments& args) {
HandleScope scope;
UNWRAP

assert(args.Length() == 1);

int ttl = args[0]->Uint32Value();
int r = uv_udp_set_multicast_ttl(&wrap->handle_, ttl);

if (r)
SetErrno(uv_last_error(uv_default_loop()));

return scope.Close(Integer::New(r));
}


Handle<Value> UDPWrap::SetMulticastLoopback(const Arguments& args) {
HandleScope scope;
UNWRAP

assert(args.Length() == 1);

int on = args[0]->Int32Value();
int r = uv_udp_set_multicast_loop(&wrap->handle_, on);

if (r)
SetErrno(uv_last_error(uv_default_loop()));

return scope.Close(Integer::New(r));
}


Handle<Value> UDPWrap::DoSend(const Arguments& args, int family) {
HandleScope scope;
int r;
Expand Down
1 change: 1 addition & 0 deletions test/simple/test-dgram-multicast-multi-process.js
Expand Up @@ -102,6 +102,7 @@ if (cluster.isMaster) {
// before calling any of the set*() functions - the bind()
// call is what creates the actual socket...

sendSocket.setTTL(1);
sendSocket.setBroadcast(true);
sendSocket.setMulticastTTL(1);
sendSocket.setMulticastLoopback(true);
Expand Down

0 comments on commit 2775c0e

Please sign in to comment.