Browse Source

added hl libuv support

Nicolas Cannasse 8 years ago
parent
commit
6cdfa80554
6 changed files with 145 additions and 0 deletions
  1. 5 0
      std/hl/Bytes.hx
  2. 19 0
      std/hl/uv/Handle.hx
  3. 3 0
      std/hl/uv/HandleData.hx
  4. 31 0
      std/hl/uv/Loop.hx
  5. 45 0
      std/hl/uv/Stream.hx
  6. 42 0
      std/hl/uv/Tcp.hx

+ 5 - 0
std/hl/Bytes.hx

@@ -213,4 +213,9 @@ package hl;
 	public static inline function fromBytes( bytes : haxe.io.Bytes ) {
 		return @:privateAccess bytes.b;
 	}
+
+	public inline function toBytes( len : Int ) {
+		return @:privateAccess new haxe.io.Bytes(this, len);
+	}
+
 }

+ 19 - 0
std/hl/uv/Handle.hx

@@ -0,0 +1,19 @@
+package hl.uv;
+
+@:hlNative("uv")
+class Handle {
+	public var handle : HandleData;
+
+	function new(h) {
+		handle = h;
+	}
+
+	public function close( ?callb ) {
+		if( handle != null ) close_handle(handle, callb);
+		handle = null;
+	}
+
+	static function close_handle( h : HandleData, callb : Void->Void ) : Void {
+	}
+
+}

+ 3 - 0
std/hl/uv/HandleData.hx

@@ -0,0 +1,3 @@
+package hl.uv;
+
+typedef HandleData = hl.Abstract<"uv_handle">;

+ 31 - 0
std/hl/uv/Loop.hx

@@ -0,0 +1,31 @@
+package hl.uv;
+
+@:enum abstract LoopRunMode(Int) {
+	var Default = 0;
+	var Once = 1;
+	var NoWait = 2;
+}
+
+abstract Loop(hl.Abstract<"uv_loop">) {
+
+	@:hlNative("uv","loop_close") public function close() : Int {
+		return 0;
+	}
+
+	@:hlNative("uv","run") public function run( mode : LoopRunMode ) : Int {
+		return 0;
+	}
+
+	@:hlNative("uv","loop_alive") public function alive() : Int {
+		return 0;
+	}
+
+	@:hlNative("uv","stop") public function stop() : Void {
+	}
+
+	@:hlNative("uv","default_loop")
+	public static function getDefault() : Loop {
+		return null;
+	}
+
+}

+ 45 - 0
std/hl/uv/Stream.hx

@@ -0,0 +1,45 @@
+package hl.uv;
+
+@:hlNative("uv")
+class Stream extends Handle {
+
+	public function write( bytes : haxe.io.Bytes, ?onWrite : Bool -> Void, pos = 0, len = -1 ) {
+		if( len < 0 ) len = bytes.length - pos;
+		if( pos < 0 || len < 0 || pos+len > bytes.length ) throw haxe.io.Error.OutsideBounds;
+		if( handle == null || !stream_write(handle, (bytes : hl.Bytes).offset(pos), len, onWrite) ) throw new haxe.io.Eof();
+	}
+
+	public function readStartRaw( onData : hl.Bytes -> Int -> Void ) {
+		if( handle == null || !stream_read_start(handle, onData) ) throw new haxe.io.Eof();
+	}
+
+	public function readStart( onData : haxe.io.Bytes -> Void ) {
+		readStartRaw(function(b, len) onData(if( len < 0 ) null else b.toBytes(len)));
+	}
+
+	public function readStop() {
+		if( handle != null ) stream_read_stop(handle);
+	}
+
+	public function listen( n : Int, onConnect : Void -> Void ) {
+		if( handle == null || !stream_listen(handle, n, onConnect) ) throw new haxe.io.Eof();
+	}
+
+	// --
+
+	static function stream_write( handle : HandleData, bytes: hl.Bytes, len : Int, callb : Bool -> Void ) : Bool {
+		return false;
+	}
+
+	static function stream_read_start( handle : HandleData, callb : hl.Bytes -> Int -> Void ) {
+		return false;
+	}
+
+	static function stream_read_stop( handle : HandleData ) {
+	}
+
+	static function stream_listen( handle : HandleData, n : Int, callb : Void -> Void ) {
+		return false;
+	}
+
+}

+ 42 - 0
std/hl/uv/Tcp.hx

@@ -0,0 +1,42 @@
+package hl.uv;
+
+@:hlNative("uv")
+class Tcp extends Stream {
+
+	public function new( loop : Loop ) {
+		super(tcp_init_wrap(loop));
+	}
+
+	public function connect( host : sys.net.Host, port : Int, onConnected : Bool -> Void ) {
+		var h = tcp_connect_wrap(handle, host.ip, port, onConnected);
+		if( h == null ) throw haxe.io.Error.Custom("Failed to connect to "+host+":"+port);
+	}
+
+	public function bind( host : sys.net.Host, port : Int ) {
+		if( !tcp_bind_wrap(handle, host.ip, port) )
+			throw haxe.io.Error.Custom("Failed to bind socket to "+host+":"+port);
+	}
+
+	public function accept() {
+		var client = handle == null ? null : tcp_accept(handle);
+		if( client == null ) throw new haxe.io.Eof();
+		return new Stream(client);
+	}
+
+	static function tcp_init_wrap( loop : Loop ) : HandleData {
+		return null;
+	}
+
+	static function tcp_connect_wrap( h : HandleData, host : Int, port : Int, onConnected : Bool -> Void ) : HandleData {
+		return null;
+	}
+
+	static function tcp_bind_wrap( h : HandleData, host : Int, port : Int ) : Bool {
+		return false;
+	}
+
+	static function tcp_accept( h : HandleData ) : HandleData {
+		return null;
+	}
+
+}