Skip to content

Commit ab8f8ec

Browse files
committed
Add some sanity check to avoid unnecessary messages copies
sync with ESP3D 3.0
1 parent cf10b4b commit ab8f8ec

File tree

4 files changed

+22
-41
lines changed

4 files changed

+22
-41
lines changed

src/core/esp3d_commands.cpp

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,8 +1144,8 @@ void ESP3DCommands::process(ESP3DMessage *msg) {
11441144
esp3d_log("Execute internal command %d", cmdId);
11451145
execute_internal_command(cmdId, espcmdpos, msg);
11461146
} else {
1147-
esp3d_log("Dispatch command, len %d, to %d", msg->size,
1148-
static_cast<uint8_t>(msg->target));
1147+
esp3d_log("Dispatch command, len %d, from %d(%s) to %d(%s)", msg->size,
1148+
static_cast<uint8_t>(msg->origin), GETCLIENTSTR(msg->origin), static_cast<uint8_t>(msg->target), GETCLIENTSTR(msg->target));
11491149

11501150
// Work around to avoid to dispatch single \n or \r to everyone as it is
11511151
// part of previous ESP3D command
@@ -1359,14 +1359,11 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
13591359
break;
13601360
#endif // PRINTER_HAS_DISPLAY
13611361
case ESP3DClientType::all_clients:
1362-
esp3d_log("All clients message");
1362+
esp3d_log("All clients message");
13631363
// Add each client one by one
13641364
#ifdef PRINTER_HAS_DISPLAY
13651365
if (msg->origin != ESP3DClientType::remote_screen &&
13661366
msg->origin != getOutputClient()) {
1367-
String msgstr = (const char *)msg->data;
1368-
msgstr.trim();
1369-
if (msgstr.length() > 0) {
13701367
if (msg->target == ESP3DClientType::all_clients) {
13711368
// become the reference message
13721369
msg->target = ESP3DClientType::remote_screen;
@@ -1380,16 +1377,12 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
13801377
esp3d_log_e("Cannot duplicate message for remote screen");
13811378
}
13821379
}
1383-
}
13841380
}
13851381
#endif // PRINTER_HAS_DISPLAY
13861382

13871383
#if defined(DISPLAY_DEVICE)
13881384
if (msg->origin != ESP3DClientType::rendering &&
13891385
msg->origin != getOutputClient()) {
1390-
String msgstr = (const char *)msg->data;
1391-
msgstr.trim();
1392-
if (msgstr.length() > 0) {
13931386
if (msg->target == ESP3DClientType::all_clients) {
13941387
// become the reference message
13951388
msg->target = ESP3DClientType::rendering;
@@ -1405,16 +1398,12 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
14051398
esp3d_log_e("Cannot duplicate message for display");
14061399
}
14071400
}
1408-
}
14091401
}
14101402
#endif // defined(DISPLAY_DEVICE)
14111403

14121404
#if COMMUNICATION_PROTOCOL == SOCKET_SERIAL
14131405
if (msg->origin != ESP3DClientType::echo_serial &&
14141406
msg->origin != ESP3DClientType::socket_serial) {
1415-
String msgstr = (const char *)msg->data;
1416-
msgstr.trim();
1417-
if (msgstr.length() > 0) {
14181407
if (msg->target == ESP3DClientType::all_clients) {
14191408
// become the reference message
14201409
msg->target = ESP3DClientType::echo_serial;
@@ -1428,15 +1417,11 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
14281417
esp3d_log_e("Cannot duplicate message for echo serial");
14291418
}
14301419
}
1431-
}
14321420
}
14331421
#endif // COMMUNICATION_PROTOCOL == SOCKET_SERIAL
14341422

14351423
#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
14361424
if (msg->origin != ESP3DClientType::serial_bridge) {
1437-
String msgstr = (const char *)msg->data;
1438-
msgstr.trim();
1439-
if (msgstr.length() > 0) {
14401425
if (msg->target == ESP3DClientType::all_clients) {
14411426
// become the reference message
14421427
msg->target = ESP3DClientType::serial_bridge;
@@ -1450,16 +1435,11 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
14501435
esp3d_log_e("Cannot duplicate message for serial bridge");
14511436
}
14521437
}
1453-
}
14541438
}
14551439
#endif // ESP_SERIAL_BRIDGE_OUTPUT
14561440

