From 095470854b48bc9f30c52a69a813945a9e908b61 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 28 Sep 2009 16:08:09 +0200 Subject: [PATCH] Move tcp library to /tcp.js --- doc/api.html | 26 +++++++++-------- doc/api.txt | 25 +++++++++-------- doc/api.xml | 28 ++++++++++--------- doc/node.1 | 24 ++++++++-------- lib/tcp.js | 12 ++++++++ src/node.js | 11 ++------ test/mjsunit/test-http-malformed-request.js | 3 +- test/mjsunit/test-http-server.js | 3 +- test/mjsunit/test-tcp-binary.js | 5 ++-- test/mjsunit/test-tcp-many-clients.js | 5 ++-- test/mjsunit/test-tcp-pingpong-delay.js | 5 ++-- test/mjsunit/test-tcp-pingpong.js | 5 ++-- test/mjsunit/test-tcp-reconnect.js | 5 ++-- .../test-tcp-throttle-kernel-buffer.js | 5 ++-- test/mjsunit/test-tcp-throttle.js | 5 ++-- test/mjsunit/test-tcp-timeout.js | 5 ++-- 16 files changed, 99 insertions(+), 73 deletions(-) create mode 100644 lib/tcp.js diff --git a/doc/api.html b/doc/api.html index d564939739..b396955dbd 100644 --- a/doc/api.html +++ b/doc/api.html @@ -1015,7 +1015,7 @@ Returns a new web server object.

The options argument is optional. The options argument accepts the same values as the -options argument for node.tcp.Server does.

+options argument for tcp.Server does.

The request_listener is a function which is automatically added to the "request" event.

@@ -1464,12 +1464,15 @@ After emitted no other events will be emitted on the response.

TCP

-

node.tcp.Server

+

To use the TCP server and client one must require("/tcp.js") or +include("/tcp.js").

+

tcp.Server

Here is an example of a echo server which listens for connections on port 7000

-
function echo (socket) {
+
include("/tcp.js");
+var server = createServer(function (socket) {
   socket.setEncoding("utf8");
   socket.addListener("connect", function () {
     socket.send("hello\r\n");
@@ -1481,8 +1484,7 @@ on port 7000

socket.send("goodbye\r\n"); socket.close(); }); -} -var server = node.tcp.createServer(echo); +}); server.listen(7000, "localhost");
@@ -1505,7 +1507,7 @@ cellspacing="0" cellpadding="4">

"connection"

connection

Emitted when a new connection is made. - connection is an instance of node.tcp.Connection.

+ connection is an instance of tcp.Connection.

"close"

@@ -1520,7 +1522,7 @@ cellspacing="0" cellpadding="4">
-node.tcp.createServer(connection_listener); +tcp.createServer(connection_listener);

@@ -1554,9 +1556,9 @@ event.

-

node.tcp.Connection

+

tcp.Connection

This object is used as a TCP client and also as a server-side -socket for node.tcp.Server.

+socket for tcp.Server.

-node.tcp.createConnection(port, host="127.0.0.1") +tcp.createConnection(port, host="127.0.0.1")

@@ -1721,7 +1723,7 @@ Resumes reading if reading was paused by readPause().

Sets the connection to timeout after timeout milliseconds of inactivity on -the connection. By default all node.tcp.Connection objects have a timeout +the connection. By default all tcp.Connection objects have a timeout of 60 seconds (60000 ms).

If timeout is 0, then the idle timeout is disabled.

