-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd23213
commit b647de0
Showing
5 changed files
with
68 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,4 +65,3 @@ module.exports = function (handle, cb) { | |
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,3 @@ pull( | |
server.close() | ||
}) | ||
) | ||
|