311 lines
9.2 KiB
Haxe
311 lines
9.2 KiB
Haxe
package leenkx.network;
|
|
|
|
import leenkx.network.Types;
|
|
import haxe.io.Bytes;
|
|
import iron.object.Object;
|
|
import leenkx.system.Event;
|
|
import leenkx.network.Buffer;
|
|
|
|
@:expose
|
|
class Leenkx {
|
|
|
|
public static var onLoadEvent: String = "Leenkx.onLoad";
|
|
public static var onOpenEvent: String = "Leenkx.onOpen";
|
|
public static var onMessageEvent: String = "Leenkx.onMessage";
|
|
public static var onErrorEvent: String = "Leenkx.onError";
|
|
public static var onCloseEvent: String = "Leenkx.onClose";
|
|
public static var onSeenEvent: String = "Leenkx.onSeen";
|
|
public static var onServerEvent: String = "Leenkx.onServer";
|
|
public static var onConnectionsEvent: String = "Leenkx.onConnections";
|
|
public static var onPingEvent: String = "Leenkx.onPing";
|
|
public static var onLeftEvent: String = "Leenkx.onLeft";
|
|
public static var onTimeOutEvent: String = "Leenkx.onTimeOut";
|
|
public static var onRpcEvent: String = "Leenkx.onRpc";
|
|
public static var onRpcResponseEvent: String = "Leenkx.onRpcResponse";
|
|
public static var onWireLeftEvent: String = "Leenkx.onWireLeft";
|
|
public static var onWireSeenEvent: String = "Leenkx.onWireSeen";
|
|
public static var onTorrentEvent: String = "Leenkx.onTorrent";
|
|
public static var onTrackerEvent: String = "Leenkx.onTracker";
|
|
public static var onAnnounceEvent: String = "Leenkx.onAnnounce";
|
|
public static var onTorrentDoneEvent: String = "Leenkx.onTorrentDone";
|
|
public static var connections:Map<String, leenkx.network.LeenkxSocket> = [];
|
|
#if js
|
|
public static var peers:js.lib.Map<String, String> = new js.lib.Map<String,String>();
|
|
public static var data:js.lib.Map<String, Dynamic> = new js.lib.Map<String,Dynamic>();
|
|
public static var id:js.lib.Map<String, String> = new js.lib.Map<String,String>();
|
|
public static var torrent:js.lib.Map<String, Dynamic> = new js.lib.Map<String,Dynamic>();
|
|
public static var file:js.lib.Map<String, Dynamic> = new js.lib.Map<String,Dynamic>();
|
|
#end
|
|
public static var lxNew:Void->Void;
|
|
|
|
public function new(net_Url: String, net_object: Object) {
|
|
if (net_Url != null && net_object != null) {
|
|
|
|
if (Leenkx.connections[net_Url] == null) {
|
|
|
|
var object = net_object;
|
|
|
|
try {
|
|
Leenkx.connections[net_Url] = new leenkx.network.LeenkxSocket(net_Url);
|
|
}
|
|
catch (error) {
|
|
trace(error);
|
|
return;
|
|
}
|
|
|
|
if (object != null) {
|
|
final loadEvent = Event.get(Leenkx.onLoadEvent);
|
|
final openEvent = Event.get(Leenkx.onOpenEvent);
|
|
final messageEvent = Event.get(Leenkx.onMessageEvent);
|
|
final errorEvent = Event.get(Leenkx.onErrorEvent);
|
|
final closeEvent = Event.get(Leenkx.onCloseEvent);
|
|
final seenEvent = Event.get(Leenkx.onSeenEvent);
|
|
final serverEvent = Event.get(Leenkx.onServerEvent);
|
|
final connectionsEvent = Event.get(Leenkx.onConnectionsEvent);
|
|
final pingEvent = Event.get(Leenkx.onPingEvent);
|
|
final leftEvent = Event.get(Leenkx.onLeftEvent);
|
|
final timeoutEvent = Event.get(Leenkx.onTimeOutEvent);
|
|
final rpcEvent = Event.get(Leenkx.onRpcEvent);
|
|
final rpcresponseEvent = Event.get(Leenkx.onRpcResponseEvent);
|
|
final wireleftEvent = Event.get(Leenkx.onWireLeftEvent);
|
|
final wireseenEvent = Event.get(Leenkx.onWireSeenEvent);
|
|
final torrentEvent = Event.get(Leenkx.onTorrentEvent);
|
|
final trackerEvent = Event.get(Leenkx.onTrackerEvent);
|
|
final announceEvent = Event.get(Leenkx.onAnnounceEvent);
|
|
final torrentDoneEvent = Event.get(Leenkx.onTorrentDoneEvent);
|
|
|
|
Leenkx.connections[net_Url].onopen = function() {
|
|
if (openEvent != null) {
|
|
for (e in openEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onmessage = function() {
|
|
if (messageEvent != null) {
|
|
for (e in messageEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onerror = function() {
|
|
if (errorEvent != null) {
|
|
for (e in errorEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onclose = function() {
|
|
if (closeEvent != null) {
|
|
for (e in closeEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onseen = function() {
|
|
if (seenEvent != null) {
|
|
for (e in seenEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onserver = function() {
|
|
if (serverEvent != null) {
|
|
for (e in serverEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onconnections = function() {
|
|
if (connectionsEvent != null) {
|
|
for (e in connectionsEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onping = function() {
|
|
if (pingEvent != null) {
|
|
for (e in pingEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onleft = function() {
|
|
if (leftEvent != null) {
|
|
for (e in leftEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].ontimeout = function() {
|
|
if (timeoutEvent != null) {
|
|
for (e in timeoutEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onrpc = function() {
|
|
if (rpcEvent != null) {
|
|
for (e in rpcEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onrpcresponse = function() {
|
|
if (rpcresponseEvent != null) {
|
|
for (e in rpcresponseEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onwireleft = function() {
|
|
if (wireleftEvent != null) {
|
|
for (e in wireleftEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onwireseen = function() {
|
|
if (wireseenEvent != null) {
|
|
for (e in wireseenEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].ontorrent = function() {
|
|
if (torrentEvent != null) {
|
|
for (e in torrentEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].ontracker = function() {
|
|
if (trackerEvent != null) {
|
|
for (e in trackerEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onannounce = function() {
|
|
if (announceEvent != null) {
|
|
for (e in announceEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].onload = function() {
|
|
if (loadEvent != null) {
|
|
for (e in loadEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
Leenkx.connections[net_Url].ontorrentdone = function() {
|
|
if (torrentDoneEvent != null) {
|
|
for (e in torrentDoneEvent) {
|
|
if (e.mask == object.uid) {
|
|
e.onEvent();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
#if js
|
|
var lnxjs:Dynamic = js.Lib.global;
|
|
var connectionUrl = net_Url;
|
|
|
|
if (lnxjs.Leenkx != null) {
|
|
if (lnxjs.lnxNew == null) {
|
|
js.Syntax.code('globalThis.lnxNew = function(url) { return new Leenkx(url); }');
|
|
}
|
|
Leenkx.connections[connectionUrl].onload();
|
|
} else {
|
|
kha.Assets.loadBlobFromPath("Leenkx.js", function(b: kha.Blob) {
|
|
if (b != null) {
|
|
js.Syntax.code("(1,eval)({0})", b.toString());
|
|
if (lnxjs.Leenkx != null && lnxjs.lnxNew == null) {
|
|
js.Syntax.code('globalThis.lnxNew = function(url) { return new Leenkx(url); }');
|
|
}
|
|
} else {
|
|
trace("Warning: Leenkx.js blob is null - file may not be in assets");
|
|
}
|
|
Leenkx.connections[connectionUrl].onload();
|
|
}, function(err: kha.AssetError) {
|
|
trace("ERROR loading Leenkx.js: " + err.url + " - " + err.error);
|
|
Leenkx.connections[connectionUrl].onload();
|
|
});
|
|
}
|
|
#end
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
class LeenkxSocket {
|
|
public var _url:String;
|
|
public var client:haxe.DynamicAccess<Dynamic>;
|
|
public var buffer:Dynamic;
|
|
public var onopen:Void->Void;
|
|
public var onclose:Void->Void;
|
|
public var onmessage:Void->Void;
|
|
public var onerror:Void->Void;
|
|
public var onseen:Void->Void;
|
|
public var onserver:Void->Void;
|
|
public var onconnections:Void->Void;
|
|
public var onping:Void->Void;
|
|
public var onleft:Void->Void;
|
|
public var ontimeout:Void->Void;
|
|
public var onrpc:Void->Void;
|
|
public var onrpcresponse:Void->Void;
|
|
public var onwireleft:Void->Void;
|
|
public var onwireseen:Void->Void;
|
|
public var ontorrent:Void->Void;
|
|
public var ontracker:Void->Void;
|
|
public var onannounce:Void->Void;
|
|
public var onload:Void->Void;
|
|
public var ontorrentdone:Void->Void;
|
|
|
|
public function new(url:String) {
|
|
_url = url;
|
|
}
|
|
|
|
}
|