Skip to content

Commit

Permalink
Merge pull request #17 from Aircoookie/improve-stream-response
Browse files Browse the repository at this point in the history
Improve AsyncResponseStream
  • Loading branch information
willmmiles authored Sep 14, 2024
2 parents bbe8bdb + 2a96c83 commit 5d1f89a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/ESPAsyncWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class AsyncWebServerRequest {
AsyncWebServerResponse *beginResponse(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback=nullptr);
AsyncWebServerResponse *beginResponse(const String& contentType, size_t len, AwsResponseFiller callback, AwsTemplateProcessor templateCallback=nullptr);
AsyncWebServerResponse *beginChunkedResponse(const String& contentType, AwsResponseFiller callback, AwsTemplateProcessor templateCallback=nullptr);
AsyncResponseStream *beginResponseStream(const String& contentType, size_t bufferSize=1460);
AsyncResponseStream *beginResponseStream(const String& contentType, size_t bufferSize=TCP_MSS);
AsyncWebServerResponse *beginResponse_P(int code, const String& contentType, const uint8_t * content, size_t len, AwsTemplateProcessor callback=nullptr);
AsyncWebServerResponse *beginResponse_P(int code, const String& contentType, PGM_P content, AwsTemplateProcessor callback=nullptr);

Expand Down
10 changes: 6 additions & 4 deletions src/WebResponseImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#endif
#include <vector>
#include "default_init_allocator.h"
#include "DynamicBuffer.h"

// It is possible to restore these defines, but one can use _min and _max instead. Or std::min, std::max.

class AsyncBasicResponse: public AsyncWebServerResponse {
Expand Down Expand Up @@ -119,13 +121,13 @@ class AsyncProgmemResponse: public AsyncAbstractResponse {
virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override;
};

class cbuf;

class AsyncResponseStream: public AsyncAbstractResponse, public Print {
private:
cbuf *_content;
DynamicBufferList _content;
DynamicBufferListPrint _print;
size_t _offset;
public:
AsyncResponseStream(const String& contentType, size_t bufferSize);
AsyncResponseStream(const String& contentType, size_t bufferSize=TCP_MSS);
~AsyncResponseStream();
bool _sourceValid() const { return (_state < RESPONSE_END); }
virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override;
Expand Down
34 changes: 22 additions & 12 deletions src/WebResponses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,32 +714,42 @@ size_t AsyncProgmemResponse::_fillBuffer(uint8_t *data, size_t len){
* Response Stream (You can print/write/printf to it, up to the contentLen bytes)
* */

AsyncResponseStream::AsyncResponseStream(const String& contentType, size_t bufferSize){
AsyncResponseStream::AsyncResponseStream(const String& contentType, size_t bufferSize) : _print(_content, bufferSize), _offset(0) {
_code = 200;
_contentLength = 0;
_contentType = contentType;
_content = new cbuf(bufferSize);
}

AsyncResponseStream::~AsyncResponseStream(){
delete _content;
;
}

size_t AsyncResponseStream::_fillBuffer(uint8_t *buf, size_t maxLen){
return _content->read((char*)buf, maxLen);
size_t read = 0;
while((maxLen > 0) && !_content.empty()) {
auto& dbuf = _content.front();
auto to_read = std::min(dbuf.size() - _offset, maxLen);
memcpy(buf, dbuf.data() + _offset, to_read);
buf += to_read;
maxLen -= to_read;
read += to_read;
_offset += to_read;
if (_offset == dbuf.size()) {
_content.pop_front();
_offset = 0;
}
}

return read;
}

size_t AsyncResponseStream::write(const uint8_t *data, size_t len){
if(_started())
return 0;

if(len > _content->room()){
size_t needed = len - _content->room();
_content->resizeAdd(needed);
}
size_t written = _content->write((const char*)data, len);
_contentLength += written;
return written;

auto size = _print.write(data, len);
_contentLength += size;
return size;
}

size_t AsyncResponseStream::write(uint8_t data){
Expand Down

0 comments on commit 5d1f89a

Please sign in to comment.