From 189691a562fcbe953284872a95a94f2c2834dd47 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Tue, 28 Aug 2018 17:21:20 -0400 Subject: [PATCH] refactor --- GUI/host/host.cc | 19 +++++++++---------- GUI/host/textthread.cc | 36 +++++++++++++++++++----------------- include/types.h | 2 +- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/GUI/host/host.cc b/GUI/host/host.cc index 4f4c38b..344a0a0 100644 --- a/GUI/host/host.cc +++ b/GUI/host/host.cc @@ -33,7 +33,7 @@ namespace void DispatchText(ThreadParam tp, const BYTE* text, int len) { if (!text || len <= 0) return; - LOCK hostLock(hostMutex); + LOCK(hostMutex); TextThread *it; if ((it = textThreadsByParams[tp]) == nullptr) OnCreate(it = textThreadsByParams[tp] = new TextThread(tp, Host::GetHookParam(tp).type)); @@ -42,7 +42,7 @@ namespace void RemoveThreads(std::function removeIf) { - LOCK hostLock(hostMutex); + LOCK(hostMutex); std::vector removedThreads; for (auto i : textThreadsByParams) if (removeIf(i.first)) @@ -56,7 +56,7 @@ namespace void RegisterProcess(DWORD pid, HANDLE hostPipe) { - LOCK hostLock(hostMutex); + LOCK(hostMutex); ProcessRecord record; record.hostPipe = hostPipe; record.section = OpenFileMappingW(FILE_MAP_READ, FALSE, (ITH_SECTION_ + std::to_wstring(pid)).c_str()); @@ -69,7 +69,7 @@ namespace void UnregisterProcess(DWORD pid) { - LOCK hostLock(hostMutex); + LOCK(hostMutex); ProcessRecord pr = processRecordsByIds[pid]; if (!pr.hostPipe) return; CloseHandle(pr.sectionMutex); @@ -148,7 +148,7 @@ namespace Host { // Artikash 7/25/2018: This is only called when NextHooker is closed, at which point Windows should free everything itself...right? #ifdef _DEBUG // Check memory leaks - LOCK hostLock(hostMutex); + LOCK(hostMutex); OnRemove = [](TextThread* textThread) { delete textThread; }; for (auto i : processRecordsByIds) UnregisterProcess(i.first); delete textThreadsByParams[CONSOLE]; @@ -223,7 +223,7 @@ namespace Host HookParam GetHookParam(DWORD pid, unsigned __int64 addr) { - LOCK hostLock(hostMutex); + LOCK(hostMutex); HookParam ret = {}; ProcessRecord pr = processRecordsByIds[pid]; if (pr.sectionMap == nullptr) return ret; @@ -241,7 +241,7 @@ namespace Host std::wstring GetHookName(DWORD pid, unsigned __int64 addr) { if (pid == 0) return L"Console"; - LOCK hostLock(hostMutex); + LOCK(hostMutex); std::string buffer = ""; ProcessRecord pr = processRecordsByIds[pid]; if (pr.sectionMap == nullptr) return L""; @@ -260,14 +260,13 @@ namespace Host TextThread* GetThread(ThreadParam tp) { - LOCK hostLock(hostMutex); + LOCK(hostMutex); return textThreadsByParams[tp]; } void AddConsoleOutput(std::wstring text) { - LOCK hostLock(hostMutex); - textThreadsByParams[CONSOLE]->AddSentence(std::wstring(text)); + GetThread(CONSOLE)->AddSentence(std::wstring(text)); } } diff --git a/GUI/host/textthread.cc b/GUI/host/textthread.cc index ac7c3d1..e9856d6 100644 --- a/GUI/host/textthread.cc +++ b/GUI/host/textthread.cc @@ -16,42 +16,44 @@ TextThread::~TextThread() std::wstring TextThread::GetStore() { - LOCK ttLock(ttMutex); + LOCK(ttMutex); return storage; } void TextThread::Flush() { - LOCK ttLock(ttMutex); - if (buffer.size() < 400 && (timestamp - GetTickCount() < 250 || buffer.size() == 0)) return; // TODO: let user change delay before sentence is flushed std::wstring sentence; - if (status & USING_UNICODE) { - sentence = std::wstring((wchar_t*)buffer.data(), buffer.size() / 2); + LOCK(ttMutex); + if (buffer.size() < 400 && (timestamp - GetTickCount() < 250 || buffer.size() == 0)) return; // TODO: let user change delay before sentence is flushed + std::wstring sentence; + if (status & USING_UNICODE) + { + sentence = std::wstring((wchar_t*)buffer.data(), buffer.size() / 2); + } + else + { + wchar_t* converted = new wchar_t[buffer.size()]; + sentence = std::wstring(converted, MultiByteToWideChar(status & USING_UTF8 ? CP_UTF8 : 932, 0, buffer.data(), buffer.size(), converted, buffer.size())); + delete[] converted; + } + memset(buffer.data(), 0, buffer.size()); + buffer.clear(); } - else - { - wchar_t* converted = new wchar_t[buffer.size()]; - sentence = std::wstring(converted, MultiByteToWideChar(status & USING_UTF8 ? CP_UTF8 : 932, 0, buffer.data(), buffer.size(), converted, buffer.size())); - delete[] converted; - } - ttMutex.unlock(); AddSentence(sentence); - ttMutex.lock(); - memset(buffer.data(), 0, buffer.size()); - buffer.clear(); } void TextThread::AddSentence(std::wstring sentence) { + // Dispatch to extensions occurs here. Don't hold mutex! Extensions might take a while! if (Output) sentence = Output(this, sentence); - LOCK ttLock(ttMutex); + LOCK(ttMutex); storage.append(sentence); } void TextThread::AddText(const BYTE *con, int len) { - LOCK ttLock(ttMutex); + LOCK(ttMutex); // Artikash 8/27/2018: add repetition filter if (len > 6 && buffer.data() && (strstr(buffer.data(), (const char*)con) || wcsstr((const wchar_t*)buffer.data(), (const wchar_t*)con))) return; buffer.insert(buffer.end(), con, con + len); diff --git a/include/types.h b/include/types.h index 0755013..8098dbf 100644 --- a/include/types.h +++ b/include/types.h @@ -68,4 +68,4 @@ struct HookRemovedNotif // From hook unsigned __int64 address; }; -typedef std::lock_guard LOCK; +#define LOCK(mutex) std::lock_guard lock(mutex)