Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDiego Rodríguez <diegorbaquero@gmail.com>2018-08-24 03:11:55 +0300
committerDiego Rodríguez <diegorbaquero@gmail.com>2018-08-24 03:11:55 +0300
commitafbef26d5fafa745dc0793ed0d6a5761f5f0eb89 (patch)
tree6ded94ab17434a770d353ba424fab90cecf29719 /lib
parenta9569513d479a92ab602dc4ad0e96d1777d94a93 (diff)
Modernize lib/tcp-pool.js
Diffstat (limited to 'lib')
-rw-r--r--lib/tcp-pool.js188
1 files changed, 94 insertions, 94 deletions
diff --git a/lib/tcp-pool.js b/lib/tcp-pool.js
index 7625d8d..1094c4f 100644
--- a/lib/tcp-pool.js
+++ b/lib/tcp-pool.js
@@ -1,10 +1,8 @@
-module.exports = TCPPool
-
-var arrayRemove = require('unordered-array-remove')
-var debug = require('debug')('webtorrent:tcp-pool')
-var net = require('net') // browser exclude
+const arrayRemove = require('unordered-array-remove')
+const debug = require('debug')('webtorrent:tcp-pool')
+const net = require('net') // browser exclude
-var Peer = require('./peer')
+const Peer = require('./peer')
/**
* TCPPool
@@ -15,114 +13,116 @@ var Peer = require('./peer')
*
* @param {number} port
*/
-function TCPPool (client) {
- var self = this
- debug('create tcp pool (port %s)', client.torrentPort)
-
- self.server = net.createServer()
- self._client = client
+class TCPPool {
+ constructor (client) {
+ debug('create tcp pool (port %s)', client.torrentPort)
- // Temporarily store incoming connections so they can be destroyed if the server is
- // closed before the connection is passed off to a Torrent.
- self._pendingConns = []
+ this.server = net.createServer()
+ this._client = client
- self._onConnectionBound = function (conn) {
- self._onConnection(conn)
- }
+ // Temporarily store incoming connections so they can be destroyed if the server is
+ // closed before the connection is passed off to a Torrent.
+ this._pendingConns = []
- self._onListening = function () {
- self._client._onListening()
- }
+ this._onConnectionBound = conn => {
+ this._onConnection(conn)
+ }
- self._onError = function (err) {
- self._client._destroy(err)
- }
+ this._onListening = () => {
+ this._client._onListening()
+ }
- self.server.on('connection', self._onConnectionBound)
- self.server.on('listening', self._onListening)
- self.server.on('error', self._onError)
+ this._onError = err => {
+ this._client._destroy(err)
+ }
- self.server.listen(client.torrentPort)
-}
+ this.server.on('connection', this._onConnectionBound)
+ this.server.on('listening', this._onListening)
+ this.server.on('error', this._onError)
-/**
- * Destroy this TCP pool.
- * @param {function} cb
- */
-TCPPool.prototype.destroy = function (cb) {
- var self = this
- debug('destroy tcp pool')
-
- self.server.removeListener('connection', self._onConnectionBound)
- self.server.removeListener('listening', self._onListening)
- self.server.removeListener('error', self._onError)
-
- // Destroy all open connection objects so server can close gracefully without waiting
- // for connection timeout or remote peer to disconnect.
- self._pendingConns.forEach(function (conn) {
- conn.on('error', noop)
- conn.destroy()
- })
-
- try {
- self.server.close(cb)
- } catch (err) {
- if (cb) process.nextTick(cb)
+ this.server.listen(client.torrentPort)
}
- self.server = null
- self._client = null
- self._pendingConns = null
-}
+ /**
+ * Destroy this TCP pool.
+ * @param {function} cb
+ */
+ destroy (cb) {
+ debug('destroy tcp pool')
+
+ this.server.removeListener('connection', this._onConnectionBound)
+ this.server.removeListener('listening', this._onListening)
+ this.server.removeListener('error', this._onError)
+
+ // Destroy all open connection objects so server can close gracefully without waiting
+ // for connection timeout or remote peer to disconnect.
+ this._pendingConns.forEach(conn => {
+ conn.on('error', noop)
+ conn.destroy()
+ })
+
+ try {
+ this.server.close(cb)
+ } catch (err) {
+ if (cb) process.nextTick(cb)
+ }
-/**
- * On incoming connections, we expect the remote peer to send a handshake first. Based
- * on the infoHash in that handshake, route the peer to the right swarm.
- */
-TCPPool.prototype._onConnection = function (conn) {
- var self = this
-
- // If the connection has already been closed before the `connect` event is fired,
- // then `remoteAddress` will not be available, and we can't use this connection.
- // - Node.js issue: https://github.com/nodejs/node-v0.x-archive/issues/7566
- // - WebTorrent issue: https://github.com/webtorrent/webtorrent/issues/398
- if (!conn.remoteAddress) {
- conn.on('error', noop)
- conn.destroy()
- return
+ this.server = null
+ this._client = null
+ this._pendingConns = null
}
- self._pendingConns.push(conn)
- conn.once('close', cleanupPending)
+ /**
+ * On incoming connections, we expect the remote peer to send a handshake first. Based
+ * on the infoHash in that handshake, route the peer to the right swarm.
+ */
+ _onConnection (conn) {
+ const self = this
+
+ // If the connection has already been closed before the `connect` event is fired,
+ // then `remoteAddress` will not be available, and we can't use this connection.
+ // - Node.js issue: https://github.com/nodejs/node-v0.x-archive/issues/7566
+ // - WebTorrent issue: https://github.com/webtorrent/webtorrent/issues/398
+ if (!conn.remoteAddress) {
+ conn.on('error', noop)
+ conn.destroy()
+ return
+ }
+
+ self._pendingConns.push(conn)
+ conn.once('close', cleanupPending)
- var peer = Peer.createTCPIncomingPeer(conn)
+ const peer = Peer.createTCPIncomingPeer(conn)
- var wire = peer.wire
- wire.once('handshake', onHandshake)
+ const wire = peer.wire
+ wire.once('handshake', onHandshake)
- function onHandshake (infoHash, peerId) {
- cleanupPending()
+ function onHandshake (infoHash, peerId) {
+ cleanupPending()
- var torrent = self._client.get(infoHash)
- if (torrent) {
- peer.swarm = torrent
- torrent._addIncomingPeer(peer)
- peer.onHandshake(infoHash, peerId)
- } else {
- var err = new Error(
- 'Unexpected info hash ' + infoHash + ' from incoming peer ' + peer.id
- )
- peer.destroy(err)
+ const torrent = self._client.get(infoHash)
+ if (torrent) {
+ peer.swarm = torrent
+ torrent._addIncomingPeer(peer)
+ peer.onHandshake(infoHash, peerId)
+ } else {
+ const err = new Error(
+ `Unexpected info hash ${infoHash} from incoming peer ${peer.id}`
+ )
+ peer.destroy(err)
+ }
}
- }
- function cleanupPending () {
- conn.removeListener('close', cleanupPending)
- wire.removeListener('handshake', onHandshake)
- if (self._pendingConns) {
- arrayRemove(self._pendingConns, self._pendingConns.indexOf(conn))
+ function cleanupPending () {
+ conn.removeListener('close', cleanupPending)
+ wire.removeListener('handshake', onHandshake)
+ if (self._pendingConns) {
+ arrayRemove(self._pendingConns, self._pendingConns.indexOf(conn))
+ }
}
}
}
function noop () {}
+
+module.exports = TCPPool