From b647de01d1631d0096daf07d3ed6758d40346d11 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 5 Aug 2016 13:23:34 +0200 Subject: [PATCH] start moving things --- .gitignore | 1 + client.js | 54 +++++++++++++++++++++++++++++-------------------- handle.js | 1 - server.js | 57 ++++++++++++++++++++++++++++++++-------------------- test/echo.js | 1 - 5 files changed, 68 insertions(+), 46 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/client.js b/client.js index c2c296e..877ef4b 100644 --- a/client.js +++ b/client.js @@ -1,33 +1,43 @@ var Handle = require('./handle') -var TCP = process.binding('tcp_wrap').TCP; -var TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap; +var TCP = process.binding('tcp_wrap').TCP +var TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap +var pull = require('pull-stream') +var net = require('net') module.exports = function (port, address, cb) { + port |= 0 var clientHandle = new TCP() - var connect = new TCPConnectWrap(), stream + var connect = new TCPConnectWrap() + var stream - connect.oncomplete = function (err) { - if(err) return cb(new Error('error connecting 1:'+err)) + connect.port = port + connect.address = address + connect.oncomplete = function afterConnect (err) { + if (err) return cb(new Error('error connecting 1:' + err)) cb && cb(null, stream) } - var err = clientHandle.connect(connect, address, port); - - stream = err ? Handle(clientHandle, function () {}) : error.duplex(err) - if(!err) return Handle(clientHandle, function () {}) - if(err) return cb(new Error('error connecting 2:'+err)) + var err + if (net.isIPv4) { + err = clientHandle.connect(connect, address, port) + } else { + err = clientHandle.connect6(connect, address, port) + } -//so, I could actually return the client stream syncly. + // stream = err ? Handle(clientHandle, function () {}) : pull.error(err) + // if (!err) return Handle(clientHandle, function () {}) + // if (err) return cb(new Error('error connecting 2:' + err)) + // so, I could actually return the client stream syncly. -// -// if(err) { -// console.log("ERROR", err) -// err = new Error('connection failed:'+err) -// return { -// source: Error(err), -// sink: function (read) {read(err, cb)} -// } -// } -// return Handle(clientHandle, cb) + if (err) { + console.log('ERROR', err) + err = new Error('connection failed: ' + err) + return { + source: pull.error(err), + sink: function () { + return function (read) { read(err, cb) } + } + } + } + return Handle(clientHandle, cb) } - diff --git a/handle.js b/handle.js index 152cc86..34e5e5d 100644 --- a/handle.js +++ b/handle.js @@ -65,4 +65,3 @@ module.exports = function (handle, cb) { } } } - diff --git a/server.js b/server.js index 0cb3d52..2cbf573 100644 --- a/server.js +++ b/server.js @@ -1,40 +1,53 @@ -var TCP = process.binding('tcp_wrap').TCP; +var TCP = process.binding('tcp_wrap').TCP +var net = require('net') var Handle = require('./handle') -function noop() {} +function noop () {} module.exports = function (onConnect) { - var server = new TCP(); + var server = new TCP() return { listen: function (port, addr, cb) { - var err = server.bind(addr, port) - if(err) throw Error('could not bind') //server.close(), cb && cb(err) + cb = cb || noop + var err + if (net.isIPv6(addr)) { + err = server.bind6(addr, port) + } else { + err = server.bind(addr, port) + } + + if (err) { + server.close() + cb(err) + return + } - //512 connections allowed in backlog + // 512 connections allowed in backlog server.listen(511) - server.onconnection = function(err, client) { - if (err) return console.error(new Error('error connected:'+err)) + server.onconnection = function (err, client) { + if (err) { + return console.error(new Error('error connected:' + err)) + } onConnect(Handle(client, noop)) } return server }, - close: function () { - server.close() + address: function () { + if (server && server.getsockname) { + var out = {} + server.getsockname(out) + return out + } else if (this._pipeName) { + return this._pipeName + } else { + return null + } + }, + close: function (cb) { + server.close(cb) return server } } } - - - - - - - - - - - - diff --git a/test/echo.js b/test/echo.js index 6841bbc..93b00e0 100644 --- a/test/echo.js +++ b/test/echo.js @@ -26,4 +26,3 @@ pull( server.close() }) ) -