调整了设备的移除机制

This commit is contained in:
2025-10-15 13:36:31 +08:00
parent fbbb96a64a
commit 8fb922e8c6

View File

@@ -73,7 +73,7 @@ void ClientContext::stop_timers() {
void ClientContext::close_handles() { void ClientContext::close_handles() {
if (!uv_is_closing((uv_handle_t*)&client)) { if (!uv_is_closing((uv_handle_t*)&client)) {
uv_close((uv_handle_t*)&client, nullptr); uv_close((uv_handle_t*)&client, on_close);
} }
if (!uv_is_closing((uv_handle_t*)&timer)) { if (!uv_is_closing((uv_handle_t*)&timer)) {
uv_close((uv_handle_t*)&timer, nullptr); uv_close((uv_handle_t*)&timer, nullptr);
@@ -775,8 +775,19 @@ void ClientManager::remove_device(const std::string& device_id) {
return; return;
} }
// 关闭连接并移除 auto& ctx = it->second;
it->second->shutdown = true; it->second->shutdown = true;
// 确保所有定时器都停止
ctx->stop_timers();
{
std::lock_guard<std::mutex> state_lock(ctx->state_mutex_);
std::queue<StateAction> empty;
std::swap(ctx->action_queue_, empty);
ctx->current_state_ = DeviceState::IDLE;
}
// 关闭连接并移除
it->second->close_handles(); it->second->close_handles();
clients_.erase(it); clients_.erase(it);
std::cout << "[Device " << device_id << "] Removed successfully\n"; std::cout << "[Device " << device_id << "] Removed successfully\n";