Skip to content

Commit

Permalink
API: connection.send() renamed to connection.write()
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Feb 16, 2010
1 parent 1bf46d1 commit 23cf502
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 44 deletions.
10 changes: 5 additions & 5 deletions doc/api.txt
Expand Up @@ -1351,13 +1351,13 @@ var tcp = require("tcp");
var server = tcp.createServer(function (socket) {
socket.setEncoding("utf8");
socket.addListener("connect", function () {
socket.send("hello\r\n");
socket.write("hello\r\n");
});
socket.addListener("data", function (data) {
socket.send(data);
socket.write(data);
});
socket.addListener("end", function () {
socket.send("goodbye\r\n");
socket.write("goodbye\r\n");
socket.close();
});
});
Expand Down Expand Up @@ -1474,7 +1474,7 @@ Either +"closed"+, +"open"+, +"opening"+, +"readOnly"+, or +"writeOnly"+.
+connection.setEncoding(encoding)+::
Sets the encoding (either +"ascii"+, +"utf8"+, or +"binary"+) for data that is received.

+connection.send(data, encoding="ascii")+::
+connection.write(data, encoding="ascii")+::
Sends data on the connection. The second parameter specifies the encoding
in the case of a string--it defaults to ASCII because encoding to UTF8 is
rather slow.
Expand Down Expand Up @@ -1507,7 +1507,7 @@ If +timeout+ is 0, then the idle timeout is disabled.
+connection.setNoDelay(noDelay=true)+::
Disables the Nagle algorithm. By default TCP connections use the Nagle
algorithm, they buffer data before sending it off. Setting +noDelay+ will
immediately fire off data each time +connection.send()+ is called.
immediately fire off data each time +connection.write()+ is called.

+connection.verifyPeer()+::
Returns an integer indicating the trusted status of the peer in a TLS
Expand Down
6 changes: 3 additions & 3 deletions doc/index.html
Expand Up @@ -74,13 +74,13 @@
var server = tcp.createServer(function (socket) {
socket.setEncoding("utf8");
socket.addListener("connect", function () {
socket.send("hello\r\n");
socket.write("hello\r\n");
});
socket.addListener("receive", function (data) {
socket.send(data);
socket.write(data);
});
socket.addListener("eof", function () {
socket.send("goodbye\r\n");
socket.write("goodbye\r\n");
socket.close();
});
});
Expand Down
2 changes: 1 addition & 1 deletion lib/http.js
Expand Up @@ -357,7 +357,7 @@ function flushMessageQueue (connection, queue) {
var data = message.output.shift();
var encoding = message.outputEncodings.shift();

connection.send(data, encoding);
connection.write(data, encoding);
}

if (!message.finished) break;
Expand Down
13 changes: 12 additions & 1 deletion src/node_net.cc
Expand Up @@ -96,6 +96,7 @@ void Connection::Initialize(v8::Handle<v8::Object> target) {

NODE_SET_PROTOTYPE_METHOD(constructor_template, "connect", Connect);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "send", Send);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "write", Write);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "close", Close);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "forceClose", ForceClose);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "setEncoding", SetEncoding);
Expand Down Expand Up @@ -586,7 +587,17 @@ Handle<Value> Connection::SetNoDelay(const Arguments& args) {
return Undefined();
}


Handle<Value> Connection::Send(const Arguments& args) {
HandleScope scope;
return ThrowException(Exception::Error(String::New(
"connection.send() has been renamed to connection.write(). "
"(Also the 'receive' event has been renamed to 'data' and "
"the 'eof' event has been renamed to 'end'.)")));
}


