From c3e91f60beb5bcdc33b97aea014928039847bba1 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Thu, 12 Dec 2024 17:50:30 -0800 Subject: [PATCH] client: defer notification of connect() failure ... to bevEvent() callback to handle early failure the same as later disconnect. --- src/clientconn.cpp | 17 +++++++++++++++-- src/clientimpl.h | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/clientconn.cpp b/src/clientconn.cpp index 30af209..b1130b3 100644 --- a/src/clientconn.cpp +++ b/src/clientconn.cpp @@ -71,8 +71,16 @@ void Connection::startConnecting() timeval tmo(totv(context->effective.tcpTimeout)); bufferevent_set_timeouts(bev.get(), &tmo, &tmo); - if(bufferevent_socket_connect(bev.get(), const_cast(&peerAddr->sa), peerAddr.size())) - throw std::runtime_error("Unable to begin connecting"); + if(bufferevent_socket_connect(bev.get(), const_cast(&peerAddr->sa), peerAddr.size())) { + // non-blocking connect() failed immediately. + // try to defer notification. + state = Disconnected; + constexpr timeval immediate{0, 0}; + if(event_add(echoTimer.get(), &immediate)) + throw std::runtime_error(SB()<<"Unable to begin connecting or schedule deferred notification "<