rm tcp conn timeout
authorkyz <kyz@kyz-vbox.myguest.virtualbox.org>
Thu, 3 Jul 2025 10:05:10 +0000 (18:05 +0800)
committerkyz <kyz@kyz-vbox.myguest.virtualbox.org>
Thu, 3 Jul 2025 10:05:10 +0000 (18:05 +0800)
app/inc/am_tcp.h
app/src/am_main.c
app/src/iot.c
app/src/sensor_board.c
app/src/tcp.c

index 09bf45d992ddcecd8e1470967345fbd8a92fcc27..18ffb685bf33a5490f26775b8fee4c393247b241 100644 (file)
@@ -12,7 +12,7 @@ typedef void (*tcp_rx_cb_t)( uv_stream_t * client, uint8_t *data, uint16_t data_
 typedef void (*tcp_conn_cb_t)( struct tcp_cli * client, int32_t conn );
 typedef struct tcp_cli{
     const char * name;
-    uv_timer_t timer;
+    uv_timer_t start_timer;
     uv_loop_t  *loop;
     char * host;
     char port[8];
@@ -31,5 +31,11 @@ typedef struct tcp_cli{
 void tcp_cli_init( tcp_cli_t * cli, char * host, uint16_t port, char *name, tcp_rx_cb_t rx_cb, tcp_conn_cb_t conn_cb );
 void tcp_cli_start( tcp_cli_t *cli, int delay_ms );
 
+enum{
+    TCP_DISCONNECTED = 0,
+    TCP_CONNECTING = 1,
+    TCP_CONNECTED  = 2,
+};
+
 #endif
 
index 31eb45294bf5662a415fb6b854aeea31b906120b..4be713749378fc40fe12eeeacc63a3dee1f67a2e 100644 (file)
@@ -375,7 +375,7 @@ int device_power_check( am_dev_t *dev )
     spk_dy = dev->sensor_data.ac_vol[0];
     ctrl_dy = dev->sensor_data.ac_vol[1];
 
-printf( "SPK ON[%d], dy[%.1f], CTL ON[%d], dy[%.1f]\n", dev->dev_ctrl.speaker_power, spk_dy, dev->dev_ctrl.controller_power, ctrl_dy );
+//printf( "SPK ON[%d], dy[%.1f], CTL ON[%d], dy[%.1f]\n", dev->dev_ctrl.speaker_power, spk_dy, dev->dev_ctrl.controller_power, ctrl_dy );
     if( dev->dev_ctrl.speaker_power && spk_dy < 100 ){
         spk_err_time++;
     }else if( !dev->dev_ctrl.speaker_power && spk_dy > 100 ){
index c8b38dde179b2e7c2f89682d5196a153676d7893..372b12d6898ffdeee9867722fceb2a8e5da156ec 100644 (file)
@@ -192,7 +192,7 @@ void connect_timer_cb( uv_timer_t * handle )
 
     static uint32_t timeout = 0;
 
-    if( p_iot->tcp_client.connected == 1 ){
+    if( p_iot->tcp_client.connected == TCP_CONNECTED ){
         switch( p_iot->connected ){
             case 0:
                 if( g_dev.idbm ){
@@ -226,19 +226,9 @@ void connect_timer_cb( uv_timer_t * handle )
                 break;
         }
     }else{
-        if( timeout++ > 10 ){
-            timeout= 0;
-            printf( "connect timeout , restart tcp client!\n" );
-
-            //uv_close((uv_handle_t*) , NULL);
-            tcp_cli_start( &p_iot->tcp_client, 1*1000); //tcp close, reconnect ....
-            uv_timer_start( &s_iot.iot_timer, connect_timer_cb, 2000, 0 );
-
-        }else{
             //wait tcp connected
             printf( "wait tcp connect ...\n" );
-            uv_timer_start( &s_iot.iot_timer, connect_timer_cb, 2000, 0 );
-        }
+            uv_timer_start( &s_iot.iot_timer, connect_timer_cb, 5000, 0 );
     }
 }
 
@@ -250,7 +240,7 @@ void heartbeat_timer_cb( uv_timer_t * handle )
 
     p_iot = (iot_t *)handle->data;
 
-    if( p_iot->tcp_client.connected ){
+    if( p_iot->tcp_client.connected == TCP_CONNECTED ){
 
         if( p_iot->connected ){
             rpt_str = iot_wrap_heartbeat_pkt( );
@@ -271,7 +261,7 @@ void rpt_timer_cb( uv_timer_t * handle )
 
     p_iot = (iot_t *)handle->data;
 
-    if( p_iot->tcp_client.connected ){
+    if( p_iot->tcp_client.connected == TCP_CONNECTED ){
 
 
         if( p_iot->connected ){
@@ -346,12 +336,37 @@ void iot_tx_cb(uv_write_t* req, int status)
     iot_tx_msg_t *msg;
     char perr;
 
+    if (status < 0) {
+        // 写入失败
+        fprintf(stderr, "Write error: %s\n", uv_strerror(status));
+        if( !uv_is_closing( (uv_handle_t*)&s_iot.tcp_client.sock ) ){
+            printf( "iot_tx_cb err: .. close sock!\n" );
+                uv_close( (uv_handle_t*)&s_iot.tcp_client.sock, NULL );
+        }else{
+                printf( "iot_tx_cb err, to close socket, ------ uv_is_closing\n" );
+        }
+        printf( "clear msg queue\n" );
+        while((msg = cQRcv( s_iot.tx_q, &perr ))){
+            free(msg);
+        }
+        s_iot.tx_busy = 0;
+        s_iot.tcp_client.connected = TCP_DISCONNECTED;
+        iot_tcp_conn_cb( &s_iot.tcp_client, 0 );
+        tcp_cli_start( &s_iot.tcp_client, 10*1000 );//tcp close, reconnect ....
+        return;
+    }
+
+    printf( "uv_tcp connect ... \n" );
     msg = cQRcv( s_iot.tx_q, &perr );
 
     if( msg  ){
         printf( "tcp write data  -- recv msg, write msg...\n" );
         uv_buf_t uvBuf = uv_buf_init( msg->base, msg->sz );
-        uv_write( &s_iot.tcp_client.write_req, (uv_stream_t*)&s_iot.tcp_client.sock, &uvBuf, 1, iot_tx_cb );
+        if( s_iot.connected ){
+            uv_write( &s_iot.tcp_client.write_req, (uv_stream_t*)&s_iot.tcp_client.sock, &uvBuf, 1, iot_tx_cb );
+        }else{
+            printf( "-- iot disconnect ---\n" );
+        }
         free( msg );
     }else{
         printf( "tcp write data  -- done, tx_bus->0\n" );
index aac5978af9285f20f48cba7599a1ce7f3c9a45cd..cfbe2f16b601947faa22690a47f52b34f0f51fd3 100644 (file)
@@ -49,7 +49,7 @@ void sound_timer_cb( uv_timer_t * handle )
 
     board = (sensor_board_t *)handle->data;
 
-    if( board->tcp_client.connected ){
+    if( board->tcp_client.connected==TCP_CONNECTED ){
         tx_buf = wrap_sensor_scan_pkt( board );
         if( tx_buf.base ){
             tcp_write_buf( &board->tcp_client, &tx_buf );
index 2fbac17d3a2b5f45c76a45add7f5ce33ee678be1..addc905aa3fefe13b740a8d84092885d106b6cf4 100644 (file)
@@ -1,25 +1,9 @@
 #include "alarmmonitor.h"
 #include "am_tcp.h"
-/*
-typedef struct{
-    const char * name;
-    uv_timer_t timer;
-    uv_loop_t  *loop;
-    char * host;
-    char port[8];
-//  int state; //0:init, 1:resolved, 2:connected,
-    int connected;
-    uv_getaddrinfo_t resolver;
-    uv_tcp_t sock;
-    uv_connect_t conn_req;
-    uv_write_t  write_req;
-
-    void * data;
-}tcp_cli_t;
-*/
 
 void tcp_cli_start( tcp_cli_t *cli, int delay_ms );
 void on_tcp_write(uv_write_t* req, int status);
+void tcp_cli_start_timer_cb(uv_timer_t * handle );
 
 void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
 {
@@ -37,7 +21,11 @@ void on_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
             p_cli->conn_cb( p_cli, 0 );
         //if (nread != UV_EOF)
             fprintf(stderr, "Read error %s\n", uv_err_name(nread));
-        uv_close((uv_handle_t*) client, NULL);
+            if( !uv_is_closing( (uv_handle_t*)client ) ){
+                uv_close((uv_handle_t*) client, NULL);
+            }else{
+                printf( "on_read err, to close socket, ------ uv_is_closing\n" );
+            }
         if( buf && buf->base )
             free(buf->base);
         //free(client);
@@ -69,27 +57,25 @@ void on_connect(uv_connect_t* req, int status)
     printf( "%s-------tcp on_connect st:%d-------\n", p_cli->name, status );
        if (status < 0)
        {
+        p_cli->connected = TCP_DISCONNECTED;
+        AM_LOGI( "[%s] TCP Connect ---------------------- FAIL", p_cli->name );
                fprintf(stderr, "Connection error %s\n", uv_strerror(status));
         uv_close( (uv_handle_t*)&p_cli->sock, NULL );
         tcp_cli_start( p_cli, 10*1000 );
                return;
        }
 
+    uv_timer_stop( &p_cli->start_timer );
+
     //uv_tcp_keepalive( &p_cli->sock, 1,  10 );
     p_cli->write_req.data = p_cli;
        uv_read_start((uv_stream_t*)&p_cli->sock, alloc_buffer, on_read );
 
-    p_cli->connected = 1;
+    p_cli->connected = TCP_CONNECTED;
     if( p_cli->conn_cb )
         p_cli->conn_cb( p_cli, 1 );
 
-#if 0
-       char* str = "hello server, this is client!";
-       uv_buf_t uvBuf = uv_buf_init(str, strlen(str));
-    fprintf(stdout, "----Write req:%p\n", &p_cli->write_req );
-       uv_write( &p_cli->write_req, (uv_stream_t*)&p_cli->sock, &uvBuf, 1, on_tcp_write);
-#endif
-       fprintf(stdout, "Connect ok\n");
+    AM_LOGI( "[%s] TCP Connect ---------------------- OK", p_cli->name );
 }
 
 void on_resolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res)
@@ -100,18 +86,23 @@ void on_resolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res)
 
     if (status < 0) {
         fprintf(stderr, "getaddrinfo callback error %s\n", uv_strerror(status) );
-        tcp_cli_start( p_cli, 5*1000 );
+        tcp_cli_start( p_cli, 10*1000 );
         return;
     }
 
     uv_ip4_name((struct sockaddr_in*) res->ai_addr, addr, 16);
-    fprintf(stderr, "on_resolved: [struct addrinfo *res:%p], - str %s\n", res, addr);
+    printf( "\n\non_resolved: [struct addrinfo *res:%p], - str %s\n", res, addr);
 
+    printf( "uv_tcp init ... start\n" );
     uv_tcp_init( p_cli->loop, &p_cli->sock);
     p_cli->conn_req.data = p_cli;
 
+    AM_LOGI( "[%s] TCP Connect ...............................", p_cli->name );
+    p_cli->connected = TCP_CONNECTING;
+    uv_timer_stop( &p_cli->start_timer );
+printf( "start conn timer ......................................\n" );
     uv_tcp_connect(&p_cli->conn_req, &p_cli->sock, (const struct sockaddr *) res->ai_addr, on_connect);
-
+    printf( "uv_tcp connect ... wait...\n" );
     uv_freeaddrinfo(res);
 }
 
@@ -132,7 +123,7 @@ void tcp_cli_resolve( uv_timer_t *handle )
 
     if (r) {
         fprintf(stderr, "getaddrinfo call error %s\n", uv_strerror( r ) );
-        tcp_cli_start( handle->data, 5*1000  );
+        tcp_cli_start( handle->data, 10*1000  );
     }
 }
 
@@ -141,10 +132,9 @@ void tcp_cli_init( tcp_cli_t * cli, char * host, uint16_t port, char *name, tcp_
     cli->loop = uv_default_loop();
     cli->host = strdup( host  );
     sprintf( cli->port, "%d", port );
-    uv_timer_init( cli->loop, &cli->timer);
-    cli->timer.data = cli;
-
-    //cli->state = 0;
+    uv_timer_init( cli->loop, &cli->start_timer);
+    cli->start_timer.data = cli;
+    cli->connected = TCP_DISCONNECTED;
 
     cli->sock.data = cli;
     cli->name = strdup( name );
@@ -153,26 +143,47 @@ void tcp_cli_init( tcp_cli_t * cli, char * host, uint16_t port, char *name, tcp_
     cli->conn_cb = conn_cb;
 }
 
-void tcp_cli_start( tcp_cli_t *cli, int delay_ms )
+void tcp_cli_start_timer_cb(uv_timer_t * handle )
+{
+    int r;
+    struct addrinfo hints;
+
+    tcp_cli_t *p_cli = (tcp_cli_t*)handle->data;
+
+    hints.ai_family = PF_INET;
+    hints.ai_socktype = SOCK_STREAM;
+    hints.ai_protocol = IPPROTO_TCP;
+    hints.ai_flags = 0;
+
+    p_cli->resolver.data = p_cli;
+    r = uv_getaddrinfo( handle->loop, &p_cli->resolver, on_resolved, p_cli->host, p_cli->port, &hints );
+
+    if (r) {
+        fprintf(stderr, "getaddrinfo call error %s\n", uv_strerror( r ) );
+        //restart ...
+        tcp_cli_start( handle->data, 10*1000  );
+    }
+}
+
+void tcp_cli_target( tcp_cli_t *cli, char *host, uint16_t port )
 {
-//    if( cli->state == 0 ){
-    uv_timer_start( &cli->timer, tcp_cli_resolve, delay_ms, 0 );
-//    }else if( cli->state == 1 ){
-        //uv_timer_start( cli->timer, tcp_cli_connect, 0, 0 );
-//        uv_timer_start( &cli->timer, tcp_cli_resolve, delay_ms, 0 );
-//    }
+    if( host && port>0 ){
+        if( cli->host )
+            free( cli->host ); 
+        cli->host = strdup( host );
+        sprintf( cli->port, "%d", port );
+    }
 }
 
-void _tcp_write_data( tcp_cli_t *cli, char *tx_data,  int tx_size )
+void tcp_cli_start( tcp_cli_t *cli, int delay_ms )
 {
-       uv_buf_t uvBuf = uv_buf_init( tx_data, tx_size );
-       uv_write( &cli->write_req, (uv_stream_t*)&cli->sock, &uvBuf, 1, on_tcp_write);
-    printf( "tcp write data  -- %s\n", tx_data );
+    cli->connected = TCP_DISCONNECTED;
+    uv_timer_start( &cli->start_timer, tcp_cli_start_timer_cb, delay_ms, 0 );
 }
 
 void tcp_write_buf( tcp_cli_t *cli, uv_buf_t *uvBuf )
 {
+    printf( "[%s]tcp write buf  -- %p -- %ld\n", cli->name, uvBuf->base, uvBuf->len );
        uv_write( &cli->write_req, (uv_stream_t*)&cli->sock, uvBuf, 1, on_tcp_write);
-    ///printf( "tcp write buf  -- %p -- %ld\n",  uvBuf->base, uvBuf->len );
 }