Avoid extra exception and allow toString of unspecified NetworkAddress values.

This commit is contained in:
Sönke Ludwig 2017-07-19 14:50:20 +02:00
parent c62f07d257
commit d39bbf19c0
No known key found for this signature in database
GPG key ID: D95E8DB493EE314C

View file

@ -363,6 +363,9 @@ struct NetworkAddress {
switch (this.family) {
default: assert(false, "toAddressString() called for invalid address family.");
case AF_UNSPEC:
sink("<UNSPEC>");
break;
case AF_INET: {
ubyte[4] ip = () @trusted { return (cast(ubyte*)&addr_ip4.sin_addr.s_addr)[0 .. 4]; } ();
sink.formattedWrite("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
@ -401,9 +404,12 @@ struct NetworkAddress {
void toString(scope void delegate(const(char)[]) @safe sink)
const nothrow {
import std.format : formattedWrite;
scope (failure) assert(false);
try {
switch (this.family) {
default: assert(false, "toString() called for invalid address family.");
case AF_UNSPEC:
sink("<UNSPEC>");
break;
case AF_INET:
toAddressString(sink);
sink.formattedWrite(":%s", port);
@ -417,6 +423,9 @@ struct NetworkAddress {
toAddressString(sink);
break;
}
} catch (Exception e) {
assert(false, "Unexpected exception: "~e.msg);
}
}
version(Have_libev) {}
@ -489,17 +498,15 @@ struct TCPConnection {
@property NetworkAddress localAddress() const nothrow {
NetworkAddress naddr;
scope addr = new RefAddress(naddr.sockAddr, naddr.sockAddrMaxLen);
try {
enforce(eventDriver.sockets.getLocalAddress(m_socket, addr), "Failed to query socket address.");
} catch (Exception e) { logWarn("Failed to get local address for TCP connection: %s", e.msg); }
if (!eventDriver.sockets.getLocalAddress(m_socket, addr))
logWarn("Failed to get local address for TCP connection");
return naddr;
}
@property NetworkAddress remoteAddress() const nothrow {
NetworkAddress naddr;
scope addr = new RefAddress(naddr.sockAddr, naddr.sockAddrMaxLen);
try {
enforce(eventDriver.sockets.getRemoteAddress(m_socket, addr), "Failed to query socket address.");
} catch (Exception e) { logWarn("Failed to get remote address for TCP connection: %s", e.msg); }
if (!eventDriver.sockets.getRemoteAddress(m_socket, addr))
logWarn("Failed to get remote address for TCP connection");
return naddr;
}
@property bool connected()