@@ -31,6 +31,20 @@ extern "C"{
3131}
3232#include  " esp_task_wdt.h" 
3333
34+ #if  ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 1, 0)
35+ #define  TCP_MUTEX_LOCK ()                                \
36+   if  (!sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) { \
37+     LOCK_TCPIP_CORE ();                                  \
38+   }
39+ 
40+ #define  TCP_MUTEX_UNLOCK ()                             \
41+   if  (sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) { \
42+     UNLOCK_TCPIP_CORE ();                               \
43+   }
44+ #else 
45+   #define  TCP_MUTEX_LOCK ()
46+   #define  TCP_MUTEX_UNLOCK ()
47+ #endif 
3448/* 
3549 * TCP/IP Event Task 
3650 * */  
@@ -690,10 +704,11 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port){
690704    ip_addr_t  addr;
691705    addr.type  = IPADDR_TYPE_V4;
692706    addr.u_addr .ip4 .addr  = ip;
693- 
707+      TCP_MUTEX_LOCK (); 
694708    tcp_pcb* pcb = tcp_new_ip_type (IPADDR_TYPE_V4);
695709    if  (!pcb){
696710        log_e (" pcb == NULL" 
711+         TCP_MUTEX_UNLOCK ();
697712        return  false ;
698713    }
699714
@@ -702,6 +717,7 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port){
702717    tcp_recv (pcb, &_tcp_recv);
703718    tcp_sent (pcb, &_tcp_sent);
704719    tcp_poll (pcb, &_tcp_poll, 1 );
720+     TCP_MUTEX_UNLOCK ();
705721    // _tcp_connect(pcb, &addr, port,(tcp_connected_fn)&_s_connected);
706722    _tcp_connect (pcb, _closed_slot, &addr, port,(tcp_connected_fn)&_tcp_connected);
707723    return  true ;
@@ -714,8 +730,9 @@ bool AsyncClient::connect(const char* host, uint16_t port){
714730      log_e (" failed to start task" 
715731      return  false ;
716732    }
717-     
733+     TCP_MUTEX_LOCK (); 
718734    err_t  err = dns_gethostbyname (host, &addr, (dns_found_callback)&_tcp_dns_found, this );
735+     TCP_MUTEX_UNLOCK ();
719736    if (err == ERR_OK) {
720737        return  connect (IPAddress (addr.u_addr .ip4 .addr ), port);
721738    } else  if (err == ERR_INPROGRESS) {
@@ -803,11 +820,13 @@ int8_t AsyncClient::_close(){
803820    int8_t  err = ERR_OK;
804821    if (_pcb) {
805822        // log_i("");
823+         TCP_MUTEX_LOCK ();
806824        tcp_arg (_pcb, NULL );
807825        tcp_sent (_pcb, NULL );
808826        tcp_recv (_pcb, NULL );
809827        tcp_err (_pcb, NULL );
810828        tcp_poll (_pcb, NULL , 0 );
829+         TCP_MUTEX_UNLOCK ();
811830        _tcp_clear_events (this );
812831        err = _tcp_close (_pcb, _closed_slot);
813832        if (err != ERR_OK) {
@@ -865,13 +884,15 @@ int8_t AsyncClient::_connected(void* pcb, int8_t err){
865884
866885void  AsyncClient::_error (int8_t  err) {
867886    if (_pcb){
887+         TCP_MUTEX_LOCK ();
868888        tcp_arg (_pcb, NULL );
869889        if (_pcb->state  == LISTEN) {
870890            tcp_sent (_pcb, NULL );
871891            tcp_recv (_pcb, NULL );
872892            tcp_err (_pcb, NULL );
873893            tcp_poll (_pcb, NULL , 0 );
874894        }
895+         TCP_MUTEX_UNLOCK ();
875896        _pcb = NULL ;
876897    }
877898    if (_error_cb) {
@@ -1274,12 +1295,14 @@ void AsyncServer::begin(){
12741295        return ;
12751296    }
12761297    int8_t  err;
1298+     TCP_MUTEX_LOCK ();
12771299    _pcb = tcp_new_ip_type (IPADDR_TYPE_V4);
12781300    if  (!_pcb){
12791301        log_e (" _pcb == NULL" 
1302+         TCP_MUTEX_UNLOCK ();
12801303        return ;
12811304    }
1282- 
1305+      TCP_MUTEX_UNLOCK (); 
12831306    ip_addr_t  local_addr;
12841307    local_addr.type  = IPADDR_TYPE_V4;
12851308    local_addr.u_addr .ip4 .addr  = (uint32_t ) _addr;
@@ -1297,17 +1320,21 @@ void AsyncServer::begin(){
12971320        log_e (" listen_pcb == NULL" 
12981321        return ;
12991322    }
1323+     TCP_MUTEX_LOCK ();
13001324    tcp_arg (_pcb, (void *) this );
13011325    tcp_accept (_pcb, &_s_accept);
1326+     TCP_MUTEX_UNLOCK ();
13021327}
13031328
13041329void  AsyncServer::end (){
13051330    if (_pcb){
1331+         TCP_MUTEX_LOCK ();
13061332        tcp_arg (_pcb, NULL );
13071333        tcp_accept (_pcb, NULL );
13081334        if (tcp_close (_pcb) != ERR_OK){
13091335            _tcp_abort (_pcb, -1 );
13101336        }
1337+         TCP_MUTEX_UNLOCK ();
13111338        _pcb = NULL ;
13121339    }
13131340}
0 commit comments