Skip to content

Commit ac379b3

Browse files
bnoordhuisry
authored andcommitted
net: bring back .setNoDelay() and .setKeepAlive()
1 parent 401c073 commit ac379b3

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

lib/net.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,14 @@ Socket.prototype._onTimeout = function() {
133133

134134

135135
Socket.prototype.setNoDelay = function() {
136-
/* TODO implement me */
136+
if (this._handle && this._handle.setNoDelay)
137+
this._handle.setNoDelay();
137138
};
138139

139140

140141
Socket.prototype.setKeepAlive = function(setting, msecs) {
141-
/* TODO implement me */
142+
if (this._handle && this._handle.setKeepAlive)
143+
this._handle.setKeepAlive(setting, ~~(msecs / 1000));
142144
};
143145

144146

src/tcp_wrap.cc

+32
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ using v8::TryCatch;
4949
using v8::Context;
5050
using v8::Arguments;
5151
using v8::Integer;
52+
using v8::Undefined;
5253

5354
static Persistent<Function> tcpConstructor;
5455
static Persistent<String> family_symbol;
@@ -96,6 +97,8 @@ void TCPWrap::Initialize(Handle<Object> target) {
9697
NODE_SET_PROTOTYPE_METHOD(t, "connect6", Connect6);
9798
NODE_SET_PROTOTYPE_METHOD(t, "getsockname", GetSockName);
9899
NODE_SET_PROTOTYPE_METHOD(t, "getpeername", GetPeerName);
100+
NODE_SET_PROTOTYPE_METHOD(t, "setNoDelay", SetNoDelay);
101+
NODE_SET_PROTOTYPE_METHOD(t, "setKeepAlive", SetKeepAlive);
99102

100103
tcpConstructor = Persistent<Function>::New(t->GetFunction());
101104

@@ -219,6 +222,35 @@ Handle<Value> TCPWrap::GetPeerName(const Arguments& args) {
219222
}
220223

221224

225+
Handle<Value> TCPWrap::SetNoDelay(const Arguments& args) {
226+
HandleScope scope;
227+
228+
UNWRAP
229+
230+
int r = uv_tcp_nodelay(&wrap->handle_, 1);
231+
if (r)
232+
SetErrno(uv_last_error(uv_default_loop()));
233+
234+
return Undefined();
235+
}
236+
237+
238+
Handle<Value> TCPWrap::SetKeepAlive(const Arguments& args) {
239+
HandleScope scope;
240+
241+
UNWRAP
242+
243+
int enable = args[0]->Int32Value();
244+
unsigned int delay = args[1]->Uint32Value();
245+
246+
int r = uv_tcp_keepalive(&wrap->handle_, enable, delay);
247+
if (r)
248+
SetErrno(uv_last_error(uv_default_loop()));
249+
250+
return Undefined();
251+
}
252+
253+
222254
Handle<Value> TCPWrap::Bind(const Arguments& args) {
223255
HandleScope scope;
224256

src/tcp_wrap.h

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class TCPWrap : public StreamWrap {
1717
static v8::Handle<v8::Value> New(const v8::Arguments& args);
1818
static v8::Handle<v8::Value> GetSockName(const v8::Arguments& args);
1919
static v8::Handle<v8::Value> GetPeerName(const v8::Arguments& args);
20+
static v8::Handle<v8::Value> SetNoDelay(const v8::Arguments& args);
21+
static v8::Handle<v8::Value> SetKeepAlive(const v8::Arguments& args);
2022
static v8::Handle<v8::Value> Bind(const v8::Arguments& args);
2123
static v8::Handle<v8::Value> Bind6(const v8::Arguments& args);
2224
static v8::Handle<v8::Value> Listen(const v8::Arguments& args);

0 commit comments

Comments
 (0)