<p>Kevin Harwell has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/17867">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_http_websocket: Add a client connection timeout<br><br>Previously there was no way to specify a connection timeout when<br>attempting to connect a websocket client to a server. This patch<br>makes it possible to now do such.<br><br>Change-Id: I5812f6f28d3d13adbc246517f87af177fa20ee9d<br>---<br>M include/asterisk/http_websocket.h<br>M res/res_http_websocket.c<br>2 files changed, 37 insertions(+), 4 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/67/17867/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/http_websocket.h b/include/asterisk/http_websocket.h</span><br><span>index 2a55e60..0c9d56b 100644</span><br><span>--- a/include/asterisk/http_websocket.h</span><br><span>+++ b/include/asterisk/http_websocket.h</span><br><span>@@ -441,6 +441,32 @@</span><br><span> enum ast_websocket_result *result), { return NULL;});</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Create, and connect, a websocket client using an optional connection timeout.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * If the client websocket successfully connects, then the accepted protocol can be</span><br><span style="color: hsl(120, 100%, 40%);">+ * checked via a call to ast_websocket_client_accept_protocol.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note While connecting this *will* block until a response is received</span><br><span style="color: hsl(120, 100%, 40%);">+ * from the remote host, or the connection timeout is reached</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note Expected uri form:</span><br><span style="color: hsl(120, 100%, 40%);">+ * \verbatim ws[s]://<address>[:port][/<path>] \endverbatim</span><br><span style="color: hsl(120, 100%, 40%);">+ * The address (can be a host name) and port are parsed out and used to connect</span><br><span style="color: hsl(120, 100%, 40%);">+ * to the remote server. If multiple IPs are returned during address</span><br><span style="color: hsl(120, 100%, 40%);">+ * resolution then the first one is chosen.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param uri uri to connect to</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param protocols a comma separated string of supported protocols</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param tls_cfg secure websocket credentials</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param result result code set on client failure</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param timeout How long (in milliseconds) to attempt to connect (-1 equals infinite)</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return a client websocket.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval NULL if object could not be created or connected</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+AST_OPTIONAL_API(struct ast_websocket *, ast_websocket_client_create_and_connect,</span><br><span style="color: hsl(120, 100%, 40%);">+ (const char *uri, const char *protocols, struct ast_tls_config *tls_cfg,</span><br><span style="color: hsl(120, 100%, 40%);">+ enum ast_websocket_result *result, int timeout), { return NULL;});</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span> * \brief Retrieve the server accepted sub-protocol on the client.</span><br><span> *</span><br><span> * \param ws the websocket client</span><br><span>diff --git a/res/res_http_websocket.c b/res/res_http_websocket.c</span><br><span>index 879762e..5bb70c9 100644</span><br><span>--- a/res/res_http_websocket.c</span><br><span>+++ b/res/res_http_websocket.c</span><br><span>@@ -1395,13 +1395,13 @@</span><br><span> return websocket_client_handshake_get_response(client);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static enum ast_websocket_result websocket_client_connect(struct ast_websocket *ws)</span><br><span style="color: hsl(120, 100%, 40%);">+static enum ast_websocket_result websocket_client_connect(struct ast_websocket *ws, int timeout)</span><br><span> {</span><br><span> enum ast_websocket_result res;</span><br><span> /* create and connect the client - note client_start</span><br><span> releases the session instance on failure */</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(ws->client->ser = ast_tcptls_client_start(</span><br><span style="color: hsl(0, 100%, 40%);">- ast_tcptls_client_create(ws->client->args)))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(ws->client->ser = ast_tcptls_client_start_timeout(</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_tcptls_client_create(ws->client->args), timeout))) {</span><br><span> return WS_CLIENT_START_ERROR;</span><br><span> }</span><br><span> </span><br><span>@@ -1422,6 +1422,13 @@</span><br><span> (const char *uri, const char *protocols, struct ast_tls_config *tls_cfg,</span><br><span> enum ast_websocket_result *result)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+ return ast_websocket_client_create_and_connect(uri, protocols, tls_cfg, result, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_websocket *AST_OPTIONAL_API_NAME(ast_websocket_client_create_and_connect)</span><br><span style="color: hsl(120, 100%, 40%);">+ (const char *uri, const char *protocols, struct ast_tls_config *tls_cfg,</span><br><span style="color: hsl(120, 100%, 40%);">+ enum ast_websocket_result *result, int timeout)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span> struct ast_websocket *ws = websocket_client_create(</span><br><span> uri, protocols, tls_cfg, result);</span><br><span> </span><br><span>@@ -1429,7 +1436,7 @@</span><br><span> return NULL;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if ((*result = websocket_client_connect(ws)) != WS_OK) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if ((*result = websocket_client_connect(ws, timeout)) != WS_OK) {</span><br><span> ao2_ref(ws, -1);</span><br><span> return NULL;</span><br><span> }</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/17867">change 17867</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/17867"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I5812f6f28d3d13adbc246517f87af177fa20ee9d </div>
<div style="display:none"> Gerrit-Change-Number: 17867 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>