From: Timo Sirainen Date: Thu, 24 Feb 2022 17:11:19 +0000 (+0100) Subject: lib: Add connection_client_connect_async() X-Git-Tag: 2.4.0~4289 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=47bb7229e257f0eda79488b497edef5963f2e78f;p=thirdparty%2Fdovecot%2Fcore.git lib: Add connection_client_connect_async() --- diff --git a/src/lib/connection.c b/src/lib/connection.c index 71ace0171c..23bda44789 100644 --- a/src/lib/connection.c +++ b/src/lib/connection.c @@ -761,6 +761,27 @@ int connection_client_connect(struct connection *conn) conn->list->set.unix_client_connect_msecs); } +static void connection_client_connect_failed(struct connection *conn) +{ + timeout_remove(&conn->to); + errno = conn->connect_failed_errno; + conn->v.client_connected(conn, FALSE); + connection_closed(conn, CONNECTION_DISCONNECT_CONN_CLOSED); +} + +int connection_client_connect_async(struct connection *conn) +{ + i_assert(conn->v.client_connected != NULL); + + if (connection_client_connect(conn) < 0) { + i_assert(conn->to == NULL); + conn->connect_failed_errno = errno; + conn->to = timeout_add_short(0, connection_client_connect_failed, conn); + return -1; + } + return 0; +} + static void connection_update_counters(struct connection *conn) { if (conn->input != NULL) diff --git a/src/lib/connection.h b/src/lib/connection.h index 398637cfde..4bd836e2c9 100644 --- a/src/lib/connection.h +++ b/src/lib/connection.h @@ -152,6 +152,7 @@ struct connection { /* handlers */ struct connection_vfuncs v; + int connect_failed_errno; enum connection_disconnect_reason disconnect_reason; bool version_received:1; @@ -200,7 +201,12 @@ void connection_init_from_streams(struct connection_list *list, struct istream *input, struct ostream *output) ATTR_NULL(3); +/* connect() to the server. If the connect() fails immediately, return -1. */ int connection_client_connect(struct connection *conn); +/* connect() to the server. If the connect() fails immediately, call the + client_connected() and destroy() asynchronously from a timeout. This + simulates what happens on a delayed connect() failure. */ +int connection_client_connect_async(struct connection *conn); /* Connect to UNIX socket. If it fails, try it up to msecs is reached. Overrides connection_settings.unix_client_connect_msecs. */ int connection_client_connect_with_retries(struct connection *conn,