Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

Commit 4c621f3

Browse files
committed
improve web socket rapid sends a bit
to fully fix any possible issues, ws should use FreeRTOS Queues on ESP32
1 parent 35adb4d commit 4c621f3

1 file changed

Lines changed: 57 additions & 17 deletions

File tree

src/AsyncWebSocket.cpp

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,31 @@ AsyncWebSocketBasicMessage::~AsyncWebSocketBasicMessage() {
337337
_status = WS_MSG_SENT;
338338
return 0;
339339
}
340-
size_t window = webSocketSendFrameWindow(client);
340+
if(_sent > _len){
341+
_status = WS_MSG_ERROR;
342+
return 0;
343+
}
344+
341345
size_t toSend = _len - _sent;
342-
if(window < toSend) toSend = window;
343-
bool final = ((toSend + _sent) == _len);
344-
size_t sent = webSocketSendFrame(client, final, (_sent == 0)?_opcode:(int)WS_CONTINUATION, _mask, (uint8_t*)(_data+_sent), toSend);
345-
_sent += sent;
346-
uint8_t headLen = ((sent < 126)?2:4)+(_mask*4);
347-
_ack += sent + headLen;
346+
size_t window = webSocketSendFrameWindow(client);
347+
348+
if(window < toSend) {
349+
toSend = window;
350+
}
351+
352+
_sent += toSend;
353+
_ack += toSend + ((toSend < 126)?2:4) + (_mask * 4);
354+
355+
bool final = (_sent == _len);
356+
uint8_t* dPtr = (uint8_t*)(_data + (_sent - toSend));
357+
uint8_t opCode = (toSend && _sent == toSend)?_opcode:(uint8_t)WS_CONTINUATION;
358+
359+
size_t sent = webSocketSendFrame(client, final, opCode, _mask, dPtr, toSend);
360+
_status = WS_MSG_SENDING;
361+
if(toSend && sent != toSend){
362+
_sent -= (toSend - sent);
363+
_ack -= (toSend - sent);
364+
}
348365
return sent;
349366
}
350367

@@ -384,6 +401,7 @@ AsyncWebSocketMultiMessage::AsyncWebSocketMultiMessage(AsyncWebSocketMessageBuff
384401
_data = buffer->get();
385402
_len = buffer->length();
386403
_status = WS_MSG_SENDING;
404+
//ets_printf("M: %u\n", _len);
387405
} else {
388406
_status = WS_MSG_ERROR;
389407
}
@@ -399,9 +417,10 @@ AsyncWebSocketMultiMessage::~AsyncWebSocketMultiMessage() {
399417

400418
void AsyncWebSocketMultiMessage::ack(size_t len, uint32_t time) {
401419
_acked += len;
402-
if(_sent == _len && _acked == _ack){
420+
if(_sent >= _len && _acked >= _ack){
403421
_status = WS_MSG_SENT;
404422
}
423+
//ets_printf("A: %u\n", len);
405424
}
406425
size_t AsyncWebSocketMultiMessage::send(AsyncClient *client) {
407426
if(_status != WS_MSG_SENDING)
@@ -410,18 +429,39 @@ AsyncWebSocketMultiMessage::~AsyncWebSocketMultiMessage() {
410429
return 0;
411430
}
412431
if(_sent == _len){
413-
if(_acked == _ack)
414-
_status = WS_MSG_SENT;
432+
_status = WS_MSG_SENT;
415433
return 0;
416434
}
417-
size_t window = webSocketSendFrameWindow(client);
435+
if(_sent > _len){
436+
_status = WS_MSG_ERROR;
437+
//ets_printf("E: %u > %u\n", _sent, _len);
438+
return 0;
439+
}
440+
418441
size_t toSend = _len - _sent;
419-
if(window < toSend) toSend = window;
420-
bool final = ((toSend + _sent) == _len);
421-
size_t sent = webSocketSendFrame(client, final, (_sent == 0)?_opcode:(int)WS_CONTINUATION, _mask, (uint8_t*)(_data+_sent), toSend);
422-
_sent += sent;
423-
uint8_t headLen = ((sent < 126)?2:4)+(_mask*4);
424-
_ack += sent + headLen;
442+
size_t window = webSocketSendFrameWindow(client);
443+
444+
if(window < toSend) {
445+
toSend = window;
446+
}
447+
448+
_sent += toSend;
449+
_ack += toSend + ((toSend < 126)?2:4) + (_mask * 4);
450+
451+
//ets_printf("W: %u %u\n", _sent - toSend, toSend);
452+
453+
bool final = (_sent == _len);
454+
uint8_t* dPtr = (uint8_t*)(_data + (_sent - toSend));
455+
uint8_t opCode = (toSend && _sent == toSend)?_opcode:(uint8_t)WS_CONTINUATION;
456+
457+
size_t sent = webSocketSendFrame(client, final, opCode, _mask, dPtr, toSend);
458+
_status = WS_MSG_SENDING;
459+
if(toSend && sent != toSend){
460+
//ets_printf("E: %u != %u\n", toSend, sent);
461+
_sent -= (toSend - sent);
462+
_ack -= (toSend - sent);
463+
}
464+
//ets_printf("S: %u %u\n", _sent, sent);
425465
return sent;
426466
}
427467

0 commit comments

Comments
 (0)