Update Files

This commit is contained in:
2025-01-22 16:18:30 +01:00
parent ed4603cf95
commit a36294b518
16718 changed files with 2960346 additions and 0 deletions

View File

@ -0,0 +1,125 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys;
import cs.system.io.DirectoryInfo;
import cs.system.io.File;
import cs.system.io.Directory;
import cs.system.io.FileInfo;
@:coreApi
class FileSystem {
public static function exists(path:String):Bool {
return (File.Exists(path) || Directory.Exists(path));
}
public static function rename(path:String, newPath:String):Void {
Directory.Move(path, newPath);
}
@:access(Date.fromNative)
public static function stat(path:String):FileStat {
if (File.Exists(path)) {
var fi = new FileInfo(path);
return {
gid: 0, // C# doesn't let you get this info
uid: 0, // same
atime: Date.fromNative(fi.LastAccessTime),
mtime: Date.fromNative(fi.LastWriteTime),
ctime: Date.fromNative(fi.CreationTime),
size: cast(fi.Length, Int), // TODO: maybe change to Int64 for Haxe 3?
dev: 0, // FIXME: not sure what that is
ino: 0, // FIXME: not sure what that is
nlink: 0, // FIXME: not sure what that is
rdev: 0, // FIXME: not sure what that is
mode: 0 // FIXME: not sure what that is
};
} else if (Directory.Exists(path)) {
var fi = new DirectoryInfo(path);
return {
gid: 0, // C# doesn't let you get this info
uid: 0, // same
atime: Date.fromNative(fi.LastAccessTime),
mtime: Date.fromNative(fi.LastWriteTime),
ctime: Date.fromNative(fi.CreationTime),
size: 0, // TODO: maybe change to Int64 for Haxe 3?
dev: 0, // FIXME: not sure what that is
ino: 0, // FIXME: not sure what that is
nlink: 0, // FIXME: not sure what that is
rdev: 0, // FIXME: not sure what that is
mode: 0 // FIXME: not sure what that is
};
} else {
throw "Path '" + path + "' doesn't exist";
}
}
public static function fullPath(relPath:String):String {
return new FileInfo(relPath).FullName;
}
public static function absolutePath(relPath:String):String {
if (haxe.io.Path.isAbsolute(relPath))
return relPath;
return haxe.io.Path.join([Sys.getCwd(), relPath]);
}
public static function isDirectory(path:String):Bool {
var isdir = Directory.Exists(path);
if (isdir != File.Exists(path))
return isdir;
throw "Path '" + path + "' doesn't exist";
}
public static function createDirectory(path:String):Void {
Directory.CreateDirectory(path);
}
public static function deleteFile(path:String):Void {
if (!File.Exists(path))
throw "Path '" + path + "' doesn't exist";
File.Delete(path);
}
public static function deleteDirectory(path:String):Void {
if (!Directory.Exists(path))
throw "Path '" + path + "' doesn't exist";
Directory.Delete(path);
}
public static function readDirectory(path:String):Array<String> {
var ret = Directory.GetFileSystemEntries(path);
if (ret.Length > 0) {
var fst = ret[0];
var sep = "/";
if (fst.lastIndexOf(sep) < fst.lastIndexOf("\\"))
sep = "\\";
for (i in 0...ret.Length) {
var path = ret[i];
ret[i] = path.substr(path.lastIndexOf(sep) + 1);
}
}
return cs.Lib.array(ret);
}
}

View File

@ -0,0 +1,59 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.db;
class Sqlite {
static var type:Class<cs.system.data.IDbConnection>;
public static function open(file:String):sys.db.Connection {
var cnxString = 'Data Source=$file';
if (type == null) {
var t = null;
var assemblies = cs.system.AppDomain.CurrentDomain.GetAssemblies();
for (i in 0...assemblies.Length) {
var a = assemblies[i];
t = a.GetType('Mono.Data.Sqlite.SqliteConnection');
if (t == null)
t = a.GetType('System.Data.SQLite.SQLiteConnection');
if (t != null) {
break;
}
}
if (t == null) {
var asm = cs.system.reflection.Assembly.Load('Mono.Data.Sqlite');
t = asm.GetType('Mono.Data.Sqlite.SqliteConnection');
}
if (t != null)
type = cast cs.Lib.fromNativeType(t);
}
if (type == null) {
throw "No ADO.NET SQLite provider was found!";
}
var ret = Type.createInstance(type, [cnxString]);
ret.Open();
return cs.db.AdoNet.create(ret, 'SQLite');
}
}

