From 2775c0e97ebdeed64743e4f56741118fdcfd8dda Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 23 Jan 2012 23:52:08 +0100 Subject: [PATCH] dgram: bring back setTTL() --- lib/dgram.js | 10 +++- src/udp_wrap.cc | 59 ++++++------------- .../test-dgram-multicast-multi-process.js | 1 + 3 files changed, 27 insertions(+), 43 deletions(-) diff --git a/lib/dgram.js b/lib/dgram.js index 8c2ec6fcea6..b5b0473de83 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -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; }; diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 99d5936b6dd..07d8d3143c8 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -96,6 +96,7 @@ public: static Handle SetMulticastTTL(const Arguments& args); static Handle SetMulticastLoopback(const Arguments& args); static Handle SetBroadcast(const Arguments& args); + static Handle SetTTL(const Arguments& args); private: static inline char* NewSlab(v8::Handle global, v8::Handle wrap_obj); @@ -159,6 +160,7 @@ void UDPWrap::Initialize(Handle 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::New(t)->GetFunction()); @@ -215,20 +217,25 @@ Handle UDPWrap::Bind6(const Arguments& args) { return DoBind(args, AF_INET6); } -Handle UDPWrap::SetBroadcast(const Arguments& args) { - HandleScope scope; - UNWRAP - assert(args.Length() == 1); +#define X(name, fn) \ + Handle 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 UDPWrap::SetMembership(const Arguments& args, uv_membership membership) { @@ -265,38 +272,6 @@ Handle UDPWrap::DropMembership(const Arguments& args) { } -Handle 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 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 UDPWrap::DoSend(const Arguments& args, int family) { HandleScope scope; int r; diff --git a/test/simple/test-dgram-multicast-multi-process.js b/test/simple/test-dgram-multicast-multi-process.js index 75fba18ae22..a473e71cbd7 100644 --- a/test/simple/test-dgram-multicast-multi-process.js +++ b/test/simple/test-dgram-multicast-multi-process.js @@ -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);