Przeglądaj źródła

started cdb inspector

Nicolas Cannasse 10 lat temu
rodzic
commit
ba974d75e6
5 zmienionych plików z 162 dodań i 1 usunięć
  1. 2 1
      .gitignore
  2. 40 0
      hxd/net/CdbInspector.hx
  3. 115 0
      hxd/net/Socket.hx
  4. 3 0
      hxd/net/inspect.hss
  5. 2 0
      hxd/net/inspect.min.css

+ 2 - 1
.gitignore

@@ -3,4 +3,5 @@
 *.js
 *.js.map
 bin
-.tmp
+.tmp
+/hxd/net/inspect.css

+ 40 - 0
hxd/net/CdbInspector.hx

@@ -0,0 +1,40 @@
+package hxd.net;
+
+class CdbInspector extends cdb.jq.Client {
+
+	static var CSS = hxd.res.Embed.getFileContent("hxd/net/inspect.min.css");
+	public var host : String = "127.0.0.1";
+	public var port = 6669;
+
+	var sock : hxd.net.Socket;
+
+	public function new( ?host, ?port ) {
+		super();
+		if( host != null )
+			this.host = host;
+		if( port != null )
+			this.port = port;
+		sock = new hxd.net.Socket();
+		sock.onError = function(_) haxe.Timer.delay(connect,500);
+		connect();
+	}
+
+	function connect() {
+		sock.close();
+		sock.connect(host, port, refresh);
+	}
+
+	override function sendBytes( msg : haxe.io.Bytes ) {
+		sock.out.wait();
+		sock.out.writeInt32(msg.length);
+		sock.out.write(msg);
+		sock.out.flush();
+	}
+
+	function refresh() {
+		j.text("");
+		send(SetCSS(CSS));
+		J("<div>").addClass("test").text("hello").appendTo(j);
+	}
+
+}

+ 115 - 0
hxd/net/Socket.hx

@@ -0,0 +1,115 @@
+package hxd.net;
+
+private class SocketOutput extends haxe.io.Output {
+
+	public function new() {
+	}
+
+	/**
+		Delay sending data until flush() is called
+	**/
+	public function wait() {
+	}
+
+	override function writeByte( c : Int ) {
+	}
+
+	override function writeBytes( s : haxe.io.Bytes, pos : Int, len : Int ) : Int {
+		return len;
+	}
+
+}
+
+#if flash
+private class FlashSocketOutput extends SocketOutput {
+	var s : flash.net.Socket;
+	var autoFlush = true;
+
+	public function new(s) {
+		super();
+		this.s = s;
+		s.endian = flash.utils.Endian.LITTLE_ENDIAN;
+	}
+
+	inline function f() if( autoFlush ) s.flush();
+
+	override function wait() {
+		autoFlush = false;
+	}
+
+	override function flush() {
+		autoFlush = true;
+		s.flush();
+	}
+
+	override function writeByte( c : Int ) {
+		s.writeByte(c);
+		f();
+	}
+
+	override function writeBytes( b : haxe.io.Bytes, pos : Int, len : Int ) : Int {
+		if( len > 0 ) {
+			s.writeBytes(b.getData(), pos, len);
+			f();
+		}
+		return len;
+	}
+
+	override function writeInt32( i : Int ) {
+		s.writeInt(i);
+		f();
+	}
+
+	override function writeString( str : String ) {
+		s.writeUTFBytes(str);
+		f();
+	}
+
+}
+#end
+
+class Socket {
+
+	#if flash
+	var s : flash.net.Socket;
+	#end
+	public var out(default, null) : SocketOutput;
+
+	public function new() {
+		out = new SocketOutput();
+	}
+
+	public function connect( host : String, port : Int, onConnect : Void -> Void ) {
+		close();
+		#if flash
+		s = new flash.net.Socket();
+		s.addEventListener(flash.events.Event.CONNECT, function(_) {
+			out = new FlashSocketOutput(s);
+			onConnect();
+		});
+		s.addEventListener(flash.events.IOErrorEvent.IO_ERROR, function(e:flash.events.IOErrorEvent) {
+			out = new SocketOutput();
+			onError(e.text);
+		});
+		s.connect(host, port);
+		#else
+		throw "Not implemented";
+		#end
+	}
+
+	public function close() {
+		#if flash
+		if( s != null ) {
+			try s.close() catch( e : Dynamic ) {};
+			s = null;
+		}
+		#else
+		throw "Not implemented";
+		#end
+	}
+
+	public dynamic function onError(msg:String) {
+		throw "Socket Error " + msg;
+	}
+
+}

+ 3 - 0
hxd/net/inspect.hss

@@ -0,0 +1,3 @@
+.test {
+	color : red;
+}

+ 2 - 0
hxd/net/inspect.min.css

@@ -0,0 +1,2 @@
+.test{color:red;}
+