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
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-30 13:30:58 +01:00
1 parent 381a7f85a0
commit f1315a8790
4 files changed
+71 -3

No files matched your search

+41 -3
View File
@@ -15305,11 +15305,49 @@ LRESULT CMy2015RemoteDlg::OnOpenClientLogDialog(WPARAM wParam, LPARAM lParam)
std::string initLog = pInitLog ? *pInitLog : "";
delete pInitLog;
CONTEXT_OBJECT* ctx = reinterpret_cast<CONTEXT_OBJECT*>(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<CClientLog*>(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<CClientLog, IDD_DIALOG_CLIENT_LOG>(0, lParam);
CONTEXT_OBJECT* ctx = reinterpret_cast<CONTEXT_OBJECT*>(lParam);
if (ctx && ctx->hDlg && !initLog.empty())
static_cast<CClientLog*>(ctx->hDlg)->AppendLog(initLog);
if (ctx && ctx->hDlg) {
if (cid != 0)
m_LogDlgWindows[cid] = static_cast<CDialogBase*>(ctx->hDlg)->GetSafeHwnd();
if (!initLog.empty())
static_cast<CClientLog*>(ctx->hDlg)->AppendLog(initLog);
}
return 0;
}
+3
View File
@@ -510,6 +510,9 @@ public:
CBitmap m_bmCompressZlib; // 实验功能→线缆压缩:ZLIB
uint64_t m_superID;
std::map<HWND, CDialogBase *> m_RemoteWnds;
// 每台主机只保留一个客户端运行日志窗口:clientID -> 窗口句柄。
// 窗口关掉后这里可能留下失效句柄,下次打开时会用 IsWindow/ClientID 校验并清理。
std::map<uint64_t, HWND> m_LogDlgWindows;
FileTransformCmd m_CmdList;
CDialogBase* GetRemoteWindow(HWND hWnd);
CDialogBase* GetRemoteWindow(CDialogBase* dlg);
+24
View File
@@ -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;
+3
View File
@@ -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);