Browse Source

UdpSocket: implement broadcast sending capability (#7677)

* ADD: Socket.setBroadcast method

This should enable sys.net.Socket instances to send data to broadcast addresses

* FIX: move setBroadcast only inside UdpSocket

Given that in a TCP socket context (which is what the Socket class implements) broadcast messaging is not supported, this should only be implemented in the UdpSocket class.

* FIX: revert to unmodified state

* MOD: further cleanups

* MOD: cleanup eval too
414n 6 năm trước cách đây
mục cha
commit
39475bc3b4

+ 8 - 0
src/macro/eval/evalStdLib.ml

@@ -1962,6 +1962,13 @@ module StdSocket = struct
 		vnull
 		vnull
 	)
 	)
 
 
+	let setBroadcast = vifun1 (fun vthis b ->
+		let this = this vthis in
+		let b = decode_bool b in
+		setsockopt this SO_BROADCAST b;
+		vnull
+	)
+
 	let setTimeout = vifun1 (fun vthis timeout ->
 	let setTimeout = vifun1 (fun vthis timeout ->
 		let this = this vthis in
 		let this = this vthis in
 		let timeout = match timeout with VNull -> 0. | VInt32 i -> Int32.to_float i | VFloat f -> f | _ -> unexpected_value timeout "number" in
 		let timeout = match timeout with VNull -> 0. | VInt32 i -> Int32.to_float i | VFloat f -> f | _ -> unexpected_value timeout "number" in
@@ -3298,6 +3305,7 @@ let init_standard_library builtins =
 		"send",StdSocket.send;
 		"send",StdSocket.send;
 		"sendChar",StdSocket.sendChar;
 		"sendChar",StdSocket.sendChar;
 		"setFastSend",StdSocket.setFastSend;
 		"setFastSend",StdSocket.setFastSend;
+		"setBroadcast", StdSocket.setBroadcast;
 		"setTimeout",StdSocket.setTimeout;
 		"setTimeout",StdSocket.setTimeout;
 		"shutdown",StdSocket.shutdown;
 		"shutdown",StdSocket.shutdown;
 	];
 	];

+ 2 - 0
std/cpp/NativeSocket.hx

@@ -124,6 +124,8 @@ extern class NativeSocket
    @:native("_hx_std_socket_set_fast_send")
    @:native("_hx_std_socket_set_fast_send")
    public static function socket_set_fast_send(o:Dynamic,b:Bool) : Void;
    public static function socket_set_fast_send(o:Dynamic,b:Bool) : Void;
 
 
+   @:native("_hx_std_socket_set_broadcast")
+   public static function socket_set_broadcast(o:Dynamic,b:Bool) : Void;
 
 
    @:native("_hx_std_socket_poll_alloc")
    @:native("_hx_std_socket_poll_alloc")
    public static function socket_poll_alloc(nsocks:Int) : Dynamic;
    public static function socket_poll_alloc(nsocks:Int) : Dynamic;

+ 4 - 0
std/cpp/_std/sys/net/UdpSocket.hx

@@ -57,4 +57,8 @@ class UdpSocket extends Socket {
 		return r;
 		return r;
 	}
 	}
 
 
+	public function setBroadcast( b : Bool ) : Void {
+		NativeSocket.socket_set_broadcast(__s,b);
+	}
+
 }
 }

+ 5 - 1
std/hl/_std/sys/net/UdpSocket.hx

@@ -57,8 +57,12 @@ class UdpSocket extends Socket {
 		return ret;
 		return ret;
 	}
 	}
 
 
-	@:hlNative("std","socket_send_to") static function socket_send_to( s : SocketHandle, bytes : hl.Bytes, len : Int, host : Int, port : Int ) : Int { return 0; }
+	public function setBroadcast( b : Bool ) : Void {
+		if( !socket_set_broadcast(__s,b) ) throw new Sys.SysError("setBroadcast() failure");
+	}
 
 
+	@:hlNative("std","socket_send_to") static function socket_send_to( s : SocketHandle, bytes : hl.Bytes, len : Int, host : Int, port : Int ) : Int { return 0; }
+	@:hlNative("std", "socket_set_broadcast") static function socket_set_broadcast( s : SocketHandle, b : Bool ) : Bool { return true; }
 	@:hlNative("std","socket_recv_from") static function socket_recv_from( s : SocketHandle, bytes : hl.Bytes, len : Int, host : hl.Ref<Int>, port : hl.Ref<Int> ) : Int { return 0; }
 	@:hlNative("std","socket_recv_from") static function socket_recv_from( s : SocketHandle, bytes : hl.Bytes, len : Int, host : hl.Ref<Int>, port : hl.Ref<Int> ) : Int { return 0; }
 	
 	
 }
 }

+ 5 - 0
std/neko/_std/sys/net/UdpSocket.hx

@@ -56,7 +56,12 @@ class UdpSocket extends Socket {
 		return r;
 		return r;
 	}
 	}
 
 
+	public function setBroadcast( b : Bool ) : Void{
+		socket_set_broadcast(__s,b);
+	}
+
 	static var socket_recv_from = neko.Lib.loadLazy("std", "socket_recv_from", 5);
 	static var socket_recv_from = neko.Lib.loadLazy("std", "socket_recv_from", 5);
 	static var socket_send_to = neko.Lib.loadLazy("std", "socket_send_to", 5);
 	static var socket_send_to = neko.Lib.loadLazy("std", "socket_send_to", 5);
+	static var socket_set_broadcast = neko.Lib.loadLazy("std","socket_set_broadcast",2);
 
 
 }
 }

+ 7 - 0
std/sys/net/UdpSocket.hx

@@ -31,6 +31,13 @@ class UdpSocket extends Socket {
 		super();
 		super();
 	}
 	}
 
 
+	/**
+		Allows the socket to send to broadcast addresses.
+	**/
+	public function setBroadcast( b : Bool ) : Void {
+		throw "Not available on this platform";
+	}
+
 	/**
 	/**
 		Sends data to the specified target host/port address.
 		Sends data to the specified target host/port address.
 	**/
 	**/