Did some cleaning, added the flush statement to make sure the request is sent when needed and added some methodes to retrieve and reset the connection retry count

This commit is contained in:
anschrammh 2022-09-27 07:24:22 +02:00
parent 636acb1be1
commit ab493ef6d8
2 changed files with 48 additions and 12 deletions

View File

@ -10,7 +10,6 @@
HttpClient::HttpClient(const char *address, uint16_t port, uint32_t timeout) : WiFiClient(), _pAddress(address), _port(port) HttpClient::HttpClient(const char *address, uint16_t port, uint32_t timeout) : WiFiClient(), _pAddress(address), _port(port)
{ {
setTimeout(timeout); setTimeout(timeout);
connectByHostOrIp();
} }
HttpClient::HttpClient(const char *address, const char *resource, uint16_t port, uint32_t timeout) : HttpClient(address, port, timeout) HttpClient::HttpClient(const char *address, const char *resource, uint16_t port, uint32_t timeout) : HttpClient(address, port, timeout)
@ -44,7 +43,10 @@ HttpClient::HttpClient(const HttpClient &object) : WiFiClient()
HttpClient::~HttpClient() HttpClient::~HttpClient()
{ {
if(_resource != NULL)free(_resource); if(connected())
stop();
if(_resource != NULL)
free(_resource);
} }
boolean HttpClient::connectByHostOrIp() boolean HttpClient::connectByHostOrIp()
@ -53,6 +55,17 @@ boolean HttpClient::connectByHostOrIp()
#ifdef DEBUG_HTTP_CLIENT #ifdef DEBUG_HTTP_CLIENT
Serial.printf("About to connect\n"); Serial.printf("About to connect\n");
#endif #endif
//If we constructed the HttpClient with a NULL server address, we don't go further.
if(!_pAddress)
{
#ifdef DEBUG_HTTP_CLIENT
Serial.printf("IP Address is NULL !\n");
#endif
_connectionStatus = FAILED;
return false;
}
if(ipAddress.fromString(_pAddress)) if(ipAddress.fromString(_pAddress))
{ {
_isIp = true; _isIp = true;
@ -93,12 +106,15 @@ int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHel
//We reset this two flags //We reset this two flags
_httpCode = HTTP_CODE::UNDEFINED_CODE; _httpCode = HTTP_CODE::UNDEFINED_CODE;
_httpCodeParsed = false; _httpCodeParsed = false;
#ifdef DEBUG_HTTP_CLIENT #ifdef DEBUG_HTTP_CLIENT
if(_keepAlive) if(_keepAlive)
Serial.printf("Link status : %d\n", status()); Serial.printf("Link status : %d\n", status());
#endif #endif
//If we did not want to keep the connection alive and it is still open, then we first close it.
if(!_keepAlive && connected())stop();
if(!connected() || _connectionStatus == FAILED) if(!connected() || _connectionStatus == FAILED)
{ {
if(_retries == _maxRetries) return -__LINE__; if(_retries == _maxRetries) return -__LINE__;
@ -111,9 +127,9 @@ int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHel
#ifdef DEBUG_HTTP_CLIENT #ifdef DEBUG_HTTP_CLIENT
if(_keepAlive) if(_keepAlive)
Serial.printf("Link broken, we try to reconnect : addr : %s port %u\nretries : %u\n", _pAddress, _port, _retries); Serial.printf("Link broken, we try to reconnect : addr : %s port %u\nretries : %u\n", _pAddress ? _pAddress : "NULL", _port, _retries);
else else
Serial.printf("We start a new connection : %s port %u\nretries : %u\n", _pAddress, _port, _retries); Serial.printf("We start a new connection : %s port %u\nretries : %u\n", _pAddress ? _pAddress : "NULL", _port, _retries);
#endif #endif
if(!connectByHostOrIp()) if(!connectByHostOrIp())
@ -131,7 +147,7 @@ int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHel
if(connected()) if(connected())
{ {
#ifdef DEBUG_HTTP_CLIENT #ifdef DEBUG_HTTP_CLIENT
Serial.printf("Server is listening\n", status()); Serial.printf("Server is listening\n");
#endif #endif
switch(method) switch(method)
@ -142,6 +158,7 @@ int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHel
//Here we send the parameters //Here we send the parameters
sendUriWithGetParams(getData); sendUriWithGetParams(getData);
sendHeader(HttpMIMEType::UNKNOWN_MIME, 0, headerData); sendHeader(HttpMIMEType::UNKNOWN_MIME, 0, headerData);
flush(); //We force the send of the request to prevent any timeout on reception !
break; break;
case HttpRequestMethod::POST: case HttpRequestMethod::POST:
//It is necessary to compute the content length //It is necessary to compute the content length
@ -150,17 +167,18 @@ int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHel
sendUriWithGetParams(getData); sendUriWithGetParams(getData);
sendHeader(HttpMIMEType::APPLICATION_X_WWW_FORM_URLENCODED, computeBodyLength(postData), headerData); sendHeader(HttpMIMEType::APPLICATION_X_WWW_FORM_URLENCODED, computeBodyLength(postData), headerData);
sendPostData(postData); sendPostData(postData);
flush(); //We force the send of the request to prevent any timeout on reception !
break; break;
default: default:
#ifdef DEBUG_HTTP_CLIENT #ifdef DEBUG_HTTP_CLIENT
Serial.printf("Http verb unspecified\n", status()); Serial.printf("Http verb unspecified\n");
#endif #endif
if(!_keepAlive)stop(); if(!_keepAlive)stop();
return -__LINE__; return -__LINE__;
break; break;
} }
} }
return 0; return 0;
} }
@ -274,7 +292,7 @@ HttpClient::HTTP_CODE HttpClient::isReplyAvailable(uint16_t timeout)
#ifdef DEBUG_HTTP_CLIENT #ifdef DEBUG_HTTP_CLIENT
safeSize = available() > 99 ? 99 : available(); safeSize = available() > 99 ? 99 : available();
buffer[peekBytes((uint8_t*)buffer,safeSize)] = '\0'; buffer[peekBytes((uint8_t*)buffer,safeSize)] = '\0';
Serial.printf("Body chunk is : %s\n",buffer); Serial.printf("Body chunk is : %s\n", buffer);
#endif #endif
break; break;
} }
@ -285,7 +303,7 @@ HttpClient::HTTP_CODE HttpClient::isReplyAvailable(uint16_t timeout)
} }
#ifdef DEBUG_HTTP_CLIENT #ifdef DEBUG_HTTP_CLIENT
Serial.println("\nAfter timeout or all data is received"); Serial.printf("\nAfter timeout or all data is received, HTTP_CODE is : %d\n", _httpCode);
#endif #endif
return _httpCode; return _httpCode;
@ -408,5 +426,20 @@ uint64_t HttpClient::computeBodyLength(Dictionary<DictionaryHelper::StringEntity
void HttpClient::setMaxRetries(int16_t retries) void HttpClient::setMaxRetries(int16_t retries)
{ {
_maxRetries = retries; _maxRetries = retries < 0 ? -1 : retries;
}
int16_t HttpClient::getMaxRetries(void) const
{
return _maxRetries;
}
uint16_t HttpClient::retriesCount(void) const
{
return _retries;
}
void HttpClient::resetRetriesCount(void)
{
_retries = 0;
} }

View File

@ -37,7 +37,10 @@ class HttpClient : public WiFiClient, public HttpConstants
Dictionary<DictionaryHelper::StringEntity> *headerData = NULL); Dictionary<DictionaryHelper::StringEntity> *headerData = NULL);
void keepAlive(boolean enabled); void keepAlive(boolean enabled);
void setMaxRetries(int16_t retries); void setMaxRetries(int16_t retries);
//100 ms is the default timeout int16_t getMaxRetries(void) const;
uint16_t retriesCount(void) const;
void resetRetriesCount(void);
//10s is the default timeout
HTTP_CODE isReplyAvailable(uint16_t timeout = 10000); HTTP_CODE isReplyAvailable(uint16_t timeout = 10000);
uint16_t readHttpBody(uint8_t *buffer, uint32_t size); uint16_t readHttpBody(uint8_t *buffer, uint32_t size);