// Cache local and remote socket addresses to keep them available
// after socket file descriptor is closed.
- if (cacheAddrOnFailure_ && -1 != getFd()) {
- cacheLocalPeerAddr();
+ if (cacheAddrOnFailure_) {
+ cacheAddresses();
}
handshakeStartTime_ = std::chrono::steady_clock::now();
}
}
-void AsyncSSLSocket::cacheLocalPeerAddr() {
- SocketAddress address;
- try {
- getLocalAddress(&address);
- getPeerAddress(&address);
- } catch (const std::system_error& e) {
- // The handle can be still valid while the connection is already closed.
- if (e.code() != std::error_code(ENOTCONN, std::system_category())) {
- throw;
- }
- }
-}
-
void AsyncSSLSocket::connect(
ConnectCallback* callback,
const folly::SocketAddress& address,
// Cache local and remote socket addresses to keep them available
// after socket file descriptor is closed.
- if (cacheAddrOnFailure_ && -1 != getFd()) {
- cacheLocalPeerAddr();
+ if (cacheAddrOnFailure_) {
+ cacheAddresses();
}
verifyPeer_ = verifyPeer;
void invokeConnectSuccess() override;
void scheduleConnectTimeout() override;
- void cacheLocalPeerAddr();
-
void startSSLConnect();
static void sslInfoCallback(const SSL *ssl, int type, int val);
return !ioHandler_.isHandlerRegistered() && !writeTimeout_.isScheduled();
}
-void AsyncSocket::getLocalAddress(folly::SocketAddress* address) const {
+void AsyncSocket::cacheAddresses() {
+ if (fd_ >= 0) {
+ try {
+ cacheLocalAddress();
+ cachePeerAddress();
+ } catch (const std::system_error& e) {
+ if (e.code() != std::error_code(ENOTCONN, std::system_category())) {
+ VLOG(1) << "Error caching addresses: " << e.code().value() << ", "
+ << e.code().message();
+ }
+ }
+ }
+}
+
+void AsyncSocket::cacheLocalAddress() const {
if (!localAddr_.isInitialized()) {
localAddr_.setFromLocalAddress(fd_);
}
- *address = localAddr_;
}
-void AsyncSocket::getPeerAddress(folly::SocketAddress* address) const {
+void AsyncSocket::cachePeerAddress() const {
if (!addr_.isInitialized()) {
addr_.setFromPeerAddress(fd_);
}
+}
+
+void AsyncSocket::getLocalAddress(folly::SocketAddress* address) const {
+ cacheLocalAddress();
+ *address = localAddr_;
+}
+
+void AsyncSocket::getPeerAddress(folly::SocketAddress* address) const {
+ cachePeerAddress();
*address = addr_;
}
evbChangeCb_ = std::move(cb);
}
+ /**
+ * Attempt to cache the current local and peer addresses (if not already
+ * cached) so that they are available from getPeerAddress() and
+ * getLocalAddress() even after the socket is closed.
+ */
+ void cacheAddresses();
+
/**
* writeReturn is the total number of bytes written, or WRITE_ERROR on error.
* If no data has been written, 0 is returned.
std::string withAddr(const std::string& s);
+ void cacheLocalAddress() const;
+ void cachePeerAddress() const;
+
StateEnum state_; ///< StateEnum describing current state
uint8_t shutdownFlags_; ///< Shutdown state (ShutdownFlags)
uint16_t eventFlags_; ///< EventBase::HandlerFlags settings