Files
LNXSDK/leenkx/Sources/leenkx/network/Leenkx.hx
2026-10-06 02:10:08 -07:00

150 lines
4.9 KiB
Haxe

package leenkx.network;
#if lnx_torrent
import leenkx.network.Types;
import haxe.io.Bytes;
import iron.object.Object;
import leenkx.system.Event;
import leenkx.network.Buffer;
import leenkx.network.torrent.LeenkxClient;
@: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 onWireLeftEvent: String = "Leenkx.onWireLeft";
public static var onWireSeenEvent: String = "Leenkx.onWireSeen";
public static var onTorrentEvent: String = "Leenkx.onTorrent";
public static var onTorrentAddedEvent: String = "Leenkx.onTorrentAdded";
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> = [];
public static var peers:Map<String, String> = [];
public static var data:Map<String, Dynamic> = [];
public static var id:Map<String, String> = [];
public static var torrent:Map<String, Dynamic> = [];
public static var file:Map<String, Dynamic> = [];
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) {
var uid = object.uid;
Leenkx.connections[net_Url].onopen = function() {
Event.send(Leenkx.onOpenEvent, uid);
};
Leenkx.connections[net_Url].onmessage = function() {
Event.send(Leenkx.onMessageEvent, uid);
};
Leenkx.connections[net_Url].onerror = function() {
Event.send(Leenkx.onErrorEvent, uid);
};
Leenkx.connections[net_Url].onclose = function() {
Event.send(Leenkx.onCloseEvent, uid);
};
Leenkx.connections[net_Url].onseen = function() {
Event.send(Leenkx.onSeenEvent, uid);
};
Leenkx.connections[net_Url].onserver = function() {
Event.send(Leenkx.onServerEvent, uid);
};
Leenkx.connections[net_Url].onconnections = function() {
Event.send(Leenkx.onConnectionsEvent, uid);
};
Leenkx.connections[net_Url].onping = function() {
Event.send(Leenkx.onPingEvent, uid);
};
Leenkx.connections[net_Url].onleft = function() {
Event.send(Leenkx.onLeftEvent, uid);
};
Leenkx.connections[net_Url].ontimeout = function() {
Event.send(Leenkx.onTimeOutEvent, uid);
};
Leenkx.connections[net_Url].onwireleft = function() {
Event.send(Leenkx.onWireLeftEvent, uid);
};
Leenkx.connections[net_Url].onwireseen = function() {
Event.send(Leenkx.onWireSeenEvent, uid);
};
Leenkx.connections[net_Url].ontorrent = function() {
Event.send(Leenkx.onTorrentEvent, uid);
};
Leenkx.connections[net_Url].ontorrentadded = function() {
Event.send(Leenkx.onTorrentAddedEvent, uid);
};
Leenkx.connections[net_Url].ontracker = function() {
Event.send(Leenkx.onTrackerEvent, uid);
};
Leenkx.connections[net_Url].onannounce = function() {
Event.send(Leenkx.onAnnounceEvent, uid);
};
Leenkx.connections[net_Url].onload = function() {
Event.send(Leenkx.onLoadEvent, uid);
};
Leenkx.connections[net_Url].ontorrentdone = function() {
Event.send(Leenkx.onTorrentDoneEvent, uid);
};
var connectionUrl = net_Url;
Leenkx.connections[connectionUrl].onload();
}
}
}
}
}
class LeenkxSocket {
public var _url:String;
public var client:LeenkxClient;
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 onwireleft:Void->Void;
public var onwireseen:Void->Void;
public var ontorrent:Void->Void;
public var ontorrentadded: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;
}
}
#end