summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Stitcher <astitcher@apache.org>2009-12-17 22:01:04 +0000
committerAndrew Stitcher <astitcher@apache.org>2009-12-17 22:01:04 +0000
commit6628b97777d79a909f7540837ca8fbd3c57313d4 (patch)
treef7f7d4a1f1d1c4ed11aec3aedb3b32bee13efd73
parentf11a81f95a4525678c081a73d39dfb93c5c8c1a1 (diff)
downloadqpid-python-6628b97777d79a909f7540837ca8fbd3c57313d4.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@891938 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--qpid/cpp/src/qpid/sys/SocketAddress.h4
-rw-r--r--qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp56
2 files changed, 44 insertions, 16 deletions
diff --git a/qpid/cpp/src/qpid/sys/SocketAddress.h b/qpid/cpp/src/qpid/sys/SocketAddress.h
index fcb9c81d43..27b9642f2c 100644
--- a/qpid/cpp/src/qpid/sys/SocketAddress.h
+++ b/qpid/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/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp b/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp
index fe8812299c..cb44f8e41f 100644
--- a/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp
+++ b/qpid/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;
}