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:
parent
636acb1be1
commit
ab493ef6d8
@ -10,7 +10,6 @@
|
||||
HttpClient::HttpClient(const char *address, uint16_t port, uint32_t timeout) : WiFiClient(), _pAddress(address), _port(port)
|
||||
{
|
||||
setTimeout(timeout);
|
||||
connectByHostOrIp();
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
if(_resource != NULL)free(_resource);
|
||||
if(connected())
|
||||
stop();
|
||||
if(_resource != NULL)
|
||||
free(_resource);
|
||||
}
|
||||
|
||||
boolean HttpClient::connectByHostOrIp()
|
||||
@ -53,6 +55,17 @@ boolean HttpClient::connectByHostOrIp()
|
||||
#ifdef DEBUG_HTTP_CLIENT
|
||||
Serial.printf("About to connect\n");
|
||||
#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))
|
||||
{
|
||||
_isIp = true;
|
||||
@ -93,12 +106,15 @@ int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHel
|
||||
//We reset this two flags
|
||||
_httpCode = HTTP_CODE::UNDEFINED_CODE;
|
||||
_httpCodeParsed = false;
|
||||
|
||||
|
||||
#ifdef DEBUG_HTTP_CLIENT
|
||||
if(_keepAlive)
|
||||
Serial.printf("Link status : %d\n", status());
|
||||
#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(_retries == _maxRetries) return -__LINE__;
|
||||
@ -111,9 +127,9 @@ int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHel
|
||||
|
||||
#ifdef DEBUG_HTTP_CLIENT
|
||||
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
|
||||
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
|
||||
|
||||
if(!connectByHostOrIp())
|
||||
@ -131,7 +147,7 @@ int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHel
|
||||
if(connected())
|
||||
{
|
||||
#ifdef DEBUG_HTTP_CLIENT
|
||||
Serial.printf("Server is listening\n", status());
|
||||
Serial.printf("Server is listening\n");
|
||||
#endif
|
||||
|
||||
switch(method)
|
||||
@ -142,6 +158,7 @@ int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHel
|
||||
//Here we send the parameters
|
||||
sendUriWithGetParams(getData);
|
||||
sendHeader(HttpMIMEType::UNKNOWN_MIME, 0, headerData);
|
||||
flush(); //We force the send of the request to prevent any timeout on reception !
|
||||
break;
|
||||
case HttpRequestMethod::POST:
|
||||
//It is necessary to compute the content length
|
||||
@ -150,17 +167,18 @@ int HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHel
|
||||
sendUriWithGetParams(getData);
|
||||
sendHeader(HttpMIMEType::APPLICATION_X_WWW_FORM_URLENCODED, computeBodyLength(postData), headerData);
|
||||
sendPostData(postData);
|
||||
flush(); //We force the send of the request to prevent any timeout on reception !
|
||||
break;
|
||||
default:
|
||||
#ifdef DEBUG_HTTP_CLIENT
|
||||
Serial.printf("Http verb unspecified\n", status());
|
||||
Serial.printf("Http verb unspecified\n");
|
||||
#endif
|
||||
if(!_keepAlive)stop();
|
||||
return -__LINE__;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -274,7 +292,7 @@ HttpClient::HTTP_CODE HttpClient::isReplyAvailable(uint16_t timeout)
|
||||
#ifdef DEBUG_HTTP_CLIENT
|
||||
safeSize = available() > 99 ? 99 : available();
|
||||
buffer[peekBytes((uint8_t*)buffer,safeSize)] = '\0';
|
||||
Serial.printf("Body chunk is : %s\n",buffer);
|
||||
Serial.printf("Body chunk is : %s\n", buffer);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
@ -285,7 +303,7 @@ HttpClient::HTTP_CODE HttpClient::isReplyAvailable(uint16_t timeout)
|
||||
}
|
||||
|
||||
#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
|
||||
|
||||
return _httpCode;
|
||||
@ -408,5 +426,20 @@ uint64_t HttpClient::computeBodyLength(Dictionary<DictionaryHelper::StringEntity
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -37,7 +37,10 @@ class HttpClient : public WiFiClient, public HttpConstants
|
||||
Dictionary<DictionaryHelper::StringEntity> *headerData = NULL);
|
||||
void keepAlive(boolean enabled);
|
||||
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);
|
||||
|
||||
uint16_t readHttpBody(uint8_t *buffer, uint32_t size);
|
||||
|
Loading…
Reference in New Issue
Block a user