Handle<Value> Connection::Write(const Arguments& args) {
HandleScope scope;
Connection *connection = ObjectWrap::Unwrap<Connection>(args.Holder());
assert(connection);
Expand All @@ -609,7 +620,7 @@ Handle<Value> Connection::Send(const Arguments& args) {
char * buf = new char[len];
ssize_t written = DecodeWrite(buf, len, args[0], enc);
assert(written == len);
connection->Send(buf, written);
connection->Write(buf, written);
delete [] buf;

return scope.Close(Integer::New(written));
Expand Down
3 changes: 2 additions & 1 deletion src/node_net.h
Expand Up @@ -27,6 +27,7 @@ class Connection : public EventEmitter {
static v8::Handle<v8::Value> New(const v8::Arguments& args);
static v8::Handle<v8::Value> Connect(const v8::Arguments& args);
static v8::Handle<v8::Value> Send(const v8::Arguments& args);
static v8::Handle<v8::Value> Write(const v8::Arguments& args);
static v8::Handle<v8::Value> SendUtf8(const v8::Arguments& args);
static v8::Handle<v8::Value> Close(const v8::Arguments& args);
static v8::Handle<v8::Value> ForceClose(const v8::Arguments& args);
Expand Down Expand Up @@ -61,7 +62,7 @@ class Connection : public EventEmitter {
return evcom_stream_connect(&stream_, address);
}

void Send(const char *buf, size_t len) {
void Write(const char *buf, size_t len) {
evcom_stream_write(&stream_, buf, len);
}

Expand Down
2 changes: 1 addition & 1 deletion test/mjsunit/test-http-1.0.js
Expand Up @@ -20,7 +20,7 @@ var c = tcp.createConnection(port);
c.setEncoding("utf8");

c.addListener("connect", function () {
c.send( "GET / HTTP/1.0\r\n\r\n" );
c.write( "GET / HTTP/1.0\r\n\r\n" );
});

c.addListener("data", function (chunk) {
Expand Down
2 changes: 1 addition & 1 deletion test/mjsunit/test-http-malformed-request.js
Expand Up @@ -23,7 +23,7 @@ s.listen(port);

var c = tcp.createConnection(port);
c.addListener("connect", function () {
c.send("GET /hello?foo=%99bar HTTP/1.1\r\n\r\n");
c.write("GET /hello?foo=%99bar HTTP/1.1\r\n\r\n");
c.close();
});

Expand Down
8 changes: 4 additions & 4 deletions test/mjsunit/test-http-server.js
Expand Up @@ -50,21 +50,21 @@ var c = tcp.createConnection(port);
c.setEncoding("utf8");

c.addListener("connect", function () {
c.send( "GET /hello?hello=world&foo=b==ar HTTP/1.1\r\n\r\n" );
c.write( "GET /hello?hello=world&foo=b==ar HTTP/1.1\r\n\r\n" );
requests_sent += 1;
});

c.addListener("data", function (chunk) {
server_response += chunk;

if (requests_sent == 1) {
c.send("POST /quit HTTP/1.1\r\n\r\n");
c.write("POST /quit HTTP/1.1\r\n\r\n");
requests_sent += 1;
}

if (requests_sent == 2) {
c.send("GET / HTTP/1.1\r\nX-X: foo\r\n\r\n"
+"GET / HTTP/1.1\r\nX-X: bar\r\n\r\n");
c.write("GET / HTTP/1.1\r\nX-X: foo\r\n\r\n"
+"GET / HTTP/1.1\r\nX-X: bar\r\n\r\n");
c.close();
assert.equal(c.readyState, "readOnly");
requests_sent += 2;
Expand Down
2 changes: 1 addition & 1 deletion test/mjsunit/test-http-wget.js
Expand Up @@ -36,7 +36,7 @@ var c = tcp.createConnection(port);
c.setEncoding("utf8");

c.addListener("connect", function () {
c.send( "GET / HTTP/1.0\r\n" +
c.write("GET / HTTP/1.0\r\n" +
"Connection: Keep-Alive\r\n\r\n");
});

Expand Down
8 changes: 4 additions & 4 deletions test/mjsunit/test-tcp-binary.js
Expand Up @@ -23,7 +23,7 @@ var echoServer = tcp.createServer(function (connection) {
connection.setEncoding("binary");
connection.addListener("data", function (chunk) {
error("recved: " + JSON.stringify(chunk));
connection.send(chunk, "binary");
connection.write(chunk, "binary");
});
connection.addListener("end", function () {
connection.close();
Expand All @@ -39,8 +39,8 @@ var c = tcp.createConnection(PORT);
c.setEncoding("binary");
c.addListener("data", function (chunk) {
if (j < 256) {
error("send " + j);
c.send(String.fromCharCode(j), "binary");
error("write " + j);
c.write(String.fromCharCode(j), "binary");
j++;
} else {
c.close();
Expand All @@ -49,7 +49,7 @@ c.addListener("data", function (chunk) {
});

c.addListener("connect", function () {
c.send(binaryString, "binary");
c.write(binaryString, "binary");
});

c.addListener("close", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/mjsunit/test-tcp-many-clients.js
Expand Up @@ -18,7 +18,7 @@ var server = tcp.createServer(function (c) {
c.addListener("connect", function () {
total_connections++;
print("#");
c.send(body);
c.write(body);
c.close();
});
});
Expand Down
6 changes: 3 additions & 3 deletions test/mjsunit/test-tcp-pingpong-delay.js
Expand Up @@ -20,7 +20,7 @@ function pingPongTest (port, host, on_complete) {
assert.equal(true, count <= N);
setTimeout(function () {
assert.equal("open", socket.readyState);
socket.send("PONG");
socket.write("PONG");
}, DELAY);
});

Expand Down Expand Up @@ -50,7 +50,7 @@ function pingPongTest (port, host, on_complete) {

client.addListener("connect", function () {
assert.equal("open", client.readyState);
client.send("PING");
client.write("PING");
});

client.addListener("data", function (data) {
Expand All @@ -61,7 +61,7 @@ function pingPongTest (port, host, on_complete) {
setTimeout(function () {
assert.equal("open", client.readyState);
if (count++ < N) {
client.send("PING");
client.write("PING");
} else {
puts("closing client");
client.close();
Expand Down
8 changes: 4 additions & 4 deletions test/mjsunit/test-tcp-pingpong.js
Expand Up @@ -26,7 +26,7 @@ function pingPongTest (port, host, on_complete) {
assert.equal("open", socket.readyState);
assert.equal(true, count <= N);
if (/PING/.exec(data)) {
socket.send("PONG");
socket.write("PONG");
}
});

Expand All @@ -49,7 +49,7 @@ function pingPongTest (port, host, on_complete) {

client.addListener("connect", function () {
assert.equal("open", client.readyState);
client.send("PING");
client.write("PING");
});

client.addListener("data", function (data) {
Expand All @@ -64,10 +64,10 @@ function pingPongTest (port, host, on_complete) {
}

if (count < N) {
client.send("PING");
client.write("PING");
} else {
sent_final_ping = true;
client.send("PING");
client.write("PING");
client.close();
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/mjsunit/test-tcp-reconnect.js
Expand Up @@ -9,7 +9,7 @@ var disconnect_count = 0;

var server = tcp.createServer(function (socket) {
socket.addListener("connect", function () {
socket.send("hello\r\n");
socket.write("hello\r\n");
});

socket.addListener("end", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/mjsunit/test-tcp-throttle-kernel-buffer.js
Expand Up @@ -13,7 +13,7 @@ puts("start server on port " + PORT);

server = tcp.createServer(function (connection) {
connection.addListener("connect", function () {
connection.send(body);
connection.write(body);
connection.close();
});
});
Expand Down
8 changes: 4 additions & 4 deletions test/mjsunit/test-tcp-throttle.js
Expand Up @@ -4,17 +4,17 @@ PORT = 20443;
N = 200;

server = tcp.createServer(function (connection) {
function send (j) {
function write (j) {
if (j >= N) {
connection.close();
return;
}
setTimeout(function () {
connection.send("C");
send(j+1);
connection.write("C");
write(j+1);
}, 10);
}
send(0);
write(0);
});
server.listen(PORT);

Expand Down
8 changes: 4 additions & 4 deletions test/mjsunit/test-tcp-timeout.js
Expand Up @@ -17,7 +17,7 @@ var echo_server = tcp.createServer(function (socket) {

socket.addListener("data", function (d) {
p(d);
socket.send(d);
socket.write(d);
});

socket.addListener("end", function () {
Expand All @@ -33,15 +33,15 @@ client.setEncoding("UTF8");
client.setTimeout(0); // disable the timeout for client
client.addListener("connect", function () {
puts("client connected.");
client.send("hello\r\n");
client.write("hello\r\n");
});

client.addListener("data", function (chunk) {
assert.equal("hello\r\n", chunk);
if (exchanges++ < 5) {
setTimeout(function () {
puts("client send 'hello'");
client.send("hello\r\n");
puts("client write 'hello'");
client.write("hello\r\n");
}, 500);

if (exchanges == 5) {
Expand Down
8 changes: 4 additions & 4 deletions test/mjsunit/test-tcp-tls.js
Expand Up @@ -31,7 +31,7 @@ function tlsTest (port, host, caPem, keyPem, certPem) {
assert.equal("open", socket.readyState);
assert.equal(true, count <= N);
if (/PING/.exec(data)) {
socket.send("PONG");
socket.write("PONG");
}
});

Expand Down Expand Up @@ -62,7 +62,7 @@ function tlsTest (port, host, caPem, keyPem, certPem) {
assert.equal(verified, 1);
assert.equal(peerDN, "C=UK,ST=Acknack Ltd,L=Rhys Jones,O=node.js,"
+ "OU=Test TLS Certificate,CN=localhost");
client.send("PING");
client.write("PING");
});

client.addListener("data", function (data) {
Expand All @@ -79,10 +79,10 @@ function tlsTest (port, host, caPem, keyPem, certPem) {
}

if (count < N) {
client.send("PING");
client.write("PING");
} else {
sent_final_ping = true;
client.send("PING");
client.write("PING");
client.close();
}
});
Expand Down

0 comments on commit 23cf502

Please sign in to comment.