dgram: bring back setTTL()

This commit is contained in:
Ben Noordhuis 2012-01-23 23:52:08 +01:00
parent 46e86aa803
commit 2775c0e97e
3 changed files with 27 additions and 43 deletions

View File

@ -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;
};

View File

@ -96,6 +96,7 @@ public:
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);
@ -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());
@ -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) {
@ -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;

View File

@ -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);