Skip to content

Commit

Permalink
UdpSocket: implement broadcast sending capability (#7677)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
414n authored and Simn committed Jan 22, 2019
1 parent eb412e2 commit 39475bc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/macro/eval/evalStdLib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,13 @@ module StdSocket = struct
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 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
Expand Down Expand Up @@ -3298,6 +3305,7 @@ let init_standard_library builtins =
"send",StdSocket.send;
"sendChar",StdSocket.sendChar;
"setFastSend",StdSocket.setFastSend;
"setBroadcast", StdSocket.setBroadcast;
"setTimeout",StdSocket.setTimeout;
"shutdown",StdSocket.shutdown;
];
Expand Down
2 changes: 2 additions & 0 deletions std/cpp/NativeSocket.hx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ extern class NativeSocket
@:native("_hx_std_socket_set_fast_send")
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")
public static function socket_poll_alloc(nsocks:Int) : Dynamic;
Expand Down
4 changes: 4 additions & 0 deletions std/cpp/_std/sys/net/UdpSocket.hx
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ class UdpSocket extends Socket {
return r;
}

public function setBroadcast( b : Bool ) : Void {
NativeSocket.socket_set_broadcast(__s,b);
}

}
6 changes: 5 additions & 1 deletion std/hl/_std/sys/net/UdpSocket.hx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ class UdpSocket extends Socket {
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; }

}
5 changes: 5 additions & 0 deletions std/neko/_std/sys/net/UdpSocket.hx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ class UdpSocket extends Socket {
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_send_to = neko.Lib.loadLazy("std", "socket_send_to", 5);
static var socket_set_broadcast = neko.Lib.loadLazy("std","socket_set_broadcast",2);

}
7 changes: 7 additions & 0 deletions std/sys/net/UdpSocket.hx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class UdpSocket extends Socket {
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.
**/
Expand Down

0 comments on commit 39475bc

Please sign in to comment.