diff options
author | Andrew Stitcher <astitcher@apache.org> | 2009-12-17 22:01:04 +0000 |
---|---|---|
committer | Andrew Stitcher <astitcher@apache.org> | 2009-12-17 22:01:04 +0000 |
commit | 662b50f4c07dfb8891e4ab1dcaec2593a99d9091 (patch) | |
tree | 9141d94ff6811b55789664e4e3b0321f9ad8fea2 /cpp | |
parent | 82911bafcccd3e2e84a081efa3b6b139b546a641 (diff) | |
download | qpid-python-662b50f4c07dfb8891e4ab1dcaec2593a99d9091.tar.gz |
QPID-2210: Rework SocketAddress class to have correct value semantics under copying
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@891938 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/src/qpid/sys/SocketAddress.h | 4 | ||||
-rw-r--r-- | cpp/src/qpid/sys/posix/SocketAddress.cpp | 56 |
2 files changed, 44 insertions, 16 deletions
diff --git a/cpp/src/qpid/sys/SocketAddress.h b/cpp/src/qpid/sys/SocketAddress.h index fcb9c81d43..27b9642f2c 100644 --- a/cpp/src/qpid/sys/SocketAddress.h +++ b/cpp/src/qpid/sys/SocketAddress.h @@ -37,6 +37,8 @@ class SocketAddress { public: /** Create a SocketAddress from hostname and port*/ QPID_COMMON_EXTERN SocketAddress(const std::string& host, const std::string& port); + QPID_COMMON_EXTERN SocketAddress(const SocketAddress&); + QPID_COMMON_EXTERN SocketAddress& operator=(const SocketAddress&); QPID_COMMON_EXTERN ~SocketAddress(); std::string asString() const; @@ -44,7 +46,7 @@ public: private: std::string host; std::string port; - ::addrinfo* addrInfo; + mutable ::addrinfo* addrInfo; }; }} diff --git a/cpp/src/qpid/sys/posix/SocketAddress.cpp b/cpp/src/qpid/sys/posix/SocketAddress.cpp index fe8812299c..cb44f8e41f 100644 --- a/cpp/src/qpid/sys/posix/SocketAddress.cpp +++ b/cpp/src/qpid/sys/posix/SocketAddress.cpp @@ -35,27 +35,34 @@ SocketAddress::SocketAddress(const std::string& host0, const std::string& port0) port(port0), addrInfo(0) { - ::addrinfo hints; - ::memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET; // In order to allow AF_INET6 we'd have to change createTcp() as well - hints.ai_socktype = SOCK_STREAM; +} - const char* node = 0; - if (host.empty()) { - hints.ai_flags |= AI_PASSIVE; - } else { - node = host.c_str(); - } - const char* service = port.empty() ? "0" : port.c_str(); +SocketAddress::SocketAddress(const SocketAddress& sa) : + host(sa.host), + port(sa.port), + addrInfo(0) +{ +} + +SocketAddress& SocketAddress::operator=(const SocketAddress& sa) +{ + if (&sa != this) { + host = sa.host; + port = sa.port; - int n = ::getaddrinfo(node, service, &hints, &addrInfo); - if (n != 0) - throw Exception(QPID_MSG("Cannot resolve " << host << ": " << ::gai_strerror(n))); + if (addrInfo) { + ::freeaddrinfo(addrInfo); + addrInfo = 0; + } + } + return *this; } SocketAddress::~SocketAddress() { - ::freeaddrinfo(addrInfo); + if (addrInfo) { + ::freeaddrinfo(addrInfo); + } } std::string SocketAddress::asString() const @@ -65,6 +72,25 @@ std::string SocketAddress::asString() const const ::addrinfo& getAddrInfo(const SocketAddress& sa) { + if (!sa.addrInfo) { + ::addrinfo hints; + ::memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; // In order to allow AF_INET6 we'd have to change createTcp() as well + hints.ai_socktype = SOCK_STREAM; + + const char* node = 0; + if (sa.host.empty()) { + hints.ai_flags |= AI_PASSIVE; + } else { + node = sa.host.c_str(); + } + const char* service = sa.port.empty() ? "0" : sa.port.c_str(); + + int n = ::getaddrinfo(node, service, &hints, &sa.addrInfo); + if (n != 0) + throw Exception(QPID_MSG("Cannot resolve " << sa.host << ": " << ::gai_strerror(n))); + } + return *sa.addrInfo; } |