diff options
Diffstat (limited to 'lib/peer.js')
-rw-r--r-- | lib/peer.js | 51 |
1 files changed, 42 insertions, 9 deletions
diff --git a/lib/peer.js b/lib/peer.js index 0ddf535..0d0b807 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 @@ -35,8 +36,37 @@ exports.createWebRTCPeer = (conn, swarm) => { * know what swarm the connection is intended for. */ exports.createTCPIncomingPeer = conn => { + return _createIncomingPeer(conn, 'tcpIncoming') +} + +/** + * 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 => { + return _createIncomingPeer(conn, 'utpIncoming') +} + +/** + * 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. + */ +exports.createTCPOutgoingPeer = (addr, swarm) => { + return _createOutgoingPeer(addr, swarm, 'tcpOutgoing') +} + +/** + * 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) => { + return _createOutgoingPeer(addr, swarm, 'utpOutgoing') +} + +const _createIncomingPeer = (conn, type) => { const addr = `${conn.remoteAddress}:${conn.remotePort}` - const peer = new Peer(addr, 'tcpIncoming') + const peer = new Peer(addr, type) peer.conn = conn peer.addr = addr @@ -45,12 +75,8 @@ exports.createTCPIncomingPeer = conn => { 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. - */ -exports.createTCPOutgoingPeer = (addr, swarm) => { - const peer = new Peer(addr, 'tcpOutgoing') +const _createOutgoingPeer = (addr, swarm, type) => { + const peer = new Peer(addr, type) peer.addr = addr peer.swarm = swarm @@ -193,9 +219,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 +245,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) |