From 3b4b487c2c51b346152d22389ea575633e1f7873 Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 12 Dec 2014 16:51:37 +0100 Subject: [PATCH 1/3] examples/express-embed --- examples/express-embed/package.json | 13 +++++++++++++ examples/express-embed/server.js | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 examples/express-embed/package.json create mode 100755 examples/express-embed/server.js diff --git a/examples/express-embed/package.json b/examples/express-embed/package.json new file mode 100644 index 0000000..07eceec --- /dev/null +++ b/examples/express-embed/package.json @@ -0,0 +1,13 @@ +{ + "name": "bittorrent-tracker-example-express-embed", + "version": "0.0.0", + "description": "Example for embedding bittorrent-tracker server in express.js", + "scripts": { + "server": "./server.js" + }, + "author": "Astro ", + "license": "MIT", + "dependencies": { + "express": "^4.10.5" + } +} diff --git a/examples/express-embed/server.js b/examples/express-embed/server.js new file mode 100755 index 0000000..8c64ce4 --- /dev/null +++ b/examples/express-embed/server.js @@ -0,0 +1,16 @@ +#!/usr/bin/env node + +var Server = require('../..').Server +var express = require('express') +var app = express() + +var server = new Server({ + http: false, // we do our own + udp: false // not interested +}) + +var onHttpRequest = server._onHttpRequest.bind(server) +app.get('/announce', onHttpRequest) +app.get('/scrape', onHttpRequest) + +app.listen(8080) From 2f7a7131d0b3e3493441c178860b3d18f75c8e2f Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 12 Dec 2014 16:52:13 +0100 Subject: [PATCH 2/3] server: make onHttpRequest() part of public API --- examples/express-embed/server.js | 2 +- server.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/express-embed/server.js b/examples/express-embed/server.js index 8c64ce4..294f013 100755 --- a/examples/express-embed/server.js +++ b/examples/express-embed/server.js @@ -9,7 +9,7 @@ var server = new Server({ udp: false // not interested }) -var onHttpRequest = server._onHttpRequest.bind(server) +var onHttpRequest = server.onHttpRequest.bind(server) app.get('/announce', onHttpRequest) app.get('/scrape', onHttpRequest) diff --git a/server.js b/server.js index a194f86..e3b19eb 100644 --- a/server.js +++ b/server.js @@ -54,7 +54,7 @@ function Server (opts) { // default to starting an http server unless the user explictly says no if (opts.http !== false) { self._httpServer = http.createServer() - self._httpServer.on('request', self._onHttpRequest.bind(self)) + self._httpServer.on('request', self.onHttpRequest.bind(self)) self._httpServer.on('error', self._onError.bind(self)) self._httpServer.on('listening', onListening) } @@ -123,7 +123,7 @@ Server.prototype.getSwarm = function (infoHash) { return swarm } -Server.prototype._onHttpRequest = function (req, res) { +Server.prototype.onHttpRequest = function (req, res) { var self = this var params From 80da7c50d011b5ffb74619270f4c12b7cf6b3b5f Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 12 Dec 2014 17:27:03 +0100 Subject: [PATCH 3/3] server: reuse udp socket for responses Reasons: * Save file-descriptors * Support clients behind NAT --- server.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/server.js b/server.js index a194f86..081ef6c 100644 --- a/server.js +++ b/server.js @@ -61,13 +61,13 @@ function Server (opts) { // default to starting a udp server unless the user explicitly says no if (opts.udp !== false) { - self._udpServer = dgram.createSocket('udp4') - self._udpServer.on('message', self._onUdpRequest.bind(self)) - self._udpServer.on('error', self._onError.bind(self)) - self._udpServer.on('listening', onListening) + self._udpSocket = dgram.createSocket('udp4') + self._udpSocket.on('message', self._onUdpRequest.bind(self)) + self._udpSocket.on('error', self._onError.bind(self)) + self._udpSocket.on('listening', onListening) } - var num = !!self._httpServer + !!self._udpServer + var num = !!self._httpServer + !!self._udpSocket function onListening () { num -= 1 if (num === 0) { @@ -95,7 +95,7 @@ Server.prototype.listen = function (port, onlistening) { if (err) return self.emit('error', err) self.port = port self._httpServer && self._httpServer.listen(port.http || port) - self._udpServer && self._udpServer.bind(port.udp || port) + self._udpSocket && self._udpSocket.bind(port.udp || port) } if (port) onPort(null, port) @@ -105,8 +105,8 @@ Server.prototype.listen = function (port, onlistening) { Server.prototype.close = function (cb) { var self = this cb = cb || function () {} - if (self._udpServer) { - self._udpServer.close() + if (self._udpSocket) { + self._udpSocket.close() } if (self._httpServer) { self._httpServer.close(cb) @@ -177,11 +177,10 @@ Server.prototype._onUdpRequest = function (msg, rinfo) { } } - var socket = dgram.createSocket('udp4') response.transactionId = params.transactionId response.connectionId = params.connectionId var buf = makeUdpPacket(response) - socket.send(buf, 0, buf.length, rinfo.port, rinfo.address, function () { + self._udpSocket.send(buf, 0, buf.length, rinfo.port, rinfo.address, function () { try { socket.close() } catch (err) {}