return;
}
- // 停止服务器
- ws_server_stop(handle);
-
// 清理资源
ws_server_cleanup(handle);
if (g_ws_servers[handle->type] == handle) {
g_ws_servers[handle->type] = NULL;
}
+ printf("WebSocket server destroyed: type=%d\n", handle->type);
free(handle);
- printf("WebSocket server destroyed: type=%d\n", handle->type);
}
/**
task_cfg.attr.stack_size =0x2000;// handle->config.stack_size;
task_cfg.func = ws_server_thread_func;
task_cfg.use_mem = USE_EXTEND_MEM;
+ task_cfg.task_handle = NULL; // 初始化为NULL,osThreadNew会设置
// 创建线程
errcode_t ret = uapi_create_osThread_custom(&task_cfg);
return ret;
}
+ // 保存线程句柄(task_cfg.task_handle 本身就是线程句柄)
+ handle->thread_handle = (osThreadId_t)task_cfg.task_handle;
+
handle->need_stop = 0;
printf("WebSocket server started: type=%d %s\n", handle->type,handle->config.name);
}
/**
- * @brief 停止WebSocket服务器
+ * @brief 停止WebSocket服务器(同步接口,等待线程退出)
*/
void ws_server_stop(ws_server_handle_t *handle)
{
send_websocket_message(handle->client, (uint8_t *)"WS_CLOSE", strlen("WS_CLOSE"), WS_CLOSE);
}
+ printf("WebSocket server stopping: type=%d\n", handle->type);
+
+ // 使用 osThreadGetState 判断线程状态,等待线程退出
+ if (handle->thread_handle != NULL) {
+ uint32_t wait_count = 0;
+ const uint32_t max_wait_count = 1000; // 最多等待1000次,每次10ms,总共10秒
+ const uint32_t check_interval_ms = 10;
+
+ while (wait_count < max_wait_count) {
+ osThreadState_t thread_state = osThreadGetState(handle->thread_handle);
+
+ // 检查线程是否已经退出
+ // Running、Blocked、Ready 表示线程还在运行
+ // Terminated、Inactive、Error 表示线程已经停止
+ bool thread_stopped = (thread_state == osThreadTerminated ||
+ thread_state == osThreadInactive ||
+ thread_state == osThreadError);
+
+ if (thread_stopped) {
+ // 线程状态已停止,检查线程名是否匹配,防止线程ID被复用
+ const char *thread_name = NULL;
+
+ // 只有在非Error状态下才尝试获取线程名
+ if (thread_state != osThreadError) {
+ thread_name = osThreadGetName(handle->thread_handle);
+ }
+
+ // 如果线程名为NULL(线程已删除或被错误状态)或线程名不匹配(ID被复用),认为线程已退出
+ if (thread_name == NULL || thread_state == osThreadError) {
+ // 线程已被删除或处于错误状态
+ printf("WebSocket server thread deleted/errored: type=%d, state=%d, wait_count=%d\n",
+ handle->type, thread_state, wait_count);
+ break;
+ } else if (strcmp(thread_name, handle->config.name) != 0) {
+ // 线程ID已被复用,原来的线程肯定已经退出
+ printf("WebSocket server thread ID reused (expected:%s, got:%s), original thread exited: type=%d\n",
+ handle->config.name, thread_name, handle->type);
+ break;
+ } else {
+ // 线程名匹配且状态已停止,说明是本线程已停止
+ printf("WebSocket server thread stopped: type=%d, state=%d, name=%s, wait_count=%d\n",
+ handle->type, thread_state, thread_name, wait_count);
+ break;
+ }
+ }
+
+ // 线程还在运行(Running 或 Blocked),继续等待
+ wait_count++;
+ msleep(check_interval_ms);
+ }
+
+ if (wait_count >= max_wait_count) {
+ printf("WebSocket server thread wait timeout: type=%d\n", handle->type);
+ // 如果超时,尝试强制终止线程
+ osThreadTerminate(handle->thread_handle);
+ }
+
+ handle->thread_handle = NULL;
+ }
+
printf("WebSocket server stopped: type=%d\n", handle->type);
}
cleanup:
msleep(100);
- ws_server_disconnect(handle);
- ws_server_cleanup(handle);
+
+ // 断开连接
+ if (handle && handle->client) {
+ close_websocket_client(handle->client);
+ handle->client = NULL;
+ }
// 清理对应服务类型的全局变量
if (handle) {
+ handle->state = WS_SERVER_STATE_DISCONNECTED;
g_thread_handles[handle->type] = NULL;
printf("WebSocket server thread ended: type=%d\n", handle->type);
}