Added the max retries option

This commit is contained in:
anschrammh 2020-01-07 12:22:29 +01:00
parent e74cf22c71
commit 488d442df3
2 changed files with 20 additions and 6 deletions

View File

@ -7,7 +7,7 @@
* End of HttpClientHelper
*/
HttpClient::HttpClient() : WiFiClient(), _connectionStatus(NO_ATTEMPT), _resource(NULL), _address(NULL), _port(0), _keepAlive(false), _maxRetries(5), _retries(0), _httpCode(HTTP_CODE::UNDEFINED_CODE), _bodyReadyToRead(false)
HttpClient::HttpClient() : WiFiClient(), _connectionStatus(NO_ATTEMPT), _resource(NULL), _address(NULL), _port(0), _keepAlive(false), _maxRetries(-1), _retries(0), _httpCode(HTTP_CODE::UNDEFINED_CODE), _bodyReadyToRead(false)
{
}
@ -106,22 +106,30 @@ boolean HttpClient::sendHttpQuery(const char *resource, HttpRequestMethod method
boolean HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHelper::StringEntity> *getData, Dictionary<DictionaryHelper::StringEntity> *postData)
{
//We reset this two flags
_httpCode = HTTP_CODE::UNDEFINED_CODE;
_bodyReadyToRead = false;
#ifdef DEBUG_HTTP_CLIENT
Serial.printf("Link status : %d\n", status());
if(_keepAlive)
Serial.printf("Link status : %d\n", status());
#endif
if(!connected() || _connectionStatus == FAILED)
{
if(_retries == _maxRetries) return false;
if(_connectionStatus == FAILED)
{
stop();
_retries++;
if(_maxRetries != -1)_retries++;
}
#ifdef DEBUG_HTTP_CLIENT
Serial.printf("Link broken, we try to reconnect : addr : %s port %u\nretries : %u\n", _address, _port, _retries);
if(_keepAlive)
Serial.printf("Link broken, we try to reconnect : addr : %s port %u\nretries : %u\n", _address, _port, _retries);
else
Serial.printf("We start a new connection : %s port %u\nretries : %u\n", _address, _port, _retries);
#endif
connectByHostOrIp();
@ -333,3 +341,8 @@ uint64_t HttpClient::computeBodyLength(Dictionary<DictionaryHelper::StringEntity
return length;
}
void HttpClient::setMaxRetries(int16_t retries)
{
_maxRetries = retries;
}

View File

@ -25,6 +25,7 @@ class HttpClient : public WiFiClient, public HttpConstants
boolean sendHttpQuery(const char *ressource, HttpRequestMethod method = HttpRequestMethod::GET, Dictionary<DictionaryHelper::StringEntity> *getData = NULL, Dictionary<DictionaryHelper::StringEntity> *postData = NULL);
boolean sendHttpQuery(HttpRequestMethod method = HttpRequestMethod::GET, Dictionary<DictionaryHelper::StringEntity> *getData = NULL, Dictionary<DictionaryHelper::StringEntity> *postData = NULL);
void keepAlive(boolean enabled);
void setMaxRetries(int16_t retries);
HTTP_CODE isReplyAvailable();
@ -42,8 +43,8 @@ class HttpClient : public WiFiClient, public HttpConstants
char *_address;
uint16_t _port;
boolean _keepAlive;
uint8_t _maxRetries;
uint8_t _retries;
int16_t _maxRetries;
uint16_t _retries;
HTTP_CODE _httpCode;
boolean _bodyReadyToRead;
};