@@ -1946,7 +1948,7 @@ init (Handle<Object> target) diff --git a/doc/api.txt b/doc/api.txt index cd7f178ff6..141be0dd91 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -625,7 +625,7 @@ Returns a new web server object. + The +options+ argument is optional. The +options+ argument accepts the same values as the -options argument for +node.tcp.Server+ does. +options argument for +tcp.Server+ does. + The +request_listener+ is a function which is automatically added to the +"request"+ event. @@ -942,13 +942,17 @@ After emitted no other events will be emitted on the response. === TCP -==== +node.tcp.Server+ +To use the TCP server and client one must +require("/tcp.js")+ or ++include("/tcp.js")+. + +==== +tcp.Server+ Here is an example of a echo server which listens for connections on port 7000 ---------------------------------------- -function echo (socket) { +include("/tcp.js"); +var server = createServer(function (socket) { socket.setEncoding("utf8"); socket.addListener("connect", function () { socket.send("hello\r\n"); @@ -960,8 +964,7 @@ function echo (socket) { socket.send("goodbye\r\n"); socket.close(); }); -} -var server = node.tcp.createServer(echo); +}); server.listen(7000, "localhost"); ---------------------------------------- @@ -970,14 +973,14 @@ server.listen(7000, "localhost"); |========================================================= |Event | Parameters | Notes |+"connection"+ | +connection+ | Emitted when a new connection is made. - +connection+ is an instance of +node.tcp.Connection+. + +connection+ is an instance of +tcp.Connection+. |+"close"+ | +errorno+ | Emitted when the server closes. +errorno+ is an integer which indicates what, if any, error caused the server to close. If no error occurred +errorno+ will be 0. |========================================================= -+node.tcp.createServer(connection_listener);+ :: ++tcp.createServer(connection_listener);+ :: Creates a new TCP server. + The +connection_listener+ argument is automatically set as a listener for @@ -1003,10 +1006,10 @@ asynchronous, the server is finally closed when the server emits a +"close"+ event. -==== +node.tcp.Connection+ +==== +tcp.Connection+ This object is used as a TCP client and also as a server-side -socket for +node.tcp.Server+. +socket for +tcp.Server+. [cols="1,2,10",options="header"] |========================================================= @@ -1034,7 +1037,7 @@ socket for +node.tcp.Server+. (TODO: access error codes.) |========================================================= -+node.tcp.createConnection(port, host="127.0.0.1")+:: ++tcp.createConnection(port, host="127.0.0.1")+:: Creates a new connection object and opens a connection to the specified +port+ and +host+. If the second parameter is omitted, localhost is assumed. + @@ -1090,7 +1093,7 @@ Resumes reading if reading was paused by +readPause()+. +connection.setTimeout(timeout)+:: Sets the connection to timeout after +timeout+ milliseconds of inactivity on -the connection. By default all +node.tcp.Connection+ objects have a timeout +the connection. By default all +tcp.Connection+ objects have a timeout of 60 seconds (60000 ms). + If +timeout+ is 0, then the idle timeout is disabled. diff --git a/doc/api.xml b/doc/api.xml index c6ccf14c12..311ec9c8ae 100644 --- a/doc/api.xml +++ b/doc/api.xml @@ -1062,7 +1062,7 @@ Returns a new web server object. The options argument is optional. The options argument accepts the same values as the -options argument for node.tcp.Server does. +options argument for tcp.Server does. The request_listener is a function which is automatically added to the "request" event. @@ -1551,11 +1551,14 @@ After emitted no other events will be emitted on the response. TCP - -<literal>node.tcp.Server</literal> +To use the TCP server and client one must require("/tcp.js") or +include("/tcp.js"). + +<literal>tcp.Server</literal> Here is an example of a echo server which listens for connections on port 7000 -function echo (socket) { +include("/tcp.js"); +var server = createServer(function (socket) { socket.setEncoding("utf8"); socket.addListener("connect", function () { socket.send("hello\r\n"); @@ -1567,8 +1570,7 @@ on port 7000 socket.send("goodbye\r\n"); socket.close(); }); -} -var server = node.tcp.createServer(echo); +}); server.listen(7000, "localhost"); "connection" connection Emitted when a new connection is made. - connection is an instance of node.tcp.Connection. + connection is an instance of tcp.Connection. "close" @@ -1606,7 +1608,7 @@ rowsep="1" colsep="1" -node.tcp.createServer(connection_listener); +tcp.createServer(connection_listener); @@ -1646,10 +1648,10 @@ event. - -<literal>node.tcp.Connection</literal> + +<literal>tcp.Connection</literal> This object is used as a TCP client and also as a server-side -socket for node.tcp.Server. +socket for tcp.Server. -node.tcp.createConnection(port, host="127.0.0.1") +tcp.createConnection(port, host="127.0.0.1") @@ -1835,7 +1837,7 @@ Resumes reading if reading was paused by readPause(). Sets the connection to timeout after timeout milliseconds of inactivity on -the connection. By default all node.tcp.Connection objects have a timeout +the connection. By default all tcp.Connection objects have a timeout of 60 seconds (60000 ms). If timeout is 0, then the idle timeout is disabled. diff --git a/doc/node.1 b/doc/node.1 index 75b9811620..1b08915ea2 100644 --- a/doc/node.1 +++ b/doc/node.1 @@ -900,7 +900,7 @@ options argument is optional\. The options argument accepts the same values as the options argument for -node\.tcp\.Server +tcp\.Server does\. .sp The @@ -1364,19 +1364,22 @@ that this response belongs to\. .RE .RE .SS "TCP" +To use the TCP server and client one must require("/tcp\.js") or include("/tcp\.js")\. +.sp .sp .it 1 an-trap .nr an-no-space-flag 1 .nr an-break-flag 1 .br -node.tcp.Server +tcp.Server .RS Here is an example of a echo server which listens for connections on port 7000 .sp .sp .RS 4 .nf -function echo (socket) { +include("/tcp\.js"); +var server = createServer(function (socket) { socket\.setEncoding("utf8"); socket\.addListener("connect", function () { socket\.send("hello\er\en"); @@ -1388,8 +1391,7 @@ function echo (socket) { socket\.send("goodbye\er\en"); socket\.close(); }); -} -var server = node\.tcp\.createServer(echo); +}); server\.listen(7000, "localhost"); .fi .RE @@ -1413,7 +1415,7 @@ T}:T{ connection .sp T}:T{ -Emitted when a new connection is made\. connection is an instance of node\.tcp\.Connection\. +Emitted when a new connection is made\. connection is an instance of tcp\.Connection\. .sp T} T{ @@ -1428,7 +1430,7 @@ Emitted when the server closes\. errorno is an integer which indicates what, if T} .TE .PP -node\.tcp\.createServer(connection_listener); +tcp\.createServer(connection_listener); .RS 4 Creates a new TCP server\. .sp @@ -1471,9 +1473,9 @@ event\. .nr an-no-space-flag 1 .nr an-break-flag 1 .br -node.tcp.Connection +tcp.Connection .RS -This object is used as a TCP client and also as a server\-side socket for node\.tcp\.Server\. +This object is used as a TCP client and also as a server\-side socket for tcp\.Server\. .sp .TS allbox tab(:); @@ -1540,7 +1542,7 @@ Emitted once the connection is fully closed\. The argument had_error is a boolea T} .TE .PP -node\.tcp\.createConnection(port, host="127\.0\.0\.1") +tcp\.createConnection(port, host="127\.0\.0\.1") .RS 4 Creates a new connection object and opens a connection to the specified port @@ -1635,7 +1637,7 @@ connection\.setTimeout(timeout) Sets the connection to timeout after timeout milliseconds of inactivity on the connection\. By default all -node\.tcp\.Connection +tcp\.Connection objects have a timeout of 60 seconds (60000 ms)\. .sp If diff --git a/lib/tcp.js b/lib/tcp.js new file mode 100644 index 0000000000..f3b8aeaa53 --- /dev/null +++ b/lib/tcp.js @@ -0,0 +1,12 @@ +exports.createServer = function (on_connection, options) { + var server = new node.tcp.Server(); + server.addListener("connection", on_connection); + //server.setOptions(options); + return server; +}; + +exports.createConnection = function (port, host) { + var connection = new node.tcp.Connection(); + connection.connect(port, host); + return connection; +}; diff --git a/src/node.js b/src/node.js index f78de6b86f..9247a02cf5 100644 --- a/src/node.js +++ b/src/node.js @@ -1,8 +1,5 @@ -node.tcp.createServer = function (on_connection, options) { - var server = new node.tcp.Server(); - server.addListener("connection", on_connection); - //server.setOptions(options); - return server; +node.tcp.createServer = function () { + throw new Error("node.tcp.createServer() has moved. Use require('/tcp.js') to access it."); }; node.createProcess = function () { @@ -28,9 +25,7 @@ node.http.createClient = function () { } node.tcp.createConnection = function (port, host) { - var connection = new node.tcp.Connection(); - connection.connect(port, host); - return connection; + throw new Error("node.tcp.createConnection() has moved. Use require('/tcp.js') to access it."); }; // Timers diff --git a/test/mjsunit/test-http-malformed-request.js b/test/mjsunit/test-http-malformed-request.js index 158ee3b90f..72ddb611d6 100644 --- a/test/mjsunit/test-http-malformed-request.js +++ b/test/mjsunit/test-http-malformed-request.js @@ -1,4 +1,5 @@ include("common.js"); +tcp = require("/tcp.js"); http = require("/http.js"); // Make sure no exceptions are thrown when receiving malformed HTTP @@ -19,7 +20,7 @@ var s = http.createServer(function (req, res) { }); s.listen(port); -var c = node.tcp.createConnection(port); +var c = tcp.createConnection(port); c.addListener("connect", function () { c.send("GET /hello?foo=%99bar HTTP/1.1\r\n\r\n"); c.close(); diff --git a/test/mjsunit/test-http-server.js b/test/mjsunit/test-http-server.js index 82bb217da8..f2c521e4c3 100644 --- a/test/mjsunit/test-http-server.js +++ b/test/mjsunit/test-http-server.js @@ -1,4 +1,5 @@ include("common.js"); +tcp = require("/tcp.js"); http = require("/http.js"); var port = 8222; @@ -32,7 +33,7 @@ http.createServer(function (req, res) { }).listen(port); -var c = node.tcp.createConnection(port); +var c = tcp.createConnection(port); c.setEncoding("utf8"); diff --git a/test/mjsunit/test-tcp-binary.js b/test/mjsunit/test-tcp-binary.js index 0758d9ac31..db431a9d06 100644 --- a/test/mjsunit/test-tcp-binary.js +++ b/test/mjsunit/test-tcp-binary.js @@ -1,4 +1,5 @@ include("common.js"); +tcp = require("/tcp.js"); PORT = 23123; binaryString = ""; @@ -18,7 +19,7 @@ for (var i = 255; i >= 0; i--) { binaryString += S; } -var echoServer = node.tcp.createServer(function (connection) { +var echoServer = tcp.createServer(function (connection) { connection.setEncoding("binary"); connection.addListener("receive", function (chunk) { error("recved: " + JSON.stringify(chunk)); @@ -33,7 +34,7 @@ echoServer.listen(PORT); var recv = ""; var j = 0; -var c = node.tcp.createConnection(PORT); +var c = tcp.createConnection(PORT); c.setEncoding("binary"); c.addListener("receive", function (chunk) { diff --git a/test/mjsunit/test-tcp-many-clients.js b/test/mjsunit/test-tcp-many-clients.js index 1717ccf2f2..0bb644aa2c 100644 --- a/test/mjsunit/test-tcp-many-clients.js +++ b/test/mjsunit/test-tcp-many-clients.js @@ -1,4 +1,5 @@ include("common.js"); +tcp = require("/tcp.js"); // settings var port = 20743; var bytes = 1024*40; @@ -13,7 +14,7 @@ for (var i = 0; i < bytes; i++) { body += "C"; } -var server = node.tcp.createServer(function (c) { +var server = tcp.createServer(function (c) { c.addListener("connect", function () { total_connections++; print("#"); @@ -24,7 +25,7 @@ var server = node.tcp.createServer(function (c) { server.listen(port); function runClient (callback) { - var client = node.tcp.createConnection(port); + var client = tcp.createConnection(port); client.connections = 0; client.setEncoding("utf8"); diff --git a/test/mjsunit/test-tcp-pingpong-delay.js b/test/mjsunit/test-tcp-pingpong-delay.js index fd0149b5aa..298aff922a 100644 --- a/test/mjsunit/test-tcp-pingpong-delay.js +++ b/test/mjsunit/test-tcp-pingpong-delay.js @@ -1,4 +1,5 @@ include("common.js"); +tcp = require("/tcp.js"); var tests_run = 0; @@ -9,7 +10,7 @@ function pingPongTest (port, host, on_complete) { var count = 0; var client_closed = false; - var server = node.tcp.createServer(function (socket) { + var server = tcp.createServer(function (socket) { socket.setEncoding("utf8"); socket.addListener("receive", function (data) { @@ -43,7 +44,7 @@ function pingPongTest (port, host, on_complete) { }); server.listen(port, host); - var client = node.tcp.createConnection(port, host); + var client = tcp.createConnection(port, host); client.setEncoding("utf8"); diff --git a/test/mjsunit/test-tcp-pingpong.js b/test/mjsunit/test-tcp-pingpong.js index 677317026b..d1e068da34 100644 --- a/test/mjsunit/test-tcp-pingpong.js +++ b/test/mjsunit/test-tcp-pingpong.js @@ -1,4 +1,5 @@ include("common.js"); +tcp = require("/tcp.js"); var tests_run = 0; @@ -8,7 +9,7 @@ function pingPongTest (port, host, on_complete) { var count = 0; var sent_final_ping = false; - var server = node.tcp.createServer(function (socket) { + var server = tcp.createServer(function (socket) { assertTrue(socket.remoteAddress !== null); assertTrue(socket.remoteAddress !== undefined); if (host === "127.0.0.1") @@ -42,7 +43,7 @@ function pingPongTest (port, host, on_complete) { }); server.listen(port, host); - var client = node.tcp.createConnection(port, host); + var client = tcp.createConnection(port, host); client.setEncoding("utf8"); diff --git a/test/mjsunit/test-tcp-reconnect.js b/test/mjsunit/test-tcp-reconnect.js index b805e9d471..c6cf6b9eee 100644 --- a/test/mjsunit/test-tcp-reconnect.js +++ b/test/mjsunit/test-tcp-reconnect.js @@ -1,4 +1,5 @@ include("common.js"); +tcp = require("/tcp.js"); var N = 50; var port = 8921; @@ -6,7 +7,7 @@ var c = 0; var client_recv_count = 0; var disconnect_count = 0; -var server = node.tcp.createServer(function (socket) { +var server = tcp.createServer(function (socket) { socket.addListener("connect", function () { socket.send("hello\r\n"); }); @@ -22,7 +23,7 @@ var server = node.tcp.createServer(function (socket) { }); server.listen(port); -var client = node.tcp.createConnection(port); +var client = tcp.createConnection(port); client.setEncoding("UTF8"); diff --git a/test/mjsunit/test-tcp-throttle-kernel-buffer.js b/test/mjsunit/test-tcp-throttle-kernel-buffer.js index eb0523cc01..86cf4613fa 100644 --- a/test/mjsunit/test-tcp-throttle-kernel-buffer.js +++ b/test/mjsunit/test-tcp-throttle-kernel-buffer.js @@ -1,4 +1,5 @@ include("common.js"); +tcp = require("/tcp.js"); PORT = 20444; N = 30*1024; // 500kb @@ -10,7 +11,7 @@ for (var i = 0; i < N; i++) { puts("start server on port " + PORT); -server = node.tcp.createServer(function (connection) { +server = tcp.createServer(function (connection) { connection.addListener("connect", function () { connection.send(body); connection.close(); @@ -24,7 +25,7 @@ npauses = 0; var paused = false; -client = node.tcp.createConnection(PORT); +client = tcp.createConnection(PORT); client.setEncoding("ascii"); client.addListener("receive", function (d) { chars_recved += d.length; diff --git a/test/mjsunit/test-tcp-throttle.js b/test/mjsunit/test-tcp-throttle.js index 063019df50..bcd3bcbb3c 100644 --- a/test/mjsunit/test-tcp-throttle.js +++ b/test/mjsunit/test-tcp-throttle.js @@ -1,8 +1,9 @@ include("common.js"); +tcp = require("/tcp.js"); PORT = 20443; N = 200; -server = node.tcp.createServer(function (connection) { +server = tcp.createServer(function (connection) { function send (j) { if (j >= N) { connection.close(); @@ -21,7 +22,7 @@ server.listen(PORT); recv = ""; chars_recved = 0; -client = node.tcp.createConnection(PORT); +client = tcp.createConnection(PORT); client.setEncoding("ascii"); client.addListener("receive", function (d) { print(d); diff --git a/test/mjsunit/test-tcp-timeout.js b/test/mjsunit/test-tcp-timeout.js index 0f84a70a47..c60fcec32b 100644 --- a/test/mjsunit/test-tcp-timeout.js +++ b/test/mjsunit/test-tcp-timeout.js @@ -1,11 +1,12 @@ include("common.js"); +tcp = require("/tcp.js"); port = 9992; exchanges = 0; starttime = null; timeouttime = null; timeout = 1000; -var echo_server = node.tcp.createServer(function (socket) { +var echo_server = tcp.createServer(function (socket) { socket.setTimeout(timeout); socket.addListener("timeout", function (d) { @@ -27,7 +28,7 @@ var echo_server = node.tcp.createServer(function (socket) { echo_server.listen(port); puts("server listening at " + port); -var client = node.tcp.createConnection(port); +var client = tcp.createConnection(port); client.setEncoding("UTF8"); client.setTimeout(0); // disable the timeout for client client.addListener("connect", function () {