diff --git a/lib/net.js b/lib/net.js index dc42fd7c118..2d09e4982b5 100644 --- a/lib/net.js +++ b/lib/net.js @@ -133,12 +133,14 @@ Socket.prototype._onTimeout = function() { Socket.prototype.setNoDelay = function() { - /* TODO implement me */ + if (this._handle && this._handle.setNoDelay) + this._handle.setNoDelay(); }; Socket.prototype.setKeepAlive = function(setting, msecs) { - /* TODO implement me */ + if (this._handle && this._handle.setKeepAlive) + this._handle.setKeepAlive(setting, ~~(msecs / 1000)); }; diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 90fc448fcfe..e676d048bd5 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -49,6 +49,7 @@ using v8::TryCatch; using v8::Context; using v8::Arguments; using v8::Integer; +using v8::Undefined; static Persistent tcpConstructor; static Persistent family_symbol; @@ -96,6 +97,8 @@ void TCPWrap::Initialize(Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "connect6", Connect6); NODE_SET_PROTOTYPE_METHOD(t, "getsockname", GetSockName); NODE_SET_PROTOTYPE_METHOD(t, "getpeername", GetPeerName); + NODE_SET_PROTOTYPE_METHOD(t, "setNoDelay", SetNoDelay); + NODE_SET_PROTOTYPE_METHOD(t, "setKeepAlive", SetKeepAlive); tcpConstructor = Persistent::New(t->GetFunction()); @@ -219,6 +222,35 @@ Handle TCPWrap::GetPeerName(const Arguments& args) { } +Handle TCPWrap::SetNoDelay(const Arguments& args) { + HandleScope scope; + + UNWRAP + + int r = uv_tcp_nodelay(&wrap->handle_, 1); + if (r) + SetErrno(uv_last_error(uv_default_loop())); + + return Undefined(); +} + + +Handle TCPWrap::SetKeepAlive(const Arguments& args) { + HandleScope scope; + + UNWRAP + + int enable = args[0]->Int32Value(); + unsigned int delay = args[1]->Uint32Value(); + + int r = uv_tcp_keepalive(&wrap->handle_, enable, delay); + if (r) + SetErrno(uv_last_error(uv_default_loop())); + + return Undefined(); +} + + Handle TCPWrap::Bind(const Arguments& args) { HandleScope scope; diff --git a/src/tcp_wrap.h b/src/tcp_wrap.h index f0bf9efab57..4a52217a1a6 100644 --- a/src/tcp_wrap.h +++ b/src/tcp_wrap.h @@ -17,6 +17,8 @@ class TCPWrap : public StreamWrap { static v8::Handle New(const v8::Arguments& args); static v8::Handle GetSockName(const v8::Arguments& args); static v8::Handle GetPeerName(const v8::Arguments& args); + static v8::Handle SetNoDelay(const v8::Arguments& args); + static v8::Handle SetKeepAlive(const v8::Arguments& args); static v8::Handle Bind(const v8::Arguments& args); static v8::Handle Bind6(const v8::Arguments& args); static v8::Handle Listen(const v8::Arguments& args);