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 lua.Io;
import haxe.io.Path;
import lua.lib.luv.fs.FileSystem as LFileSystem;
class FileSystem {
public static function exists(path:String):Bool {
if (path == null)
return false;
else {
var res = LFileSystem.stat(path);
return res.result != null;
}
}
public inline static function rename(path:String, newPath:String):Void {
var ret = lua.Os.rename(path, newPath);
if (!ret.success) {
throw ret.message;
}
}
public inline static function stat(path:String):FileStat {
var ls = LFileSystem.stat(path);
if (ls.result == null)
throw ls.message;
var l = ls.result;
return {
gid: l.gid,
uid: l.uid,
rdev: l.rdev,
size: l.size,
nlink: l.nlink,
mtime: Date.fromTime(l.mtime.sec + l.mtime.nsec / 1000000),
mode: l.mode,
ino: l.ino,
dev: l.dev,
ctime: Date.fromTime(l.ctime.sec + l.ctime.nsec / 1000000),
atime: Date.fromTime(l.atime.sec + l.atime.nsec / 1000000)
};
}
public inline static function fullPath(relPath:String):String {
return LFileSystem.realpath(Path.normalize(absolutePath(relPath)));
}
public inline static function absolutePath(relPath:String):String {
if (haxe.io.Path.isAbsolute(relPath)) {
return relPath;
}
var pwd = lua.lib.luv.Misc.cwd();
if (pwd == null)
return relPath;
return Path.join([pwd, relPath]);
}
public inline static function deleteFile(path:String):Void {
var ret = lua.Os.remove(path);
if (!ret.success) {
throw ret.message;
}
}
public inline static function readDirectory(path:String):Array<String> {
var scandir = LFileSystem.scandir(path);
var itr = function() {
var next = LFileSystem.scandir_next(scandir).name;
return next;
}
return lua.Lib.fillArray(itr);
}
public inline static function isDirectory(path:String):Bool {
var result = LFileSystem.stat(path).result;
if (result == null)
return false;
else
return result.type == "directory";
}
public inline static function deleteDirectory(path:String):Void {
var ret = LFileSystem.rmdir(path);
if (ret.result == null) {
throw ret.message;
}
}
public static function createDirectory(path:String):Void {
var path = haxe.io.Path.addTrailingSlash(path);
var _p = null;
var parts = [];
while (path != (_p = haxe.io.Path.directory(path))) {
parts.unshift(path);
path = _p;
}
for (part in parts) {
if (part.charCodeAt(part.length - 1) != ":".code && !exists(part) && !LFileSystem.mkdir(part, 511).result)
throw "Could not create directory:" + part;
}
}
}

View File

