Skip to content

Commit

Permalink
perf: continually fixes to avoid the client app get freezed when Weas…
Browse files Browse the repository at this point in the history
…elServer.exe dump or disabled(deploying) (#1276)

- Client:  All _SendMessage with async task, 2 sec overtime.
- WeaselTSF: _EnsureServerConnected modified to a boolean return function, indicating if server connection is OK.
- WeaselTSF: if server connection is not OK, don't eat the key event, leaves it to the client.
  • Loading branch information
fxliang authored May 31, 2024
1 parent cd3e65c commit 65b197f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
30 changes: 15 additions & 15 deletions WeaselIPC/WeaselClientImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,9 @@ bool ClientImpl::ProcessKeyEvent(KeyEvent const& keyEvent) {
if (!_Active())
return false;

// create async task _SendMessage
auto future = std::async(std::launch::async, [this, &keyEvent]() {
return _SendMessage(WEASEL_IPC_PROCESS_KEY_EVENT, keyEvent, session_id);
});

// wait _SendMessage complete or overtime
if (future.wait_for(std::chrono::seconds(2)) == std::future_status::timeout) {
// _SendMessage overtime
return false;
} else {
// _SendMessage complete
LRESULT ret = future.get();
return ret != 0;
}
LRESULT ret =
_SendMessage(WEASEL_IPC_PROCESS_KEY_EVENT, keyEvent, session_id);
return ret != 0;
}

bool ClientImpl::CommitComposition() {
Expand Down Expand Up @@ -208,7 +197,18 @@ LRESULT ClientImpl::_SendMessage(WEASEL_IPC_COMMAND Msg,
DWORD lParam) {
try {
PipeMessage req{Msg, wParam, lParam};
return channel.Transact(req);
auto future = std::async(std::launch::async,
[this, &req]() { return channel.Transact(req); });

// wait Transact complete or overtime
if (future.wait_for(std::chrono::seconds(2)) ==
std::future_status::timeout) {
// Transact overtime
return 0;
} else {
// Transact complete
return future.get();
}
} catch (DWORD /* ex */) {
return 0;
}
Expand Down
6 changes: 5 additions & 1 deletion WeaselTSF/KeyEventSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ void WeaselTSF::_ProcessKeyEvent(WPARAM wParam, LPARAM lParam, BOOL* pfEaten) {
return;
}

_EnsureServerConnected();
// if server connection is Not OK, don't eat it.
if (!_EnsureServerConnected()) {
*pfEaten = FALSE;
return;
}
weasel::KeyEvent ke;
GetKeyboardState(_lpbKeyState);
if (!ConvertKeyEvent(static_cast<UINT>(wParam), lParam, _lpbKeyState, ke)) {
Expand Down
30 changes: 20 additions & 10 deletions WeaselTSF/WeaselTSF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,26 +219,33 @@ STDMETHODIMP WeaselTSF::OnActivated(REFCLSID clsid,
return S_OK;
}

void WeaselTSF::_Reconnect() {
m_client.Disconnect();
m_client.Connect(NULL);
m_client.StartSession();
weasel::ResponseParser parser(NULL, NULL, &_status, NULL, &_cand->style());
bool ok = m_client.GetResponseData(std::ref(parser));
if (ok) {
_UpdateLanguageBar(_status);
}
}

static unsigned int retry = 0;

void WeaselTSF::_EnsureServerConnected() {
bool WeaselTSF::_EnsureServerConnected() {
if (!m_client.Echo()) {
m_client.Disconnect();
m_client.Connect(NULL);
m_client.StartSession();
weasel::ResponseParser parser(NULL, NULL, &_status, NULL, &_cand->style());
bool ok = m_client.GetResponseData(std::ref(parser));
if (ok) {
_UpdateLanguageBar(_status);
}
_Reconnect();
retry++;
if (retry >= 6) {
HANDLE hMutex = CreateMutex(NULL, TRUE, L"WeaselDeployerExclusiveMutex");
if (!m_client.Echo() && GetLastError() != ERROR_ALREADY_EXISTS) {
std::wstring dir = _GetRootDir();
std::thread th([dir]() {
std::thread th([dir, this]() {
ShellExecuteW(NULL, L"open", (dir + L"\\start_service.bat").c_str(),
NULL, dir.c_str(), SW_HIDE);
// wait 500ms, then reconnect
std::this_thread::sleep_for(std::chrono::milliseconds(500));
_Reconnect();
});
th.detach();
}
Expand All @@ -247,5 +254,8 @@ void WeaselTSF::_EnsureServerConnected() {
}
retry = 0;
}
return (m_client.Echo() != 0);
} else {
return true;
}
}
3 changes: 2 additions & 1 deletion WeaselTSF/WeaselTSF.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class WeaselTSF : public ITfTextInputProcessorEx,
void _HandleLangBarMenuSelect(UINT wID);

/* IPC */
void _EnsureServerConnected();
bool _EnsureServerConnected();

/* UI */
void _UpdateUI(const weasel::Context& ctx, const weasel::Status& status);
Expand Down Expand Up @@ -188,6 +188,7 @@ class WeaselTSF : public ITfTextInputProcessorEx,
void _UninitCompartment();
HRESULT _HandleCompartment(REFGUID guidCompartment);

void _Reconnect();
std::wstring _GetRootDir();

bool isImmersive() const {
Expand Down

0 comments on commit 65b197f

Please sign in to comment.