diff --git a/LFtid1056/client2.cpp b/LFtid1056/client2.cpp index 412be7b..4add88e 100644 --- a/LFtid1056/client2.cpp +++ b/LFtid1056/client2.cpp @@ -776,10 +776,12 @@ void ClientManager::remove_device(const std::string& device_id) { } auto& ctx = it->second; - it->second->shutdown = true; + ctx->shutdown = true; - // 确保所有定时器都停止 + // 1. 先停止所有定时器,避免定时器回调干扰 ctx->stop_timers(); + + // 2. 清空动作队列,防止新的操作被触发 { std::lock_guard state_lock(ctx->state_mutex_); std::queue empty; @@ -787,10 +789,37 @@ void ClientManager::remove_device(const std::string& device_id) { ctx->current_state_ = DeviceState::IDLE; } - // 关闭连接并移除 - it->second->close_handles(); + // 3. 关闭TCP客户端句柄(关键修改:确保只关闭一次且正确同步) + if (!uv_is_closing((uv_handle_t*)&ctx->client)) { + // 设置句柄数据为nullptr,避免关闭回调中访问已释放资源 + ctx->client.data = nullptr; + uv_close((uv_handle_t*)&ctx->client, [](uv_handle_t* handle) { + // 回调中不再操作ClientContext,因为可能已被释放 + std::cout << "TCP handle closed successfully\n"; + }); + } + + // 4. 关闭定时器句柄(如果未关闭) + if (!uv_is_closing((uv_handle_t*)&ctx->timer)) { + ctx->timer.data = nullptr; + uv_close((uv_handle_t*)&ctx->timer, nullptr); + } + if (!uv_is_closing((uv_handle_t*)&ctx->reconnect_timer)) { + ctx->reconnect_timer.data = nullptr; + uv_close((uv_handle_t*)&ctx->reconnect_timer, nullptr); + } + + // 5. 从管理器中移除设备(关键修改:延迟到事件循环处理完关闭操作后再释放) + // 将智能指针转移到栈上,确保在本函数结束后才销毁 + auto ctx_ptr = std::move(it->second); clients_.erase(it); - std::cout << "[Device " << device_id << "] Removed successfully\n"; + + std::cout << "[Device " << device_id << "] Removal process initiated\n"; + + // 6. 强制事件循环处理一次关闭操作(非阻塞) + if (loop_) { + uv_run(loop_, UV_RUN_NOWAIT); + } } void ClientManager::stop_all() { diff --git a/LFtid1056/main_thread.cpp b/LFtid1056/main_thread.cpp index 9e0d208..1463de5 100644 --- a/LFtid1056/main_thread.cpp +++ b/LFtid1056/main_thread.cpp @@ -154,26 +154,26 @@ void* client_manager_thread(void* arg) { printf("Started client connections\n"); // - /*std::vector points1 = { - {"P001", "Main Voltage", "D001",1 ,1, 1, 1, 1,"0.38k",0}, - {"P002", "Backup Voltage", "D001",2 ,1, 1, 1, 1,"0.38k",0} - }; - std::vector points2 = { - {"P003", "Main Voltage", "D002",1 ,1, 1, 1, 1,"0.38k",0}, - {"P004", "Backup Voltage", "D002",2 ,1, 1, 1, 1,"0.38k",0} - };*/ - //00B78DA800D6 00-B7-8D-01-79-06 00-B7-8D-A8-00-D6 00-B7-8D-01-71-09 00-B7-8D-01-88-7f - // װб - /*std::vector devices = { - { - "D001", "Primary Device", "Model-X", "00-B7-8D-01-88-7f", - 1, points1,true - }, - { - "D002", "Primary Device1", "Model-X1", "00-B7-8D-01-71-09", - 1, points2,true - } - };*/ + //std::vector points1 = { + // {"P001", "Main Voltage", "D001",1 ,1, 1, 1, 1,"0.38k",0}, + // {"P002", "Backup Voltage", "D001",2 ,1, 1, 1, 1,"0.38k",0} + //}; + //std::vector points2 = { + // {"P003", "Main Voltage", "D002",1 ,1, 1, 1, 1,"0.38k",0}, + // {"P004", "Backup Voltage", "D002",2 ,1, 1, 1, 1,"0.38k",0} + //}; + ////00B78DA800D6 00-B7-8D-01-79-06 00-B7-8D-A8-00-D6 00-B7-8D-01-71-09 00-B7-8D-01-88-7f + //// װб + //std::vector devices = { + // { + // "D001", "Primary Device", "Model-X", "00-B7-8D-01-88-7f", + // 1, points1,true + // }, + // { + // "D002", "Primary Device1", "Model-X1", "00-B7-8D-01-71-09", + // 1, points2,true + // } + //}; // 100װ //std::vector test_devices = generate_test_devices(100); @@ -408,23 +408,47 @@ int main(int argc ,char** argv) {// } };*/ + // + //std::vector points1 = { + // {"P001", "Main Voltage", "D001",1 ,1, 1, 1, 1,"0.38k",0}, + // {"P002", "Backup Voltage", "D001",2 ,1, 1, 1, 1,"0.38k",0} + //}; + //std::vector points2 = { + // {"P003", "Main Voltage", "D002",1 ,1, 1, 1, 1,"0.38k",0}, + // {"P004", "Backup Voltage", "D002",2 ,1, 1, 1, 1,"0.38k",0} + //}; + ////00B78DA800D6 00-B7-8D-01-79-06 00-B7-8D-A8-00-D6 00-B7-8D-01-71-09 00-B7-8D-01-88-7f + //// װб + //std::vector devices = { + // { + // "D001", "Primary Device", "Model-X", "00-B7-8D-01-88-7f", + // 1, points1,true + // }, + // { + // "D002", "Primary Device1", "Model-X1", "00-B7-8D-01-71-09", + // 1, points2,true + // } + //}; + // socket״̬ static int queue_monitor = 0; + static bool flag = false; //static int count = 3; - if (++queue_monitor >= 20) { // һ豸 + if (++queue_monitor >= 60) { // һ豸 printf("Message queue size: %zu\n", message_queue.size()); queue_monitor = 0; - - /*for (const auto& device : devices) { - //ClientManager::instance().add_device(device); - }*/ - - /*std::vector test_devices = generate_test_devices(count); - count++; - - for (const auto& device : test_devices) { + /*if (flag) { + flag = false; + for (const auto& device : devices) { + ClientManager::instance().add_device(device); + } + } + else { + flag = true; ClientManager::instance().remove_device("D001"); + ClientManager::instance().remove_device("D002"); }*/ + } }