View File

@ -0,0 +1,95 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.io;
@:coreApi
class File {
public static function getContent(path:String):String {
var f = read(path, false);
var ret = f.readAll().toString();
f.close();
return ret;
}
public static function saveContent(path:String, content:String):Void {
var f = write(path, false);
f.writeString(content);
f.close();
}
public static function getBytes(path:String):haxe.io.Bytes {
var f = read(path, true);
var ret = f.readAll();
f.close();
return ret;
}
public static function saveBytes(path:String, bytes:haxe.io.Bytes):Void {
var f = write(path, true);
f.writeBytes(bytes, 0, bytes.length);
f.close();
}
public static function read(path:String, binary:Bool = true):FileInput {
#if std_buffer // standardize 4kb buffers
var stream = new cs.system.io.FileStream(path, Open, Read, ReadWrite, 4096);
#else
var stream = new cs.system.io.FileStream(path, Open, Read, ReadWrite);
#end
return @:privateAccess new FileInput(stream);
}
public static function write(path:String, binary:Bool = true):FileOutput {
#if std_buffer // standardize 4kb buffers
var stream = new cs.system.io.FileStream(path, Create, Write, ReadWrite, 4096);
#else
var stream = new cs.system.io.FileStream(path, Create, Write, ReadWrite);
#end
return @:privateAccess new FileOutput(stream);
}
public static function append(path:String, binary:Bool = true):FileOutput {
#if std_buffer // standardize 4kb buffers
var stream = new cs.system.io.FileStream(path, Append, Write, ReadWrite, 4096);
#else
var stream = new cs.system.io.FileStream(path, Append, Write, ReadWrite);
#end
return @:privateAccess new FileOutput(stream);
}
public static function update(path:String, binary:Bool = true):FileOutput {
if (!FileSystem.exists(path)) {
write(path).close();
}
#if std_buffer // standardize 4kb buffers
var stream = new cs.system.io.FileStream(path, OpenOrCreate, Write, ReadWrite, 4096);
#else
var stream = new cs.system.io.FileStream(path, OpenOrCreate, Write, ReadWrite);
#end
return @:privateAccess new FileOutput(stream);
}
public static function copy(srcPath:String, dstPath:String):Void {
cs.system.io.File.Copy(srcPath, dstPath, true);
}
}

View File

@ -0,0 +1,29 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.io;
class FileInput extends cs.io.NativeInput {
function new(stream:cs.system.io.FileStream) {
super(stream);
}
}

View File

@ -0,0 +1,29 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.io;
class FileOutput extends cs.io.NativeOutput {
function new(stream:cs.system.io.FileStream) {
super(stream);
}
}

View File

