Add bind address support for outgoing TCP connections.
This commit is contained in:
parent
35f5852d8f
commit
d181a6730f
|
@ -78,7 +78,7 @@ interface EventDriverCore {
|
||||||
|
|
||||||
interface EventDriverSockets {
|
interface EventDriverSockets {
|
||||||
@safe: /*@nogc:*/ nothrow:
|
@safe: /*@nogc:*/ nothrow:
|
||||||
StreamSocketFD connectStream(scope Address peer_address, ConnectCallback on_connect);
|
StreamSocketFD connectStream(scope Address peer_address, scope Address bind_address, ConnectCallback on_connect);
|
||||||
StreamListenSocketFD listenStream(scope Address bind_address, AcceptCallback on_accept);
|
StreamListenSocketFD listenStream(scope Address bind_address, AcceptCallback on_accept);
|
||||||
void waitForConnections(StreamListenSocketFD sock, AcceptCallback on_accept);
|
void waitForConnections(StreamListenSocketFD sock, AcceptCallback on_accept);
|
||||||
ConnectionState getConnectionState(StreamSocketFD sock);
|
ConnectionState getConnectionState(StreamSocketFD sock);
|
||||||
|
|
|
@ -78,7 +78,7 @@ final class LibasyncEventDriverCore : EventDriverCore {
|
||||||
|
|
||||||
final class LibasyncEventDriverSockets : EventDriverSockets {
|
final class LibasyncEventDriverSockets : EventDriverSockets {
|
||||||
@safe: /*@nogc:*/ nothrow:
|
@safe: /*@nogc:*/ nothrow:
|
||||||
override StreamSocketFD connectStream(scope Address peer_address, ConnectCallback on_connect)
|
override StreamSocketFD connectStream(scope Address peer_address, scope Address bind_address, ConnectCallback on_connect)
|
||||||
{
|
{
|
||||||
assert(false, "TODO!");
|
assert(false, "TODO!");
|
||||||
}
|
}
|
||||||
|
|
|
@ -200,7 +200,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets
|
||||||
|
|
||||||
this(Loop loop) { m_loop = loop; }
|
this(Loop loop) { m_loop = loop; }
|
||||||
|
|
||||||
final override StreamSocketFD connectStream(scope Address address, ConnectCallback on_connect)
|
final override StreamSocketFD connectStream(scope Address address, scope Address bind_address, ConnectCallback on_connect)
|
||||||
{
|
{
|
||||||
auto sock = cast(StreamSocketFD)createSocket(address.addressFamily, SOCK_STREAM);
|
auto sock = cast(StreamSocketFD)createSocket(address.addressFamily, SOCK_STREAM);
|
||||||
if (sock == -1) return StreamSocketFD.invalid;
|
if (sock == -1) return StreamSocketFD.invalid;
|
||||||
|
@ -208,12 +208,17 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets
|
||||||
void invalidateSocket() @nogc @trusted nothrow { closeSocket(sock); sock = StreamSocketFD.invalid; }
|
void invalidateSocket() @nogc @trusted nothrow { closeSocket(sock); sock = StreamSocketFD.invalid; }
|
||||||
|
|
||||||
int bret;
|
int bret;
|
||||||
() @trusted { // scope
|
() @trusted { // scope + bind()
|
||||||
scope bind_addr = new UnknownAddress;
|
if (bind_address !is null) {
|
||||||
bind_addr.name.sa_family = cast(ushort)address.addressFamily;
|
bret = bind(sock, bind_address.name, bind_address.nameLen);
|
||||||
bind_addr.name.sa_data[] = 0;
|
} else {
|
||||||
bret = bind(sock, bind_addr.name, bind_addr.nameLen);
|
scope bind_addr = new UnknownAddress;
|
||||||
|
bind_addr.name.sa_family = cast(ushort)address.addressFamily;
|
||||||
|
bind_addr.name.sa_data[] = 0;
|
||||||
|
bret = bind(sock, bind_addr.name, bind_addr.nameLen);
|
||||||
|
}
|
||||||
} ();
|
} ();
|
||||||
|
|
||||||
if (bret != 0) {
|
if (bret != 0) {
|
||||||
invalidateSocket();
|
invalidateSocket();
|
||||||
on_connect(sock, ConnectStatus.bindFailure);
|
on_connect(sock, ConnectStatus.bindFailure);
|
||||||
|
|
|
@ -85,7 +85,7 @@ final class WinAPIEventDriverCore : EventDriverCore {
|
||||||
|
|
||||||
final class WinAPIEventDriverSockets : EventDriverSockets {
|
final class WinAPIEventDriverSockets : EventDriverSockets {
|
||||||
@safe: /*@nogc:*/ nothrow:
|
@safe: /*@nogc:*/ nothrow:
|
||||||
override StreamSocketFD connectStream(scope Address peer_address, ConnectCallback on_connect)
|
override StreamSocketFD connectStream(scope Address peer_address, scope Address bind_address, ConnectCallback on_connect)
|
||||||
{
|
{
|
||||||
assert(false, "TODO!");
|
assert(false, "TODO!");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue