Browse Source

API: tcp events 'receive' to 'data', 'eof' to 'end'

No deprecation messages. Not sure how...
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
bc17d94a0a
  1. 12
      doc/api.txt
  2. 6
      lib/http.js
  3. 6
      src/node_http.cc
  4. 12
      src/node_net.cc
  5. 4
      test/mjsunit/test-http-1.0.js
  6. 2
      test/mjsunit/test-http-client-reconnect-bug.js
  7. 4
      test/mjsunit/test-http-server.js
  8. 6
      test/mjsunit/test-http-wget.js
  9. 2
      test/mjsunit/test-stdio.js
  10. 6
      test/mjsunit/test-tcp-binary.js
  11. 4
      test/mjsunit/test-tcp-many-clients.js
  12. 6
      test/mjsunit/test-tcp-pingpong-delay.js
  13. 6
      test/mjsunit/test-tcp-pingpong.js
  14. 4
      test/mjsunit/test-tcp-reconnect.js
  15. 4
      test/mjsunit/test-tcp-throttle-kernel-buffer.js
  16. 4
      test/mjsunit/test-tcp-throttle.js
  17. 10
      test/mjsunit/test-tcp-timeout.js
  18. 6
      test/mjsunit/test-tcp-tls.js

12
doc/api.txt