14571441
#ifdef BLUETOOTH_FEATURE
1458-
//FIXME: add test if connected to avoid unnecessary copy
1459-
if (msg->origin != ESP3DClientType::bluetooth) {
1460-
String msgstr = (const char *)msg->data;
1461-
msgstr.trim();
1462-
if (msgstr.length() > 0) {
1442+
if (msg->origin != ESP3DClientType::bluetooth && bt_service.isConnected()) {
14631443
if (msg->target == ESP3DClientType::all_clients) {
14641444
// become the reference message
14651445
msg->target = ESP3DClientType::bluetooth;
@@ -1473,15 +1453,11 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
14731453
esp3d_log_e("Cannot duplicate message for bluetooth");
14741454
}
14751455
}
1476-
}
14771456
}
14781457
#endif // BLUETOOTH_FEATURE
14791458

14801459
#ifdef TELNET_FEATURE
14811460
if (msg->origin != ESP3DClientType::telnet && telnet_server.isConnected()) {
1482-
String msgstr = (const char *)msg->data;
1483-
msgstr.trim();
1484-
if (msgstr.length() > 0) {
14851461
if (msg->target == ESP3DClientType::all_clients) {
14861462
// become the reference message
14871463
msg->target = ESP3DClientType::telnet;
@@ -1495,17 +1471,14 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
14951471
esp3d_log_e("Cannot duplicate message for telnet");
14961472
}
14971473
}
1498-
}
1474+
} else{
1475+
if (msg->origin != ESP3DClientType::telnet)esp3d_log("Telnet not connected");
14991476
}
15001477
#endif // TELNET_FEATURE
15011478

15021479
#ifdef HTTP_FEATURE // http cannot be in all client because it depend of any
15031480
// connection of the server
1504-
//FIXME: add test if connected to avoid unnecessary copy
1505-
if (msg->origin != ESP3DClientType::webui_websocket) {
1506-
String msgstr = (const char *)msg->data;
1507-
msgstr.trim();
1508-
if (msgstr.length() > 0) {
1481+
if (msg->origin != ESP3DClientType::webui_websocket && websocket_terminal_server.isConnected()) {
15091482
if (msg->target == ESP3DClientType::all_clients) {
15101483
// become the reference message
15111484
msg->target = ESP3DClientType::webui_websocket;
@@ -1519,16 +1492,13 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
15191492
esp3d_log_e("Cannot duplicate message for webui_websocket");
15201493
}
15211494
}
1522-
}
1495+
} else {
1496+
if (msg->origin != ESP3DClientType::webui_websocket)esp3d_log("Webui websocket not connected");
15231497
}
15241498
#endif // HTTP_FEATURE
15251499

15261500
#ifdef WS_DATA_FEATURE
1527-
//FIXME: add test if connected to avoid unnecessary copy
1528-
if (msg->origin != ESP3DClientType::websocket) {
1529-
String msgstr = (const char *)msg->data;
1530-
msgstr.trim();
1531-
if (msgstr.length() > 0) {
1501+
if (msg->origin != ESP3DClientType::websocket && websocket_data_server.isConnected()) {
15321502
if (msg->target == ESP3DClientType::all_clients) {
15331503
// become the reference message
15341504
msg->target = ESP3DClientType::websocket;
@@ -1542,14 +1512,16 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
15421512
esp3d_log_e("Cannot duplicate message for websocket");
15431513
}
15441514
}
1545-
}
1515+
} else {
1516+
if (msg->origin != ESP3DClientType::websocket)esp3d_log("Websocket not connected");
15461517
}
15471518
#endif // WS_DATA_FEATURE
15481519

15491520
//...
15501521

15511522
// Send pending if any or cancel message is no client did handle it
15521523
if (msg->target == ESP3DClientType::all_clients) {
1524+
esp3d_log("No client handled message, send pending");
15531525
sendOk = false;
15541526
} else {
15551527
return dispatch(msg);

src/modules/serial/serial_service.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ void ESP3DSerialService::flushbuffer() {
287287
getAuthentication());
288288
if (message) {
289289
// process command
290+
message->type = ESP3DMessageType::unique;
290291
esp3d_commands.process(message);
291292
} else {
292293
esp3d_log_e("Cannot create message");

src/modules/websocket/websocket_server.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ bool WebSocket_Server::pushMSG(uint num, const char *data) {
7777
return false;
7878
}
7979

80+
bool WebSocket_Server::isConnected(){
81+
if (_websocket_server) {
82+
return _websocket_server->connectedClients(true) > 0;
83+
}
84+
return false;
85+
}
86+
8087
void WebSocket_Server::closeClients() {
8188
if (_websocket_server) {
8289
_websocket_server->disconnect();

src/modules/websocket/websocket_server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class WebSocket_Server {
5454
void initAuthentication();
5555
void setAuthentication(ESP3DAuthenticationLevel auth) { _auth = auth; }
5656
ESP3DAuthenticationLevel getAuthentication();
57+
bool isConnected();
5758

5859
private:
5960
ESP3DClientType _type;

0 commit comments

Comments
 (0)