From dd2ccfcf8caab953bd8540a034f162468774463f Mon Sep 17 00:00:00 2001 From: fanlusky <1730976608@qq.com> Date: Mon, 6 Jul 2026 10:17:45 +0800 Subject: [PATCH] Revert some IPC changes and add some logging. --- server/src/ipc/event_listener.cpp | 40 +++++++++++++++---------------- server/src/ipc/ipc.cpp | 12 ++++++++++ 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/server/src/ipc/event_listener.cpp b/server/src/ipc/event_listener.cpp index adfa1896..b3deb65d 100644 --- a/server/src/ipc/event_listener.cpp +++ b/server/src/ipc/event_listener.cpp @@ -196,6 +196,18 @@ void LogPipeEvent(const wchar_t *pipe_name, UINT event_type, UINT keycode, WCHAR keycode, static_cast(wch), modifiers_down); } +void LogClientLifecycle(const wchar_t *phase, uint64_t client_id, UINT event_type) +{ + FANY_IPC_LOGF(L"[msime]: [ipc] client lifecycle: phase={}, client_id={}, event_type={}", phase, client_id, + event_type); +} + +void LogClientRouting(uint64_t client_id, UINT event_type, bool is_active) +{ + FANY_IPC_LOGF(L"[msime]: [ipc] client routing: client_id={}, event_type={}, is_active={}", client_id, event_type, + is_active); +} + bool WaitForPipeClient(HANDLE pipe) { BOOL connected = ConnectNamedPipe(pipe, NULL); @@ -409,6 +421,7 @@ void EnqueuePinCandidateTask(const std::string &pinyin, const std::string &word) void SendCurrentDataToActiveTsf() { const UINT msg_type = Global::MsgTypeToTsf; + FANY_IPC_LOGF(L"[msime]: [ipc] send-current-data: msg_type={}, text={}", msg_type, ::Global::candidate_ui.selected_text); SendToTsfViaNamedpipe(msg_type, ::Global::candidate_ui.selected_text); if (msg_type == Global::DataFromServerMsgType::Normal) { @@ -470,17 +483,20 @@ void MainPipeClientThread(HANDLE clientPipe) clientId = pipeData.client_id; if (pipeData.event_type == FanyImePipeEventType::ClientHello) { + LogClientLifecycle(L"hello", clientId, pipeData.event_type); RegisterMainPipeClient(clientId, clientPipe); continue; } if (pipeData.event_type == FanyImePipeEventType::ClientActivated) { + LogClientLifecycle(L"activated", clientId, pipeData.event_type); RegisterMainPipeClient(clientId, clientPipe); ActivatePipeClient(clientId); continue; } if (pipeData.event_type == FanyImePipeEventType::ClientDeactivated) { + LogClientLifecycle(L"deactivated", clientId, pipeData.event_type); if (IsActivePipeClient(clientId)) { EnqueueTask(TaskType::ClientDeactivated, pipeData); @@ -489,7 +505,9 @@ void MainPipeClientThread(HANDLE clientPipe) continue; } - if (!IsActivePipeClient(clientId)) + const bool isActiveClient = IsActivePipeClient(clientId); + LogClientRouting(clientId, pipeData.event_type, isActiveClient); + if (!isActiveClient) { FANY_IPC_LOGF(L"[msime]: [ipc] ignored inactive main-pipe event: client_id={}, type={}", clientId, pipeData.event_type); @@ -628,11 +646,10 @@ void ToTsfWorkerThreadPipeEventListenerLoopThread() void RegisteredPipeMonitorThread(HANDLE clientPipe, UINT pipeRole) { - const wchar_t *pipeName = pipeRole == FanyImePipeRole::ToTsf ? L"to-tsf-pipe" : L"to-tsf-worker-pipe"; FanyImePipeHello hello = {}; if (!ReadPipeHello(clientPipe, hello)) { - LogPipeReadFailure(pipeName, 0); + LogPipeReadFailure(pipeRole == FanyImePipeRole::ToTsf ? L"to-tsf-pipe" : L"to-tsf-worker-pipe", 0); DisconnectNamedPipe(clientPipe); CloseHandle(clientPipe); return; @@ -646,23 +663,6 @@ void RegisteredPipeMonitorThread(HANDLE clientPipe, UINT pipeRole) { RegisterToTsfWorkerThreadPipeClient(hello.client_id, clientPipe); } - - while (true) - { - BYTE buffer = 0; - DWORD bytesRead = 0; - BOOL readResult = ReadFile(clientPipe, &buffer, sizeof(buffer), &bytesRead, NULL); - if (!readResult || bytesRead == 0) - { - LogPipeReadFailure(pipeName, bytesRead); - break; - } - } - - UnregisterPipeClientHandle(hello.client_id, pipeRole, clientPipe); - LogPipeDisconnect(pipeName); - DisconnectNamedPipe(clientPipe); - CloseHandle(clientPipe); } void AuxPipeEventListenerLoopThread() diff --git a/server/src/ipc/ipc.cpp b/server/src/ipc/ipc.cpp index 50510b01..0c83b5dd 100644 --- a/server/src/ipc/ipc.cpp +++ b/server/src/ipc/ipc.cpp @@ -110,6 +110,12 @@ void LogPipeDisconnected(const wchar_t *pipe_name, UINT msg_type) FANY_IPC_LOGF(L"[msime]: [ipc] Attempted to write to disconnected pipe {}: msg_type={}", pipe_name, msg_type); } +void LogPipeWriteTarget(const wchar_t *pipe_name, uint64_t client_id, UINT msg_type, const std::wstring &payload) +{ + FANY_IPC_LOGF(L"[msime]: [ipc] write target: pipe={}, client_id={}, msg_type={}, payload={}", pipe_name, client_id, + msg_type, payload); +} + HANDLE CreateNamedPipeInstance(const wchar_t *pipe_name, DWORD out_buffer_size, DWORD in_buffer_size) { PSECURITY_DESCRIPTOR pd = nullptr; @@ -547,11 +553,13 @@ void UnregisterPipeClientHandle(uint64_t client_id, UINT pipe_role, HANDLE pipe) if (it->second.to_tsf_pipe != INVALID_HANDLE_VALUE) { DisconnectNamedPipe(it->second.to_tsf_pipe); + CloseHandle(it->second.to_tsf_pipe); it->second.to_tsf_pipe = INVALID_HANDLE_VALUE; } if (it->second.to_tsf_worker_thread_pipe != INVALID_HANDLE_VALUE) { DisconnectNamedPipe(it->second.to_tsf_worker_thread_pipe); + CloseHandle(it->second.to_tsf_worker_thread_pipe); it->second.to_tsf_worker_thread_pipe = INVALID_HANDLE_VALUE; } } @@ -703,6 +711,7 @@ void SendToTsfViaNamedpipe(UINT msg_type, const std::wstring &pipeData) namedpipeDataToTsf.msg_type = msg_type; wcscpy_s(namedpipeDataToTsf.candidate_string, pipeData.c_str()); + LogPipeWriteTarget(L"to-tsf", target.client_id, msg_type, pipeData); BOOL ret = WriteFile( // target.pipe, // @@ -722,6 +731,7 @@ void SendToTsfViaNamedpipe(UINT msg_type, const std::wstring &pipeData) LogPipeWriteFailure(L"to-tsf", msg_type, bytesWritten, sizeof(namedpipeDataToTsf)); UnregisterPipeClientHandle(target.client_id, FanyImePipeRole::ToTsf, target.pipe); DisconnectNamedPipe(target.pipe); + CloseHandle(target.pipe); } } @@ -741,6 +751,7 @@ void SendToTsfWorkerThreadViaNamedpipe(UINT msg_type, const std::wstring &pipeDa namedpipeDataToTsfWorkerThread.msg_type = msg_type; wcscpy_s(namedpipeDataToTsfWorkerThread.data, pipeData.c_str()); + LogPipeWriteTarget(L"to-tsf-worker", target.client_id, msg_type, pipeData); BOOL ret = WriteFile( // target.pipe, // @@ -761,5 +772,6 @@ void SendToTsfWorkerThreadViaNamedpipe(UINT msg_type, const std::wstring &pipeDa LogPipeWriteFailure(L"to-tsf-worker", msg_type, bytesWritten, sizeof(namedpipeDataToTsfWorkerThread)); UnregisterPipeClientHandle(target.client_id, FanyImePipeRole::ToTsfWorkerThread, target.pipe); DisconnectNamedPipe(target.pipe); + CloseHandle(target.pipe); } }