Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit a7c4dfb

Browse files
nut-code-monkeyme-no-dev
authored andcommitted
Template container instead of direct pointer manipulation. (#95)
* using Container instead ofdirect pointers list manipulation * fixed different type of virtual methods. * Fixed typo in conditional * Renamed ListArray to LinkedList * Const reference to String as method parameters to prevent additional copy and memory allocation when String passed by value * fix 'min' redefinition * removed #include <iterator> begin/end methods it's enough for 'for( : )' loop count() renamed to length() spacing fixing * Const reference to String as method parameters to prevent additional copy and memory allocation when String passed by value * Fixed unused params warnings
1 parent 3e6e890 commit a7c4dfb

17 files changed

+481
-662
lines changed

examples/ESP_AsyncFSBrowser/ESP_AsyncFSBrowser.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void setup(){
189189

190190
request->send(404);
191191
});
192-
server.onFileUpload([](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
192+
server.onFileUpload([](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final){
193193
if(!index)
194194
Serial.printf("UploadStart: %s\n", filename.c_str());
195195
Serial.printf("%s", (const char*)data);

src/AsyncEventSource.cpp

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ AsyncEventSourceClient::AsyncEventSourceClient(AsyncWebServerRequest *request, A
110110
_client = request->client();
111111
_server = server;
112112
_lastId = 0;
113-
next = NULL;
114113
if(request->hasHeader("Last-Event-ID"))
115114
_lastId = atoi(request->getHeader("Last-Event-ID")->value().c_str());
116115

@@ -119,7 +118,7 @@ AsyncEventSourceClient::AsyncEventSourceClient(AsyncWebServerRequest *request, A
119118
_client->onAck(NULL, NULL);
120119
_client->onPoll(NULL, NULL);
121120
_client->onData(NULL, NULL);
122-
_client->onTimeout([](void *r, AsyncClient* c, uint32_t time){ ((AsyncEventSourceClient*)(r))->_onTimeout(time); }, this);
121+
_client->onTimeout([](void *r, AsyncClient* c __attribute__((unused)), uint32_t time){ ((AsyncEventSourceClient*)(r))->_onTimeout(time); }, this);
123122
_client->onDisconnect([](void *r, AsyncClient* c){ ((AsyncEventSourceClient*)(r))->_onDisconnect(); delete c; }, this);
124123
_server->_addClient(this);
125124
delete request;
@@ -129,7 +128,7 @@ AsyncEventSourceClient::~AsyncEventSourceClient(){
129128
close();
130129
}
131130

132-
void AsyncEventSourceClient::_onTimeout(uint32_t time){
131+
void AsyncEventSourceClient::_onTimeout(uint32_t time __attribute__((unused))){
133132
_client->close(true);
134133
}
135134

@@ -161,9 +160,9 @@ void AsyncEventSourceClient::send(const char *message, const char *event, uint32
161160

162161
// Handler
163162

164-
AsyncEventSource::AsyncEventSource(String url)
163+
AsyncEventSource::AsyncEventSource(const String& url)
165164
: _url(url)
166-
, _clients(NULL)
165+
, _clients(LinkedList<AsyncEventSourceClient *>([](AsyncEventSourceClient *c){ delete c; }))
167166
, _connectcb(NULL)
168167
{}
169168

@@ -188,68 +187,38 @@ void AsyncEventSource::_addClient(AsyncEventSourceClient * client){
188187
client->write((const char *)temp, 2053);
189188
free(temp);
190189
}*/
191-
if(_clients == NULL){
192-
_clients = client;
193-
if(_connectcb)
194-
_connectcb(client);
195-
return;
196-
}
197-
AsyncEventSourceClient * c = _clients;
198-
while(c->next != NULL) c = c->next;
199-
c->next = client;
190+
191+
_clients.add(client);
200192
if(_connectcb)
201193
_connectcb(client);
202194
}
203195

204196
void AsyncEventSource::_handleDisconnect(AsyncEventSourceClient * client){
205-
if(_clients == NULL){
206-
return;
207-
}
208-
if(_clients == client){
209-
_clients = client->next;
210-
delete client;
211-
return;
212-
}
213-
AsyncEventSourceClient * c = _clients;
214-
while(c->next != NULL && c->next != client) c = c->next;
215-
if(c->next == NULL){
216-
return;
217-
}
218-
c->next = client->next;
219-
delete client;
197+
_clients.remove(client);
220198
}
221199

222200
void AsyncEventSource::close(){
223-
AsyncEventSourceClient * c = _clients;
224-
while(c != NULL){
201+
for(const auto &c: _clients){
225202
if(c->connected())
226203
c->close();
227-
c = c->next;
228204
}
229205
}
230206

231207
void AsyncEventSource::send(const char *message, const char *event, uint32_t id, uint32_t reconnect){
232-
if(_clients == NULL)
208+
if(_clients.isEmpty())
233209
return;
234210

235211
String ev = generateEventMessage(message, event, id, reconnect);
236-
AsyncEventSourceClient * c = _clients;
237-
while(c != NULL){
212+
for(const auto &c: _clients){
238213
if(c->connected())
239214
c->write(ev.c_str(), ev.length());
240-
c = c->next;
241215
}
242216
}
243217

244-
size_t AsyncEventSource::count(){
245-
size_t i = 0;
246-
AsyncEventSourceClient * c = _clients;
247-
while(c != NULL){
248-
if(c->connected())
249-
i++;
250-
c = c->next;
251-
}
252-
return i;
218+
size_t AsyncEventSource::count() const {
219+
return _clients.count_if([](AsyncEventSourceClient *c){
220+
return c->connected();
221+
});
253222
}
254223

255224
bool AsyncEventSource::canHandle(AsyncWebServerRequest *request){
@@ -280,7 +249,7 @@ void AsyncEventSourceResponse::_respond(AsyncWebServerRequest *request){
280249
_state = RESPONSE_WAIT_ACK;
281250
}
282251

283-
size_t AsyncEventSourceResponse::_ack(AsyncWebServerRequest *request, size_t len, uint32_t time){
252+
size_t AsyncEventSourceResponse::_ack(AsyncWebServerRequest *request, size_t len, uint32_t time __attribute__((unused))){
284253
if(len){
285254
new AsyncEventSourceClient(request, _server);
286255
}

src/AsyncEventSource.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class AsyncEventSourceClient {
3636
uint32_t _lastId;
3737

3838
public:
39-
AsyncEventSourceClient * next;
4039

4140
AsyncEventSourceClient(AsyncWebServerRequest *request, AsyncEventSource *server);
4241
~AsyncEventSourceClient();
@@ -45,8 +44,8 @@ class AsyncEventSourceClient {
4544
void close();
4645
void write(const char * message, size_t len);
4746
void send(const char *message, const char *event=NULL, uint32_t id=0, uint32_t reconnect=0);
48-
bool connected(){ return (_client != NULL) && _client->connected(); }
49-
uint32_t lastId(){ return _lastId; }
47+
bool connected() const { return (_client != NULL) && _client->connected(); }
48+
uint32_t lastId() const { return _lastId; }
5049

5150
//system callbacks (do not call)
5251
void _onTimeout(uint32_t time);
@@ -56,23 +55,23 @@ class AsyncEventSourceClient {
5655
class AsyncEventSource: public AsyncWebHandler {
5756
private:
5857
String _url;
59-
AsyncEventSourceClient * _clients;
58+
LinkedList<AsyncEventSourceClient *> _clients;
6059
ArEventHandlerFunction _connectcb;
6160
public:
62-
AsyncEventSource(String url);
61+
AsyncEventSource(const String& url);
6362
~AsyncEventSource();
6463

65-
const char * url(){ return _url.c_str(); }
64+
const char * url() const { return _url.c_str(); }
6665
void close();
6766
void onConnect(ArEventHandlerFunction cb);
6867
void send(const char *message, const char *event=NULL, uint32_t id=0, uint32_t reconnect=0);
69-
size_t count(); //number clinets connected
68+
size_t count() const; //number clinets connected
7069

7170
//system callbacks (do not call)
7271
void _addClient(AsyncEventSourceClient * client);
7372
void _handleDisconnect(AsyncEventSourceClient * client);
74-
bool canHandle(AsyncWebServerRequest *request);
75-
void handleRequest(AsyncWebServerRequest *request);
73+
virtual bool canHandle(AsyncWebServerRequest *request) override final;
74+
virtual void handleRequest(AsyncWebServerRequest *request) override final;
7675
};
7776

7877
class AsyncEventSourceResponse: public AsyncWebServerResponse {

src/AsyncJson.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class AsyncJsonResponse: public AsyncAbstractResponse {
6969
size_t setLength() {
7070
_contentLength = _root.measureLength();
7171
if (_contentLength) { _isValid = true; }
72-
return _contentLength;
72+
return _contentLength;
7373
}
7474

7575
size_t _fillBuffer(uint8_t *data, size_t len){

0 commit comments

Comments
 (0)