Skip to content

Commit

Permalink
Cleanup URL timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Nov 16, 2023
1 parent 7b79b0c commit aa9a5e0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
15 changes: 14 additions & 1 deletion src/AudioHttp/HttpHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class HttpHeader {
}

/// reads the full header from the request (stream)
void read(Client &in) {
bool read(Client &in) {
LOGD("HttpHeader::read");
// remove all existing value
clear();
Expand All @@ -237,12 +237,19 @@ class HttpHeader {
if (in.connected()){
if (in.available()==0) {
int count = 0;
uint32_t timeout = millis() + timeout_ms;
while(in.available()==0){
delay(50);
count++;
if (count==2){
LOGI("Waiting for data...");
}
// If we dont get an answer, we abort
if(millis() > timeout ){
LOGE("Request timed out after %d ms", timeout_ms);
status_code = 401;
return false;
}
}
LOGI("Data availble");
}
Expand All @@ -261,6 +268,7 @@ class HttpHeader {
}
}
}
return true;
}

/// writes the full header to the indicated HttpStreamedMultiOutput stream
Expand Down Expand Up @@ -301,6 +309,10 @@ class HttpHeader {
temp_buffer.resize(0);
}

void setTimeout(int timeoutMs){
timeout_ms = timeoutMs;
}

protected:
int status_code = UNDEFINED;
bool is_written = false;
Expand All @@ -315,6 +327,7 @@ class HttpHeader {
Vector<HttpHeaderLine*> lines;
HttpLineReader reader;
const char* CRLF = "\r\n";
int timeout_ms = URL_CLIENT_TIMEOUT;

char* tempBuffer(){
temp_buffer.resize(HTTP_MAX_LEN);
Expand Down
17 changes: 6 additions & 11 deletions src/AudioHttp/HttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@
#include "AudioHttp/Url.h"
#include "AudioTools/AudioLogger.h"

#ifndef URL_CLIENT_TIMEOUT
#define URL_CLIENT_TIMEOUT 60000
#endif

#ifndef URL_HANDSHAKE_TIMEOUT
#define URL_HANDSHAKE_TIMEOUT 120000
#endif

namespace audio_tools {


Expand Down Expand Up @@ -308,8 +300,9 @@ class HttpRequest {
http_connect_callback = callback;
}

void setTimeout(int timeout){
clientTimeout = timeout;
/// Defines the client timeout in ms
void setTimeout(int timeoutMs){
clientTimeout = timeoutMs;
}

protected:
Expand All @@ -329,7 +322,9 @@ class HttpRequest {

// opens a connection to the indicated host
virtual int connect(const char *ip, uint16_t port, int32_t timeout) {
client_ptr->setTimeout(timeout);
client_ptr->setTimeout(timeout/1000); // client timeout is in seconds!
request_header.setTimeout(timeout);
reply_header.setTimeout(timeout);
int is_connected = this->client_ptr->connect(ip, port);
LOGI("connected %d timeout %d", is_connected, (int) timeout);
return is_connected;
Expand Down
8 changes: 0 additions & 8 deletions src/AudioHttp/URLStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@
#include "AudioHttp/HttpRequest.h"
#include "AudioHttp/AbstractURLStream.h"

#ifndef URL_CLIENT_TIMEOUT
#define URL_CLIENT_TIMEOUT 60000
#endif

#ifndef URL_HANDSHAKE_TIMEOUT
#define URL_HANDSHAKE_TIMEOUT 120000
#endif


namespace audio_tools {

Expand Down
8 changes: 0 additions & 8 deletions src/AudioTools/AudioStreams.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@
#include "AudioTools/AudioLogger.h"
#include "AudioEffects/SoundGenerator.h"

#ifndef URL_CLIENT_TIMEOUT
# define URL_CLIENT_TIMEOUT 60000
#endif

#ifndef URL_HANDSHAKE_TIMEOUT
# define URL_HANDSHAKE_TIMEOUT 120000
#endif

#ifndef IRAM_ATTR
# define IRAM_ATTR
#endif
Expand Down

0 comments on commit aa9a5e0

Please sign in to comment.