@ -0,0 +1,97 @@
/*
* 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.SysTools;
import lua.Lua;
import lua.Io;
import lua.Os;
import lua.FileHandle;
import lua.Boot;
@:coreApi
class File {
public static function getContent(path:String):String {
var f = Io.open(path, "r");
if (f == null)
throw 'Invalid path : $path';
var s = f.read("*all");
f.close();
return s;
}
public static function append(path:String, binary:Bool = true):FileOutput {
return @:privateAccess new FileOutput(Io.open(path, "a"));
}
public static function update(path:String, binary:Bool = true):FileOutput {
if (!FileSystem.exists(path)) {
write(path).close();
}
return @:privateAccess new FileOutput(Io.open(path, binary ? "r+b" : "r+"));
}
public static function copy(srcPath:String, dstPath:String):Void {
var result = switch (Sys.systemName()) {
case "Windows": Os.execute('copy ${SysTools.quoteWinArg(srcPath, true)} ${SysTools.quoteWinArg(dstPath, true)}');
default: Os.execute('cp ${SysTools.quoteUnixArg(srcPath)} ${SysTools.quoteUnixArg(dstPath)}');
};
if (#if (lua_ver >= 5.2) !result.success #elseif (lua_ver < 5.2) result != 0 #else ((result : Dynamic) != true && (result : Dynamic) != 0) #end
) {
throw 'Failed to copy $srcPath to $dstPath';
}
}
public static function getBytes(path:String):haxe.io.Bytes {
var finput = read(path, true);
var res = finput.readAll();
finput.close();
return res;
}
public static function read(path:String, binary:Bool = true):FileInput {
var fh = Io.open(path, binary ? 'rb' : 'r');
if (fh == null)
throw 'Invalid path : $path';
return @:privateAccess new FileInput(fh);
}
public static function write(path:String, binary:Bool = true):FileOutput {
var fh = Io.open(path, binary ? 'wb' : 'w');
if (fh == null)
throw 'Invalid path : $path';
return @:privateAccess new FileOutput(fh);
}
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 saveContent(path:String, content:String):Void {
var f = write(path, true);
f.writeString(content);
f.close();
}
}

View File

@ -0,0 +1,100 @@
/*
* 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 lua.FileHandle;
import lua.Io;
import lua.NativeStringTools;
import lua.Boot;
import lua.Os;
import haxe.io.Bytes;
import haxe.io.Error;
import haxe.io.Eof;
class FileInput extends haxe.io.Input {
var f:FileHandle;
var _eof:Bool;
function new(f:FileHandle) {
if (f == null)
throw 'Invalid filehandle : $f';
this.bigEndian = Boot.platformBigEndian;
this.f = f;
this._eof = false;
}
inline public function seek(p:Int, pos:FileSeek):Void {
var arg = switch (pos) {
case SeekBegin: "set";
case SeekCur: "cur";
case SeekEnd: "end";
}
_eof = false;
return f.seek(arg, p);
}
inline public function tell():Int {
return f.seek();
}
inline public function eof():Bool {
return _eof;
}
override inline public function readByte():Int {
var byte = f.read(1);
if (byte == null) {
_eof = true;
throw new haxe.io.Eof();
}
return NativeStringTools.byte(byte);
}
override function readBytes(s:Bytes, pos:Int, len:Int):Int {
if (eof())
throw new haxe.io.Eof();
return super.readBytes(s, pos, len);
}
override inline public function close():Void {
f.close();
}
override public function readAll(?bufsize:Int):Bytes {
if (bufsize == null)
bufsize = (1 << 14); // 16 Ko
var buf = Bytes.alloc(bufsize);
var total = new haxe.io.BytesBuffer();
try {
while (true) {
var len = readBytes(buf, 0, bufsize);
if (len == 0)
break;
total.addBytes(buf, 0, len);
}
} catch (e:Eof) {
_eof = true;
}
return total.getBytes();
}
}

View File

@ -0,0 +1,62 @@
/*
* 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 lua.FileHandle;
import haxe.io.Bytes;
class FileOutput extends haxe.io.Output {
var f:FileHandle;
function new(f:FileHandle) {
if (f == null)
throw 'Invalid filehandle : $f';
this.f = f;
}
public inline function seek(p:Int, pos:FileSeek):Void {
var arg = switch (pos) {
case SeekBegin: "set";
case SeekCur: "cur";
case SeekEnd: "end";
}
return f.seek(arg, p);
}
public inline function tell():Int {
return f.seek();
}
override inline public function writeByte(c:Int):Void {
f.write(String.fromCharCode(c));
}
override inline public function writeBytes(s:Bytes, pos:Int, len:Int):Int {
f.write(s.getString(pos, len));
return s.length;
}
override public function close() {
f.close();
}
}

View File

@ -0,0 +1,228 @@
/*
* 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 lua.Io;
import lua.lib.luv.Pipe;
import lua.lib.luv.Signal;
import lua.lib.luv.Loop;
import lua.Boot;
import lua.Table;
import lua.NativeStringTools;
import haxe.SysTools;
import haxe.io.Bytes;
import haxe.io.Error;
import haxe.io.Eof;
@:coreApi
class Process {
var _pid:Int;
var _handle:lua.lib.luv.Process;
var _code:Int;
var closef:Int->Signal->Void;
public var stdout(default, null):haxe.io.Input;
public var stderr(default, null):haxe.io.Input;
public var stdin(default, null):haxe.io.Output;
static var argQuote = Sys.systemName() == "Windows" ? function(x) return SysTools.quoteWinArg(x, true) : SysTools.quoteUnixArg;
static var _shell = Sys.systemName() == "Windows" ? 'cmd.exe' : '/bin/sh';
/**
Sets the args for the shell, which will include the cmd to be executed
by the shell.
**/
static function setArgs(cmd:String, ?args:Array<String>):Table<Int, String> {
var pargs = lua.Table.create();
var idx = 1;
if (sys.FileSystem.exists(cmd))
cmd = '"$cmd"'; // escape simple paths
var all = [cmd];
if (args != null) {
for (a in args) {
all.push(argQuote(a));
}
}
if (Sys.systemName() == "Windows") {
pargs[idx++] = '/s';
pargs[idx++] = '/c';
pargs[idx++] = all.join(" ");
} else {
pargs[idx++] = "-c";
pargs[idx++] = all.join(" ");
}
return pargs;
}
public function new(cmd:String, ?args:Array<String>, ?detached:Bool) {
if (detached)
throw "Detached process is not supported on this platform";
var _stdout = new Pipe(false);
var _stderr = new Pipe(false);
var _stdin = new Pipe(false);
stdout = new ProcessInput(_stdout);
stderr = new ProcessInput(_stderr);
stdin = new ProcessOutput(_stdin);
var stdio = untyped __lua_table__([_stdin, _stdout, _stderr]);
var opt = {args: setArgs(cmd, args), stdio: stdio};
var p = lua.lib.luv.Process.spawn(_shell, opt, function(code:Int, signal:Signal) {
_code = code;
if (!_handle.is_closing()){
_handle.close();
}
_stdin.shutdown(()->_stdin.close());
_stderr.shutdown(()->_stderr.close());
_stdout.shutdown(()->_stdout.close());
});
_handle = p.handle;
if (p.handle == null)
throw p.pid;
_pid = p.pid;
}
public function getPid():Int {
return _pid;
}
public function close():Void {
if (!_handle.is_closing()){
_handle.close();
}
}
public function exitCode(block:Bool = true):Null<Int> {
if (!block)
return _code;
while (_handle.is_active()) {
Loop.run(); // process io until the handle closes (emulate blocking)
}
return _code;
}
public function kill():Void {
_handle.kill("sigterm");
}
}
private class ProcessInput extends haxe.io.Input {
var b:Pipe;
var buf:String;
var idx:Int;
var _eof:Bool;
public function new(pipe:Pipe) {
b = pipe;
_eof = false;
}
inline public function eof():Bool {
return _eof;
}
override function readBytes(s:Bytes, pos:Int, len:Int):Int {
if (eof())
throw new haxe.io.Eof();
return super.readBytes(s, pos, len);
}
override public function readByte() {
var err_str = null;
if (buf == null || idx >= NativeStringTools.len(buf)) {
buf = null;
idx = 0;
var pending = true;
b.read_start(function(err, chunk) {
if (chunk != null) {
if (buf != null) {
buf = buf + chunk;
} else {
buf = chunk;
}
}
if (err != null)
err_str = err;
pending = false;
});
// process io until we read our input (emulate blocking)
while (pending)
Loop.run();
}
if (buf == null) {
_eof = true;
throw new haxe.io.Eof();
}
if (err_str != null)
throw err_str;
var code = NativeStringTools.byte(buf, ++idx);
return code;
}
override public function readAll(?bufsize:Int):Bytes {
if (bufsize == null)
bufsize = (1 << 14); // 16 Ko
var buf = Bytes.alloc(bufsize);
var total = new haxe.io.BytesBuffer();
try {
while (true) {
var len = readBytes(buf, 0, bufsize);
// don't throw blocked error here
if (len != 0)
total.addBytes(buf, 0, len);
if (len < bufsize)
break;
}
} catch (e:Eof) {
_eof = true;
}
return total.getBytes();
}
override public function close() {
b.close();
}
}
private class ProcessOutput extends haxe.io.Output {
var b:Pipe;
public function new(pipe:Pipe) {
b = pipe;
set_bigEndian(Boot.platformBigEndian);
}
override public function writeByte(c:Int):Void {
b.write(NativeStringTools.char(c));
}
override public function close() {
b.close();
}
}

