diff options
author | Julen Garcia Leunda <hicom150@gmail.com> | 2020-09-30 22:39:09 +0300 |
---|---|---|
committer | Julen Garcia Leunda <hicom150@gmail.com> | 2020-10-02 16:37:33 +0300 |
commit | 692b3d113c0a08a17ee53bca7e16180a94b4fbfe (patch) | |
tree | 07ba6df67701f9397d46a0ddaf26f7f5b2c880a1 /lib/peer.js | |
parent | 7aee819796c540df0b247fec1853098f9a591d4c (diff) |
Add uTP support (BEP29)
Diffstat (limited to 'lib/peer.js')
-rw-r--r-- | lib/peer.js | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/lib/peer.js b/lib/peer.js index 0ddf535..b0fea6d 100644 --- a/lib/peer.js +++ b/lib/peer.js @@ -5,6 +5,7 @@ const Wire = require('bittorrent-protocol') const WebConn = require('./webconn') const CONNECT_TIMEOUT_TCP = 5000 +const CONNECT_TIMEOUT_UTP = 5000 const CONNECT_TIMEOUT_WEBRTC = 25000 const HANDSHAKE_TIMEOUT = 25000 @@ -46,6 +47,22 @@ exports.createTCPIncomingPeer = conn => { } /** + * Incoming uTP peers start out connected, because the remote peer connected to the + * listening port of the uTP server. Until the remote peer sends a handshake, we don't + * 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 +} + +/** * Outgoing TCP peers start out with just an IP address. At some point (when there is an * available connection), the client can attempt to connect to the address. */ @@ -58,6 +75,18 @@ exports.createTCPOutgoingPeer = (addr, swarm) => { } /** + * Outgoing uTP peers start out with just an IP address. At some point (when there is an + * available connection), the client can attempt to connect to the address. + */ +exports.createUTPOutgoingPeer = (addr, swarm) => { + const peer = new Peer(addr, 'utpOutgoing') + peer.addr = addr + peer.swarm = swarm + + return peer +} + +/** * Peer that represents a Web Seed (BEP17 / BEP19). */ exports.createWebSeedPeer = (url, swarm) => { @@ -193,9 +222,16 @@ class Peer { startConnectTimeout () { clearTimeout(this.connectTimeout) + + const connectTimeoutValues = { + webrtc: CONNECT_TIMEOUT_WEBRTC, + tcpOutgoing: CONNECT_TIMEOUT_TCP, + utpOutgoing: CONNECT_TIMEOUT_UTP + } + this.connectTimeout = setTimeout(() => { this.destroy(new Error('connect timeout')) - }, this.type === 'webrtc' ? CONNECT_TIMEOUT_WEBRTC : CONNECT_TIMEOUT_TCP) + }, connectTimeoutValues[this.type]) if (this.connectTimeout.unref) this.connectTimeout.unref() } @@ -212,7 +248,7 @@ class Peer { this.destroyed = true this.connected = false - debug('destroy %s (error: %s)', this.id, err && (err.message || err)) + debug('destroy %s %s (error: %s)', this.type, this.id, err && (err.message || err)) clearTimeout(this.connectTimeout) clearTimeout(this.handshakeTimeout) |