@ -234,7 +234,7 @@ there is a connection, a child process emits an event when it exits. All
objects which emit events are instances of +events.EventEmitter+. objects which emit events are instances of +events.EventEmitter+.
Events are represented by a camel-cased string. Here are some examples: Events are represented by a camel-cased string. Here are some examples:
+"connection"+, +"receive"+, +"messageBegin"+. +"connection"+, +"data"+, +"messageBegin"+.
Functions can be then be attached to objects, to be executed when an event Functions can be then be attached to objects, to be executed when an event
is emitted. These functions are called _listeners_. is emitted. These functions are called _listeners_.
@ -1353,10 +1353,10 @@ var server = tcp.createServer(function (socket) {
socket.addListener("connect", function () { socket.addListener("connect", function () {
socket.send("hello\r\n"); socket.send("hello\r\n");
}); });
socket.addListener("receive", function (data) { socket.addListener("data", function (data) {
socket.send(data); socket.send(data);
}); });
socket.addListener("eof", function () { socket.addListener("end", function () {
socket.send("goodbye\r\n"); socket.send("goodbye\r\n");
socket.close(); socket.close();
}); });
@ -1421,11 +1421,11 @@ socket for +tcp.Server+.
|+"connect"+ | | Call once the connection is established |+"connect"+ | | Call once the connection is established
after a call to +createConnection()+ or after a call to +createConnection()+ or
+connect()+. +connect()+.
|+"receive"+ | +data+ | Called when data is received on the |+"data"+ | +data+ | Called when data is received on the
connection. +data+ will be a string. connection. +data+ will be a string.
Encoding of data is set by Encoding of data is set by
+connection.setEncoding()+. +connection.setEncoding()+.
|+"eof"+ | | Called when the other end of the |+"end"+ | | Called when the other end of the
connection sends a FIN packet. connection sends a FIN packet.
After this is emitted the +readyState+ After this is emitted the +readyState+
will be +"writeOnly"+. One should probably will be +"writeOnly"+. One should probably
@ -1491,7 +1491,7 @@ Ensures that no more I/O activity happens on this socket. Only
necessary in case of errors (parse error or so). necessary in case of errors (parse error or so).
+connection.readPause()+:: +connection.readPause()+::
Pauses the reading of data. That is, +"receive"+ events will not be emitted. Pauses the reading of data. That is, +"data"+ events will not be emitted.
Useful to throttle back an upload. Useful to throttle back an upload.
+connection.readResume()+:: +connection.readResume()+::

6
lib/http.js

@ -387,7 +387,7 @@ function connectionListener (connection) {
connection.resetParser(); connection.resetParser();
// is this really needed? // is this really needed?
connection.addListener("eof", function () { connection.addListener("end", function () {
if (responses.length == 0) { if (responses.length == 0) {
connection.close(); connection.close();
} else { } else {
@ -456,8 +456,8 @@ exports.createClient = function (port, host) {
currentRequest.flush(); currentRequest.flush();
}); });
client.addListener("eof", function () { client.addListener("end", function () {
//sys.debug("client got eof closing. readyState = " + client.readyState); //sys.debug("client got end closing. readyState = " + client.readyState);
client.close(); client.close();
}); });

6
src/node_http.cc

@ -27,7 +27,7 @@ static Persistent<String> header_field_symbol;
static Persistent<String> header_value_symbol; static Persistent<String> header_value_symbol;
static Persistent<String> header_complete_symbol; static Persistent<String> header_complete_symbol;
static Persistent<String> body_symbol; static Persistent<String> body_symbol;
static Persistent<String> eof_symbol; static Persistent<String> end_symbol;
static Persistent<String> delete_sym; static Persistent<String> delete_sym;
static Persistent<String> get_sym; static Persistent<String> get_sym;
@ -66,7 +66,7 @@ HTTPConnection::Initialize (Handle<Object> target)
NODE_SET_PROTOTYPE_METHOD(server_constructor_template, "resetParser", ResetParser); NODE_SET_PROTOTYPE_METHOD(server_constructor_template, "resetParser", ResetParser);
server_constructor_template->SetClassName(String::NewSymbol("ServerSideConnection")); server_constructor_template->SetClassName(String::NewSymbol("ServerSideConnection"));
eof_symbol = NODE_PSYMBOL("eof"); end_symbol = NODE_PSYMBOL("end");
} }
@ -123,7 +123,7 @@ HTTPConnection::OnEOF ()
assert(refs_); assert(refs_);
size_t nparsed; size_t nparsed;
nparsed = http_parser_execute(&parser_, NULL, 0); nparsed = http_parser_execute(&parser_, NULL, 0);
Emit(eof_symbol, 0, NULL); Emit(end_symbol, 0, NULL);
} }
int int

12
src/node_net.cc

@ -32,12 +32,12 @@ static Persistent<String> write_only_symbol;
static Persistent<String> closing_symbol; static Persistent<String> closing_symbol;
static Persistent<String> closed_symbol; static Persistent<String> closed_symbol;
static Persistent<String> receive_symbol; static Persistent<String> data_symbol;
static Persistent<String> connection_symbol; static Persistent<String> connection_symbol;
static Persistent<String> connect_symbol; static Persistent<String> connect_symbol;
static Persistent<String> timeout_symbol; static Persistent<String> timeout_symbol;
static Persistent<String> drain_symbol; static Persistent<String> drain_symbol;
static Persistent<String> eof_symbol; static Persistent<String> end_symbol;
static Persistent<String> close_symbol; static Persistent<String> close_symbol;
static const struct addrinfo server_tcp_hints = static const struct addrinfo server_tcp_hints =
@ -76,12 +76,12 @@ void Connection::Initialize(v8::Handle<v8::Object> target) {
closing_symbol = NODE_PSYMBOL("closing"); closing_symbol = NODE_PSYMBOL("closing");
closed_symbol = NODE_PSYMBOL("closed"); closed_symbol = NODE_PSYMBOL("closed");
receive_symbol = NODE_PSYMBOL("receive"); data_symbol = NODE_PSYMBOL("data");
connection_symbol = NODE_PSYMBOL("connection"); connection_symbol = NODE_PSYMBOL("connection");
connect_symbol = NODE_PSYMBOL("connect"); connect_symbol = NODE_PSYMBOL("connect");
timeout_symbol = NODE_PSYMBOL("timeout"); timeout_symbol = NODE_PSYMBOL("timeout");
drain_symbol = NODE_PSYMBOL("drain"); drain_symbol = NODE_PSYMBOL("drain");
eof_symbol = NODE_PSYMBOL("eof"); end_symbol = NODE_PSYMBOL("end");
close_symbol = NODE_PSYMBOL("close"); close_symbol = NODE_PSYMBOL("close");
Local<FunctionTemplate> t = FunctionTemplate::New(New); Local<FunctionTemplate> t = FunctionTemplate::New(New);
@ -618,7 +618,7 @@ Handle<Value> Connection::Send(const Arguments& args) {
void Connection::OnReceive(const void *buf, size_t len) { void Connection::OnReceive(const void *buf, size_t len) {
HandleScope scope; HandleScope scope;
Local<Value> data = Encode(buf, len, encoding_); Local<Value> data = Encode(buf, len, encoding_);
Emit(receive_symbol, 1, &data); Emit(data_symbol, 1, &data);
} }
void Connection::OnClose() { void Connection::OnClose() {
@ -656,7 +656,7 @@ void Connection::OnDrain() {
void Connection::OnEOF() { void Connection::OnEOF() {
HandleScope scope; HandleScope scope;
Emit(eof_symbol, 0, NULL); Emit(end_symbol, 0, NULL);
} }
Persistent<FunctionTemplate> Server::constructor_template; Persistent<FunctionTemplate> Server::constructor_template;

4
test/mjsunit/test-http-1.0.js

@ -23,12 +23,12 @@ c.addListener("connect", function () {
c.send( "GET / HTTP/1.0\r\n\r\n" ); c.send( "GET / HTTP/1.0\r\n\r\n" );
}); });
c.addListener("receive", function (chunk) { c.addListener("data", function (chunk) {
puts(chunk); puts(chunk);
server_response += chunk; server_response += chunk;
}); });
c.addListener("eof", function () { c.addListener("end", function () {
client_got_eof = true; client_got_eof = true;
c.close(); c.close();
server.close(); server.close();

2
test/mjsunit/test-http-client-reconnect-bug.js

@ -21,7 +21,7 @@ client.addListener("error", function() {
errorCount++; errorCount++;
}); });
client.addListener("eof", function() { client.addListener("end", function() {
sys.puts("EOF!"); sys.puts("EOF!");
eofCount++; eofCount++;
}); });

4
test/mjsunit/test-http-server.js

@ -54,7 +54,7 @@ c.addListener("connect", function () {
requests_sent += 1; requests_sent += 1;
}); });
c.addListener("receive", function (chunk) { c.addListener("data", function (chunk) {
server_response += chunk; server_response += chunk;
if (requests_sent == 1) { if (requests_sent == 1) {
@ -72,7 +72,7 @@ c.addListener("receive", function (chunk) {
}); });
c.addListener("eof", function () { c.addListener("end", function () {
client_got_eof = true; client_got_eof = true;
}); });

6
test/mjsunit/test-http-wget.js

@ -40,14 +40,14 @@ c.addListener("connect", function () {
"Connection: Keep-Alive\r\n\r\n"); "Connection: Keep-Alive\r\n\r\n");
}); });
c.addListener("receive", function (chunk) { c.addListener("data", function (chunk) {
puts(chunk); puts(chunk);
server_response += chunk; server_response += chunk;
}); });
c.addListener("eof", function () { c.addListener("end", function () {
client_got_eof = true; client_got_eof = true;
puts('got eof'); puts('got end');
c.close(); c.close();
}); });

2
test/mjsunit/test-stdio.js

@ -24,7 +24,7 @@ child.addListener("output", function (data){
child.close(); child.close();
} }
} else { } else {
puts('child eof'); puts('child end');
} }
}); });

