From bb7ac8e04aa47d37b19dc6f9669dcc2afb93bb2f Mon Sep 17 00:00:00 2001 From: Andrew Stitcher Date: Thu, 26 May 2011 20:38:43 +0000 Subject: NO-JIRA: Add cached localname as well as peername to Socket git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1128067 13f79535-47bb-0310-9956-ffa450edef68 --- qpid/cpp/src/qpid/sys/Socket.h | 3 ++- qpid/cpp/src/qpid/sys/posix/Socket.cpp | 24 +++++++++++++++--------- qpid/cpp/src/qpid/sys/windows/Socket.cpp | 12 +++++++----- 3 files changed, 24 insertions(+), 15 deletions(-) (limited to 'qpid/cpp') diff --git a/qpid/cpp/src/qpid/sys/Socket.h b/qpid/cpp/src/qpid/sys/Socket.h index db4cab4c15..9f62f3be1c 100644 --- a/qpid/cpp/src/qpid/sys/Socket.h +++ b/qpid/cpp/src/qpid/sys/Socket.h @@ -93,7 +93,8 @@ private: void createSocket(const SocketAddress&) const; Socket(IOHandlePrivate*); - mutable std::string connectname; + mutable std::string localname; + mutable std::string peername; mutable bool nonblocking; mutable bool nodelay; }; diff --git a/qpid/cpp/src/qpid/sys/posix/Socket.cpp b/qpid/cpp/src/qpid/sys/posix/Socket.cpp index 1b25a25824..be757ba00f 100644 --- a/qpid/cpp/src/qpid/sys/posix/Socket.cpp +++ b/qpid/cpp/src/qpid/sys/posix/Socket.cpp @@ -122,7 +122,7 @@ void Socket::connect(const std::string& host, const std::string& port) const void Socket::connect(const SocketAddress& addr) const { - connectname = addr.asString(); + peername = addr.asString(); createSocket(addr); @@ -130,7 +130,7 @@ void Socket::connect(const SocketAddress& addr) const // TODO the correct thing to do here is loop on failure until you've used all the returned addresses if ((::connect(socket, getAddrInfo(addr).ai_addr, getAddrInfo(addr).ai_addrlen) < 0) && (errno != EINPROGRESS)) { - throw Exception(QPID_MSG(strError(errno) << ": " << connectname)); + throw Exception(QPID_MSG(strError(errno) << ": " << peername)); } // When connecting to a port on the same host which no longer has // a process associated with it, the OS occasionally chooses the @@ -147,7 +147,7 @@ void Socket::connect(const SocketAddress& addr) const // if (getLocalAddress() == getPeerAddress()) { close(); - throw Exception(QPID_MSG("Connection refused: " << connectname)); + throw Exception(QPID_MSG("Connection refused: " << peername)); } } @@ -190,8 +190,11 @@ int Socket::listen(const SocketAddress& sa, int backlog) const Socket* Socket::accept() const { int afd = ::accept(impl->fd, 0, 0); - if ( afd >= 0) - return new Socket(new IOHandlePrivate(afd)); + if ( afd >= 0) { + Socket* s = new Socket(new IOHandlePrivate(afd)); + s->localname = localname; + return s; + } else if (errno == EAGAIN) return 0; else throw QPID_POSIX_ERROR(errno); @@ -209,15 +212,18 @@ int Socket::write(const void *buf, size_t count) const std::string Socket::getPeerAddress() const { - if (connectname.empty()) { - connectname = getName(impl->fd, false); + if (peername.empty()) { + peername = getName(impl->fd, false); } - return connectname; + return peername; } std::string Socket::getLocalAddress() const { - return getName(impl->fd, true); + if (localname.empty()) { + localname = getName(impl->fd, true); + } + return localname; } int Socket::getError() const diff --git a/qpid/cpp/src/qpid/sys/windows/Socket.cpp b/qpid/cpp/src/qpid/sys/windows/Socket.cpp index 90984639a2..fa47308cc2 100755 --- a/qpid/cpp/src/qpid/sys/windows/Socket.cpp +++ b/qpid/cpp/src/qpid/sys/windows/Socket.cpp @@ -178,7 +178,7 @@ Socket::connect(const SocketAddress& addr) const addrs = addrs->ai_next; } if (error) - throw qpid::Exception(QPID_MSG(strError(error) << ": " << connectname)); + throw qpid::Exception(QPID_MSG(strError(error) << ": " << peername)); } void @@ -241,14 +241,16 @@ Socket* Socket::accept() const std::string Socket::getPeerAddress() const { - if (!connectname.empty()) - connectname = getName(impl->fd, false); - return connectname; + if (peername.empty()) + peername = getName(impl->fd, false); + return peername; } std::string Socket::getLocalAddress() const { - return getName(impl->fd, true); + if (localname.empty()) + localname = getName(impl->fd, true); + return localname; } int Socket::getError() const -- cgit v1.2.1