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