From 1d330d6330c50e8fc0f1ead984a83a1ff4953701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Fri, 10 Mar 2017 22:46:43 +0100 Subject: [PATCH] Extend datagram socket API. Adds .setTargetAddress() and makes .getXAddress() work for datagram sockets, too. --- source/eventcore/driver.d | 8 ++++++-- source/eventcore/drivers/posix/sockets.d | 9 +++++++-- source/eventcore/drivers/winapi/sockets.d | 4 ++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/source/eventcore/driver.d b/source/eventcore/driver.d index d890bd9..b783096 100644 --- a/source/eventcore/driver.d +++ b/source/eventcore/driver.d @@ -157,10 +157,10 @@ interface EventDriverSockets { ConnectionState getConnectionState(StreamSocketFD sock); /// Retrieves the bind address of a socket. - bool getLocalAddress(StreamSocketFD sock, scope RefAddress dst); + bool getLocalAddress(SocketFD sock, scope RefAddress dst); /// Retrieves the address of the connected peer. - bool getRemoteAddress(StreamSocketFD sock, scope RefAddress dst); + bool getRemoteAddress(SocketFD sock, scope RefAddress dst); /// Sets the `TCP_NODELAY` option on a socket void setTCPNoDelay(StreamSocketFD socket, bool enable); @@ -248,6 +248,10 @@ interface EventDriverSockets { */ DatagramSocketFD adoptDatagramSocket(int socket); + /** Sets an address to use as the default target address for sent datagrams. + */ + void setTargetAddress(DatagramSocketFD socket, scope Address target_address); + /// Sets the `SO_BROADCAST` socket option. bool setBroadcast(DatagramSocketFD socket, bool enable); diff --git a/source/eventcore/drivers/posix/sockets.d b/source/eventcore/drivers/posix/sockets.d index 33d5f62..93df6f8 100644 --- a/source/eventcore/drivers/posix/sockets.d +++ b/source/eventcore/drivers/posix/sockets.d @@ -213,7 +213,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets return m_loop.m_fds[sock].streamSocket.state; } - final override bool getLocalAddress(StreamSocketFD sock, scope RefAddress dst) + final override bool getLocalAddress(SocketFD sock, scope RefAddress dst) { socklen_t addr_len = dst.nameLen; if (() @trusted { return getpeername(cast(sock_t)sock, dst.name, &addr_len); } () != 0) @@ -222,7 +222,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets return true; } - final override bool getRemoteAddress(StreamSocketFD sock, scope RefAddress dst) + final override bool getRemoteAddress(SocketFD sock, scope RefAddress dst) { socklen_t addr_len = dst.nameLen; if (() @trusted { return getsockname(cast(sock_t)sock, dst.name, &addr_len); } () != 0) @@ -552,6 +552,11 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets return fd; } + final override void setTargetAddress(DatagramSocketFD socket, scope Address target_address) + { + () @trusted { connect(cast(sock_t)socket, target_address.name, target_address.nameLen); } (); + } + final override bool setBroadcast(DatagramSocketFD socket, bool enable) { int tmp_broad = enable; diff --git a/source/eventcore/drivers/winapi/sockets.d b/source/eventcore/drivers/winapi/sockets.d index 8e228d4..c9381fd 100644 --- a/source/eventcore/drivers/winapi/sockets.d +++ b/source/eventcore/drivers/winapi/sockets.d @@ -35,12 +35,12 @@ final class WinAPIEventDriverSockets : EventDriverSockets { assert(false, "TODO!"); } - override bool getLocalAddress(StreamSocketFD sock, scope RefAddress dst) + override bool getLocalAddress(SocketFD sock, scope RefAddress dst) { assert(false, "TODO!"); } - override bool getRemoteAddress(StreamSocketFD sock, scope RefAddress dst) + override bool getRemoteAddress(SocketFD sock, scope RefAddress dst) { assert(false, "TODO!"); }