diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2013-11-10 06:39:15 +0400 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2013-11-10 06:39:15 +0400 |
commit | b0613e52b9e424b56521f6f5f8749d55297f6d3b (patch) | |
tree | b74bb8a8368c1cb684cefc5b3bd0c29c3d5cd1c2 /lib | |
parent | 6202bf89cba0ebdf7fda54db1641414bc8e0533f (diff) |
dht: use node-style dgram API
Diffstat (limited to 'lib')
-rw-r--r-- | lib/bittorrent-dht/index.js | 8 | ||||
-rw-r--r-- | lib/string.js | 95 |
2 files changed, 4 insertions, 99 deletions
diff --git a/lib/bittorrent-dht/index.js b/lib/bittorrent-dht/index.js index 2b306c4..c707fbf 100644 --- a/lib/bittorrent-dht/index.js +++ b/lib/bittorrent-dht/index.js @@ -10,7 +10,7 @@ var bencode = require('bncode') var bops = require('bops') var compact2string = require('compact2string') var EventEmitter = require('events').EventEmitter -var socket = require('chrome-app-socket') +var dgram = require('chrome-dgram') var is = require('core-util-is') // added in Node 0.12 var util = require('util') @@ -95,8 +95,8 @@ function DHT (infoHash) { self.pendingRequests[self.requestId] = 1 - self.socket = new socket.UDPSocket() - self.socket.on('data', self._onData.bind(self)) + self.socket = dgram.createSocket('udp4') + self.socket.on('message', self._onData.bind(self)) } /** @@ -175,7 +175,7 @@ DHT.prototype.query = function (addr) { var host = addr.split(':')[0] var port = Number(addr.split(':')[1]) - self.socket.sendTo(self.message, host, port) + self.socket.send(self.message, 0, self.message.length, port, host) } DHT.prototype.findPeers = function (num) { diff --git a/lib/string.js b/lib/string.js deleted file mode 100644 index 2e91033..0000000 --- a/lib/string.js +++ /dev/null @@ -1,95 +0,0 @@ -// Eliminate this in favor of bops? - -/* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding */ - -exports.fromUTF8Arr = UTF8ArrToStr -exports.toUTF8Arr = strToUTF8Arr - -/* UTF-8 array to DOMString and vice versa */ - -function UTF8ArrToStr (aBytes) { - if (!aBytes.buffer) { - aBytes = new Uint8Array(aBytes) - } - - var sView = ""; - - for (var nPart, nLen = aBytes.length, nIdx = 0; nIdx < nLen; nIdx++) { - nPart = aBytes[nIdx]; - sView += String.fromCharCode( - nPart > 251 && nPart < 254 && nIdx + 5 < nLen ? /* six bytes */ - /* (nPart - 252 << 32) is not possible in ECMAScript! So...: */ - (nPart - 252) * 1073741824 + (aBytes[++nIdx] - 128 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128 - : nPart > 247 && nPart < 252 && nIdx + 4 < nLen ? /* five bytes */ - (nPart - 248 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128 - : nPart > 239 && nPart < 248 && nIdx + 3 < nLen ? /* four bytes */ - (nPart - 240 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128 - : nPart > 223 && nPart < 240 && nIdx + 2 < nLen ? /* three bytes */ - (nPart - 224 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128 - : nPart > 191 && nPart < 224 && nIdx + 1 < nLen ? /* two bytes */ - (nPart - 192 << 6) + aBytes[++nIdx] - 128 - : /* nPart < 127 ? */ /* one byte */ - nPart - ); - } - - return sView; - -} - -function strToUTF8Arr (sDOMStr) { - - var aBytes, nChr, nStrLen = sDOMStr.length, nArrLen = 0; - - /* mapping... */ - - for (var nMapIdx = 0; nMapIdx < nStrLen; nMapIdx++) { - nChr = sDOMStr.charCodeAt(nMapIdx); - nArrLen += nChr < 0x80 ? 1 : nChr < 0x800 ? 2 : nChr < 0x10000 ? 3 : nChr < 0x200000 ? 4 : nChr < 0x4000000 ? 5 : 6; - } - - aBytes = new Uint8Array(nArrLen); - - /* transcription... */ - - for (var nIdx = 0, nChrIdx = 0; nIdx < nArrLen; nChrIdx++) { - nChr = sDOMStr.charCodeAt(nChrIdx); - if (nChr < 128) { - /* one byte */ - aBytes[nIdx++] = nChr; - } else if (nChr < 0x800) { - /* two bytes */ - aBytes[nIdx++] = 192 + (nChr >>> 6); - aBytes[nIdx++] = 128 + (nChr & 63); - } else if (nChr < 0x10000) { - /* three bytes */ - aBytes[nIdx++] = 224 + (nChr >>> 12); - aBytes[nIdx++] = 128 + (nChr >>> 6 & 63); - aBytes[nIdx++] = 128 + (nChr & 63); - } else if (nChr < 0x200000) { - /* four bytes */ - aBytes[nIdx++] = 240 + (nChr >>> 18); - aBytes[nIdx++] = 128 + (nChr >>> 12 & 63); - aBytes[nIdx++] = 128 + (nChr >>> 6 & 63); - aBytes[nIdx++] = 128 + (nChr & 63); - } else if (nChr < 0x4000000) { - /* five bytes */ - aBytes[nIdx++] = 248 + (nChr >>> 24); - aBytes[nIdx++] = 128 + (nChr >>> 18 & 63); - aBytes[nIdx++] = 128 + (nChr >>> 12 & 63); - aBytes[nIdx++] = 128 + (nChr >>> 6 & 63); - aBytes[nIdx++] = 128 + (nChr & 63); - } else /* if (nChr <= 0x7fffffff) */ { - /* six bytes */ - aBytes[nIdx++] = 252 + /* (nChr >>> 32) is not possible in ECMAScript! So...: */ (nChr / 1073741824); - aBytes[nIdx++] = 128 + (nChr >>> 24 & 63); - aBytes[nIdx++] = 128 + (nChr >>> 18 & 63); - aBytes[nIdx++] = 128 + (nChr >>> 12 & 63); - aBytes[nIdx++] = 128 + (nChr >>> 6 & 63); - aBytes[nIdx++] = 128 + (nChr & 63); - } - } - - return aBytes; - -}
\ No newline at end of file |