Skip to content

Commit

Permalink
start moving things
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Aug 5, 2016
1 parent bd23213 commit b647de0
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
54 changes: 32 additions & 22 deletions client.js
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)
}

1 change: 0 additions & 1 deletion handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,3 @@ module.exports = function (handle, cb) {
}
}
}

57 changes: 35 additions & 22 deletions server.js
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
}
}
}












1 change: 0 additions & 1 deletion test/echo.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ pull(
server.close()
})
)

0 comments on commit b647de0

Please sign in to comment.