Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit 07f3fcc

Browse files
authored
v1.2.1 to not destroy original CString
#### Releases v1.2.1 1. Don't need `memmove()`, CString no longer destroyed. Check [All memmove() removed - string no longer destroyed #11](khoih-prog/Portenta_H7_AsyncWebServer#11)
1 parent e98fdf0 commit 07f3fcc

File tree

1 file changed

+57
-42
lines changed

1 file changed

+57
-42
lines changed

README.md

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -116,55 +116,72 @@
116116

117117
### Important Note from v1.2.0
118118

119-
The new `v1.2.0` has added a new and powerful feature to permit using `CString` to save heap to send `very large data`.
119+
The new `v1.2.0+` has added a new and powerful feature to permit using `CString` to save heap to send `very large data`.
120120

121-
Check the `marvelleous` PR of **@salasidis** [request->send(200, textPlainStr, jsonChartDataCharStr); - Without using String Class - to save heap #8](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/pull/8) and these new examples
121+
Check the `marvelleous` PRs of **@salasidis** in [Portenta_H7_AsyncWebServer library](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer)
122+
- [request->send(200, textPlainStr, jsonChartDataCharStr); - Without using String Class - to save heap #8](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/pull/8)
123+
- [All memmove() removed - string no longer destroyed #11](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/pull/11)
124+
125+
and these new examples
122126

123127
1. [Async_AdvancedWebServer_MemoryIssues_Send_CString](https://github.com/khoih-prog/AsyncWebServer_RP2040W/tree/main/examples/Async_AdvancedWebServer_MemoryIssues_Send_CString)
124128
2. [Async_AdvancedWebServer_MemoryIssues_SendArduinoString](https://github.com/khoih-prog/AsyncWebServer_RP2040W/tree/main/examples/Async_AdvancedWebServer_MemoryIssues_SendArduinoString)
125129

126-
If using Arduino `String`, to send a buffer around 40 KBytes, the used `Max Heap` is around **75,240 bytes (~2 times)**
130+
If using Arduino String, to send a buffer around 30 KBytes, the used `Max Heap` is around **75,264 bytes**
127131

128-
If using CString, with the same 40 KByte, the used `Max Heap` is around **43,976 bytes (~1 times)**
132+
If using CString in regular memory, with the same 30 KBytes, the used `Max Heap` is around **44,000 bytes, saving around a buffer size (30 KBytes)**
129133

130134
This is very critical in use-cases where sending `very large data` is necessary, without `heap-allocation-error`.
131135

132136

133137
1. The traditional function used to send `Arduino String` is
134138

135-
https://github.com/khoih-prog/AsyncWebServer_RP2040W/blob/9075cb4cce8be687ae4a79d96afc1c46180b3304/src/AsyncWebServer_RP2040W.h#L413
139+
https://github.com/khoih-prog/AsyncWebServer_RP2040W/blob/e98fdf0f1aa79f17071822522cb1a50ce4fdf6f4/src/AsyncWebServer_RP2040W.h#L414
140+
141+
```cpp
142+
void send(int code, const String& contentType = String(), const String& content = String());
143+
```
136144
137145
such as
138146
139147
```cpp
140148
request->send(200, textPlainStr, ArduinoStr);
141149
```
142-
The required HEAP is about **2 times of the String size**
150+
The required additional HEAP is about **3 times of the String size**
143151

144-
2. To use `CString` but don't destroy it after sending. Use function
145152

146-
https://github.com/khoih-prog/AsyncWebServer_RP2040W/blob/9075cb4cce8be687ae4a79d96afc1c46180b3304/src/AsyncWebServer_RP2040W.h#L414
153+
2. To use `CString` with copying while sending. Use function
154+
155+
https://github.com/khoih-prog/AsyncWebServer_RP2040W/blob/e98fdf0f1aa79f17071822522cb1a50ce4fdf6f4/src/AsyncWebServer_RP2040W.h#L415
156+
157+
```cpp
158+
void send(int code, const String& contentType, const char *content, bool nonDetructiveSend = true); // RSMOD
159+
```
147160
148161
such as
149162
150163
```cpp
151164
request->send(200, textPlainStr, cStr);
152165
```
153166

154-
The required HEAP is also about **2 times of the CString size**
167+
The required additional HEAP is also about **2 times of the CString size** because of `unnecessary copies` of the CString in HEAP. Avoid this `unefficient` way.
168+
155169

170+
3. To use `CString` without copying while sending. Use function
156171

157-
3. To use `CString` but destroy it after sending. Use function
172+
https://github.com/khoih-prog/AsyncWebServer_RP2040W/blob/e98fdf0f1aa79f17071822522cb1a50ce4fdf6f4/src/AsyncWebServer_RP2040W.h#L415
158173

159-
https://github.com/khoih-prog/AsyncWebServer_RP2040W/blob/9075cb4cce8be687ae4a79d96afc1c46180b3304/src/AsyncWebServer_RP2040W.h#L414
174+
```cpp
175+
void send(int code, const String& contentType, const char *content, bool nonDetructiveSend = true); // RSMOD
176+
```
160177
161178
such as
162179
163180
```cpp
164181
request->send(200, textPlainStr, cStr, false);
165182
```
166183

167-
The required HEAP is also about **1 times of the CString size**.
184+
The required additional HEAP is about **1 times of the CString size**. This way is the best and **most efficient way** to use by avoiding of `unnecessary copies` of the CString in HEAP
168185

169186

170187

@@ -214,7 +231,7 @@ to apply the better and faster **asynchronous** feature of the **powerful** [ESP
214231
## Prerequisites
215232

216233
1. [`Arduino IDE 1.8.19+` for Arduino](https://github.com/arduino/Arduino). [![GitHub release](https://img.shields.io/github/release/arduino/Arduino.svg)](https://github.com/arduino/Arduino/releases/latest)
217-
2. [`Earle Philhower's arduino-pico core v2.5.4+`](https://github.com/earlephilhower/arduino-pico) for **RASPBERRY_PI_PICO_W with CYW43439 WiFi**, etc. [![GitHub release](https://img.shields.io/github/release/earlephilhower/arduino-pico.svg)](https://github.com/earlephilhower/arduino-pico/releases/latest)
234+
2. [`Earle Philhower's arduino-pico core v2.6.0+`](https://github.com/earlephilhower/arduino-pico) for **RASPBERRY_PI_PICO_W with CYW43439 WiFi**, etc. [![GitHub release](https://img.shields.io/github/release/earlephilhower/arduino-pico.svg)](https://github.com/earlephilhower/arduino-pico/releases/latest)
218235
3. [`AsyncTCP_RP2040W library v1.1.0+`](https://github.com/khoih-prog/AsyncTCP_RP2040W) for RASPBERRY_PI_PICO_W with CYW43439 WiFi. [![GitHub release](https://img.shields.io/github/release/khoih-prog/AsyncTCP_RP2040W.svg)](https://github.com/khoih-prog/AsyncTCP_RP2040W/releases/latest)
219236

220237
---
@@ -245,7 +262,7 @@ The best and easiest way is to use `Arduino Library Manager`. Search for `AsyncW
245262
## Important things to remember
246263

247264
- This is fully asynchronous server and as such does not run on the loop thread.
248-
- You can not use yield() or delay() or any function that uses them inside the callbacks
265+
- You can not use `yield()` or `delay()` or any function that uses them inside the callbacks
249266
- The server is smart enough to know when to close the connection and free resources
250267
- You can not send more than one response to a single request
251268

@@ -1011,7 +1028,7 @@ client->binary((uint8_t*)binary, (size_t)len);
10111028
10121029
### Direct access to web socket message buffer
10131030
1014-
When sending a web socket message using the above methods a buffer is created. Under certain circumstances you might want to manipulate or populate this buffer directly from your application, for example to prevent unnecessary duplications of the data. This example below shows how to create a buffer and print data to it from an ArduinoJson object then send it.
1031+
When sending a `websocket` message using the above methods a buffer is created. Under certain circumstances you might want to manipulate or populate this buffer directly from your application, for example to prevent unnecessary duplications of the data. This example below shows how to create a buffer and print data to it from an `ArduinoJson` object then send it.
10151032
10161033
```cpp
10171034
void sendDataWs(AsyncWebSocketClient * client)
@@ -1044,7 +1061,7 @@ void sendDataWs(AsyncWebSocketClient * client)
10441061

10451062
### Limiting the number of web socket clients
10461063

1047-
Browsers sometimes do not correctly close the websocket connection, even when the close() function is called in javascript. This will eventually exhaust the web server's resources and will cause the server to crash. Periodically calling the cleanClients() function from the main loop() function limits the number of clients by closing the oldest client when the maximum number of clients has been exceeded. This can called be every cycle, however, if you wish to use less power, then calling as infrequently as once per second is sufficient.
1064+
Browsers sometimes do not correctly close the websocket connection, even when the `close()` function is called in javascript. This will eventually exhaust the web server's resources and will cause the server to crash. Periodically calling the `cleanClients()` function from the main `loop()` function limits the number of clients by closing the oldest client when the maximum number of clients has been exceeded. This can called be every cycle, however, if you wish to use less power, then calling as infrequently as once per second is sufficient.
10481065

10491066
```cpp
10501067
void loop(){
@@ -1056,8 +1073,8 @@ void loop(){
10561073

10571074
## Async Event Source Plugin
10581075

1059-
The server includes EventSource (Server-Sent Events) plugin which can be used to send short text events to the browser.
1060-
Difference between EventSource and WebSockets is that EventSource is single direction, text-only protocol.
1076+
The server includes `EventSource` (Server-Sent Events) plugin which can be used to send short text events to the browser.
1077+
Difference between `EventSource` and `WebSockets` is that `EventSource` is single direction, text-only protocol.
10611078

10621079
### Setup Event Source on the server
10631080

@@ -1408,9 +1425,9 @@ void loop()
14081425

14091426
### Adding Default Headers
14101427

1411-
In some cases, such as when working with CORS, or with some sort of custom authentication system,
1428+
In some cases, such as when working with `CORS`, or with some sort of custom authentication system,
14121429
you might need to define a header that should get added to all responses (including static, websocket and EventSource).
1413-
The DefaultHeaders singleton allows you to do this.
1430+
The `DefaultHeaders` singleton allows you to do this.
14141431

14151432
Example:
14161433

@@ -1517,7 +1534,7 @@ Following is the debug terminal when running example [Async_AdvancedWebServer](e
15171534
```
15181535
Start Async_AdvancedWebServer on RASPBERRY_PI_PICO_W with RP2040W CYW43439 WiFi
15191536
AsyncTCP_RP2040W v1.1.0
1520-
AsyncWebServer_RP2040W v1.2.0
1537+
AsyncWebServer_RP2040W v1.2.1
15211538
Connecting to SSID: HueNet1
15221539
SSID: HueNet1
15231540
Local IP Address: 192.168.2.180
@@ -1541,7 +1558,7 @@ Following is debug terminal output when running example [WebClient](examples/Web
15411558
```
15421559
Start WebClient on RASPBERRY_PI_PICO_W with RP2040W CYW43439 WiFi
15431560
AsyncTCP_RP2040W v1.1.0
1544-
AsyncWebServer_RP2040W v1.2.0
1561+
AsyncWebServer_RP2040W v1.2.1
15451562
Connecting to SSID: HueNet1
15461563
SSID: HueNet1
15471564
Local IP Address: 192.168.2.180
@@ -1619,7 +1636,7 @@ Following is debug terminal output when running example [MQTTClient_Auth](exampl
16191636
```
16201637
Start MQTTClient_Auth on RASPBERRY_PI_PICO_W with RP2040W CYW43439 WiFi
16211638
AsyncTCP_RP2040W v1.1.0
1622-
AsyncWebServer_RP2040W v1.2.0
1639+
AsyncWebServer_RP2040W v1.2.1
16231640
Connecting to SSID: HueNet1
16241641
SSID: HueNet1
16251642
Local IP Address: 192.168.2.180
@@ -1641,7 +1658,7 @@ Following is debug terminal output when running example [MQTTClient_Basic](examp
16411658
```
16421659
Start MQTTClient_Basic on RASPBERRY_PI_PICO_W with RP2040W CYW43439 WiFi
16431660
AsyncTCP_RP2040W v1.1.0
1644-
AsyncWebServer_RP2040W v1.2.0
1661+
AsyncWebServer_RP2040W v1.2.1
16451662
Connecting to SSID: HueNet1
16461663
SSID: HueNet1
16471664
Local IP Address: 192.168.2.180
@@ -1663,7 +1680,7 @@ Following is debug terminal output when running example [MQTT_ThingStream](examp
16631680
```
16641681
Start MQTT_ThingStream on RASPBERRY_PI_PICO_W with RP2040W CYW43439 WiFi
16651682
AsyncTCP_RP2040W v1.1.0
1666-
AsyncWebServer_RP2040W v1.2.0
1683+
AsyncWebServer_RP2040W v1.2.1
16671684
Connecting to SSID: HueNet1
16681685
SSID: HueNet1
16691686
Local IP Address: 192.168.2.180
@@ -1691,7 +1708,7 @@ Following is the debug terminal when running example [Async_AdvancedWebServer_Co
16911708
```
16921709
Start Async_AdvancedWebServer_Country on RASPBERRY_PI_PICO_W with RP2040W CYW43439 WiFi
16931710
AsyncTCP_RP2040W v1.1.0
1694-
AsyncWebServer_RP2040W v1.2.0
1711+
AsyncWebServer_RP2040W v1.2.1
16951712
Connecting to SSID: HueNet1
16961713
SSID: HueNet1
16971714
Local IP Address: 192.168.2.180
@@ -1727,7 +1744,7 @@ Following is the debug terminal when running example [Async_AdvancedWebServer_fa
17271744
```
17281745
14:22:06.632 -> Start Async_AdvancedWebServer_favicon on RASPBERRY_PI_PICO_W with RP2040W CYW43439 WiFi
17291746
14:22:06.632 -> AsyncTCP_RP2040W v1.1.0
1730-
14:22:06.632 -> AsyncWebServer_RP2040W v1.2.0
1747+
14:22:06.632 -> AsyncWebServer_RP2040W v1.2.1
17311748
14:22:06.632 -> Connecting to SSID: HueNet1
17321749
14:22:13.328 -> SSID: HueNet1
17331750
14:22:13.328 -> Local IP Address: 192.168.2.180
@@ -1759,27 +1776,25 @@ You can see the `favicon.ico` at the upper left corner
17591776
Following is the debug terminal and screen shot when running example [Async_AdvancedWebServer_MemoryIssues_Send_CString](examples/Async_AdvancedWebServer_MemoryIssues_Send_CString) on RASPBERRY_PI_PICO_W to demonstrate the new and powerful `HEAP-saving` feature
17601777

17611778

1762-
##### Using CString ===> small heap (43,976 bytes)
1779+
##### Using CString ===> small heap (44,000 bytes)
17631780

17641781
```
17651782
Start Async_AdvancedWebServer_MemoryIssues_Send_CString on RASPBERRY_PI_PICO_W with RP2040W CYW43439 WiFi
17661783
AsyncTCP_RP2040W v1.1.0
1767-
AsyncWebServer_RP2040W v1.2.0
1784+
AsyncWebServer_RP2040W v1.2.1
17681785
Connecting to SSID: HueNet1
17691786
SSID: HueNet1
17701787
Local IP Address: 192.168.2.74
17711788
Country code: XX
17721789
HTTP EthernetWebServer is @ IP : 192.168.2.74
17731790
17741791
HEAP DATA - Pre Create Arduino String Cur heap: 193000 Free heap: 150928 Max heap: 42072
1775-
..
1792+
.75264
17761793
HEAP DATA - Pre Send Cur heap: 193000 Free heap: 149176 Max heap: 43824
17771794
1778-
HEAP DATA - Post Send Cur heap: 193000 Free heap: 149048 Max heap: 43952
1779-
1780-
HEAP DATA - Post Send Cur heap: 193000 Free heap: 149032 Max heap: 43968
1781-
........ .
1782-
HEAP DATA - Post Send Cur heap: 193000 Free heap: 149024 Max heap: 43976
1795+
HEAP DATA - Post Send Cur heap: 193000 Free heap: 149016 Max heap: 43984
1796+
.
1797+
HEAP DATA - Post Send Cur heap: 193000 Free heap: 149000 Max heap: 44000
17831798
.......... .......... .......... ........
17841799
Out String Length=31247
17851800
.. .......... .......... .......... ..........
@@ -1788,12 +1803,12 @@ Out String Length=31247
17881803
While using Arduino String, the HEAP usage is very large
17891804

17901805

1791-
#### Async_AdvancedWebServer_MemoryIssues_SendArduinoString ===> very large heap (75,240 bytes)
1806+
#### Async_AdvancedWebServer_MemoryIssues_SendArduinoString ===> very large heap (75,264 bytes)
17921807

17931808
```
17941809
Start Async_AdvancedWebServer_MemoryIssues_SendArduinoString on RASPBERRY_PI_PICO_W with RP2040W CYW43439 WiFi
17951810
AsyncTCP_RP2040W v1.1.0
1796-
AsyncWebServer_RP2040W v1.2.0
1811+
AsyncWebServer_RP2040W v1.2.1
17971812
Connecting to SSID: HueNet1
17981813
SSID: HueNet1
17991814
Local IP Address: 192.168.2.74
@@ -1804,11 +1819,9 @@ HEAP DATA - Pre Create Arduino String Cur heap: 193256 Free heap: 191192 Max
18041819
.
18051820
HEAP DATA - Pre Send Cur heap: 193256 Free heap: 149432 Max heap: 43824
18061821
1807-
HEAP DATA - Post Send Cur heap: 193256 Free heap: 118056 Max heap: 75200
1808-
......
18091822
HEAP DATA - Post Send Cur heap: 193256 Free heap: 118024 Max heap: 75232
1810-
... .......... .......... .......... ...
1811-
HEAP DATA - Post Send Cur heap: 193256 Free heap: 118016 Max heap: 75240
1823+
1824+
HEAP DATA - Post Send Cur heap: 193256 Free heap: 117992 Max heap: 75264
18121825
....... .......... .......... ..........
18131826
.......... .......... .......... ........
18141827
Out String Length=31247
@@ -1875,7 +1888,9 @@ Submit issues to: [AsyncWebServer_RP2040W issues](https://github.com/khoih-prog/
18751888
2. Thanks to [revell1](https://github.com/revell1) to
18761889
- report the bug in [LED state appears to be reversed. #2](https://github.com/khoih-prog/AsyncWebServer_RP2040W/issues/2), leading to v1.0.2
18771890
- request enhancement in [Target stops responding after variable time when using Firefox on Windows 10 #3](https://github.com/khoih-prog/AsyncWebServer_RP2040W/issues/3), leading to v1.1.0
1878-
3. Thanks to [salasidis](https://github.com/salasidis) aka [rs77can](https://forum.arduino.cc/u/rs77can) to discuss and make the mavellous PR [request->send(200, textPlainStr, jsonChartDataCharStr); - Without using String Class - to save heap #8](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/pull/8), leading to `v1.2.0` to support using `CString` to save heap to send `very large data`
1891+
3. Thanks to [salasidis](https://github.com/salasidis) aka [rs77can](https://forum.arduino.cc/u/rs77can) to discuss and make the following `marvellous` PRs in [Portenta_H7_AsyncWebServer library](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer)
1892+
- [request->send(200, textPlainStr, jsonChartDataCharStr); - Without using String Class - to save heap #8](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/pull/8), leading to `v1.2.0` to support using `CString` to save heap to send `very large data`
1893+
- [All memmove() removed - string no longer destroyed #11](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/pull/11), leading to `v1.2.1` to remove `memmove()` and not to destroy String anymore
18791894

18801895
<table>
18811896
<tr>

0 commit comments

Comments
 (0)