@ -0,0 +1,127 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.io;
import haxe.io.BytesInput;
import cs.system.io.StreamReader;
import cs.system.io.StreamWriter;
import cs.system.diagnostics.Process as NativeProcess;
import cs.system.diagnostics.ProcessStartInfo as NativeStartInfo;
@:coreApi
class Process {
public var stdout(default, null):haxe.io.Input;
public var stderr(default, null):haxe.io.Input;
public var stdin(default, null):haxe.io.Output;
private var native:NativeProcess;
public function new(cmd:String, ?args:Array<String>, ?detached:Bool):Void {
if (detached)
throw "Detached process is not supported on this platform";
this.native = createNativeProcess(cmd, args);
native.Start();
this.stdout = new cs.io.NativeInput(native.StandardOutput.BaseStream);
this.stderr = new cs.io.NativeInput(native.StandardError.BaseStream);
this.stdin = new cs.io.NativeOutput(native.StandardInput.BaseStream);
}
@:allow(Sys)
private static function createNativeProcess(cmd:String, ?args:Array<String>):NativeProcess {
var native = new NativeProcess();
native.StartInfo.CreateNoWindow = true;
native.StartInfo.RedirectStandardError = native.StartInfo.RedirectStandardInput = native.StartInfo.RedirectStandardOutput = true;
if (args != null) {
// mono 4.2.1 on Windows doesn't support relative path correctly
if (cmd.indexOf("/") != -1 || cmd.indexOf("\\") != -1)
cmd = sys.FileSystem.fullPath(cmd);
native.StartInfo.FileName = cmd;
native.StartInfo.UseShellExecute = false;
native.StartInfo.Arguments = buildArgumentsString(args);
} else {
switch (Sys.systemName()) {
case "Windows":
native.StartInfo.FileName = switch (Sys.getEnv("COMSPEC")) {
case null: "cmd.exe";
case var comspec: comspec;
}
native.StartInfo.Arguments = '/C "$cmd"';
case _:
native.StartInfo.FileName = "/bin/sh";
native.StartInfo.Arguments = buildArgumentsString(["-c", cmd]);
}
native.StartInfo.UseShellExecute = false;
}
return native;
}
private static function buildArgumentsString(args:Array<String>):String {
return switch (Sys.systemName()) {
case "Windows":
[
for (a in args)
haxe.SysTools.quoteWinArg(a, false)
].join(" ");
case _:
// mono uses a slightly different quoting/escaping rule...
// https://bugzilla.xamarin.com/show_bug.cgi?id=19296
[
for (arg in args) {
var b = new StringBuf();
b.add('"');
for (i in 0...arg.length) {
var c = arg.charCodeAt(i);
switch (c) {
case '"'.code | '\\'.code:
b.addChar('\\'.code);
case _: // pass
}
b.addChar(c);
}
b.add('"');
b.toString();
}
].join(" ");
}
}
public function getPid():Int {
return native.Id;
}
public function exitCode(block:Bool = true):Null<Int> {
if (block == false && !native.HasExited)
return null;
native.WaitForExit();
return native.ExitCode;
}
public function close():Void {
native.Close();
}
public function kill():Void {
native.Kill();
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.net;
import cs.system.Array;
import cs.system.net.Dns;
import cs.system.net.IPAddress;
import cs.system.net.IPHostEntry;
import cs.system.net.sockets.AddressFamily;
import haxe.io.Bytes;
import haxe.io.BytesInput;
@:coreapi
class Host {
public var hostEntry(default, null):IPHostEntry;
public var ipAddress(default, null):IPAddress;
public var host(default, null):String;
public var ip(get, null):Int;
private function get_ip():Int {
return new BytesInput(Bytes.ofData(ipAddress.GetAddressBytes())).readInt32();
}
public function new(name:String):Void {
host = name;
try{
hostEntry = Dns.GetHostEntry(host);
for (i in 0...hostEntry.AddressList.Length) {
if (hostEntry.AddressList[i].AddressFamily == InterNetwork) {
ipAddress = hostEntry.AddressList[i];
break;
}
}
}catch (e:Dynamic){
ipAddress = IPAddress.Any;
if (!IPAddress.TryParse(host, ipAddress)){
throw "Unknown host.";
}
}
}
public function toString():String {
return ipAddress.ToString();
}
public function reverse():String {
return hostEntry.HostName;
}
static public function localhost():String {
return Dns.GetHostName();
}
}

View File

@ -0,0 +1,190 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.net;
import cs.NativeArray;
import cs.system.collections.ArrayList;
import cs.system.net.IPEndPoint;
import cs.system.net.sockets.AddressFamily;
import cs.system.net.sockets.NetworkStream;
import cs.system.net.sockets.ProtocolType;
import cs.system.net.sockets.SocketFlags;
import cs.system.net.sockets.SocketShutdown;
import cs.system.net.sockets.SocketType;
import cs.system.threading.Thread;
import cs.system.net.sockets.Socket in NativeSocket;
import cs.types.UInt8;
import haxe.io.Bytes;
import haxe.io.Error;
import haxe.io.Input;
import haxe.io.Output;
@:coreApi
class Socket {
private var sock:NativeSocket = null;
public var input(default, null):haxe.io.Input;
public var output(default, null):haxe.io.Output;
public var custom:Dynamic;
/**
Creates a new unconnected socket.
**/
public function new():Void {
init();
}
private function init():Void {
sock = new NativeSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Blocking = true;
}
public function close():Void {
sock.Close();
input = null;
output = null;
}
public function read():String {
return input.readAll().toString();
}
public function write(content:String):Void {
output.writeString(content);
}
public function connect(host:Host, port:Int):Void {
sock.Connect(host.ipAddress, port);
if (sock.Connected) {
this.output = new cs.io.NativeOutput(new NetworkStream(sock));
this.input = new cs.io.NativeInput(new NetworkStream(sock));
} else {
throw "Connection failed.";
}
}
public function listen(connections:Int):Void {
sock.Listen(connections);
}
public function shutdown(read:Bool, write:Bool):Void {
if (read && write) {
sock.Shutdown(SocketShutdown.Both);
input = null;
output = null;
} else if (read) {
sock.Shutdown(SocketShutdown.Receive);
input = null;
} else if (write) {
sock.Shutdown(SocketShutdown.Send);
output = null;
}
}
public function bind(host:Host, port:Int):Void {
sock = new NativeSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Bind(new IPEndPoint(host.ipAddress, port));
}
public function accept():Socket {
var r = new Socket();
r.sock = sock.Accept();
r.output = new cs.io.NativeOutput(new NetworkStream(r.sock));
r.input = new cs.io.NativeInput(new NetworkStream(r.sock));
return r;
}
public function peer():{host:Host, port:Int} {
var remoteIP = cast(sock.RemoteEndPoint, IPEndPoint);
return {host: new Host(remoteIP.Address.ToString()), port: remoteIP.Port};
}
public function host():{host:Host, port:Int} {
var localIP = cast(sock.LocalEndPoint, IPEndPoint);
return {host: new Host(localIP.Address.ToString()), port: localIP.Port};
}
public function setTimeout(timeout:Float):Void {
sock.ReceiveTimeout = sock.SendTimeout = Math.round(timeout * 1000);
}
public function waitForRead():Void {
var end = Date.now().getTime() + ((sock.ReceiveTimeout <= 0) ? Math.POSITIVE_INFINITY : sock.ReceiveTimeout);
while (sock.Available == 0 && Date.now().getTime() < end) {
Thread.Sleep(5);
}
}
public function setBlocking(b:Bool):Void {
sock.Blocking = b;
}
public function setFastSend(b:Bool):Void {
sock.NoDelay = b;
}
static public function select(read:Array<Socket>, write:Array<Socket>, others:Array<Socket>,
?timeout:Float):{read:Array<Socket>, write:Array<Socket>, others:Array<Socket>} {
var map:Map<Int, Socket> = new Map();
inline function addSockets(sockets:Array<Socket>) {
if (sockets != null)
for (s in sockets)
map[s.sock.Handle.ToInt32()] = s;
}
inline function getRaw(sockets:Array<Socket>):ArrayList {
var a = new ArrayList(sockets == null ? 0 : sockets.length);
if (sockets != null)
for (s in sockets) {
a.Add(s.sock);
}
return a;
}
inline function getOriginal(result:ArrayList) {
var a:Array<Socket> = [];
for (i in 0...result.Count) {
var s:NativeSocket = result[i];
a.push(map[s.Handle.ToInt32()]);
}
return a;
}
addSockets(read);
addSockets(write);
addSockets(others);
// unwrap Sockets into native sockets
var rawRead:ArrayList = getRaw(read),
rawWrite:ArrayList = getRaw(write),
rawOthers:ArrayList = getRaw(others);
var microsec = timeout == null ? -1 : Std.int(timeout * 1000000);
NativeSocket.Select(rawRead, rawWrite, rawOthers, microsec);
// convert native sockets back to Socket objects
return {
read: getOriginal(rawRead),
write: getOriginal(rawWrite),
others: getOriginal(rawOthers),
}
}
}

View File

@ -0,0 +1,99 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.net;
import haxe.extern.Rest;
import sys.net.Socket;
import cs.NativeArray;
import cs.system.collections.ArrayList;
import cs.system.net.IPEndPoint;
import cs.system.net.EndPoint;
import cs.system.net.IPAddress;
import cs.system.net.sockets.AddressFamily;
import cs.system.net.sockets.NetworkStream;
import cs.system.net.sockets.ProtocolType;
import cs.system.net.sockets.SocketFlags;
import cs.system.net.sockets.SocketShutdown;
import cs.system.net.sockets.SocketType;
import cs.system.threading.Thread;
import cs.system.net.sockets.Socket in NativeSocket;
import cs.types.UInt8;
import cs.Ref;
import haxe.io.Bytes;
import haxe.io.Error;
import haxe.io.Input;
import haxe.io.Output;
@:coreapi
class UdpSocket extends Socket {
public function new() {
super();
}
override private function init():Void {
sock = new NativeSocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
override public function bind(host:Host, port:Int):Void {
sock = new NativeSocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
var endpoint:IPEndPoint = new IPEndPoint(host.ipAddress, port);
sock.Bind(endpoint);
}
public function sendTo(buf:haxe.io.Bytes, pos:Int, len:Int, addr:Address):Int {
var data = new NativeArray<UInt8>(len);
var indices:NativeArray<Int>;
for (i in 0...len) {
indices = NativeArray.make(i);
data.SetValue(cast buf.get(pos + i), indices);
}
var host = addr.getHost();
var ip:IPAddress = IPAddress.Parse(host.toString());
var endpoint:IPEndPoint = new IPEndPoint(ip, addr.port);
return this.sock.SendTo(data, endpoint);
}
public function readFrom(buf:haxe.io.Bytes, pos:Int, len:Int, addr:Address):Int {
var endpoint:EndPoint = cast new IPEndPoint(IPAddress.Any, 0);
var data:NativeArray<UInt8> = new NativeArray(len);
var length:Int = -1;
try {
length = this.sock.ReceiveFrom(data, endpoint);
} catch (e:Dynamic) {
return length;
}
var ipEndpoint:IPEndPoint = cast endpoint;
addr.host = ipEndpoint.Address.Address.high;
addr.port = ipEndpoint.Port;
var i:Int = 0;
for (each in data.iterator()) {
buf.set(pos + i, each);
i += 1;
}
return length;
}
public function setBroadcast(b:Bool):Void {
sock.EnableBroadcast = b;
}
}

View File

@ -0,0 +1,60 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.thread;
import cs.system.threading.ManualResetEvent;
import cs.Lib;
@:coreApi class Deque<T> {
final storage:Array<T> = [];
final lockObj = {};
final addEvent = new ManualResetEvent(false);
public function new():Void {}
public function add(i:T):Void {
Lib.lock(lockObj, {
storage.push(i);
addEvent.Set();
});
}
public function push(i:T):Void {
Lib.lock(lockObj, {
storage.unshift(i);
addEvent.Set();
});
}
public function pop(block:Bool):Null<T> {
do {
Lib.lock(lockObj, {
if (storage.length > 0) {
return storage.shift();
}
addEvent.Reset();
});
} while (block && addEvent.WaitOne());
return null;
}
}

View File

@ -0,0 +1,79 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.thread;
import haxe.Timer;
import cs.Lib;
import cs.system.threading.ManualResetEvent;
class Lock {
final lockObj = {};
final releaseEvent = new ManualResetEvent(false);
var waitCount = 1; // initially locked
var releaseCount = 0;
public function new():Void {}
public function wait(?timeout:Float):Bool {
var myTicket;
// Get a ticket in queue
Lib.lock(lockObj, {
myTicket = waitCount;
waitCount++;
if (myTicket <= releaseCount) {
return true;
}
releaseEvent.Reset();
});
if (timeout == null) {
do {
releaseEvent.WaitOne();
if (myTicket <= releaseCount) {
return true;
}
} while (true);
} else {
var timeoutStamp = Timer.stamp() + timeout;
do {
var secondsLeft = timeoutStamp - Timer.stamp();
if (secondsLeft <= 0 || !releaseEvent.WaitOne(Std.int(secondsLeft * 1000))) {
// Timeout. Do not occupy a place in queue anymore
release();
return false;
}
if (myTicket <= releaseCount) {
return true;
}
} while (true);
}
}
public function release():Void {
Lib.lock(lockObj, {
releaseCount++;
releaseEvent.Set();
});
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.thread;
import cs.system.threading.Mutex as NativeMutex;
class Mutex {
final native = new NativeMutex();
public function new():Void {}
public function acquire():Void {
native.WaitOne();
}
public function tryAcquire():Bool {
return native.WaitOne(0);
}
public function release():Void {
native.ReleaseMutex();
}
}

View File

@ -0,0 +1,184 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.thread;
import cs.system.threading.Thread as NativeThread;
import cs.system.threading.Mutex as NativeMutex;
import cs.system.WeakReference;
import cs.Lib;
private typedef ThreadImpl = HaxeThread;
abstract Thread(ThreadImpl) from ThreadImpl {
public var events(get,never):EventLoop;
inline function new(thread:HaxeThread) {
this = thread;
}
public static function create(job:Void->Void):Thread {
var hx:Null<HaxeThread> = null;
var native = new NativeThread(job);
native.IsBackground = true;
hx = HaxeThread.allocate(native, false);
native.Start();
return new Thread(hx);
}
public static inline function runWithEventLoop(job:()->Void):Void {
HaxeThread.runWithEventLoop(job);
}
public static inline function createWithEventLoop(job:()->Void):Thread {
var hx:Null<HaxeThread> = null;
var native = new NativeThread(() -> {
job();
if(hx == null) {
HaxeThread.get(NativeThread.CurrentThread).events.loop();
} else {
hx.events.loop();
}
});
native.IsBackground = true;
hx = HaxeThread.allocate(native, true);
native.Start();
return new Thread(hx);
}
public static inline function current():Thread {
return new Thread(HaxeThread.get(NativeThread.CurrentThread));
}
public static function readMessage(block:Bool):Dynamic {
return current().readMessageImpl(block);
}
public inline function sendMessage(msg:Dynamic):Void {
this.sendMessage(msg);
}
inline function readMessageImpl(block:Bool):Dynamic {
return this.readMessage(block);
}
function get_events():EventLoop {
if(this.events == null)
throw new NoEventLoopException();
return this.events;
}
@:keep
static function processEvents():Void {
HaxeThread.get(NativeThread.CurrentThread).events.loop();
}
}
private class HaxeThread {
static var mainNativeThread:NativeThread;
static var mainHaxeThread:HaxeThread;
static var threads:Map<Int, WeakReference>;
static var threadsMutex:NativeMutex;
static var allocateCount:Int;
static function __init__() {
threads = new Map();
threadsMutex = new NativeMutex();
allocateCount = 0;
mainNativeThread = NativeThread.CurrentThread;
mainHaxeThread = new HaxeThread(NativeThread.CurrentThread);
mainHaxeThread.events = new EventLoop();
}
public final native:NativeThread;
public var events(default,null):Null<EventLoop>;
final messages = new Deque<Dynamic>();
public static function get(native:NativeThread):HaxeThread {
if(native == mainNativeThread) {
return mainHaxeThread;
}
var native = NativeThread.CurrentThread;
var key = native.ManagedThreadId;
threadsMutex.WaitOne();
var ref = threads.get(key);
threadsMutex.ReleaseMutex();
if (ref == null || !ref.IsAlive) {
return allocate(native, false);
}
return ref.Target;
}
public static function allocate(native:NativeThread, withEventLoop:Bool):HaxeThread {
threadsMutex.WaitOne();
allocateCount++;
inline function cleanup() {
if (allocateCount % 100 == 0) {
for (key => ref in threads) {
if (!ref.IsAlive) {
threads.remove(key);
}
}
}
}
var hx = new HaxeThread(native);
if(withEventLoop)
hx.events = new EventLoop();
var ref = new WeakReference(hx);
cleanup();
threads.set(native.ManagedThreadId, ref);
threadsMutex.ReleaseMutex();
return hx;
}
public static function runWithEventLoop(job:()->Void):Void {
var thread = get(NativeThread.CurrentThread);
if(thread.events == null) {
thread.events = new EventLoop();
try {
job();
thread.events.loop();
thread.events = null;
} catch(e) {
thread.events = null;
throw e;
}
} else {
job();
}
}
function new(native:NativeThread) {
this.native = native;
}
public inline function readMessage(block:Bool):Dynamic {
return messages.pop(block);
}
public function sendMessage(msg:Dynamic):Void {
messages.add(msg);
}
}

View File

@ -0,0 +1,45 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package sys.thread;
import cs.system.threading.Thread as NativeThread;
import cs.system.LocalDataStoreSlot;
class Tls<T> {
public var value(get, set):T;
final slot:LocalDataStoreSlot;
public function new():Void {
slot = NativeThread.GetNamedDataSlot('__hx__Tls');
}
function get_value():T {
return NativeThread.GetData(slot);
}
function set_value(value:T):T {
NativeThread.SetData(slot, value);
return value;
}
}