Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uTP support #1328

Closed
wants to merge 7 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Native uTP

  • Loading branch information
pldubouilh committed Mar 7, 2018
commit b9250af6bd30935751a977570da725cd0dc3a589
@@ -3,7 +3,7 @@ module.exports = ConnPool
var arrayRemove = require('unordered-array-remove')
var debug = require('debug')('webtorrent:conn-pool')
var net = require('net') // browser exclude
var utp = require('utp') // browser exclude
var utp = require('utp-native') // browser exclude

var Peer = require('./peer')

@@ -24,20 +24,13 @@ function ConnPool (client) {
// closed before the connection is passed off to a Torrent.
self._pendingConns = []

self._onConnectionBoundTcp = function (conn) {
self._onConnectionBound = function (conn) {
self._onConnection(conn)
}

self._onConnectionBoundUtp = function (socket) {
self._onConnection(socket)
}

self._onListeningTCP = function (e) {
// Start listening UDP when TCP is listening
self.utpServer = utp.createServer(self._onConnectionBoundUtp)
self.utpServer.on('error', self._onError)
self.utpServer.listen(this.address().port)

var listening = 0
self._onListening = function (e) {
if (++listening !== 2) return
self._client._onListening()
}

@@ -47,12 +40,22 @@ function ConnPool (client) {

self._client = client

// Start listening TCP
// Setup TCP
self.tcpServer = net.createServer()
self.tcpServer.on('connection', self._onConnectionBoundTcp)
self.tcpServer.on('listening', self._onListeningTCP)
self.tcpServer.on('connection', self._onConnectionBound)
self.tcpServer.on('listening', self._onListening)
self.tcpServer.on('error', self._onError)
self.tcpServer.listen(client.torrentPort)

// Setup uTP
self.utpServer = utp.createServer()
self.utpServer.on('connection', self._onConnectionBound)
self.utpServer.on('listening', self._onListening)
self.utpServer.on('error', self._onError)

// Start listening TCP then UDP
self.tcpServer.listen(client.torrentPort, function () {
self.utpServer.listen(this.address().port)
})
}

/**
@@ -63,8 +66,12 @@ ConnPool.prototype.destroy = function (cb) {
var self = this
debug('destroy pool')

self.tcpServer.removeListener('connection', self._onConnectionBoundTcp)
self.tcpServer.removeListener('listening', self._onListeningTCP)
self.utpServer.removeListener('connection', self._onConnectionBound)
self.utpServer.removeListener('listening', self._onListening)
self.utpServer.removeListener('error', self._onError)

self.tcpServer.removeListener('connection', self._onConnectionBound)
self.tcpServer.removeListener('listening', self._onListening)
self.tcpServer.removeListener('error', self._onError)

// Destroy all open connection objects so server can close gracefully without waiting
@@ -102,7 +109,7 @@ ConnPool.prototype._onConnection = function (conn) {
// - Node.js issue: https://github.com/nodejs/node-v0.x-archive/issues/7566
// - WebTorrent issue: https://github.com/webtorrent/webtorrent/issues/398

if (!conn.socket && !conn.remoteAddress) {
if (!conn.address().address) {
conn.on('error', noop)
conn.destroy()
return
@@ -111,7 +118,7 @@ ConnPool.prototype._onConnection = function (conn) {
self._pendingConns.push(conn)
conn.once('close', cleanupPending)

var peer = Peer.createIncomingPeer(conn, conn.socket ? 'utpIncoming' : 'tcpIncoming')
var peer = Peer.createIncomingPeer(conn, conn._utp ? 'utpIncoming' : 'tcpIncoming')

var wire = peer.wire
wire.once('handshake', onHandshake)
@@ -36,13 +36,12 @@ exports.createWebRTCPeer = function (conn, swarm) {
* know what swarm the connection is intended for.
*/
exports.createIncomingPeer = function (conn, type) {
var host = conn.host || conn.remoteAddress
var port = conn.port || conn.remotePort
var addr = host + ':' + port
var peer = new Peer(addr)
var addr = conn.address()
var addrStr = addr.address + ':' + addr.port
var peer = new Peer(addrStr)
peer.type = type
peer.conn = conn
peer.addr = addr
peer.addr = addrStr

peer.onConnect()
return peer
@@ -30,7 +30,7 @@ var speedometer = require('speedometer')
var uniq = require('uniq')
var utMetadata = require('ut_metadata')
var utPex = require('ut_pex') // browser exclude
var utp = require('utp') // browser exclude
var utp = require('utp-native') // browser exclude

var File = require('./file')
var Peer = require('./peer')
@@ -1693,18 +1693,17 @@ Torrent.prototype._utpConnect = function (peer, connOpts) {
peer.type = 'utpOutgoing'

function fallbackTCP (err) {
if (peer.destroyed || peer.connected) return
this._debug('utp timeout/error %s, (error: %s)', peer.addr, err)
if (!err && (peer.destroyed || peer.connected)) return
self._debug('utp timeout/error %s, (%s)', peer.addr, err)
conn.once('connect', noop)
conn.once('error', noop)
peer.conn.destroy()
self._tcpConnect(peer, connOpts)
}

setTimeout(fallbackTCP.bind(this), 3000)

peer.connectTimeout = setTimeout(fallbackTCP, 3000)
conn.once('connect', function () { peer.onConnect() })
conn.once('error', function (e) { fallbackTCP(e).bind(this) })
conn.once('error', e => fallbackTCP(e))
// conn.once('close', function () { })
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.