2011-03-10 00:54:52 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2010-12-04 15:20:34 -08:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
|
|
|
var net = require('net');
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2010-12-05 01:45:52 +03:00
|
|
|
var binaryString = '';
|
2009-09-21 12:27:22 +02:00
|
|
|
for (var i = 255; i >= 0; i--) {
|
2010-12-05 01:45:52 +03:00
|
|
|
var s = '\'\\' + i.toString(8) + '\'';
|
2010-12-04 16:11:57 -08:00
|
|
|
var S = eval(s);
|
2010-12-05 01:45:52 +03:00
|
|
|
common.error(s +
|
|
|
|
' ' +
|
|
|
|
JSON.stringify(S) +
|
|
|
|
' ' +
|
|
|
|
JSON.stringify(String.fromCharCode(i)) +
|
|
|
|
' ' +
|
|
|
|
S.charCodeAt(0));
|
2011-01-27 17:02:25 -08:00
|
|
|
assert.ok(S.charCodeAt(0) == i);
|
|
|
|
assert.ok(S == String.fromCharCode(i));
|
2009-09-21 12:27:22 +02:00
|
|
|
binaryString += S;
|
|
|
|
}
|
|
|
|
|
2010-08-27 15:38:46 -07:00
|
|
|
// safe constructor
|
2010-12-05 01:45:52 +03:00
|
|
|
var echoServer = net.Server(function(connection) {
|
|
|
|
connection.setEncoding('binary');
|
2011-10-15 01:08:36 +02:00
|
|
|
connection.on('data', function(chunk) {
|
2010-12-05 01:45:52 +03:00
|
|
|
common.error('recved: ' + JSON.stringify(chunk));
|
|
|
|
connection.write(chunk, 'binary');
|
2009-09-21 12:27:22 +02:00
|
|
|
});
|
2011-10-15 01:08:36 +02:00
|
|
|
connection.on('end', function() {
|
2010-04-08 10:44:22 -07:00
|
|
|
connection.end();
|
2009-09-21 12:27:22 +02:00
|
|
|
});
|
|
|
|
});
|
2010-07-15 11:47:25 -07:00
|
|
|
echoServer.listen(common.PORT);
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2010-12-05 01:45:52 +03:00
|
|
|
var recv = '';
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
echoServer.on('listening', function() {
|
2010-08-12 01:38:42 +02:00
|
|
|
var j = 0;
|
2010-08-27 15:38:46 -07:00
|
|
|
var c = net.createConnection(common.PORT);
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2010-12-05 01:45:52 +03:00
|
|
|
c.setEncoding('binary');
|
2011-10-15 01:08:36 +02:00
|
|
|
c.on('data', function(chunk) {
|
2010-08-12 01:38:42 +02:00
|
|
|
if (j < 256) {
|
2010-12-05 01:45:52 +03:00
|
|
|
common.error('write ' + j);
|
|
|
|
c.write(String.fromCharCode(j), 'binary');
|
2010-08-12 01:38:42 +02:00
|
|
|
j++;
|
|
|
|
} else {
|
|
|
|
c.end();
|
|
|
|
}
|
|
|
|
recv += chunk;
|
|
|
|
});
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
c.on('connect', function() {
|
2010-12-05 01:45:52 +03:00
|
|
|
c.write(binaryString, 'binary');
|
2010-08-12 01:38:42 +02:00
|
|
|
});
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
c.on('close', function() {
|
2010-10-12 10:09:02 +11:00
|
|
|
console.dir(recv);
|
2010-08-12 01:38:42 +02:00
|
|
|
echoServer.close();
|
|
|
|
});
|
2009-09-21 12:27:22 +02:00
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
process.on('exit', function() {
|
2010-12-05 01:45:52 +03:00
|
|
|
console.log('recv: ' + JSON.stringify(recv));
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2010-12-05 01:45:52 +03:00
|
|
|
assert.equal(2 * 256, recv.length);
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2010-12-05 01:45:52 +03:00
|
|
|
var a = recv.split('');
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2010-12-05 01:45:52 +03:00
|
|
|
var first = a.slice(0, 256).reverse().join('');
|
|
|
|
console.log('first: ' + JSON.stringify(first));
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2010-12-05 01:45:52 +03:00
|
|
|
var second = a.slice(256, 2 * 256).join('');
|
|
|
|
console.log('second: ' + JSON.stringify(second));
|
2009-09-21 12:27:22 +02:00
|
|
|
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal(first, second);
|
2009-09-21 12:27:22 +02:00
|
|
|
});
|