Extend datagram socket API.

Adds .setTargetAddress() and makes .getXAddress() work for datagram sockets, too.
This commit is contained in:
Sönke Ludwig 2017-03-10 22:46:43 +01:00
parent 2e5b13ee98
commit 1d330d6330
No known key found for this signature in database
GPG key ID: D95E8DB493EE314C
3 changed files with 15 additions and 6 deletions

View file

@ -157,10 +157,10 @@ interface EventDriverSockets {
ConnectionState getConnectionState(StreamSocketFD sock); ConnectionState getConnectionState(StreamSocketFD sock);
/// Retrieves the bind address of a socket. /// 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. /// 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 /// Sets the `TCP_NODELAY` option on a socket
void setTCPNoDelay(StreamSocketFD socket, bool enable); void setTCPNoDelay(StreamSocketFD socket, bool enable);
@ -248,6 +248,10 @@ interface EventDriverSockets {
*/ */
DatagramSocketFD adoptDatagramSocket(int socket); 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. /// Sets the `SO_BROADCAST` socket option.
bool setBroadcast(DatagramSocketFD socket, bool enable); bool setBroadcast(DatagramSocketFD socket, bool enable);

View file

@ -213,7 +213,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets
return m_loop.m_fds[sock].streamSocket.state; 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; socklen_t addr_len = dst.nameLen;
if (() @trusted { return getpeername(cast(sock_t)sock, dst.name, &addr_len); } () != 0) 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; 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; socklen_t addr_len = dst.nameLen;
if (() @trusted { return getsockname(cast(sock_t)sock, dst.name, &addr_len); } () != 0) 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; 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) final override bool setBroadcast(DatagramSocketFD socket, bool enable)
{ {
int tmp_broad = enable; int tmp_broad = enable;

View file

@ -35,12 +35,12 @@ final class WinAPIEventDriverSockets : EventDriverSockets {
assert(false, "TODO!"); assert(false, "TODO!");
} }
override bool getLocalAddress(StreamSocketFD sock, scope RefAddress dst) override bool getLocalAddress(SocketFD sock, scope RefAddress dst)
{ {
assert(false, "TODO!"); assert(false, "TODO!");
} }
override bool getRemoteAddress(StreamSocketFD sock, scope RefAddress dst) override bool getRemoteAddress(SocketFD sock, scope RefAddress dst)
{ {
assert(false, "TODO!"); assert(false, "TODO!");
} }