fix: catch errors on incomming sockets

This commit is contained in:
David Dias
2017-04-07 11:51:56 -04:00
parent 4694f9dd47
commit e204517a51
3 changed files with 39 additions and 36 deletions

View File

@ -12,40 +12,41 @@ const log = debug('libp2p:tcp:dial')
const createListener = require('./listener')
module.exports = class TCP {
dial (ma, options, cb) {
function noop () {}
class TCP {
dial (ma, options, callback) {
if (isFunction(options)) {
cb = options
callback = options
options = {}
}
if (!cb) {
cb = () => {}
}
callback = callback || noop
cb = once(cb)
callback = once(callback)
const cOpts = ma.toOptions()
log('Connecting to %s %s', cOpts.port, cOpts.host)
const rawSocket = net.connect(cOpts)
rawSocket.once('timeout', () => {
log('timeout')
rawSocket.emit('error', new Error('Timeout'))
})
rawSocket.once('error', cb)
rawSocket.once('error', callback)
rawSocket.once('connect', () => {
rawSocket.removeListener('error', cb)
cb()
rawSocket.removeListener('error', callback)
callback()
})
const socket = toPull.duplex(rawSocket)
const conn = new Connection(socket)
conn.getObservedAddrs = (cb) => {
return cb(null, [ma])
conn.getObservedAddrs = (callback) => {
return callback(null, [ma])
}
return conn
@ -74,3 +75,5 @@ module.exports = class TCP {
})
}
}
module.exports = TCP