qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From astitc...@apache.org
Subject svn commit: r891938 - in /qpid/trunk/qpid/cpp/src/qpid/sys: SocketAddress.h posix/SocketAddress.cpp
Date Thu, 17 Dec 2009 22:01:04 GMT
Author: astitcher
Date: Thu Dec 17 22:01:04 2009
New Revision: 891938

URL: http://svn.apache.org/viewvc?rev=891938&view=rev
Log:
QPID-2210: Rework SocketAddress class to have correct value semantics under copying

Modified:
    qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h
    qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp

Modified: qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h?rev=891938&r1=891937&r2=891938&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h Thu Dec 17 22:01:04 2009
@@ -37,6 +37,8 @@
 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 @@
 private:
     std::string host;
     std::string port;
-    ::addrinfo* addrInfo;
+    mutable ::addrinfo* addrInfo;
 };
 
 }}

Modified: qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp?rev=891938&r1=891937&r2=891938&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp Thu Dec 17 22:01:04 2009
@@ -35,27 +35,34 @@
     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();
+}
 
-    int n = ::getaddrinfo(node, service, &hints, &addrInfo);
-    if (n != 0)
-        throw Exception(QPID_MSG("Cannot resolve " << host << ": " << ::gai_strerror(n)));
+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;
+
+        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 @@
 
 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;
 }
 



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:commits-subscribe@qpid.apache.org


Mime
View raw message