@@ -5,6 +5,7 @@ extern "C" {
55#include " Arduino.h"
66
77#include " lwipClient.h"
8+ #include " lwippbuf.h"
89#include " CNetIf.h"
910#include " utils.h"
1011// FIXME understand hos to syncronize the interrupt thread and "userspace"
@@ -310,8 +311,10 @@ int lwipClient::read(uint8_t* buffer, size_t size) {
310311 arduino::lock ();
311312 uint16_t copied = pbuf_copy_partial (this ->tcp_info ->pbuf_head , buffer, size, this ->tcp_info ->pbuf_offset );
312313
313- this ->free_pbuf_chain (copied);
314- // __enable_irq();
314+ this ->tcp_info ->pbuf_head = free_pbuf_chain (this ->tcp_info ->pbuf_head , copied, &this ->tcp_info ->pbuf_offset );
315+
316+ // acknowledge the received data
317+ tcp_recved (this ->tcp_info ->pcb , copied);
315318 arduino::unlock ();
316319
317320 return copied;
@@ -433,50 +436,11 @@ size_t lwipClient::read_until_token(
433436
434437 uint16_t copied = pbuf_copy_partial (this ->tcp_info ->pbuf_head , (uint8_t *)buffer, buf_copy_len, this ->tcp_info ->pbuf_offset );
435438
436- this ->free_pbuf_chain (copied);
437- arduino::unlock ();
438-
439- return copied;
440- }
441-
442- void lwipClient::free_pbuf_chain (uint16_t copied) {
443- arduino::lock ();
444- /*
445- * free pbufs that have been copied, if copied == 0 we have an error
446- * free the buffer chain starting from the head up to the last entire pbuf ingested
447- * taking into account the previously not entirely consumed pbuf
448- */
449- uint32_t tobefreed = 0 ;
450- copied += this ->tcp_info ->pbuf_offset ;
451-
452- // in order to clean up the chain we need to find the pbuf in the last pbuf in the chain
453- // that got completely consumed by the application, dechain it from it successor and delete the chain before it
454-
455- struct pbuf *head = this ->tcp_info ->pbuf_head , *last=head, *prev=nullptr ; // FIXME little optimization prev can be substituted by last->next
456-
457- while (last!=nullptr && last->len + tobefreed <= copied) {
458- tobefreed += last->len ;
459- prev = last;
460- last = last->next ;
461- }
462-
463- // dechain if we are not at the end of the chain (last == nullptr)
464- // and if we haven't copied entirely the first pbuf (prev == nullptr) (head == last)
465- // if we reached the end of the chain set the this pbuf pointer to nullptr
466- if (prev != nullptr ) {
467- prev->next = nullptr ;
468- this ->tcp_info ->pbuf_head = last;
469- }
470-
471- // the chain that is referenced by head is detached by the one referenced by this->tcp_info->pbuf_head
472- // free the chain if we haven't copied entirely the first pbuf (prev == nullptr)
473- if (this ->tcp_info ->pbuf_head != head) {
474- uint8_t refs = pbuf_free (head);
475- }
476-
477- this ->tcp_info ->pbuf_offset = copied - tobefreed; // This offset should be referenced to the first pbuf in queue
439+ this ->tcp_info ->pbuf_head = free_pbuf_chain (this ->tcp_info ->pbuf_head , copied, &this ->tcp_info ->pbuf_offset );
478440
479441 // acknowledge the received data
480442 tcp_recved (this ->tcp_info ->pcb , copied);
481443 arduino::unlock ();
444+
445+ return copied;
482446}
0 commit comments