Skip to content

Commit

Permalink
added a dirty destroy method
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Aug 8, 2013
1 parent 6fb64e7 commit 52c568a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ var createStorage = require('./storage');
var createServer = require('./server');

module.exports = function(filename, opts, ready) {
if (!ready) {
ready = opts;
opts = null;
}

var peerflix = {}; // peerflix handle
var options = opts || {};

Expand Down Expand Up @@ -47,6 +52,7 @@ module.exports = function(filename, opts, ready) {
var storage = peerflix.storage = createStorage(torrent, selected, { destination:destination });
var server = peerflix.server = createServer(storage, selected, { buffer:options.buffer && numeral().unformat(options.buffer), port: options.port });
var peers = peerflix.peers = [];
var destroyed = false;

var speed = peerflix.speed = speedometer();
peerflix.uploaded = 0;
Expand Down Expand Up @@ -158,6 +164,7 @@ module.exports = function(filename, opts, ready) {

protocol.once('handshake', function() {
clearTimeout(timeout);
if (destroyed) return;

peers.push(protocol);

Expand Down Expand Up @@ -238,6 +245,21 @@ module.exports = function(filename, opts, ready) {
sw.listen();
}

peerflix.destroy = function() {
destroyed = true;
peers.forEach(function(peer) {
peer.destroy();
});
if (!peerflix.swarm) return;
// hackish
peerflix.swarm.maxSize = 0;
peerflix.swarm.connections.forEach(function(conn) {
conn.destroy();
});
peerflix.swarm._sock.close();
server.close();
};

ready(null, peerflix);
});

Expand Down

0 comments on commit 52c568a

Please sign in to comment.