View File

@ -0,0 +1,71 @@
/*
* 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.io.Bytes;
import haxe.io.BytesInput;
import lua.NativeStringTools.find;
import lua.lib.luv.net.Dns;
import lua.lib.luv.Os;
@:coreapi
class Host {
public var host(default, null):String;
public var ip(default, null):Int;
var _ip:String;
public function new(name:String):Void {
host = name;
if (find(name, "(%d+)%.(%d+)%.(%d+)%.(%d+)").begin != null) {
_ip = name;
} else {
var res = lua.lib.luv.net.Dns.getaddrinfo(name);
if (res.result == null)
throw "Unrecognized node name";
_ip = res.result[1].addr;
if (_ip == "::1")
_ip = "127.0.0.0";
}
var num = 0;
for (a in _ip.split(".")) {
num = num * 256 + lua.Lua.tonumber(a);
}
ip = num;
}
public function toString():String {
return _ip;
}
public function reverse():String {
return Dns.getnameinfo({ip: _ip}).result;
}
static public function localhost():String {
return Os.gethostname();
}
}

View File

@ -0,0 +1,234 @@
/*
* 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 lua.lib.luasocket.Socket as LuaSocket;
import lua.lib.luasocket.socket.*;
import lua.*;
import haxe.io.Bytes;
import haxe.io.Error;
class Socket {
public var input(default, null):haxe.io.Input;
public var output(default, null):haxe.io.Output;
var custom:Dynamic;
var _socket:LuaSocket;
var blocking = false;
var timeout = null;
public function new():Void {}
public function close():Void {
_socket.close();
}
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 {
var res = LuaSocket.connect(host.host, port);
if (res.message != null)
throw 'Socket Error : ${res.message}';
input = new SocketInput(res.result);
output = new SocketOutput(res.result);
_socket = res.result;
_socket.settimeout(timeout);
}
public function listen(connections:Int):Void {
var res = LuaSocket.tcp();
if (res.message != null)
throw 'Socket Listen Error : ${res.message}';
res.result.listen(connections);
_socket = res.result;
_socket.settimeout(timeout);
}
public function shutdown(read:Bool, write:Bool):Void {
var client:TcpClient = cast _socket;
switch [read, write] {
case [true, true]:
client.shutdown(Both);
case [true, false]:
client.shutdown(Receive);
case [false, true]:
client.shutdown(Send);
default:
null;
}
}
public function bind(host:Host, port:Int):Void {
var res = LuaSocket.bind(host.host, port);
if (res.message != null)
throw 'Socket Bind Error : ${res.message}';
_socket = res.result;
}
public function accept():Socket {
var server:TcpServer = cast _socket;
var res = server.accept();
if (res.message != null)
throw 'Error : ${res.message}';
var sock = new Socket();
sock._socket = res.result;
sock.input = new SocketInput(res.result);
sock.output = new SocketOutput(res.result);
return sock;
}
public function peer():{host:Host, port:Int} {
var client:TcpClient = cast _socket;
var res = client.getpeername();
var host = new Host(res.address);
return {host: host, port: Std.parseInt(res.port)};
}
public function host():{host:Host, port:Int} {
var server:TcpServer = cast _socket;
var res = server.getsockname();
var host = new Host(res.address);
return {host: host, port: Std.parseInt(res.port)};
}
public inline function setTimeout(timeout:Float):Void {
this.timeout = timeout;
if (_socket != null) {
var client:TcpClient = cast _socket;
client.settimeout(timeout);
}
}
public function waitForRead():Void {
select([this], null, null);
}
public function setBlocking(b:Bool):Void {
blocking = b;
}
public function setFastSend(b:Bool):Void {
var client:TcpClient = cast _socket;
client.setoption(TcpNoDelay, true);
}
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 read_tbl = read == null ? Table.create() : Table.fromArray([for (r in read) cast r._socket]);
var write_tbl = write == null ? Table.create() : Table.fromArray(([for (r in write) cast r._socket]));
var res = LuaSocket.select(read_tbl, write_tbl, timeout);
var convert_socket = function(x:LuaSocket) {
var sock = new Socket();
sock.input = new SocketInput(cast x);
sock.output = new SocketOutput(cast x);
return sock;
}
var read_arr = res.read == null ? [] : Table.toArray(res.read).map(convert_socket);
var write_arr = res.write == null ? [] : Table.toArray(res.write).map(convert_socket);
return {read: read_arr, write: write_arr, others: []};
}
}
private class SocketInput extends haxe.io.Input {
var tcp:TcpClient;
public function new(tcp:TcpClient) {
this.tcp = tcp;
}
override public function readByte():Int {
var res = tcp.receive(1);
if (res.message == "closed"){
throw new haxe.io.Eof();
}
else if (res.message != null)
throw 'Error : ${res.message}';
return res.result.charCodeAt(0);
}
override public function readBytes(s:Bytes, pos:Int, len:Int):Int {
var leftToRead = len;
var b = s.getData();
if (pos < 0 || len < 0 || pos + len > s.length)
throw haxe.io.Error.OutsideBounds;
var readCount = 0;
try {
while (leftToRead > 0) {
b[pos] = cast readByte();
pos++;
readCount++;
leftToRead--;
}
} catch (e:haxe.io.Eof) {
if (readCount == 0) {
throw e;
}
}
return readCount;
}
}
private class SocketOutput extends haxe.io.Output {
var tcp:TcpClient;
public function new(tcp:TcpClient) {
this.tcp = tcp;
}
override public function writeByte(c:Int):Void {
var char = NativeStringTools.char(c);
var res = tcp.send(char);
if (res.message != null){
throw 'Error : Socket writeByte : ${res.message}';
}
}
override public function writeBytes(s:Bytes, pos:Int, len:Int):Int {
if (pos < 0 || len < 0 || pos + len > s.length)
throw Error.OutsideBounds;
var b = s.getData().slice(pos, pos +len).map(function(byte){
return lua.NativeStringTools.char(byte);
});
var encoded = Table.concat(cast b, 0);
var res = tcp.send(encoded);
if (res.message != null){
throw 'Error : Socket writeByte : ${res.message}';
}
return len;
}
}