@@ -80,7 +80,9 @@ lwipClient& lwipClient::operator=(lwipClient&& rhs) {
8080}
8181
8282lwipClient::~lwipClient () {
83- this ->stop ();
83+ if (this ->tcp_info ->state != TCP_CLOSING) {
84+ this ->stop ();
85+ }
8486}
8587
8688int lwipClient::connect (const char * host, uint16_t port) {
@@ -231,7 +233,7 @@ err_t lwipClient::recv_callback(struct tcp_pcb* tpcb, struct pbuf* p, err_t err)
231233 return ERR_OK;
232234 }
233235 arduino::lock ();
234- if (this ->tcp_info ->state == TCP_CONNECTED) {
236+ if (this ->tcp_info ->state == TCP_CONNECTED || this -> tcp_info -> state == TCP_ACCEPTED ) {
235237 if (this ->tcp_info ->pbuf_head == nullptr ) {
236238 // no need to increment the references of the pbuf,
237239 // since it is already 1 and lwip shifts the control to this code
@@ -273,16 +275,11 @@ size_t lwipClient::write(const uint8_t* buffer, size_t size) {
273275 } else if (res == ERR_MEM) {
274276 // FIXME handle this: we get into this case only if the sent data cannot be put in the send queue
275277 }
276-
277- // TODO understand if the tcp_write will send data if the buffer is not full
278- // force send only if we filled the send buffer
279- // if (ERR_OK != tcp_output(this->tcp_info->pcb)) {
280- // // return 0;
281- // break;
282- // }
283278 } while (buffer_cursor < buffer + size);
284- arduino::unlock ();
285279
280+ tcp_output (this ->tcp_info ->pcb );
281+
282+ arduino::unlock ();
286283 return buffer - buffer_cursor;
287284}
288285
@@ -342,13 +339,13 @@ void lwipClient::flush() {
342339}
343340
344341void lwipClient::stop () {
345- tcp_recv (this ->tcp_info ->pcb , nullptr );
346- tcp_sent (this ->tcp_info ->pcb , nullptr );
347- tcp_poll (this ->tcp_info ->pcb , nullptr , 0 );
348- tcp_err (this ->tcp_info ->pcb , nullptr );
349- tcp_accept (this ->tcp_info ->pcb , nullptr );
350-
351342 if (this ->tcp_info ->pcb != nullptr ) {
343+ tcp_recv (this ->tcp_info ->pcb , nullptr );
344+ tcp_sent (this ->tcp_info ->pcb , nullptr );
345+ tcp_poll (this ->tcp_info ->pcb , nullptr , 0 );
346+ tcp_err (this ->tcp_info ->pcb , nullptr );
347+ tcp_accept (this ->tcp_info ->pcb , nullptr );
348+
352349 err_t err = tcp_close (this ->tcp_info ->pcb );
353350 this ->tcp_info ->state = TCP_CLOSING;
354351
@@ -362,6 +359,12 @@ void lwipClient::stop() {
362359 // if(tcp->p != nullptr) {
363360 // pbuf_free(tcp->p); // FIXME it happens that a pbuf, with ref == 0 is added for some reason
364361 // }
362+ if (this ->tcp_info ->server != nullptr ) {
363+ // need to first make the server point to nullptr, then remove the client, can cause infinite recursion
364+ auto server = this ->tcp_info ->server ;
365+ this ->tcp_info ->server = nullptr ;
366+ server->remove (this );
367+ }
365368}
366369
367370uint8_t lwipClient::connected () {
0 commit comments