Nicolas Cannasse 19 năm trước cách đây
mục cha
commit
fe8a28d811
3 tập tin đã thay đổi với 190 bổ sung0 xóa
  1. 3 0
      std/haxe/ImportAll.hx
  2. 92 0
      std/haxe/remoting/AsyncConnection.hx
  3. 95 0
      std/neko/RemoteServer.hx

+ 3 - 0
std/haxe/ImportAll.hx

@@ -43,8 +43,10 @@ import StdTypes;
 import String;
 import StringBuf;
 import StringTools;
+import Xml;
 import XmlParser;
 
+import haxe.AsyncConnection;
 import haxe.Connection;
 import haxe.ImportAll;
 import haxe.Log;
@@ -125,6 +127,7 @@ import neko.Boot;
 import neko.File;
 import neko.FileSystem;
 import neko.Lib;
+import neko.RemoteServer;
 import neko.Socket;
 import neko.Stack;
 import neko.Sys;

+ 92 - 0
std/haxe/remoting/AsyncConnection.hx

@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package haxe;
+
+class AsyncConnection implements Dynamic<AsyncConnection> {
+
+	var __data : Dynamic;
+	var __path : Array<String>;
+
+	function new( data, path ) {
+		__data = data;
+		__path = path;
+	}
+
+	function __resolve(field) {
+		var s = new AsyncConnection(__data,__path.copy());
+		s.onError = onError;
+		s.__path.push(field);
+		return s;
+	}
+
+	public function onError( err : Dynamic ) {
+	}
+
+	public function eval( onData : Dynamic -> Void ) : Void {
+		var h = new Http(__data);
+		var me = this;
+		h.setHeader("X-Haxe-Remoting","eval");
+		h.setParameter("__x",__path.join("."));
+		h.onData = function(data : String) {
+			try {
+				if( data.length < 3 || data.substr(0,3) != "hxr" )
+					throw "Invalid response : '"+data+"'";
+				var s = new Unserializer(data.substr(3,data.length-3));
+				onData(s.unserialize());
+			} catch( err : Dynamic ) {
+				me.onError(err);
+			}
+		};
+		h.onError = onError;
+		h.request(true);
+	}
+
+	public function call( params : Array<Dynamic>, onData : Dynamic -> Void ) : Void {
+		var h = new Http(__data);
+		var me = this;
+		var s = new Serializer();
+		s.serialize(__path);
+		s.serialize(params);
+		h.setHeader("X-Haxe-Remoting","call");
+		h.setParameter("__x",s.toString());
+		h.onData = function(data : String) {
+			try {
+				if( data.length < 3 || data.substr(0,3) != "hxr" )
+					throw "Invalid response : '"+data+"'";
+				var s = new Unserializer(data.substr(3,data.length-3));
+				onData(s.unserialize());
+			} catch( err : Dynamic ) {
+				me.onError(err);
+			}
+		};
+		h.onError = onError;
+		h.request(true);
+	}
+
+	public static function urlConnect( url : String ) {
+		return new AsyncConnection(url,[]);
+	}
+
+}

+ 95 - 0
std/neko/RemoteServer.hx

@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package neko;
+
+class RemoteServer {
+
+	var objects : Hash<Dynamic>;
+
+	public function new() {
+		objects = new Hash();
+	}
+
+	public function addObject( name : String, obj : Dynamic ) {
+		objects.set(name,obj);
+	}
+
+	function resolvePath( path : Array<String> ) {
+		var objname = path.shift();
+		if( objname == null )
+			throw "Empty path";
+		var obj = objects.get(objname);
+		if( obj == null )
+			throw "Object '"+objname+"' is not accessible";
+		for( x in path ) {
+			if( obj == null )
+				throw null;
+			obj = Reflect.field(obj,x);
+		}
+		return obj;
+	}
+
+	public function handleRequest() {
+		var kind = Web.getClientHeader("X-Haxe-Remoting");
+		if( kind == null )
+			return false;
+		var v = Web.getParams().get("__x");
+		try {
+			if( v == null )
+				throw "Missing remoting data";
+			if( kind == "eval" ) {
+				var obj = resolvePath(v.split("."));
+				var s = new haxe.Serializer();
+				s.serialize(obj);
+				neko.Lib.print("hxr");
+				neko.Lib.print(s.toString());
+			}
+			if( kind == "call" ) {
+				var u = new haxe.Unserializer(v);
+				var path = u.unserialize();
+				var args = u.unserialize();
+				var f = path.pop();
+				var obj = resolvePath(path);
+				var funptr = Reflect.field(obj,f);
+				if( !Reflect.isFunction(funptr) )
+					throw "Calling not-a-function '"+f+"'";
+                var v = Reflect.callMethod(obj,funptr,args);
+				var s = new haxe.Serializer();
+				s.serialize(v);
+				neko.Lib.print("hxr");
+				neko.Lib.print(s.toString());
+			}
+			throw "Unsupported remoting kind '"+kind+"'";
+		} catch( e : Dynamic ) {
+			var s = new haxe.Serializer();
+			s.serializeException(e);
+			neko.Lib.print("hxr");
+			neko.Lib.print(s.toString());
+		}
+		return true;
+	}
+
+}
+