Skip to content

Commit d198a19

Browse files
committed
Fix webUI loose websocket connection on long upload / download (>30MB)
Add benchmark code for download Replace Download size packet from 1024 to 2048 on ESP32 Bump version
1 parent d699b03 commit d198a19

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

esp3d/src/include/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#define _VERSION_ESP3D_H
2323

2424
//version and sources location
25-
#define FW_VERSION "3.0.0.a214"
25+
#define FW_VERSION "3.0.0.a215"
2626
#define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0"
2727

2828
#endif //_VERSION_ESP3D_H

esp3d/src/modules/http/handles/upload-SD-files.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#if defined(ESP3DLIB_ENV) && COMMUNICATION_PROTOCOL == SOCKET_SERIAL
3636
#include "../../serial2socket/serial2socket.h"
3737
#endif // ESP3DLIB_ENV && COMMUNICATION_PROTOCOL == SOCKET_SERIAL
38+
#include "../../websocket/websocket_server.h"
3839

3940
//SD files uploader handle
4041
void HTTP_Server::SDFileupload ()
@@ -43,6 +44,7 @@ void HTTP_Server::SDFileupload ()
4344
static uint64_t bench_start;
4445
static size_t bench_transfered;
4546
#endif//ESP_BENCHMARK_FEATURE
47+
static uint64_t last_WS_update;
4648
//get authentication status
4749
level_authenticate_type auth_level= AuthenticationService::authenticated_level();
4850
static String filename;
@@ -60,6 +62,7 @@ void HTTP_Server::SDFileupload ()
6062
#endif // ESP3DLIB_ENV && COMMUNICATION_PROTOCOL == SOCKET_SERIAL
6163
//Upload start
6264
if (upload.status == UPLOAD_FILE_START) {
65+
last_WS_update = millis();
6366
#ifdef ESP_BENCHMARK_FEATURE
6467
bench_start = millis();
6568
bench_transfered = 0;
@@ -167,6 +170,11 @@ void HTTP_Server::SDFileupload ()
167170
#ifdef ESP_BENCHMARK_FEATURE
168171
bench_transfered += upload.currentSize;
169172
#endif//ESP_BENCHMARK_FEATURE
173+
//update websocket every 2000 ms
174+
if (millis()-last_WS_update > 2000) {
175+
websocket_terminal_server.handle();
176+
last_WS_update = millis();
177+
}
170178
//no error so write post date
171179
int writeddatanb=fsUploadFile.write(upload.buf, upload.currentSize);
172180
if(upload.currentSize != (size_t)writeddatanb) {

esp3d/src/modules/http/http_server.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
#if defined (HTTP_FEATURE)
2525
#if defined (ARDUINO_ARCH_ESP32)
2626
#include <WebServer.h>
27+
#define DOWNLOAD_PACKET_SIZE 2048
2728
#endif //ARDUINO_ARCH_ESP32
2829
#if defined (ARDUINO_ARCH_ESP8266)
2930
#include <ESP8266WebServer.h>
31+
#define DOWNLOAD_PACKET_SIZE 1024
3032
#endif //ARDUINO_ARCH_ESP8266
3133
#include "http_server.h"
3234
#include "../authentication/authentication_service.h"
@@ -36,8 +38,11 @@
3638
#include "../websocket/websocket_server.h"
3739
#if defined(SD_DEVICE)
3840
#include "../filesystem/esp_sd.h"
39-
4041
#endif //SD_DEVICE
42+
#ifdef ESP_BENCHMARK_FEATURE
43+
#include "../../core/benchmark.h"
44+
#endif //ESP_BENCHMARK_FEATURE
45+
4146
bool HTTP_Server::_started = false;
4247
uint16_t HTTP_Server::_port = 0;
4348
WEBSERVER * HTTP_Server::_webserver = nullptr;
@@ -91,6 +96,7 @@ void HTTP_Server::init_handlers()
9196
bool HTTP_Server::StreamFSFile(const char* filename, const char * contentType)
9297
{
9398
ESP_File datafile = ESP_FileSystem::open(filename);
99+
uint64_t last_WS_update = millis();
94100
if (!datafile) {
95101
return false;
96102
}
@@ -99,10 +105,10 @@ bool HTTP_Server::StreamFSFile(const char* filename, const char * contentType)
99105
bool done = false;
100106
_webserver->setContentLength(totalFileSize);
101107
_webserver->send(200, contentType, "");
102-
uint8_t buf[1024];
108+
uint8_t buf[DOWNLOAD_PACKET_SIZE];
103109
while (!done && _webserver->client().connected()) {
104110
Hal::wait(0);
105-
int v = datafile.read(buf,1024);
111+
int v = datafile.read(buf,DOWNLOAD_PACKET_SIZE);
106112
if ((v == -1) || (v == 0)) {
107113
done = true;
108114
} else {
@@ -112,6 +118,11 @@ bool HTTP_Server::StreamFSFile(const char* filename, const char * contentType)
112118
if (i >= totalFileSize) {
113119
done = true;
114120
}
121+
//update websocket every 2000 ms
122+
if (millis()-last_WS_update > 2000) {
123+
websocket_terminal_server.handle();
124+
last_WS_update = millis();
125+
}
115126
}
116127
datafile.close();
117128
if ( i != totalFileSize) {
@@ -124,6 +135,11 @@ bool HTTP_Server::StreamFSFile(const char* filename, const char * contentType)
124135
bool HTTP_Server::StreamSDFile(const char* filename, const char * contentType)
125136
{
126137
ESP_SDFile datafile = ESP_SD::open(filename);
138+
uint64_t last_WS_update = millis();
139+
#ifdef ESP_BENCHMARK_FEATURE
140+
uint64_t bench_start = millis();
141+
size_t bench_transfered = 0;
142+
#endif//ESP_BENCHMARK_FEATURE
127143
if (!datafile) {
128144
return false;
129145
}
@@ -132,24 +148,36 @@ bool HTTP_Server::StreamSDFile(const char* filename, const char * contentType)
132148
bool done = false;
133149
_webserver->setContentLength(totalFileSize);
134150
_webserver->send(200, contentType, "");
135-
uint8_t buf[1024];
151+
uint8_t buf[DOWNLOAD_PACKET_SIZE];
136152
while (!done && _webserver->client().connected()) {
137153
Hal::wait(0);
138-
int v = datafile.read(buf,1024);
154+
int v = datafile.read(buf,DOWNLOAD_PACKET_SIZE);
139155
if ((v == -1) || (v == 0)) {
140156
done = true;
141157
} else {
142158
_webserver->client().write(buf,v);
143159
i+=v;
160+
#ifdef ESP_BENCHMARK_FEATURE
161+
bench_transfered += v;
162+
#endif//ESP_BENCHMARK_FEATURE
144163
}
145164
if (i >= totalFileSize) {
146165
done = true;
147166
}
167+
168+
//update websocket every 2000 ms
169+
if (millis()-last_WS_update > 2000) {
170+
websocket_terminal_server.handle();
171+
last_WS_update = millis();
172+
}
148173
}
149174
datafile.close();
150175
if ( i != totalFileSize) {
151176
return false;
152177
}
178+
#ifdef ESP_BENCHMARK_FEATURE
179+
benchMark("SD download", bench_start, millis(), bench_transfered);
180+
#endif//ESP_BENCHMARK_FEATURE
153181
return true;
154182
}
155183
#endif //SD_DEVICE

0 commit comments

Comments
 (0)