瀏覽代碼

[std] remove neko.net and js.XMLSocket

Simon Krajewski 7 年之前
父節點
當前提交
aac845bfbc
共有 6 個文件被更改,包括 0 次插入1060 次删除
  1. 0 64
      std/js/XMLSocket.hx
  2. 0 89
      std/neko/net/Poll.hx
  3. 0 202
      std/neko/net/ProxyDetect.hx
  4. 0 231
      std/neko/net/ServerLoop.hx
  5. 0 114
      std/neko/net/ThreadRemotingServer.hx
  6. 0 360
      std/neko/net/ThreadServer.hx

+ 0 - 64
std/js/XMLSocket.hx

@@ -1,64 +0,0 @@
-/*
- * Copyright (C)2005-2018 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 js;
-
-/**
-	By compiling the `haxe.remoting.SocketWrapper` into a SWF, you can create 
-	and use XMLSockets directly from Javascript.
-**/
-class XMLSocket {
-
-	var cnx : haxe.remoting.ExternalConnection;
-
-	public function new( flashObject : String ) {
-		var ctx = new haxe.remoting.Context();
-		var cnx = haxe.remoting.ExternalConnection.flashConnect("SocketWrapper",flashObject,ctx);
-		var sockId = cnx.api.create.call([flashObject]);
-		cnx.close();
-		ctx.addObject("api",this,false);
-		this.cnx = haxe.remoting.ExternalConnection.flashConnect(sockId,flashObject,ctx);
-	}
-
-	public function connect( host : String, port : Int ) {
-		cnx.sock.connect.call([host,port]);
-	}
-
-	public function send( data : String ) {
-		cnx.sock.send.call([data]);
-	}
-
-	public function close() {
-		cnx.sock.close.call([]);
-		cnx.api.destroy.call([]);
-		cnx.close();
-	}
-
-	public dynamic function onData( data : String ) {
-	}
-
-	public dynamic function onClose() {
-	}
-
-	public dynamic function onConnect( b : Bool ) {
-	}
-
-}

+ 0 - 89
std/neko/net/Poll.hx

