]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | |
2 | var HOST = 'localhost' | |
3 | var PORT = 12345; | |
4 | var EXIT_TIMEOUT = 300e3; | |
5 | ||
6 | print('automatic exit after ' + (EXIT_TIMEOUT / 1e3) + ' seconds'); | |
7 | setTimeout(function () { | |
8 | print('exit timer'); | |
9 | EventLoop.requestExit(); | |
10 | }, EXIT_TIMEOUT); | |
11 | ||
12 | print('listen on ' + HOST + ':' + PORT); | |
13 | EventLoop.server(HOST, PORT, function (fd, addr, port) { | |
14 | print('new connection on fd ' + fd + ' from ' + addr + ':' + port); | |
15 | EventLoop.setReader(fd, function (fd, data) { | |
16 | var b, i, n, x; | |
17 | ||
18 | // Handle socket data carefully: if you convert it to a string, | |
19 | // it may not be valid UTF-8 etc. Here we operate on the data | |
20 | // directly in the buffer. | |
21 | ||
22 | b = data.valueOf(); // ensure we get a plain buffer | |
23 | n = b.length; | |
24 | for (i = 0; i < n; i++) { | |
25 | x = b[i]; | |
26 | if (x >= 0x61 && x <= 0x7a) { | |
27 | b[i] = x - 0x20; // uppercase | |
28 | } | |
29 | } | |
30 | ||
31 | print('read data on fd ' + fd + ', length ' + data.length); | |
32 | EventLoop.write(fd, data); | |
33 | }); | |
34 | }); |