Fixes for PR #129

This commit is contained in:
Feross Aboukhadijeh 2016-03-16 17:58:47 -07:00
parent 5127aad052
commit d079171044
3 changed files with 39 additions and 41 deletions

View File

@ -31,6 +31,7 @@ function WebSocketTracker (client, announceUrl, opts) {
self.socket = null self.socket = null
self.reconnecting = false self.reconnecting = false
self.retries = 0 self.retries = 0
self.reconnectTimer = null
self._openSocket() self._openSocket()
} }
@ -89,17 +90,6 @@ WebSocketTracker.prototype.destroy = function (cb) {
clearInterval(self.interval) clearInterval(self.interval)
clearTimeout(self.reconnectTimer) clearTimeout(self.reconnectTimer)
// Destroy peers
for (var peerId in self.peers) {
var peer = self.peers[peerId]
clearTimeout(peer.trackerTimeout)
peer.destroy()
}
delete self.peers
// Close socked
if (socketPool[self.announceUrl]) socketPool[self.announceUrl].consumers--
self.socket.removeListener('connect', self._onSocketConnectBound) self.socket.removeListener('connect', self._onSocketConnectBound)
self.socket.removeListener('data', self._onSocketDataBound) self.socket.removeListener('data', self._onSocketDataBound)
self.socket.removeListener('close', self._onSocketCloseBound) self.socket.removeListener('close', self._onSocketCloseBound)
@ -110,6 +100,18 @@ WebSocketTracker.prototype.destroy = function (cb) {
self._onSocketDataBound = null self._onSocketDataBound = null
self._onSocketCloseBound = null self._onSocketCloseBound = null
// Destroy peers
for (var peerId in self.peers) {
var peer = self.peers[peerId]
clearTimeout(peer.trackerTimeout)
peer.destroy()
}
self.peers = null
if (socketPool[self.announceUrl]) {
socketPool[self.announceUrl].consumers -= 1
}
if (socketPool[self.announceUrl].consumers === 0) { if (socketPool[self.announceUrl].consumers === 0) {
delete socketPool[self.announceUrl] delete socketPool[self.announceUrl]
@ -118,7 +120,7 @@ WebSocketTracker.prototype.destroy = function (cb) {
try { try {
self.socket.destroy(cb) self.socket.destroy(cb)
} catch (err) { } catch (err) {
if (cb) cb() cb(null)
} }
} }
@ -135,12 +137,12 @@ WebSocketTracker.prototype._openSocket = function () {
self._onSocketCloseBound = self._onSocketClose.bind(self) self._onSocketCloseBound = self._onSocketClose.bind(self)
self.socket = socketPool[self.announceUrl] self.socket = socketPool[self.announceUrl]
if (!self.socket) { if (self.socket) {
socketPool[self.announceUrl].consumers += 1
} else {
self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl) self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl)
self.socket.consumers = 1 self.socket.consumers = 1
self.socket.on('connect', self._onSocketConnectBound) self.socket.on('connect', self._onSocketConnectBound)
} else {
socketPool[self.announceUrl].consumers++
} }
self.socket.on('data', self._onSocketDataBound) self.socket.on('data', self._onSocketDataBound)

View File

@ -599,7 +599,6 @@ Server.prototype._onWebSocketClose = function (socket) {
var swarm = self.torrents[infoHash] var swarm = self.torrents[infoHash]
if (swarm) { if (swarm) {
swarm.announce({ swarm.announce({
type: 'ws',
event: 'stopped', event: 'stopped',
numwant: 0, numwant: 0,
peer_id: socket.peerId peer_id: socket.peerId

View File

@ -8,8 +8,8 @@ var peerId = new Buffer('01234567890123456789')
var peerId2 = new Buffer('12345678901234567890') var peerId2 = new Buffer('12345678901234567890')
var torrentLength = 50000 var torrentLength = 50000
function serverTest (t, serverType, serverFamily) { function serverTest (t, serverType, serverFamily, cb) {
t.plan(25) t.plan(30)
var hostname = serverFamily === 'inet6' var hostname = serverFamily === 'inet6'
? '[::1]' ? '[::1]'
@ -47,17 +47,20 @@ function serverTest (t, serverType, serverFamily) {
t.equal(swarm.incomplete, 1) t.equal(swarm.incomplete, 1)
t.equal(Object.keys(swarm.peers).length, 1) t.equal(Object.keys(swarm.peers).length, 1)
if (serverType !== 'ws') { var id = serverType === 'ws'
t.deepEqual(swarm.peers[hostname + ':6881'], { ? peerId.toString('hex')
type: serverType, : hostname + ':6881'
ip: clientIp,
port: 6881, t.equal(swarm.peers[id].type, serverType)
peerId: peerId.toString('hex'), t.equal(swarm.peers[id].ip, clientIp)
complete: false, t.equal(swarm.peers[id].peerId, peerId.toString('hex'))
socket: undefined t.equal(swarm.peers[id].complete, false)
}) if (serverType === 'ws') {
t.equal(typeof swarm.peers[id].port, 'number')
t.ok(swarm.peers[id].socket)
} else { } else {
t.equal(swarm.peers[peerId.toString('hex')].complete, false) t.equal(swarm.peers[id].port, 6881)
t.notOk(swarm.peers[id].socket)
} }
client1.complete() client1.complete()
@ -104,7 +107,7 @@ function serverTest (t, serverType, serverFamily) {
t.equal(data.incomplete, 0) t.equal(data.incomplete, 0)
client1.destroy() client1.destroy()
server.close() server.close(cb)
}) })
}) })
}) })
@ -115,19 +118,13 @@ function serverTest (t, serverType, serverFamily) {
}) })
} }
test('create daemon', function (t) {
wrtc = require('electron-webrtc')()
wrtc.electronDaemon.once('ready', t.end)
})
test('websocket server', function (t) { test('websocket server', function (t) {
serverTest(t, 'ws', 'inet') wrtc = require('electron-webrtc')()
}) wrtc.electronDaemon.once('ready', function () {
serverTest(t, 'ws', 'inet', function () {
// cleanup wrtc.close()
test('cleanup electron-eval daemon', function (t) { })
wrtc.close() })
t.end()
}) })
test('http ipv4 server', function (t) { test('http ipv4 server', function (t) {