@@ -166,64 +166,65 @@ AsyncWebSocketMessage::AsyncWebSocketMessage(AsyncWebSocketSharedBuffer buffer,
166166
167167size_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
179178size_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-
349349void 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 }
0 commit comments