Skip to content

Commit 490cb87

Browse files
committed
Merge remote-tracking branch 'origin/master' into response_queue
2 parents d59a712 + 5d1f89a commit 490cb87

3 files changed

Lines changed: 28 additions & 22 deletions

File tree

src/ESPAsyncWebServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class AsyncWebServerRequest {
254254
AsyncWebServerResponse *beginResponse(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback=nullptr);
255255
AsyncWebServerResponse *beginResponse(const String& contentType, size_t len, AwsResponseFiller callback, AwsTemplateProcessor templateCallback=nullptr);
256256
AsyncWebServerResponse *beginChunkedResponse(const String& contentType, AwsResponseFiller callback, AwsTemplateProcessor templateCallback=nullptr);
257-
AsyncResponseStream *beginResponseStream(const String& contentType, size_t bufferSize=1460);
257+
AsyncResponseStream *beginResponseStream(const String& contentType, size_t bufferSize=TCP_MSS);
258258
AsyncWebServerResponse *beginResponse_P(int code, const String& contentType, const uint8_t * content, size_t len, AwsTemplateProcessor callback=nullptr);
259259
AsyncWebServerResponse *beginResponse_P(int code, const String& contentType, PGM_P content, AwsTemplateProcessor callback=nullptr);
260260

src/WebResponseImpl.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
#ifdef Arduino_h
2525
// arduino is not compatible with std::vector
26+
// It is possible to restore these defines, but one can use _min and _max instead. Or std::min, std::max.
2627
#undef min
2728
#undef max
2829
#endif
2930
#include "DynamicBuffer.h"
30-
// It is possible to restore these defines, but one can use _min and _max instead. Or std::min, std::max.
3131

3232
class AsyncBasicResponse: public AsyncWebServerResponse {
3333
private:
@@ -42,10 +42,6 @@ class AsyncBasicResponse: public AsyncWebServerResponse {
4242
class AsyncAbstractResponse: public AsyncWebServerResponse {
4343
private:
4444
String _head;
45-
// Data is inserted into cache at begin().
46-
// This is inefficient with vector, but if we use some other container,
47-
// we won't be able to access it as contiguous array of bytes when reading from it,
48-
// so by gaining performance in one place, we'll lose it in another.
4945
Walkable<DynamicBuffer> _packet, _cache;
5046
size_t _readDataFromCacheOrContent(uint8_t* data, const size_t len);
5147
size_t _fillBufferAndProcessTemplates(uint8_t* buf, size_t maxLen);
@@ -118,13 +114,13 @@ class AsyncProgmemResponse: public AsyncAbstractResponse {
118114
virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override;
119115
};
120116

121-
class cbuf;
122-
123117
class AsyncResponseStream: public AsyncAbstractResponse, public Print {
124118
private:
125-
cbuf *_content;
119+
DynamicBufferList _content;
120+
DynamicBufferListPrint _print;
121+
size_t _offset;
126122
public:
127-
AsyncResponseStream(const String& contentType, size_t bufferSize);
123+
AsyncResponseStream(const String& contentType, size_t bufferSize=TCP_MSS);
128124
~AsyncResponseStream();
129125
bool _sourceValid() const { return (_state < RESPONSE_END); }
130126
virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override;

src/WebResponses.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -730,32 +730,42 @@ size_t AsyncProgmemResponse::_fillBuffer(uint8_t *data, size_t len){
730730
* Response Stream (You can print/write/printf to it, up to the contentLen bytes)
731731
* */
732732

733-
AsyncResponseStream::AsyncResponseStream(const String& contentType, size_t bufferSize){
733+
AsyncResponseStream::AsyncResponseStream(const String& contentType, size_t bufferSize) : _print(_content, bufferSize), _offset(0) {
734734
_code = 200;
735735
_contentLength = 0;
736736
_contentType = contentType;
737-
_content = new cbuf(bufferSize);
738737
}
739738

740739
AsyncResponseStream::~AsyncResponseStream(){
741-
delete _content;
740+
;
742741
}
743742

744743
size_t AsyncResponseStream::_fillBuffer(uint8_t *buf, size_t maxLen){
745-
return _content->read((char*)buf, maxLen);
744+
size_t read = 0;
745+
while((maxLen > 0) && !_content.empty()) {
746+
auto& dbuf = _content.front();
747+
auto to_read = std::min(dbuf.size() - _offset, maxLen);
748+
memcpy(buf, dbuf.data() + _offset, to_read);
749+
buf += to_read;
750+
maxLen -= to_read;
751+
read += to_read;
752+
_offset += to_read;
753+
if (_offset == dbuf.size()) {
754+
_content.pop_front();
755+
_offset = 0;
756+
}
757+
}
758+
759+
return read;
746760
}
747761

748762
size_t AsyncResponseStream::write(const uint8_t *data, size_t len){
749763
if(_started())
750764
return 0;
751-
752-
if(len > _content->room()){
753-
size_t needed = len - _content->room();
754-
_content->resizeAdd(needed);
755-
}
756-
size_t written = _content->write((const char*)data, len);
757-
_contentLength += written;
758-
return written;
765+
766+
auto size = _print.write(data, len);
767+
_contentLength += size;
768+
return size;
759769
}
760770

761771
size_t AsyncResponseStream::write(uint8_t data){

0 commit comments

Comments
 (0)