@@ -1,89 +0,0 @@
-/*
- * Copyright (C)2005-2018 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 neko.net;
-import sys.net.Socket;
-
-class Poll {
-
-	var d : Dynamic;
-	public var readIndexes : ArrayAccess<Int>;
-	public var writeIndexes : ArrayAccess<Int>;
-
-	public function new( n : Int ) {
-		d = socket_poll_alloc(n);
-		readIndexes = writeIndexes = untyped __dollar__array(-1);
-	}
-
-	public function prepare( read : Array<Socket>, write : Array<Socket> ) {
-		untyped {
-			var r = __dollar__amake(read.length);
-			var w = __dollar__amake(write.length);
-			var i = 0;
-			var len = read.length;
-			while( i < len ) {
-				r[i] = read[i].__s;
-				i += 1;
-			}
-			i = 0;
-			len = write.length;
-			while( i < len ) {
-				w[i] = write[i].__s;
-				i += 1;
-			}
-			var k = socket_poll_prepare(d,r,w);
-			readIndexes = k[0];
-			writeIndexes = k[1];
-		}
-	}
-
-	public function events( ?t : Float ) {
-		socket_poll_events(d,t);
-	}
-
-	public function poll( a : Array<Socket>, ?t : Float ) : Array<Socket> {
-		untyped {
-			var c = __dollar__hnew(16);
-			var r = neko.NativeArray.alloc(a.length);
-			var i = 0;
-			var len = a.length;
-			while( i < len ){
-				r[i] = a[i].__s;
-				__dollar__hadd(c,a[i].__s,a[i]);
-				i += 1;
-			}
-			r = socket_poll(r,d,t);
-			i = 0;
-			len = __dollar__asize(r);
-			while( i < len ) {
-				r[i] = __dollar__hget(c,r[i],null);
-				i += 1;
-			}
-			return Array.new1(r,len);
-		}
-	}
-
-	static var socket_poll_alloc = neko.Lib.load("std","socket_poll_alloc",1);
-	static var socket_poll = neko.Lib.load("std","socket_poll",3);
-	static var socket_poll_prepare = neko.Lib.loadLazy("std","socket_poll_prepare",3);
-	static var socket_poll_events = neko.Lib.loadLazy("std","socket_poll_events",2);
-
-}

+ 0 - 202
std/neko/net/ProxyDetect.hx

@@ -1,202 +0,0 @@
-/*
- * Copyright (C)2005-2018 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 neko.net;
-
-typedef ProxySettings = {
-	var host : String;
-	var port : Int;
-	var auth : {
-		var user : String;
-		var pass : String;
-	};
-}
-
-class ProxyDetect {
-
-	static function parseSettings( settings : String ) {
-		var r = ~/^([^:]+):([^@]*)@([^:]+):([0-9]+)$/;
-		if( r.match(settings) )
-			return {
-				auth : {
-					user : r.matched(1),
-					pass : r.matched(2),
-				},
-				host : r.matched(3),
-				port : Std.parseInt(r.matched(4)),
-			};
-		var r = ~/^([^:]+):([0-9]+)$/;
-		if( !r.match(settings) ) {
-			var r2 = ~/http=([^:]+):([0-9]+)/;
-			if( !r2.match(settings) )
-				throw "Invalid settings '"+settings+"'";
-			r = r2;
-		}
-		return {
-			host : r.matched(1),
-			port : Std.parseInt(r.matched(2)),
-			auth : null,
-		};
-	}
-
-	static function detectFF( basedir : String ) {
-		var files = try sys.FileSystem.readDirectory(basedir) catch( e : Dynamic ) return null;
-		var profile = null;
-		for( f in files )
-			if( f.substr(-8) == ".default" ) {
-				profile = f;
-				break;
-			}
-		if( profile == null )
-			return null;
-		var prefs = sys.io.File.getContent(basedir+"/"+profile+"/prefs.js");
-		// enabled ?
-		var r = ~/user_pref\("network\.proxy\.type", 1\);/;
-		if( !r.match(prefs) )
-			return null;
-		// prefs
-		var r = ~/user_pref\("network\.proxy\.http", "([^"]+)"\);/;
-		if( !r.match(prefs) )
-			return null;
-		var host = r.matched(1);
-		var r = ~/user_pref\("network\.proxy\.http_port", ([0-9]+)\);/;
-		if( !r.match(prefs) )
-			return null;
-		var port = r.matched(1);
-		return parseSettings(host+":"+port);
-	}
-
-	static function detectIE() {
-		var dir = Sys.getEnv("TMP");
-		if( dir == null )
-			dir = ".";
-		var temp = dir + "/proxy.txt";
-		if( Sys.command('regedit /E "'+temp+'" "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"') != 0 ) {
-			// might fail without appropriate rights
-			return null;
-		}
-		// it's possible that if registry access was disabled the proxy file is not created
-		var content = try sys.io.File.getContent(temp) catch( e : Dynamic ) return null;
-		sys.FileSystem.deleteFile(temp);
-		// turn 16-bit string into 8-bit one
-		var b = new StringBuf();
-		var p = 0;
-		while( p < content.length ) {
-			b.addChar(content.charCodeAt(p));
-			p += 2;
-		}
-		content = b.toString();
-		// enabled ?
-		var renabled = ~/"ProxyEnable"=dword:0000000(0|1)/;
-		if( !renabled.match(content) )
-			return null;
-		if( renabled.matched(1) == "0" )
-			return null;
-		// value ?
-		var rproxy = ~/"ProxyServer"="([^"]+)"/;
-		if( !rproxy.match(content) )
-			return null;
-		return parseSettings(rproxy.matched(1));
-	}
-
-	static function parseOSXConfiguration(xml : Xml) : Dynamic {
-		switch( xml.nodeName ) {
-		case "dict":
-			var o = {};
-			var it = xml.elements();
-			for( x in it ) {
-				if( x.nodeName != "key" ) throw "Missing key";
-				var v = x.firstChild().nodeValue;
-				var r = parseOSXConfiguration(it.next());
-				Reflect.setField(o,v,r);
-			}
-			return o;
-		case "string":
-			return xml.firstChild().nodeValue;
-		case "integer":
-			return Std.parseInt(xml.firstChild().nodeValue);
-		case "array":
-			var a = new Array();
-			for( x in xml.elements() )
-				a.push(parseOSXConfiguration(x));
-			return a;
-		case "true":
-			return true;
-		case "false":
-			return false;
-		case "data":
-			return xml.firstChild().nodeValue;
-		default:
-			throw "Invalid value type '"+xml.nodeName+"'";
-		}
-	}
-
-	static function detectOSX() {
-		var prefs = sys.io.File.getContent("/Library/Preferences/SystemConfiguration/preferences.plist");
-		var xml = Xml.parse(prefs).firstElement().firstElement(); // plist/dict
-		var data : Dynamic = parseOSXConfiguration(xml);
-		for( nsname in Reflect.fields(data.NetworkServices) ) {
-			var ns : Dynamic = Reflect.field(data.NetworkServices,nsname);
-			if( ns.Proxies != null && ns.Proxies.HTTPEnable == 1 )
-				return { host : ns.Proxies.HTTPProxy, port : ns.Proxies.HTTPPort, auth : null };
-		}
-		return null;
-	}
-
-	static function detectAll() : ProxySettings {
-		switch( Sys.systemName() ) {
-		case "Windows":
-			try {
-				var ffdir = Sys.getEnv("APPDATA")+"/Mozilla/Firefox/Profiles";
-				var p = detectFF(ffdir);
-				if( p == null )
-					throw "No Firefox proxy";
-				return p;
-			} catch( e : Dynamic ) {
-				return detectIE();
-			}
-		case "Mac":
-			var p = detectOSX();
-			if( p != null )
-				return p;
-			var ffdir = Sys.getEnv("HOME")+"/Library/Application Support/Firefox/Profiles";
-			return detectFF(ffdir);
-		case "Linux":
-			var ffdir = Sys.getEnv("HOME")+"/.mozilla/firefox";
-			return detectFF(ffdir);
-		default:
-			throw "This system is not supported";
-		}
-	}
-
-	static var save : { r : ProxySettings } = null;
-
-	public static function detect() {
-		if( save == null )
-			save = { r : try detectAll() catch( e : Dynamic ) null };
-		return save.r;
-	}
-
-	static function main() {
-		trace(detect());
-	}
-
-}

+ 0 - 231
std/neko/net/ServerLoop.hx

@@ -1,231 +0,0 @@
-/*
- * Copyright (C)2005-2018 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 neko.net;
-import sys.net.Socket;
-
-private typedef ServerClient<ClientData> = {
-	var sock : Socket;
-	var buffer : haxe.io.Bytes;
-	var bufbytes : Int;
-	var data : ClientData;
-}
-
-/**
-	This class enables you to quickly create a custom server that can
-	serve several clients in parallel. This server is using a single
-	thread and process so the server itself processing is not parallel.
-	Non-blocking sockets are used to ensure that a slow client does not
-	block the others.
-**/
-class ServerLoop<ClientData> {
-
-	/**
-		Each client has an associated buffer. This is the initial buffer size which
-		is set to 128 bytes by default.
-	**/
-	public static var DEFAULT_BUFSIZE = 128;
-
-	/**
-		Each client has an associated buffer. This is the maximum buffer size which
-		is set to 64K by default. When that size is reached and some data can't be processed,
-		the client is disconnected.
-	**/
-	public static var MAX_BUFSIZE = (1 << 16);
-
-	/**
-		This is the value of number client requests that the server socket
-		listen for. By default this number is 10 but can be increased for
-		servers supporting a large number of simultaneous requests.
-	**/
-	public var listenCount : Int;
-
-	/**
-		See `update`.
-	**/
-	public var updateTime : Float;
-
-	var newData : Socket -> ClientData;
-	var socks : Array<sys.net.Socket>;
-	public var clients : List<ClientData>;
-
-	/**
-		Creates a server instance. The `newData` methods must return
-		the data associated with the Client.
-	**/
-	public function new( ?newData ) {
-		this.newData = if( newData == null ) function(_) { return null; } else newData;
-		clients = new List();
-		socks = new Array();
-		listenCount = 10;
-		updateTime = 1;
-	}
-
-	/**
-		Closes the client connection and removes it from the client List.
-	**/
-	public function closeConnection( s : Socket ) : Bool {
-		var cl : ServerClient<ClientData> = untyped s.__client;
-		if( cl == null || !clients.remove(cl.data) )
-			return false;
-		socks.remove(s);
-		try s.close() catch( e : Dynamic ) { };
-		clientDisconnected(cl.data);
-		return true;
-	}
-
-	/**
-		The `update` method is called after each socket event has been
-		processed or when `updateTime` has been reached. It can be used
-		to perform time-regular tasks such as pings. By default `updateTime`
-		is set to one second.
-	**/
-	public function update() {
-	}
-
-	/**
-		This method is called after a client has been disconnected.
-	**/
-	public function clientDisconnected( d : ClientData ) {
-	}
-
-	/**
-		This method can be used instead of writing directly to the socket.
-		It ensures that all the data is correctly sent. If an error occurs
-		while sending the data, no exception will occur but the client will
-		be gracefully disconnected.
-	**/
-	public function clientWrite( s : Socket, buf : haxe.io.Bytes, pos : Int, len : Int ) {
-		try {
-			while( len > 0 ) {
-				var nbytes = s.output.writeBytes(buf,pos,len);
-				pos += nbytes;
-				len -= nbytes;
-			}
-		} catch( e : Dynamic ) {
-			closeConnection(s);
-		}
-	}
-
-	/**
-		This method is called when some data has been read into a Client buffer.
-		If the data can be handled, then you can return the number of bytes handled
-		that needs to be removed from the buffer. It the data can't be handled (some
-		part of the message is missing for example), returns 0.
-	**/
-	public function processClientData( d : ClientData, buf : haxe.io.Bytes, bufpos : Int, buflen : Int ) {
-		throw "ServerLoop::processClientData is not implemented";
-		return 0;
-	}
-
-	/**
-		Called when an error occurred. This enable you to log the error somewhere.
-		By default the error is displayed using `trace`.
-	**/
-	public function onError( e : Dynamic ) {
-		trace(Std.string(e)+"\n"+haxe.CallStack.toString(haxe.CallStack.exceptionStack()));
-	}
-
-	function readData( cl : ServerClient<ClientData> ) {
-		var buflen = cl.buffer.length;
-		// eventually double the buffer size
-		if( cl.bufbytes == buflen ) {
-			var nsize = buflen * 2;
-			if( nsize > MAX_BUFSIZE ) {
-				if( buflen == MAX_BUFSIZE )
-					throw "Max buffer size reached";
-				nsize = MAX_BUFSIZE;
-			}
-			var buf2 = haxe.io.Bytes.alloc(nsize);
-			buf2.blit(0,cl.buffer,0,buflen);
-			buflen = nsize;
-			cl.buffer = buf2;
-		}
-		// read the available data
-		var nbytes = cl.sock.input.readBytes(cl.buffer,cl.bufbytes,buflen - cl.bufbytes);
-		cl.bufbytes += nbytes;
-	}
-
-	function processData( cl : ServerClient<ClientData> ) {
-		var pos = 0;
-		while( cl.bufbytes > 0 ) {
-			var nbytes = processClientData(cl.data,cl.buffer,pos,cl.bufbytes);
-			if( nbytes == 0 )
-				break;
-			pos += nbytes;
-			cl.bufbytes -= nbytes;
-		}
-		if( pos > 0 )
-			cl.buffer.blit(0,cl.buffer,pos,cl.bufbytes);
-	}
-
-	/**
-		Run the server. This function should never return.
-	**/
-	public function run( host : sys.net.Host, port : Int ) {
-		var serv = new Socket();
-		serv.bind(host,port);
-		serv.listen(listenCount);
-		socks = [serv];
-		while( true ) {
-			for( s in Socket.select(socks,null,null,updateTime).read ) {
-				var cl : ServerClient<ClientData> = untyped s.__client;
-				if( cl == null ) {
-					// no associated client : it's our server socket
-					var sock = serv.accept();
-					sock.setBlocking(true);
-					cl = {
-						sock : sock,
-						data : null,
-						buffer : haxe.io.Bytes.alloc(DEFAULT_BUFSIZE),
-						bufbytes : 0,
-					};
-					// bind the client
-					untyped sock.__client = cl;
-					// creates the data
-					try {
-						cl.data = newData(sock);
-					} catch( e : Dynamic ) {
-						onError(e);
-						try sock.close() catch( e : Dynamic ) { };
-						continue;
-					}
-					// adds the client to the lists
-					socks.push(sock);
-					clients.add(cl.data);
-				} else {
-					// read & process the data
-					try {
-						readData(cl);
-						processData(cl);
-					} catch( e : Dynamic ) {
-						if( !Std.is(e,haxe.io.Eof) )
-							onError(e);
-						closeConnection(cl.sock);
-					}
-				}
-			}
-			update();
-		}
-		serv.close();
-	}
-
-}

