From ba35ea8ae35fff4a6179e5cb5a14f33cbd84c533 Mon Sep 17 00:00:00 2001 From: wangfq Date: Wed, 1 Jul 2026 11:18:43 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8C=89=20WCH=20=E5=AE=98?= =?UTF-8?q?=E6=96=B9=20TCPServer=20=E4=BE=8B=E7=A8=8B=E9=87=8D=E5=86=99=20?= =?UTF-8?q?TCP=20JSON=20server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 核心变更:去掉 g_json_socket_client,listen socket 直接承载收发数据。 参考 EVT/EXAM/ETH/TCPServer 例程: - 创建 PROTO_TYPE_TCP socket → WCHNET_SocketListen - 同一 socket 处理 CONNECT + RECV + DISCONNECT + TIMEOUT - 不需要 'accepted socket' 检测 移除的复杂逻辑: - g_json_socket_client 变量及所有 'newly accepted' 检测代码 - WCHNET_HandleSockInt 第二路由条件(socketid!=listen,!=TCP,!=UDP 那串) - tcp_json_handle_sock_int 中 listen/client 分离处理 - listen==client overlap 的迂回保护 修改文件: - tcp_json_srv.h: 移除 g_json_socket_client extern - tcp_json_srv.c: 移除 g_json_socket_client,handler 简化为 4 个 if - net_srv.c: 路由简化为仅 socketid==g_json_socket_listen --- .gitignore | 4 +- .../APP/include/tcp_json_srv.h | 3 +- .../OnlyUpdateApp_Peripheral/APP/net_srv.c | 11 +-- .../APP/tcp_json_srv.c | 87 ++++++------------- 4 files changed, 34 insertions(+), 71 deletions(-) diff --git a/.gitignore b/.gitignore index cfc0343..d152f72 100644 --- a/.gitignore +++ b/.gitignore @@ -67,7 +67,6 @@ mdk_v5/ *.url *.txt - at32_ide_proj/ *.uvguix* *.cproject @@ -83,3 +82,6 @@ at32_ide_proj/ *.pdf *.PDF + +wchreference/ + diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/tcp_json_srv.h b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/tcp_json_srv.h index 00528be..3a6304d 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/tcp_json_srv.h +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/tcp_json_srv.h @@ -49,8 +49,7 @@ typedef enum { *===========================================================================*/ extern uint8_t SocketId_TCP; extern uint8_t SocketId_UDP; -extern uint8_t g_json_socket_listen; // listen socket ID -extern uint8_t g_json_socket_client; // accepted client socket ID (0xFF = none) +extern uint8_t g_json_socket_listen; // TCP socket ID (listen + data) extern TcpJsonAuthState g_json_auth_state; extern uint32_t g_json_auth_timer; // ms timer for auth timeout extern uint8_t g_json_pwd_retry; // password retry counter diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c index a938752..cbe5ec7 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c @@ -481,15 +481,8 @@ void WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat) { uint8_t i; - // Route JSON protocol socket events (listen socket, client socket) - if (socketid == g_json_socket_listen || socketid == g_json_socket_client) { - tcp_json_handle_sock_int(socketid, intstat); - return; - } - // Also catch newly accepted TCP connections that might be JSON clients - // Exclude: listen socket, SSC TCP, UDP - if ((intstat & SINT_STAT_CONNECT) && socketid != g_json_socket_listen - && socketid != SocketId_TCP && socketid != SocketId_UDP) { + // Route JSON protocol socket events (same socket handles listen + data) + if (socketid == g_json_socket_listen) { tcp_json_handle_sock_int(socketid, intstat); return; } diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c index b60d248..78f3048 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c @@ -24,8 +24,7 @@ /*=========================================================================== * Global State *===========================================================================*/ -uint8_t g_json_socket_listen = 0xFF; // listen socket ID -uint8_t g_json_socket_client = 0xFF; // accepted client socket ID +uint8_t g_json_socket_listen = 0xFF; // TCP socket ID — handles both listen + client data TcpJsonAuthState g_json_auth_state = JSON_STATE_WAIT_AUTH; uint32_t g_json_auth_timer = 0; uint8_t g_json_pwd_retry = 0; @@ -624,8 +623,22 @@ void tcp_json_srv_init(void) { } void tcp_json_handle_sock_int(uint8_t socketid, uint8_t intstat) { - // === RECV: Always process for the client socket (handles listen==client overlap) === - if (socketid == g_json_socket_client && (intstat & SINT_STAT_RECV)) { + // Only process events on our TCP listen/data socket + if (socketid != g_json_socket_listen) return; + + // === CONNECT: client connected — configure recv buffer, init auth state === + if (intstat & SINT_STAT_CONNECT) { + WCHNET_ModifyRecvBuf(socketid, (uint32_t)g_json_wchnet_buf, RECE_BUF_LEN); + g_json_auth_state = JSON_STATE_WAIT_AUTH; + g_json_pwd_retry = 0; + g_json_auth_timer = mstick(); + g_json_recv_len = 0; + memset(g_json_recv_buf, 0, sizeof(g_json_recv_buf)); + PRINT("JSON: Client connected on socket %d, recv buf configured\n", socketid); + } + + // === RECV: read and process incoming data === + if (intstat & SINT_STAT_RECV) { uint32_t recv_len = WCHNET_SocketRecvLen(socketid, NULL); if (recv_len > 0) { uint16_t space = TCP_JSON_RECV_BUF_LEN - g_json_recv_len; @@ -652,76 +665,32 @@ void tcp_json_handle_sock_int(uint8_t socketid, uint8_t intstat) { } } - // === Listen socket events (log only, no action needed) === - if (socketid == g_json_socket_listen && socketid != g_json_socket_client) { - if (intstat & SINT_STAT_CONNECT) { - PRINT("JSON: Listen socket got CONNECT\n"); - } - if (intstat & SINT_STAT_DISCONNECT) { - PRINT("JSON: Listen socket disconnect (unexpected)\n"); - } - } - - // === Newly accepted client (CONNECT on unknown socket, but NOT the listen socket) === - if ((intstat & SINT_STAT_CONNECT) && socketid != g_json_socket_client - && socketid != g_json_socket_listen - && socketid != SocketId_TCP && socketid != SocketId_UDP) { - g_json_socket_client = socketid; + // === DISCONNECT: client disconnected, reset state === + if (intstat & SINT_STAT_DISCONNECT) { + PRINT("JSON: Client disconnected (id=%d)\n", socketid); g_json_auth_state = JSON_STATE_WAIT_AUTH; g_json_pwd_retry = 0; - g_json_auth_timer = mstick(); g_json_recv_len = 0; - memset(g_json_recv_buf, 0, sizeof(g_json_recv_buf)); - WCHNET_ModifyRecvBuf(socketid, (uint32_t)g_json_wchnet_buf, RECE_BUF_LEN); - PRINT("JSON: Client accepted on socket %d, recv buf configured\n", socketid); - return; } - // === Client socket events (non-RECV: CONNECT, DISCONNECT, TIMEOUT) === - if (socketid == g_json_socket_client) { - if (intstat & SINT_STAT_CONNECT) { - PRINT("JSON: Client socket connected (id=%d)\n", socketid); - g_json_auth_state = JSON_STATE_WAIT_AUTH; - g_json_pwd_retry = 0; - g_json_auth_timer = mstick(); - g_json_recv_len = 0; - } - if (intstat & SINT_STAT_DISCONNECT) { - PRINT("JSON: Client disconnected (id=%d)\n", socketid); - g_json_socket_client = 0xFF; - g_json_auth_state = JSON_STATE_WAIT_AUTH; - g_json_pwd_retry = 0; - g_json_recv_len = 0; - } - if (intstat & SINT_STAT_TIM_OUT) { - PRINT("JSON: Client timeout (id=%d)\n", socketid); - g_json_socket_client = 0xFF; - g_json_auth_state = JSON_STATE_WAIT_AUTH; - g_json_pwd_retry = 0; - g_json_recv_len = 0; - } - return; - } - - // === Fallback: connect on a socket we haven't tracked yet === - if (intstat & SINT_STAT_CONNECT && g_json_socket_client == 0xFF && socketid != g_json_socket_listen) { - g_json_socket_client = socketid; + // === TIMEOUT: connection timed out === + if (intstat & SINT_STAT_TIM_OUT) { + PRINT("JSON: Client timeout (id=%d)\n", socketid); g_json_auth_state = JSON_STATE_WAIT_AUTH; g_json_pwd_retry = 0; - g_json_auth_timer = mstick(); g_json_recv_len = 0; - memset(g_json_recv_buf, 0, sizeof(g_json_recv_buf)); - PRINT("JSON: Client accepted on socket %d (fallback)\n", socketid); } } void tcp_json_poll(void) { + // Only poll if we have an active connection (socket configured) + if (g_json_socket_listen == 0xFF) return; + // Auth timeout check - if (g_json_auth_state == JSON_STATE_WAIT_AUTH && g_json_socket_client != 0xFF) { + if (g_json_auth_state == JSON_STATE_WAIT_AUTH) { if (mstick() - g_json_auth_timer > TCP_JSON_AUTH_TIMEOUT_MS) { PRINT("JSON: Auth timeout, closing connection\n"); - WCHNET_SocketClose(g_json_socket_client, TCP_CLOSE_NORMAL); - g_json_socket_client = 0xFF; + WCHNET_SocketClose(g_json_socket_listen, TCP_CLOSE_NORMAL); g_json_auth_state = JSON_STATE_WAIT_AUTH; g_json_pwd_retry = 0; g_json_recv_len = 0;