Skip to content

Commit 7f17723

Browse files
committed
PR Review & Testing
- Increase throughput of WS example - Remove \n at the end of log line - Fix status that was incorrectly kept to 0 even when all bytes acked - Added traces in sent method
1 parent 293944d commit 7f17723

3 files changed

Lines changed: 31 additions & 23 deletions

File tree

examples/WebSocket/WebSocket.ino

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,13 @@ void setup() {
167167
server.begin();
168168
}
169169

170-
static uint32_t lastWS = 0;
171-
static uint32_t deltaWS = 500;
170+
#ifdef ESP32
171+
static const uint32_t deltaWS = 50;
172+
#else
173+
static const uint32_t deltaWS = 200;
174+
#endif
172175

176+
static uint32_t lastWS = 0;
173177
static uint32_t lastHeap = 0;
174178

175179
void loop() {
@@ -186,6 +190,10 @@ void loop() {
186190
// this can be called to also set a soft limit on the number of connected clients
187191
ws.cleanupClients(2); // no more than 2 clients
188192

193+
// ping twice (2 control frames)
194+
ws.pingAll();
195+
ws.pingAll();
196+
189197
#ifdef ESP32
190198
Serial.printf("Free heap: %" PRIu32 "\n", ESP.getFreeHeap());
191199
#endif

src/AsyncWebSocket.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -166,64 +166,65 @@ AsyncWebSocketMessage::AsyncWebSocketMessage(AsyncWebSocketSharedBuffer buffer,
166166

167167
size_t AsyncWebSocketMessage::ack(size_t len, uint32_t time) {
168168
(void)time;
169-
const size_t pending = _ack - _acked;
170-
const size_t received = std::min(len, pending);
171-
_acked += received;
172-
if (_sent >= _WSbuffer->size() && !pending) {
169+
const size_t pending = std::min(len, _ack - _acked);
170+
_acked += pending;
171+
if (_sent >= _WSbuffer->size() && _acked >= _ack) {
173172
_status = WS_MSG_SENT;
174173
}
175-
async_ws_log_v("status: %d, ack: %u/%u\n", static_cast<int>(_status), _acked, _ack);
176-
return len - received;
174+
async_ws_log_v("msg code: %" PRIu8 ", ack: %u/%u, remain=%u/%u, status: %d", _opcode, _acked, _ack, len - pending, len, static_cast<int>(_status));
175+
return len - pending;
177176
}
178177

179178
size_t AsyncWebSocketMessage::send(AsyncClient *client) {
180179
if (!client) {
180+
async_ws_log_v("No client");
181181
return 0;
182182
}
183183

184184
if (_status != WS_MSG_SENDING) {
185+
async_ws_log_v("C[%" PRIu16 "] Wrong status: got: %d, expected: %d", client->remotePort(), static_cast<int>(_status), static_cast<int>(WS_MSG_SENDING));
185186
return 0;
186187
}
187188

188189
if (_sent == _WSbuffer->size()) {
189190
if (_acked == _ack) {
190191
_status = WS_MSG_SENT;
191192
}
193+
async_ws_log_v("C[%" PRIu16 "] Already sent: %u/%u", client->remotePort(), _sent, _WSbuffer->size());
192194
return 0;
193195
}
194196
if (_sent > _WSbuffer->size()) {
195197
_status = WS_MSG_ERROR;
196-
// ets_printf("E: %u > %u\n", _sent, _WSbuffer->length());
198+
async_ws_log_v("C[%" PRIu16 "] Error, sent more: %u/%u", client->remotePort(), _sent, _WSbuffer->size());
197199
return 0;
198200
}
199201

200202
size_t toSend = _WSbuffer->size() - _sent;
201203
const size_t window = webSocketSendFrameWindow(client);
202204

203-
// not enough space in lwip buffer ?
205+
// not enough space in lwip buffer ?
204206
if (!window) {
207+
async_ws_log_v("C[%" PRIu16 "] No space left to send more data: acked: %u, sent: %u, remaining: %u", client->remotePort(), _acked, _sent, toSend);
205208
return 0;
206209
}
207-
210+
208211
toSend = std::min(toSend, window);
209212

210213
_sent += toSend;
211214
_ack += toSend + ((toSend < 126) ? 2 : 4) + (_mask * 4);
212215

213-
// ets_printf("W: %u %u\n", _sent - toSend, toSend);
214-
215216
bool final = (_sent == _WSbuffer->size());
216217
uint8_t *dPtr = (uint8_t *)(_WSbuffer->data() + (_sent - toSend));
217218
uint8_t opCode = (toSend && _sent == toSend) ? _opcode : (uint8_t)WS_CONTINUATION;
218219

219220
size_t sent = webSocketSendFrame(client, final, opCode, _mask, dPtr, toSend);
220221
_status = WS_MSG_SENDING;
221222
if (toSend && sent != toSend) {
222-
// ets_printf("E: %u != %u\n", toSend, sent);
223223
_sent -= (toSend - sent);
224224
_ack -= (toSend - sent);
225225
}
226-
// ets_printf("S: %u %u\n", _sent, sent);
226+
227+
async_ws_log_v("C[%" PRIu16 "] Sent %u/%u, ack: %u/%u, final: %d", client->remotePort(), _sent, _WSbuffer->size(), _acked, _ack, final);
227228
return sent;
228229
}
229230

@@ -345,7 +346,6 @@ void AsyncWebSocketClient::_onAck(size_t len, uint32_t time) {
345346
_runQueue();
346347
}
347348

348-
349349
void AsyncWebSocketClient::_onPoll() {
350350
if (!_client) {
351351
return;
@@ -375,22 +375,22 @@ void AsyncWebSocketClient::_runQueue() {
375375
if (!_controlQueue.empty() && !_controlQueue.front().finished() && (_messageQueue.empty() || _messageQueue.front().betweenFrames())
376376
&& webSocketSendFrameWindow(_client) > (size_t)(_controlQueue.front().len() - 1)) {
377377
_controlQueue.front().send(_client);
378-
}
379-
378+
}
379+
380380
if (webSocketSendFrameWindow(_client)) {
381381
for (auto &msg : _messageQueue) {
382382
if (msg._remainingBytesToSend()) {
383383
msg.send(_client);
384384
}
385-
385+
386386
// If we haven't finished sending this message, we must stop here to preserve WebSocket ordering.
387387
// We can only pipeline subsequent messages if the current one is fully passed to TCP buffer.
388388
if (msg._remainingBytesToSend()) {
389389
break;
390390
}
391-
391+
392392
// not enough space for another message
393-
if(!webSocketSendFrameWindow(_client)) {
393+
if (!webSocketSendFrameWindow(_client)) {
394394
return;
395395
}
396396
}

src/AsyncWebSocket.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class AsyncWebSocketMessageBuffer {
185185

186186
class AsyncWebSocketMessage {
187187
friend AsyncWebSocketClient;
188-
188+
189189
private:
190190
size_t _remainingBytesToSend() const {
191191
return _WSbuffer->size() - _sent;
@@ -208,7 +208,7 @@ class AsyncWebSocketMessage {
208208
bool betweenFrames() const {
209209
return _acked == _ack;
210210
}
211-
211+
212212
size_t ack(size_t len, uint32_t time);
213213
size_t send(AsyncClient *client);
214214
};

0 commit comments

Comments
 (0)