From dd649808d174a88063846d6bdabb6cbc1a593c77 Mon Sep 17 00:00:00 2001 From: boarchuz <46267286+boarchuz@users.noreply.github.com> Date: Wed, 10 Apr 2019 05:19:25 +1000 Subject: [PATCH] Add server connect timeout to HTTPClient (#2606) --- libraries/HTTPClient/src/HTTPClient.cpp | 13 +++++++++++-- libraries/HTTPClient/src/HTTPClient.h | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp index 3005d55e..fcddf9b1 100644 --- a/libraries/HTTPClient/src/HTTPClient.cpp +++ b/libraries/HTTPClient/src/HTTPClient.cpp @@ -428,6 +428,15 @@ void HTTPClient::setAuthorization(const char * auth) } } +/** + * set the timeout (ms) for establishing a connection to the server + * @param connectTimeout int32_t + */ +void HTTPClient::setConnectTimeout(int32_t connectTimeout) +{ + _connectTimeout = connectTimeout; +} + /** * set the timeout for the TCP connection * @param timeout unsigned int @@ -442,7 +451,7 @@ void HTTPClient::setTimeout(uint16_t timeout) /** * use HTTP1.0 - * @param timeout + * @param use */ void HTTPClient::useHTTP10(bool useHTTP10) { @@ -966,7 +975,7 @@ bool HTTPClient::connect(void) return false; } - if(!_client->connect(_host.c_str(), _port)) { + if(!_client->connect(_host.c_str(), _port, _connectTimeout)) { log_d("failed connect to %s:%u", _host.c_str(), _port); return false; } diff --git a/libraries/HTTPClient/src/HTTPClient.h b/libraries/HTTPClient/src/HTTPClient.h index fc6773aa..88d67a05 100644 --- a/libraries/HTTPClient/src/HTTPClient.h +++ b/libraries/HTTPClient/src/HTTPClient.h @@ -153,6 +153,7 @@ public: void setUserAgent(const String& userAgent); void setAuthorization(const char * user, const char * password); void setAuthorization(const char * auth); + void setConnectTimeout(int32_t connectTimeout); void setTimeout(uint16_t timeout); void useHTTP10(bool usehttp10 = true); @@ -213,6 +214,7 @@ protected: /// request handling String _host; uint16_t _port = 0; + int32_t _connectTimeout = -1; bool _reuse = false; uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT; bool _useHTTP10 = false;