From 8b8169b8680ccb934d8d57e9c9f898f38ee67cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sun, 15 Jan 2017 21:42:05 +0100 Subject: [PATCH] Add EventDriverSockets.setBroadcast. --- source/eventcore/driver.d | 3 +++ source/eventcore/drivers/libasync.d | 10 ++++++++++ source/eventcore/drivers/posix.d | 18 +++++++++++++++++- source/eventcore/drivers/winapi.d | 10 ++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/source/eventcore/driver.d b/source/eventcore/driver.d index 594af4f..16fa91e 100644 --- a/source/eventcore/driver.d +++ b/source/eventcore/driver.d @@ -65,6 +65,8 @@ interface EventDriverCore { /// Low-level user data access. Use `getUserData` instead. protected void* rawUserData(StreamSocketFD descriptor, size_t size, DataInitializer initialize, DataInitializer destroy) @system; + /// ditto + protected void* rawUserData(DatagramSocketFD descriptor, size_t size, DataInitializer initialize, DataInitializer destroy) @system; /** Retrieves a reference to a user-defined value associated with a descriptor. */ @@ -94,6 +96,7 @@ interface EventDriverSockets { void shutdown(StreamSocketFD socket, bool shut_read, bool shut_write); DatagramSocketFD createDatagramSocket(scope Address bind_address, scope Address target_address); + bool setBroadcast(DatagramSocketFD socket, bool enable); void receive(DatagramSocketFD socket, ubyte[] buffer, IOMode mode, DatagramIOCallback on_receive_finish); void cancelReceive(DatagramSocketFD socket); void send(DatagramSocketFD socket, const(ubyte)[] buffer, IOMode mode, Address target_address, DatagramIOCallback on_send_finish); diff --git a/source/eventcore/drivers/libasync.d b/source/eventcore/drivers/libasync.d index 4b04b4f..d412934 100644 --- a/source/eventcore/drivers/libasync.d +++ b/source/eventcore/drivers/libasync.d @@ -75,6 +75,11 @@ final class LibasyncEventDriverCore : EventDriverCore { { assert(false, "TODO!"); } + + protected override void* rawUserData(DatagramSocketFD descriptor, size_t size, DataInitializer initialize, DataInitializer destroy) @system + { + assert(false, "TODO!"); + } } final class LibasyncEventDriverSockets : EventDriverSockets { @@ -149,6 +154,11 @@ final class LibasyncEventDriverSockets : EventDriverSockets { assert(false, "TODO!"); } + override bool setBroadcast(DatagramSocketFD socket, bool enable) + { + assert(false, "TODO!"); + } + override void receive(DatagramSocketFD socket, ubyte[] buffer, IOMode mode, DatagramIOCallback on_receive_finish) { assert(false, "TODO!"); diff --git a/source/eventcore/drivers/posix.d b/source/eventcore/drivers/posix.d index f9f98e9..ad5f7eb 100644 --- a/source/eventcore/drivers/posix.d +++ b/source/eventcore/drivers/posix.d @@ -183,6 +183,16 @@ final class PosixEventDriverCore(Loop : PosixEventLoop, Timers : EventDriverTime } final protected override void* rawUserData(StreamSocketFD descriptor, size_t size, DataInitializer initialize, DataInitializer destroy) + @system { + return rawUserDataImpl(descriptor, size, initialize, destroy); + } + + final protected override void* rawUserData(DatagramSocketFD descriptor, size_t size, DataInitializer initialize, DataInitializer destroy) + @system { + return rawUserDataImpl(descriptor, size, initialize, destroy); + } + + private void* rawUserDataImpl(FD descriptor, size_t size, DataInitializer initialize, DataInitializer destroy) @system { FDSlot* fds = &m_loop.m_fds[descriptor].common; assert(fds.userDataDestructor is null || fds.userDataDestructor is destroy, @@ -624,7 +634,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets m_loop.m_fds[socket].streamSocket.state = shut_read ? shut_write ? ConnectionState.closed : ConnectionState.passiveClose : shut_write ? ConnectionState.activeClose : ConnectionState.connected; } - DatagramSocketFD createDatagramSocket(scope Address bind_address, scope Address target_address) + final override DatagramSocketFD createDatagramSocket(scope Address bind_address, scope Address target_address) { auto sock = cast(DatagramSocketFD)createSocket(bind_address.addressFamily, SOCK_DGRAM); if (sock == -1) return DatagramSocketFD.invalid; @@ -646,6 +656,12 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets return sock; } + final override bool setBroadcast(DatagramSocketFD socket, bool enable) + { + int tmp_broad = enable; + return () @trusted { return setsockopt(socket, SOL_SOCKET, SO_BROADCAST, &tmp_broad, tmp_broad.sizeof); } () == 0; + } + void receive(DatagramSocketFD socket, ubyte[] buffer, IOMode mode, DatagramIOCallback on_receive_finish) @trusted { // DMD 2.072.0-b2: scope considered unsafe import std.typecons : scoped; diff --git a/source/eventcore/drivers/winapi.d b/source/eventcore/drivers/winapi.d index 7808813..28e61ff 100644 --- a/source/eventcore/drivers/winapi.d +++ b/source/eventcore/drivers/winapi.d @@ -82,6 +82,11 @@ final class WinAPIEventDriverCore : EventDriverCore { { assert(false, "TODO!"); } + + protected override void* rawUserData(DatagramSocketFD descriptor, size_t size, DataInitializer initialize, DataInitializer destroy) @system + { + assert(false, "TODO!"); + } } final class WinAPIEventDriverSockets : EventDriverSockets { @@ -156,6 +161,11 @@ final class WinAPIEventDriverSockets : EventDriverSockets { assert(false, "TODO!"); } + override bool setBroadcast(DatagramSocketFD socket, bool enable) + { + assert(false, "TODO!"); + } + override void receive(DatagramSocketFD socket, ubyte[] buffer, IOMode mode, DatagramIOCallback on_receive_finish) { assert(false, "TODO!");