diff options
author | Julen Garcia Leunda <hicom150@gmail.com> | 2020-10-03 23:50:59 +0300 |
---|---|---|
committer | Julen Garcia Leunda <hicom150@gmail.com> | 2020-10-03 23:50:59 +0300 |
commit | 6a649a2fab00adf862f2a3879144a09cbfb3bcb8 (patch) | |
tree | 5552cea51da16eb86d8d55172c5c166eb60b9773 /lib | |
parent | 692b3d113c0a08a17ee53bca7e16180a94b4fbfe (diff) |
PR review improvements
Diffstat (limited to 'lib')
-rw-r--r-- | lib/conn-pool.js | 20 | ||||
-rw-r--r-- | lib/peer.js | 41 | ||||
-rw-r--r-- | lib/torrent.js | 1 |
3 files changed, 32 insertions, 30 deletions
diff --git a/lib/conn-pool.js b/lib/conn-pool.js index ddc0672..5746ca5 100644 --- a/lib/conn-pool.js +++ b/lib/conn-pool.js @@ -24,8 +24,12 @@ class ConnPool { // closed before the connection is passed off to a Torrent. this._pendingConns = [] - this._onConnectionBound = (conn) => { - this._onConnection(conn) + this._onTCPConnectionBound = (conn) => { + this._onConnection(conn, 'tcp') + } + + this._onUTPConnectionBound = (conn) => { + this._onConnection(conn, 'utp') } this._onListening = () => { @@ -42,7 +46,7 @@ class ConnPool { // Setup TCP this.tcpServer = net.createServer() - this.tcpServer.on('connection', this._onConnectionBound) + this.tcpServer.on('connection', this._onTCPConnectionBound) this.tcpServer.on('error', this._onTCPError) // Start TCP @@ -51,7 +55,7 @@ class ConnPool { if (this._client.utp) { // Setup uTP this.utpServer = utp.createServer() - this.utpServer.on('connection', this._onConnectionBound) + this.utpServer.on('connection', this._onUTPConnectionBound) this.utpServer.on('listening', this._onListening) this.utpServer.on('error', this._onUTPError) @@ -72,12 +76,12 @@ class ConnPool { debug('destroy conn pool') if (this.utpServer) { - this.utpServer.removeListener('connection', this._onConnectionBound) + this.utpServer.removeListener('connection', this._onUTPConnectionBound) this.utpServer.removeListener('listening', this._onListening) this.utpServer.removeListener('error', this._onUTPError) } - this.tcpServer.removeListener('connection', this._onConnectionBound) + this.tcpServer.removeListener('connection', this._onTCPConnectionBound) this.tcpServer.removeListener('error', this._onTCPError) // Destroy all open connection objects so server can close gracefully without waiting @@ -111,7 +115,7 @@ class ConnPool { * 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) { + _onConnection (conn, type) { const self = this // If the connection has already been closed before the `connect` event is fired, @@ -127,7 +131,7 @@ class ConnPool { self._pendingConns.push(conn) conn.once('close', cleanupPending) - const peer = this.utpServer ? Peer.createUTPIncomingPeer(conn) : Peer.createTCPIncomingPeer(conn) + const peer = type === 'utp' ? Peer.createUTPIncomingPeer(conn) : Peer.createTCPIncomingPeer(conn) const wire = peer.wire wire.once('handshake', onHandshake) diff --git a/lib/peer.js b/lib/peer.js index b0fea6d..0d0b807 100644 --- a/lib/peer.js +++ b/lib/peer.js @@ -36,14 +36,7 @@ exports.createWebRTCPeer = (conn, swarm) => { * know what swarm the connection is intended for. */ exports.createTCPIncomingPeer = conn => { - const addr = `${conn.remoteAddress}:${conn.remotePort}` - const peer = new Peer(addr, 'tcpIncoming') - peer.conn = conn - peer.addr = addr - - peer.onConnect() - - return peer + return _createIncomingPeer(conn, 'tcpIncoming') } /** @@ -52,14 +45,7 @@ exports.createTCPIncomingPeer = conn => { * know what swarm the connection is intended for. */ exports.createUTPIncomingPeer = conn => { - const addr = `${conn.remoteAddress}:${conn.remotePort}` - const peer = new Peer(addr, 'utpIncoming') - peer.conn = conn - peer.addr = addr - - peer.onConnect() - - return peer + return _createIncomingPeer(conn, 'utpIncoming') } /** @@ -67,11 +53,7 @@ exports.createUTPIncomingPeer = conn => { * available connection), the client can attempt to connect to the address. */ exports.createTCPOutgoingPeer = (addr, swarm) => { - const peer = new Peer(addr, 'tcpOutgoing') - peer.addr = addr - peer.swarm = swarm - - return peer + return _createOutgoingPeer(addr, swarm, 'tcpOutgoing') } /** @@ -79,7 +61,22 @@ exports.createTCPOutgoingPeer = (addr, swarm) => { * available connection), the client can attempt to connect to the address. */ exports.createUTPOutgoingPeer = (addr, swarm) => { - const peer = new Peer(addr, 'utpOutgoing') + return _createOutgoingPeer(addr, swarm, 'utpOutgoing') +} + +const _createIncomingPeer = (conn, type) => { + const addr = `${conn.remoteAddress}:${conn.remotePort}` + const peer = new Peer(addr, type) + peer.conn = conn + peer.addr = addr + + peer.onConnect() + + return peer +} + +const _createOutgoingPeer = (addr, swarm, type) => { + const peer = new Peer(addr, type) peer.addr = addr peer.swarm = swarm diff --git a/lib/torrent.js b/lib/torrent.js index 56326fd..a5bf720 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -751,6 +751,7 @@ class Torrent extends EventEmitter { } } + // if the utp connection fails to connect, then it is replaced with a tcp connection to the same ip:port const wasAdded = !!this._addPeer(peer, this.client.utp ? 'utp' : 'tcp') if (wasAdded) { this.emit('peer', peer) |