[udp server] add stub implementation

This commit is contained in:
Feross Aboukhadijeh 2014-04-18 23:05:21 -07:00
parent 60b7f8dd09
commit b0a659f00c
2 changed files with 45 additions and 13 deletions

View File

@ -3,15 +3,16 @@ exports.Server = Server
var bncode = require('bncode') var bncode = require('bncode')
var compact2string = require('compact2string') var compact2string = require('compact2string')
var dgram = require('dgram')
var EventEmitter = require('events').EventEmitter var EventEmitter = require('events').EventEmitter
var extend = require('extend.js') var extend = require('extend.js')
var hat = require('hat') var hat = require('hat')
var http = require('http') var http = require('http')
var inherits = require('inherits') var inherits = require('inherits')
var parallel = require('run-parallel')
var querystring = require('querystring') var querystring = require('querystring')
var string2compact = require('string2compact') var string2compact = require('string2compact')
var dgram = require('dgram') var url = require('url')
var parseUrl = require('url').parse
var CONNECTION_ID = Buffer.concat([ toUInt32(0x417), toUInt32(0x27101980) ]) var CONNECTION_ID = Buffer.concat([ toUInt32(0x417), toUInt32(0x27101980) ])
var ACTIONS = { CONNECT: 0, ANNOUNCE: 1 } var ACTIONS = { CONNECT: 0, ANNOUNCE: 1 }
@ -118,9 +119,9 @@ Client.prototype._request = function (opts) {
Client.prototype._requestHttp = function (announceUrl, opts) { Client.prototype._requestHttp = function (announceUrl, opts) {
var self = this var self = this
var url = announceUrl + '?' + querystring.stringify(opts) var fullUrl = announceUrl + '?' + querystring.stringify(opts)
var req = http.get(url, function (res) { var req = http.get(fullUrl, function (res) {
var data = '' var data = ''
if (res.statusCode !== 200) { if (res.statusCode !== 200) {
res.resume() // consume the whole stream res.resume() // consume the whole stream
@ -142,7 +143,7 @@ Client.prototype._requestHttp = function (announceUrl, opts) {
Client.prototype._requestUdp = function (announceUrl, opts) { Client.prototype._requestUdp = function (announceUrl, opts) {
var self = this var self = this
var parsedUrl = parseUrl(announceUrl) var parsedUrl = url.parse(announceUrl)
var socket = dgram.createSocket('udp4') var socket = dgram.createSocket('udp4')
var transactionId = new Buffer(hat(32), 'hex') var transactionId = new Buffer(hat(32), 'hex')
@ -311,26 +312,52 @@ function Server (opts) {
self.torrents = {} self.torrents = {}
self._server = http.createServer() // default to starting an http server unless the user explictly says no
self._server.on('request', self._onRequest.bind(self)) if (opts.http !== false) {
self._server.on('error', function (err) { self._httpServer = http.createServer()
self._httpServer.on('request', self._onHttpRequest.bind(self))
self._httpServer.on('error', function (err) {
self.emit('error', err) self.emit('error', err)
}) })
} }
// 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))
}
}
Server.prototype.listen = function (port) { Server.prototype.listen = function (port) {
var self = this var self = this
self._server.listen(port, function () { var tasks = []
self._httpServer && tasks.push(function (cb) {
self._httpServer.listen(port, cb)
})
self._udpServer && tasks.push(function (cb) {
self._udpServer.bind(port, cb)
})
parallel(tasks, function (err) {
if (err) return self.emit('error', err)
self.emit('listening') self.emit('listening')
}) })
} }
Server.prototype.close = function (cb) { Server.prototype.close = function (cb) {
var self = this var self = this
self._server.close(cb) if (self._udpServer) {
self._udpServer.close()
}
if (self._httpServer) {
self._httpServer.close(cb)
} else {
cb(null)
}
} }
Server.prototype._onRequest = function (req, res) { Server.prototype._onHttpRequest = function (req, res) {
var self = this var self = this
function error (message) { function error (message) {
@ -450,6 +477,10 @@ Server.prototype._onRequest = function (req, res) {
} }
} }
Server.prototype._onUdpRequest = function (req, res) {
// TODO: implement UDP server
}
Server.prototype._getPeers = function (swarm) { Server.prototype._getPeers = function (swarm) {
var self = this var self = this
var peers = [] var peers = []

View File

@ -17,6 +17,7 @@
"hat": "0.0.3", "hat": "0.0.3",
"inherits": "^2.0.1", "inherits": "^2.0.1",
"querystring": "^0.2.0", "querystring": "^0.2.0",
"run-parallel": "^0.3.0",
"string2compact": "^1.1.0" "string2compact": "^1.1.0"
}, },
"devDependencies": { "devDependencies": {