forked from Onek8/LNXSDK
129 lines
3.2 KiB
Haxe
129 lines
3.2 KiB
Haxe
package leenkx.network.torrent;
|
|
|
|
import haxe.io.Bytes;
|
|
|
|
class WebSeed {
|
|
public var url:String;
|
|
public var onError:String -> Void;
|
|
public var onDone:Void -> Void;
|
|
|
|
var t:Torrent;
|
|
var busy = false;
|
|
var key:String;
|
|
|
|
public function new(url:String, t:Torrent) {
|
|
this.url = url;
|
|
this.t = t;
|
|
this.key = "ws:" + url;
|
|
}
|
|
|
|
public function work():Void {
|
|
if (busy || !supported() || t.pieces == null
|
|
|| t.pieces.complete) return;
|
|
var i = pickPiece();
|
|
if (i < 0) return;
|
|
var range = rangeFor(i);
|
|
if (range == null) return;
|
|
busy = true;
|
|
var off = 0;
|
|
var size = t.meta.pieceSize(i);
|
|
while (off < size) {
|
|
t.pieces.markRequested(key, i, off);
|
|
off += PieceManager.BLOCK_SIZE;
|
|
}
|
|
fetch(range.url, range.start, range.end, function(body, err) {
|
|
busy = false;
|
|
if (t.pieces != null) t.pieces.clearAllRequested(key);
|
|
if (body != null && t.pieces != null) {
|
|
t.pieces.addBlock(i, 0, body);
|
|
if (onDone != null) onDone();
|
|
} else if (onError != null) {
|
|
onError(err != null ? err : "empty response");
|
|
}
|
|
});
|
|
}
|
|
|
|
function pickPiece():Int {
|
|
var pieces = t.pieces;
|
|
for (sel in pieces.selections) {
|
|
for (i in sel.start...sel.end + 1) {
|
|
if (pieces.untouched(i) && rangeFor(i) != null) {
|
|
return i;
|
|
}
|
|
}
|
|
}
|
|
for (i in 0...t.meta.numPieces) {
|
|
if (pieces.untouched(i) && rangeFor(i) != null) return i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
function fileForByteRange(start:Int, end:Int):MetaInfo.TorrentFileEntry {
|
|
for (f in t.meta.files) {
|
|
if (start >= f.offset && end < f.offset + f.length) {
|
|
return f;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function rangeFor(i:Int):{url:String, start:Int, end:Int} {
|
|
var pl = t.meta.pieceLength;
|
|
var start = i * pl;
|
|
var end = start + t.meta.pieceSize(i) - 1;
|
|
var f = fileForByteRange(start, end);
|
|
if (f == null) return null;
|
|
var u = url;
|
|
if (StringTools.endsWith(u, "/") || t.meta.files.length > 1) {
|
|
if (!StringTools.endsWith(u, "/")) u += "/";
|
|
u += f.path.split("\\").join("/");
|
|
}
|
|
return {url: u, start: start - f.offset,
|
|
end: end - f.offset};
|
|
}
|
|
|
|
static function supported():Bool {
|
|
#if kha_krom
|
|
return false;
|
|
#elseif (sys || js)
|
|
return true;
|
|
#else
|
|
return false;
|
|
#end
|
|
}
|
|
|
|
function fetch(url:String, start:Int, end:Int, cb:Bytes -> String -> Void):Void {
|
|
#if kha_krom
|
|
cb(null, "webseeds unsupported on krom");
|
|
#elseif sys
|
|
var req = new haxe.Http(url);
|
|
req.setHeader("Range", "bytes=" + start + "-" + end);
|
|
req.cnxTimeout = 60;
|
|
req.onBytes = function(b) {
|
|
TorrentClient.postMain(function() cb(b, null));
|
|
};
|
|
req.onError = function(e) {
|
|
TorrentClient.postMain(function() cb(null, e));
|
|
};
|
|
sys.thread.Thread.create(function() req.request(false));
|
|
#elseif js
|
|
var xhr = new js.html.XMLHttpRequest();
|
|
xhr.open("GET", url, true);
|
|
xhr.setRequestHeader("Range", "bytes=" + start + "-" + end);
|
|
xhr.timeout = 60000;
|
|
xhr.ontimeout = function() cb(null, "request timed out");
|
|
xhr.responseType = js.html.XMLHttpRequestResponseType
|
|
.ARRAYBUFFER;
|
|
xhr.onload = function() {
|
|
var buf:js.lib.ArrayBuffer = xhr.response;
|
|
cb(buf != null ? haxe.io.Bytes.ofData(buf) : null,
|
|
buf != null ? null : "empty response");
|
|
};
|
|
xhr.onerror = function() cb(null, "http error");
|
|
xhr.send();
|
|
#else
|
|
cb(null, "webseeds unsupported on this target");
|
|
#end
|
|
}
|
|
}
|