From f1315a8790d19421e73cc0330865b9551f0467bc Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Mon, 30 Nov 2026 13:30:58 +0100 Subject: [PATCH] Fix: let the client log window close and reuse itself CClientLog was the only sub-connection dialog without ON_WM_CLOSE, so the close button did nothing and the window stayed until the master exited. It now wires WM_CLOSE like the other dialogs: it detaches the context's dialog pointer and cancels the log sub-connection before destroying, so the client's three-second log push loop stops instead of posting increments to a destroyed window. Every menu click also opened a new dialog and a new log sub-connection: the client starts one per COMMAND_QUERY_LOG, and each arrives on a new context whose hDlg is empty, so the existing "append to open dialog" path never matched. The master now keeps one log window per client id; a repeat request closes the previous sub-connection, rebinds the window to the new one and refreshes the snapshot instead of opening another. Verified on the Debug server: WM_CLOSE destroys the window, two extra clicks keep a single window, and the main connection stays online. Co-Authored-By: deepseek-flash --- server/2015Remote/2015RemoteDlg.cpp | 44 +++++++++++++++++++++++++++-- server/2015Remote/2015RemoteDlg.h | 3 ++ server/2015Remote/CClientLog.cpp | 24 ++++++++++++++++ server/2015Remote/CClientLog.h | 3 ++ 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index f7c78c5..f3b3e72 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -15305,11 +15305,49 @@ LRESULT CMy2015RemoteDlg::OnOpenClientLogDialog(WPARAM wParam, LPARAM lParam) std::string initLog = pInitLog ? *pInitLog : ""; delete pInitLog; + CONTEXT_OBJECT* ctx = reinterpret_cast(lParam); + const uint64_t cid = ctx ? ctx->GetClientID() : 0; + + // 同一主机只保留一个日志窗口。每点一次菜单客户端都会新开一条日志子连接, + // 旧实现每来一条就新建窗口,点七八次就有七八个窗口、七八条推送连接。 + // 复用旧窗口:关掉旧子连接,把窗口接到新子连接上,并刷新整份快照。 + if (cid != 0) { + auto it = m_LogDlgWindows.find(cid); + if (it != m_LogDlgWindows.end()) { + CClientLog* dlg = dynamic_cast(CWnd::FromHandlePermanent(it->second)); + if (dlg != nullptr && ::IsWindow(dlg->GetSafeHwnd()) && dlg->GetClientID() == cid) { + CONTEXT_OBJECT* oldCtx = dlg->m_ContextObject; + if (oldCtx && oldCtx != ctx) { + if (oldCtx->hDlg == dlg) { + oldCtx->hDlg = nullptr; + oldCtx->hWnd = nullptr; + } + oldCtx->CancelIO(); + // 等旧的 OnReceiveComplete 收尾,再换上下文,避免并发读 m_ContextObject。 + for (int i = 0; i < 50 && dlg->IsProcessing(); ++i) + Sleep(10); + } + if (ctx) + dlg->UpdateContext(ctx, cid); + dlg->ResetLog(initLog); + if (dlg->IsIconic()) + dlg->ShowWindow(SW_RESTORE); + dlg->ShowWindow(SW_SHOW); + dlg->SetForegroundWindow(); + return 0; + } + m_LogDlgWindows.erase(it); + } + } + OpenDialog(0, lParam); - CONTEXT_OBJECT* ctx = reinterpret_cast(lParam); - if (ctx && ctx->hDlg && !initLog.empty()) - static_cast(ctx->hDlg)->AppendLog(initLog); + if (ctx && ctx->hDlg) { + if (cid != 0) + m_LogDlgWindows[cid] = static_cast(ctx->hDlg)->GetSafeHwnd(); + if (!initLog.empty()) + static_cast(ctx->hDlg)->AppendLog(initLog); + } return 0; } diff --git a/server/2015Remote/2015RemoteDlg.h b/server/2015Remote/2015RemoteDlg.h index af76eeb..f7014cc 100644 --- a/server/2015Remote/2015RemoteDlg.h +++ b/server/2015Remote/2015RemoteDlg.h @@ -510,6 +510,9 @@ public: CBitmap m_bmCompressZlib; // 实验功能→线缆压缩:ZLIB uint64_t m_superID; std::map m_RemoteWnds; + // 每台主机只保留一个客户端运行日志窗口:clientID -> 窗口句柄。 + // 窗口关掉后这里可能留下失效句柄,下次打开时会用 IsWindow/ClientID 校验并清理。 + std::map m_LogDlgWindows; FileTransformCmd m_CmdList; CDialogBase* GetRemoteWindow(HWND hWnd); CDialogBase* GetRemoteWindow(CDialogBase* dlg); diff --git a/server/2015Remote/CClientLog.cpp b/server/2015Remote/CClientLog.cpp index bc1d2c9..dcda81a 100644 --- a/server/2015Remote/CClientLog.cpp +++ b/server/2015Remote/CClientLog.cpp @@ -25,6 +25,7 @@ void CClientLog::DoDataExchange(CDataExchange* pDX) } BEGIN_MESSAGE_MAP(CClientLog, CDialogBase) + ON_WM_CLOSE() ON_WM_DESTROY() ON_WM_SIZE() ON_MESSAGE(WM_REPORT_CLIENT_LOG, OnReportLog) @@ -50,8 +51,25 @@ void CClientLog::OnSize(UINT nType, int cx, int cy) m_editLog.MoveWindow(7, 7, cx - 14, cy - 14); } +void CClientLog::OnClose() +{ + // 关闭前先断开日志子连接:客户端日志推送线程以 IsConnected 为循环条件, + // 不关子连接会一直往已销毁的窗口推增量;同时把上下文里的对话框指针摘掉, + // 避免 Close 期间的迟到包再次 PostMessage 到已释放的对象。 + if (m_ContextObject && m_ContextObject->hDlg == this) { + m_ContextObject->hDlg = nullptr; + m_ContextObject->hWnd = nullptr; + } + CancelIO(); + CDialogBase::OnClose(); +} + void CClientLog::OnDestroy() { + if (m_ContextObject && m_ContextObject->hDlg == this) { + m_ContextObject->hDlg = nullptr; + m_ContextObject->hWnd = nullptr; + } if (m_ContextObject && m_bConnected) { BYTE cmd = COMMAND_BYE; m_ContextObject->Send2Client(&cmd, 1); @@ -59,6 +77,12 @@ void CClientLog::OnDestroy() CDialogBase::OnDestroy(); } +void CClientLog::ResetLog(const std::string& text) +{ + m_editLog.SetWindowText(_T("")); + AppendLog(text); +} + void CClientLog::AppendLog(const std::string& text) { if (text.empty()) return; diff --git a/server/2015Remote/CClientLog.h b/server/2015Remote/CClientLog.h index 3d09c05..dab1dd6 100644 --- a/server/2015Remote/CClientLog.h +++ b/server/2015Remote/CClientLog.h @@ -12,10 +12,13 @@ public: virtual void OnReceiveComplete(); void AppendLog(const std::string& text); + // 复用窗口(同一主机再次打开日志)时清空并重填整份快照 + void ResetLog(const std::string& text); protected: virtual BOOL OnInitDialog(); virtual void DoDataExchange(CDataExchange* pDX); + afx_msg void OnClose(); afx_msg void OnDestroy(); afx_msg void OnSize(UINT nType, int cx, int cy); afx_msg LRESULT OnReportLog(WPARAM wParam, LPARAM lParam);