[Asterisk-code-review] res_http_websocket: Add a client connection timeout (asterisk[master])

Kevin Harwell asteriskteam at digium.com
Thu Jan 13 16:38:42 CST 2022


Kevin Harwell has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/17867 )


Change subject: res_http_websocket: Add a client connection timeout
......................................................................

res_http_websocket: Add a client connection timeout

Previously there was no way to specify a connection timeout when
attempting to connect a websocket client to a server. This patch
makes it possible to now do such.

Change-Id: I5812f6f28d3d13adbc246517f87af177fa20ee9d
---
M include/asterisk/http_websocket.h
M res/res_http_websocket.c
2 files changed, 37 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/67/17867/1

diff --git a/include/asterisk/http_websocket.h b/include/asterisk/http_websocket.h
index 2a55e60..0c9d56b 100644
--- a/include/asterisk/http_websocket.h
+++ b/include/asterisk/http_websocket.h
@@ -441,6 +441,32 @@
 		  enum ast_websocket_result *result), { return NULL;});
 
 /*!
+ * \brief Create, and connect, a websocket client using an optional connection timeout.
+ *
+ * If the client websocket successfully connects, then the accepted protocol can be
+ * checked via a call to ast_websocket_client_accept_protocol.
+ *
+ * \note While connecting this *will* block until a response is received
+ *       from the remote host, or the connection timeout is reached
+ * \note Expected uri form:
+ *       \verbatim ws[s]://<address>[:port][/<path>] \endverbatim
+ *       The address (can be a host name) and port are parsed out and used to connect
+ *       to the remote server.  If multiple IPs are returned during address
+ *       resolution then the first one is chosen.
+ *
+ * \param uri uri to connect to
+ * \param protocols a comma separated string of supported protocols
+ * \param tls_cfg secure websocket credentials
+ * \param result result code set on client failure
+ * \param timeout How long (in milliseconds) to attempt to connect (-1 equals infinite)
+ * \return a client websocket.
+ * \retval NULL if object could not be created or connected
+ */
+AST_OPTIONAL_API(struct ast_websocket *, ast_websocket_client_create_and_connect,
+		 (const char *uri, const char *protocols, struct ast_tls_config *tls_cfg,
+		  enum ast_websocket_result *result, int timeout), { return NULL;});
+
+/*!
  * \brief Retrieve the server accepted sub-protocol on the client.
  *
  * \param ws the websocket client
diff --git a/res/res_http_websocket.c b/res/res_http_websocket.c
index 879762e..5bb70c9 100644
--- a/res/res_http_websocket.c
+++ b/res/res_http_websocket.c
@@ -1395,13 +1395,13 @@
 	return websocket_client_handshake_get_response(client);
 }
 
-static enum ast_websocket_result websocket_client_connect(struct ast_websocket *ws)
+static enum ast_websocket_result websocket_client_connect(struct ast_websocket *ws, int timeout)
 {
 	enum ast_websocket_result res;
 	/* create and connect the client - note client_start
 	   releases the session instance on failure */
-	if (!(ws->client->ser = ast_tcptls_client_start(
-		      ast_tcptls_client_create(ws->client->args)))) {
+	if (!(ws->client->ser = ast_tcptls_client_start_timeout(
+		      ast_tcptls_client_create(ws->client->args), timeout))) {
 		return WS_CLIENT_START_ERROR;
 	}
 
@@ -1422,6 +1422,13 @@
 	(const char *uri, const char *protocols, struct ast_tls_config *tls_cfg,
 	 enum ast_websocket_result *result)
 {
+	return ast_websocket_client_create_and_connect(uri, protocols, tls_cfg, result, -1);
+}
+
+struct ast_websocket *AST_OPTIONAL_API_NAME(ast_websocket_client_create_and_connect)
+	(const char *uri, const char *protocols, struct ast_tls_config *tls_cfg,
+	 enum ast_websocket_result *result, int timeout)
+{
 	struct ast_websocket *ws = websocket_client_create(
 		uri, protocols, tls_cfg, result);
 
@@ -1429,7 +1436,7 @@
 		return NULL;
 	}
 
-	if ((*result = websocket_client_connect(ws)) != WS_OK) {
+	if ((*result = websocket_client_connect(ws, timeout)) != WS_OK) {
 		ao2_ref(ws, -1);
 		return NULL;
 	}

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/17867
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I5812f6f28d3d13adbc246517f87af177fa20ee9d
Gerrit-Change-Number: 17867
Gerrit-PatchSet: 1
Gerrit-Owner: Kevin Harwell <kharwell at digium.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220113/dd06d915/attachment.html>


More information about the asterisk-code-review mailing list