+ 0 - 114
std/neko/net/ThreadRemotingServer.hx

@@ -1,114 +0,0 @@
-/*
- * Copyright (C)2005-2018 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 neko.net;
-
-class ThreadRemotingServer extends ThreadServer<haxe.remoting.SocketConnection,String> {
-
-	var domains : Array<String>;
-	var port : Int;
-
-	public function new( ?domains ) {
-		super();
-		messageHeaderSize = 2;
-		this.domains = domains;
-	}
-
-	public dynamic function initClientApi( cnx : haxe.remoting.SocketConnection, ctx : haxe.remoting.Context ) {
-		throw "Not implemented";
-	}
-
-	public dynamic function onXml( cnx : haxe.remoting.SocketConnection, data : String ) {
-		throw "Unhandled XML data '"+data+"'";
-	}
-
-	public dynamic function makePolicyFile() {
-		var str = "<cross-domain-policy>";
-		for( d in domains )
-			str += '<allow-access-from domain="'+d+'" to-ports="'+port+'"/>';
-		str += "</cross-domain-policy>";
-		return str;
-	}
-
-	public override function run( host, port ) {
-		this.port = port;
-		super.run(host,port);
-	}
-
-	public override function clientConnected( s : sys.net.Socket ) {
-		var ctx = new haxe.remoting.Context();
-		var cnx = haxe.remoting.SocketConnection.create(s,ctx);
-		var me = this;
-		cnx.setErrorHandler(function(e) {
-			if( !Std.is(e,haxe.io.Eof) && !Std.is(e,haxe.io.Error) )
-				me.logError(e);
-			me.stopClient(s);
-		});
-		initClientApi(cnx,ctx);
-		return cnx;
-	}
-
-	override function readClientMessage( cnx : haxe.remoting.SocketConnection, buf : haxe.io.Bytes, pos : Int, len : Int ) {
-		var msgLen = cnx.getProtocol().messageLength(buf.get(pos),buf.get(pos+1));
-		if( msgLen == null ) {
-			if( buf.get(pos) != 60 )
-				throw "Invalid remoting message '"+buf.getString(pos,len)+"'";
-			var p = pos;
-			while( p < len ) {
-				if( buf.get(p) == 0 )
-					break;
-				p++;
-			}
-			if( p == len )
-				return null;
-			p -= pos;
-			return {
-				msg : buf.getString(pos,p),
-				bytes : p + 1,
-			};
-		}
-		if( len < msgLen )
-			return null;
-		if( buf.get(pos + msgLen-1) != 0 )
-			throw "Truncated message";
-		return {
-			msg : buf.getString(pos+2,msgLen-3),
-			bytes : msgLen,
-		};
-	}
-
-	public override function clientMessage( cnx : haxe.remoting.SocketConnection, msg : String ) {
-		try {
-			if( msg.charCodeAt(0) == 60 ) {
-				if( domains != null && msg == "<policy-file-request/>" )
-					cnx.getProtocol().socket.write(makePolicyFile()+"\x00");
-				else
-					onXml(cnx,msg);
-			} else
-				cnx.processMessage(msg);
-		} catch( e : Dynamic ) {
-			if( !Std.is(e,haxe.io.Eof) && !Std.is(e,haxe.io.Error) )
-				logError(e);
-			stopClient(cnx.getProtocol().socket);
-		}
-	}
-
-}

+ 0 - 360
std/neko/net/ThreadServer.hx

@@ -1,360 +0,0 @@
-/*
- * Copyright (C)2005-2018 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 neko.net;
-
-private typedef ThreadInfos = {
-	var id : Int;
-	var t : neko.vm.Thread;
-	var p : neko.net.Poll;
-	var socks : Array<sys.net.Socket>;
-}
-
-private typedef ClientInfos<Client> = {
-	var client : Client;
-	var sock : sys.net.Socket;
-	var thread : ThreadInfos;
-	var buf : haxe.io.Bytes;
-	var bufpos : Int;
-}
-
-/**
-	The ThreadServer can be used to easily create a multithreaded server where each thread polls multiple connections.
-	To use it, at a minimum you must override or rebind clientConnected, readClientMessage, and clientMessage and you must define your Client and Message.
-**/
-class ThreadServer<Client,Message> {
-
-	var threads : Array<ThreadInfos>;
-	var sock : sys.net.Socket;
-	var worker : neko.vm.Thread;
-	var timer : neko.vm.Thread;
-
-	/**
-		Number of total connections the server will accept.
-	**/
-	public var listen : Int;
-
-	/**
-		Number of server threads.
-	**/
-	public var nthreads : Int;
-
-	/**
-		Polling timeout.
-	**/
-	public var connectLag : Float;
-
-	/**
-		Stream to send error messages.
-	**/
-	public var errorOutput : haxe.io.Output;
-
-	/**
-		Space allocated to buffers when they are created.
-	**/
-	public var initialBufferSize : Int;
-
-	/**
-		Maximum size of buffered data read from a socket. An exception is thrown if the buffer exceeds this value.
-	**/
-	public var maxBufferSize : Int;
-
-	/**
-		Minimum message size.
-	**/
-	public var messageHeaderSize : Int;
-
-	/**
-		Time between calls to update.
-	**/
-	public var updateTime : Float;
-
-	/**
-		The most sockets a thread will handle.
-	**/
-	public var maxSockPerThread : Int;
-
-	/**
-		Creates a ThreadServer.
-	**/
-	public function new() {
-		threads = new Array();
-		nthreads = if( Sys.systemName() == "Windows" ) 150 else 10;
-		messageHeaderSize = 1;
-		listen = 10;
-		connectLag = 0.5;
-		errorOutput = Sys.stderr();
-		initialBufferSize = (1 << 10);
-		maxBufferSize = (1 << 16);
-		maxSockPerThread = 64;
-		updateTime = 1;
-	}
-
-	function runThread(t) {
-		while( true ) {
-			try {
-				loopThread(t);
-			} catch( e : Dynamic ) {
-				logError(e);
-			}
-		}
-	}
-
-	function readClientData( c : ClientInfos<Client> ) {
-		var available = c.buf.length - c.bufpos;
-		if( available == 0 ) {
-			var newsize = c.buf.length * 2;
-			if( newsize > maxBufferSize ) {
-				newsize = maxBufferSize;
-				if( c.buf.length == maxBufferSize )
-					throw "Max buffer size reached";
-			}
-			var newbuf = haxe.io.Bytes.alloc(newsize);
-			newbuf.blit(0,c.buf,0,c.bufpos);
-			c.buf = newbuf;
-			available = newsize - c.bufpos;
-		}
-		var bytes = c.sock.input.readBytes(c.buf,c.bufpos,available);
-		var pos = 0;
-		var len = c.bufpos + bytes;
-		while( len >= messageHeaderSize ) {
-			var m = readClientMessage(c.client,c.buf,pos,len);
-			if( m == null )
-				break;
-			pos += m.bytes;
-			len -= m.bytes;
-			work(clientMessage.bind(c.client,m.msg));
-		}
-		if( pos > 0 )
-			c.buf.blit(0,c.buf,pos,len);
-		c.bufpos = len;
-	}
-
-	function loopThread( t : ThreadInfos ) {
-		if( t.socks.length > 0 )
-			for( s in t.p.poll(t.socks,connectLag) ) {
-				var infos : ClientInfos<Client> = s.custom;
-				try {
-					readClientData(infos);
-				} catch( e : Dynamic ) {
-					t.socks.remove(s);
-					if( !Std.is(e,haxe.io.Eof) && !Std.is(e,haxe.io.Error) )
-						logError(e);
-					work(doClientDisconnected.bind(s,infos.client));
-				}
-			}
-		while( true ) {
-			var m : { s : sys.net.Socket, cnx : Bool } = neko.vm.Thread.readMessage(t.socks.length == 0);
-			if( m == null )
-				break;
-			if( m.cnx )
-				t.socks.push(m.s);
-			else if( t.socks.remove(m.s) ) {
-				var infos : ClientInfos<Client> = m.s.custom;
-				work(doClientDisconnected.bind(m.s,infos.client));
-			}
-		}
-	}
-
-	function doClientDisconnected(s:sys.net.Socket,c) {
-		try s.close() catch( e : Dynamic ) {};
-		clientDisconnected(c);
-	}
-
-	function runWorker() {
-		while( true ) {
-			var f = neko.vm.Thread.readMessage(true);
-			try {
-				f();
-			} catch( e : Dynamic ) {
-				logError(e);
-			}
-			try {
-				afterEvent();
-			} catch( e : Dynamic ) {
-				logError(e);
-			}
-		}
-	}
-
-	/**
-		Internally used to delegate something to the worker thread.
-	**/
-	public function work( f : Void -> Void ) {
-		worker.sendMessage(f);
-	}
-
-	function logError( e : Dynamic ) {
-		var stack = haxe.CallStack.exceptionStack();
-		if( neko.vm.Thread.current() == worker )
-			onError(e,stack);
-		else
-			work(onError.bind(e,stack));
-	}
-
-	function addClient( sock : sys.net.Socket ) {
-		var start = Std.random(nthreads);
-		for( i in 0...nthreads ) {
-			var t = threads[(start + i)%nthreads];
-			if( t.socks.length < maxSockPerThread ) {
-				var infos : ClientInfos<Client> = {
-					thread : t,
-					client : clientConnected(sock),
-					sock : sock,
-					buf : haxe.io.Bytes.alloc(initialBufferSize),
-					bufpos : 0,
-				};
-				sock.custom = infos;
-				infos.thread.t.sendMessage({ s : sock, cnx : true });
-				return;
-			}
-		}
-		refuseClient(sock);
-	}
-
-	function refuseClient( sock : sys.net.Socket) {
-		// we have reached maximum number of active clients
-		sock.close();
-	}
-
-	function runTimer() {
-		var l = new neko.vm.Lock();
-		while( true ) {
-			l.wait(updateTime);
-			work(update);
-		}
-	}
-
-	function init() {
-		worker = neko.vm.Thread.create(runWorker);
-		timer = neko.vm.Thread.create(runTimer);
-		for( i in 0...nthreads ) {
-			var t = {
-				id : i,
-				t : null,
-				socks : new Array(),
-				p : new neko.net.Poll(maxSockPerThread),
-			};
-			threads.push(t);
-			t.t = neko.vm.Thread.create(runThread.bind(t));
-		}
-	}
-
-	/**
-		Called when the server gets a new connection.
-	**/
-	public function addSocket( s : sys.net.Socket ) {
-		s.setBlocking(false);
-		work(addClient.bind(s));
-	}
-
-	/**
-		Start the server at the specified host and port.
-	**/
-	public function run( host, port ) {
-		sock = new sys.net.Socket();
-		sock.bind(new sys.net.Host(host),port);
-		sock.listen(listen);
-		init();
-		while( true ) {
-			try {
-				addSocket(sock.accept());
-			} catch( e : Dynamic ) {
-				logError(e);
-			}
-		}
-	}
-
-	/**
-		Sends data to a client.
-	**/
-	public function sendData( s : sys.net.Socket, data : String ) {
-		try {
-			s.write(data);
-		} catch( e : Dynamic ) {
-			stopClient(s);
-		}
-	}
-
-	/**
-		Shutdown a client's connection and remove them from the server.
-	**/
-	public function stopClient( s : sys.net.Socket ) {
-		var infos : ClientInfos<Client> = s.custom;
-		try s.shutdown(true,true) catch( e : Dynamic ) { };
-		infos.thread.t.sendMessage({ s : s, cnx : false });
-	}
-
-	// --- CUSTOMIZABLE API ---
-
-	/**
-		Called when an error has occurred.
-	**/
-	public dynamic function onError( e : Dynamic, stack ) {
-		var estr = try Std.string(e) catch( e2 : Dynamic ) "???" + try "["+Std.string(e2)+"]" catch( e : Dynamic ) "";
-		errorOutput.writeString( estr + "\n" + haxe.CallStack.toString(stack) );
-		errorOutput.flush();
-	}
-
-	/**
-		Called when a client connects. Returns a client object.
-	**/
-	public dynamic function clientConnected( s : sys.net.Socket ) : Client {
-		return null;
-	}
-
-	/**
-		Called when a client disconnects or an error forces the connection to close.
-	**/
-	public dynamic function clientDisconnected( c : Client ) {
-	}
-
-	/**
-		Called when data has been read from a socket. This method should try to extract a message from the buffer.
-		The available data resides in buf, starts at pos, and is len bytes wide. Return the new message and the number of bytes read from the buffer.
-		If no message could be read, return null.
-	**/
-	public dynamic function readClientMessage( c : Client, buf : haxe.io.Bytes, pos : Int, len : Int ) : { msg : Message, bytes : Int } {
-		return {
-			msg : null,
-			bytes : len,
-		};
-	}
-
-	/**
-		Called when a message has been received. Message handling code should go here.
-	**/
-	public dynamic function clientMessage( c : Client, msg : Message ) {
-	}
-
-	/**
-		This method is called periodically. It can be used to do server maintenance.
-	**/
-	public dynamic function update() {
-	}
-
-	/**
-		Called after a client connects, disconnects, a message is received, or an update is performed.
-	**/
-	public dynamic function afterEvent() {
-	}
-
-}