Add timeout and HTTP size to streamer EOF checks

HTTPStreamer assumed the server would drop the connection when no more
data was available to be transferred from the source file.
For individual files, though, the web server will send the expected length
in the headers and leave the connection open (i.e. to recycle the connection
and accelerate web browsing).  In this case, the streamer would effectively
hang waiting for more data than was being transmitted.

Now check the read data against the expected data, and also add a 0.5s
timeout no matter what to the reading.

Tested on streaming and served files, seems to work fine.
This commit is contained in:
Earle F. Philhower, III committed 2017-10-22 10:04:19 -07:00
1 parent 2dac08ef4e
commit c5da8c3062
2 files changed
+11 -3

No files matched your search

+9 -2
View File
@@ -40,6 +40,7 @@ bool AudioFileSourceHTTPStream::open(const char *url)
http.end();
return false;
}
size = http.getSize();
return true;
}
@@ -51,9 +52,15 @@ AudioFileSourceHTTPStream::~AudioFileSourceHTTPStream()
uint32_t AudioFileSourceHTTPStream::read(void *data, uint32_t len)
{
if (!http.connected()) return 0;
if ((size>0) && (pos >= size)) return 0;
WiFiClient *stream = http.getStreamPtr();
while (stream->available() < (int)len) yield();
// Can't read past EOF...
if ( (size > 0) && (len > (uint32_t)(pos - size)) ) len = pos - size;
int start = millis();
while ((stream->available() < (int)len) && (millis() - start < 500)) yield();
size_t avail = stream->available();
if (!avail) return 0;
@@ -84,7 +91,7 @@ bool AudioFileSourceHTTPStream::isOpen()
uint32_t AudioFileSourceHTTPStream::getSize()
{
return 1L<<31;
return size;
}
uint32_t AudioFileSourceHTTPStream::getPos()