6
test/mjsunit/test-tcp-binary.js

@ -21,11 +21,11 @@ for (var i = 255; i >= 0; i--) {
var echoServer = tcp.createServer(function (connection) { var echoServer = tcp.createServer(function (connection) {
connection.setEncoding("binary"); connection.setEncoding("binary");
connection.addListener("receive", function (chunk) { connection.addListener("data", function (chunk) {
error("recved: " + JSON.stringify(chunk)); error("recved: " + JSON.stringify(chunk));
connection.send(chunk, "binary"); connection.send(chunk, "binary");
}); });
connection.addListener("eof", function () { connection.addListener("end", function () {
connection.close(); connection.close();
}); });
}); });
@ -37,7 +37,7 @@ var j = 0;
var c = tcp.createConnection(PORT); var c = tcp.createConnection(PORT);
c.setEncoding("binary"); c.setEncoding("binary");
c.addListener("receive", function (chunk) { c.addListener("data", function (chunk) {
if (j < 256) { if (j < 256) {
error("send " + j); error("send " + j);
c.send(String.fromCharCode(j), "binary"); c.send(String.fromCharCode(j), "binary");

4
test/mjsunit/test-tcp-many-clients.js

@ -35,11 +35,11 @@ function runClient (callback) {
client.connections += 1; client.connections += 1;
}); });
client.addListener("receive", function (chunk) { client.addListener("data", function (chunk) {
this.recved += chunk; this.recved += chunk;
}); });
client.addListener("eof", function (had_error) { client.addListener("end", function (had_error) {
client.close(); client.close();
}); });

6
test/mjsunit/test-tcp-pingpong-delay.js

@ -13,7 +13,7 @@ function pingPongTest (port, host, on_complete) {
var server = tcp.createServer(function (socket) { var server = tcp.createServer(function (socket) {
socket.setEncoding("utf8"); socket.setEncoding("utf8");
socket.addListener("receive", function (data) { socket.addListener("data", function (data) {
puts(data); puts(data);
assert.equal("PING", data); assert.equal("PING", data);
assert.equal("open", socket.readyState); assert.equal("open", socket.readyState);
@ -29,7 +29,7 @@ function pingPongTest (port, host, on_complete) {
assert.equal(false, true); assert.equal(false, true);
}); });
socket.addListener("eof", function () { socket.addListener("end", function () {
puts("server-side socket EOF"); puts("server-side socket EOF");
assert.equal("writeOnly", socket.readyState); assert.equal("writeOnly", socket.readyState);
socket.close(); socket.close();
@ -53,7 +53,7 @@ function pingPongTest (port, host, on_complete) {
client.send("PING"); client.send("PING");
}); });
client.addListener("receive", function (data) { client.addListener("data", function (data) {
puts(data); puts(data);
assert.equal("PONG", data); assert.equal("PONG", data);
assert.equal("open", client.readyState); assert.equal("open", client.readyState);

6
test/mjsunit/test-tcp-pingpong.js

@ -21,7 +21,7 @@ function pingPongTest (port, host, on_complete) {
socket.setNoDelay(); socket.setNoDelay();
socket.timeout = 0; socket.timeout = 0;
socket.addListener("receive", function (data) { socket.addListener("data", function (data) {
puts("server got: " + JSON.stringify(data)); puts("server got: " + JSON.stringify(data));
assert.equal("open", socket.readyState); assert.equal("open", socket.readyState);
assert.equal(true, count <= N); assert.equal(true, count <= N);
@ -30,7 +30,7 @@ function pingPongTest (port, host, on_complete) {
} }
}); });
socket.addListener("eof", function () { socket.addListener("end", function () {
assert.equal("writeOnly", socket.readyState); assert.equal("writeOnly", socket.readyState);
socket.close(); socket.close();
}); });
@ -52,7 +52,7 @@ function pingPongTest (port, host, on_complete) {
client.send("PING"); client.send("PING");
}); });
client.addListener("receive", function (data) { client.addListener("data", function (data) {
assert.equal("PONG", data); assert.equal("PONG", data);
count += 1; count += 1;

4
test/mjsunit/test-tcp-reconnect.js

@ -12,7 +12,7 @@ var server = tcp.createServer(function (socket) {
socket.send("hello\r\n"); socket.send("hello\r\n");
}); });
socket.addListener("eof", function () { socket.addListener("end", function () {
socket.close(); socket.close();
}); });
@ -31,7 +31,7 @@ client.addListener("connect", function () {
puts("client connected."); puts("client connected.");
}); });
client.addListener("receive", function (chunk) { client.addListener("data", function (chunk) {
client_recv_count += 1; client_recv_count += 1;
puts("client_recv_count " + client_recv_count); puts("client_recv_count " + client_recv_count);
assert.equal("hello\r\n", chunk); assert.equal("hello\r\n", chunk);

4
test/mjsunit/test-tcp-throttle-kernel-buffer.js

@ -27,7 +27,7 @@ npauses = 0;
var paused = false; var paused = false;
client = tcp.createConnection(PORT); client = tcp.createConnection(PORT);
client.setEncoding("ascii"); client.setEncoding("ascii");
client.addListener("receive", function (d) { client.addListener("data", function (d) {
chars_recved += d.length; chars_recved += d.length;
puts("got " + chars_recved); puts("got " + chars_recved);
if (!paused) { if (!paused) {
@ -45,7 +45,7 @@ client.addListener("receive", function (d) {
} }
}); });
client.addListener("eof", function () { client.addListener("end", function () {
server.close(); server.close();
client.close(); client.close();
}); });

4
test/mjsunit/test-tcp-throttle.js

@ -24,7 +24,7 @@ chars_recved = 0;
client = tcp.createConnection(PORT); client = tcp.createConnection(PORT);
client.setEncoding("ascii"); client.setEncoding("ascii");
client.addListener("receive", function (d) { client.addListener("data", function (d) {
print(d); print(d);
recv += d; recv += d;
}); });
@ -57,7 +57,7 @@ setTimeout(function () {
}, 500); }, 500);
client.addListener("eof", function () { client.addListener("end", function () {
server.close(); server.close();
client.close(); client.close();
}); });

10
test/mjsunit/test-tcp-timeout.js

@ -15,12 +15,12 @@ var echo_server = tcp.createServer(function (socket) {
p(timeouttime); p(timeouttime);
}); });
socket.addListener("receive", function (d) { socket.addListener("data", function (d) {
p(d); p(d);
socket.send(d); socket.send(d);
}); });
socket.addListener("eof", function () { socket.addListener("end", function () {
socket.close(); socket.close();
}); });
}); });
@ -36,7 +36,7 @@ client.addListener("connect", function () {
client.send("hello\r\n"); client.send("hello\r\n");
}); });
client.addListener("receive", function (chunk) { client.addListener("data", function (chunk) {
assert.equal("hello\r\n", chunk); assert.equal("hello\r\n", chunk);
if (exchanges++ < 5) { if (exchanges++ < 5) {
setTimeout(function () { setTimeout(function () {
@ -57,8 +57,8 @@ client.addListener("timeout", function () {
assert.equal(false, true); assert.equal(false, true);
}); });
client.addListener("eof", function () { client.addListener("end", function () {
puts("client eof"); puts("client end");
client.close(); client.close();
}); });

6
test/mjsunit/test-tcp-tls.js

@ -21,7 +21,7 @@ function tlsTest (port, host, caPem, keyPem, certPem) {
socket.setNoDelay(); socket.setNoDelay();
socket.timeout = 0; socket.timeout = 0;
socket.addListener("receive", function (data) { socket.addListener("data", function (data) {
var verified = socket.verifyPeer(); var verified = socket.verifyPeer();
var peerDN = socket.getPeerCertificate("DNstring"); var peerDN = socket.getPeerCertificate("DNstring");
assert.equal(verified, 1); assert.equal(verified, 1);
@ -35,7 +35,7 @@ function tlsTest (port, host, caPem, keyPem, certPem) {
} }
}); });
socket.addListener("eof", function () { socket.addListener("end", function () {
assert.equal("writeOnly", socket.readyState); assert.equal("writeOnly", socket.readyState);
socket.close(); socket.close();
}); });
@ -65,7 +65,7 @@ function tlsTest (port, host, caPem, keyPem, certPem) {
client.send("PING"); client.send("PING");
}); });
client.addListener("receive", function (data) { client.addListener("data", function (data) {
assert.equal("PONG", data); assert.equal("PONG", data);
count += 1; count += 1;

Loading…
Cancel
Save