From 0160578c2d43a5df88ee9debc1b4db1d13d3dd9d Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sat, 3 Nov 2018 21:41:38 -0400 Subject: [PATCH] rename --- GUI/host/host.cc | 2 +- GUI/host/textthread.cc | 45 +++++++++++++++++++++--------------------- GUI/host/textthread.h | 4 ++-- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/GUI/host/host.cc b/GUI/host/host.cc index d21a771..f42597d 100644 --- a/GUI/host/host.cc +++ b/GUI/host/host.cc @@ -62,7 +62,7 @@ namespace if (textThreadsByParams.size() > MAX_THREAD_COUNT) return Host::AddConsoleOutput(L"too many text threads: can't create more"); OnCreate(textThreadsByParams[tp] = std::make_shared(tp, Host::GetHookParam(tp), Host::GetHookName(tp))); } - textThreadsByParams[tp]->AddText(text, len); + textThreadsByParams[tp]->Push(text, len); } void RemoveThreads(std::function removeIf) diff --git a/GUI/host/textthread.cc b/GUI/host/textthread.cc index c876fe2..94258bd 100644 --- a/GUI/host/textthread.cc +++ b/GUI/host/textthread.cc @@ -6,7 +6,6 @@ #include "host.h" #include "const.h" #include -#include TextThread::TextThread(ThreadParam tp, HookParam hp, std::wstring name) : handle(threadCounter++), name(name), tp(tp), hp(hp) {} @@ -23,13 +22,34 @@ std::wstring TextThread::GetStorage() return storage; } +void TextThread::AddSentence(std::wstring sentence) +{ + // Dispatch to extensions occurs here. Don't hold mutex! Extensions might take a while! + if (Output(this, sentence)) + { + LOCK(threadMutex); + storage += sentence; + } +} + +void TextThread::Push(const BYTE* data, int len) +{ + if (len < 0) return; + LOCK(threadMutex); + buffer += hp.type & USING_UNICODE + ? std::wstring((wchar_t*)data, len / 2) + : StringToWideString(std::string((char*)data, len), hp.codepage != 0 ? hp.codepage : DEFAULT_CODEPAGE); + if (std::all_of(buffer.begin(), buffer.end(), [&](wchar_t c) { return repeatingChars.count(c) > 0; })) buffer.clear(); + lastPushTime = GetTickCount(); +} + void TextThread::Flush() { std::wstring sentence; { LOCK(threadMutex); if (buffer.empty()) return; - if (buffer.size() < maxBufferSize && GetTickCount() - timestamp < flushDelay) return; + if (buffer.size() < maxBufferSize && GetTickCount() - lastPushTime < flushDelay) return; sentence = buffer; buffer.clear(); @@ -41,25 +61,4 @@ void TextThread::Flush() AddSentence(sentence); } -void TextThread::AddSentence(std::wstring sentence) -{ - // Dispatch to extensions occurs here. Don't hold mutex! Extensions might take a while! - if (Output(this, sentence)) - { - LOCK(threadMutex); - storage += sentence; - } -} - -void TextThread::AddText(const BYTE* data, int len) -{ - if (len < 0) return; - LOCK(threadMutex); - buffer += hp.type & USING_UNICODE - ? std::wstring((wchar_t*)data, len / 2) - : StringToWideString(std::string((char*)data, len), hp.codepage != 0 ? hp.codepage : DEFAULT_CODEPAGE); - if (std::all_of(buffer.begin(), buffer.end(), [&](wchar_t c) { return repeatingChars.count(c) > 0; })) buffer.clear(); - timestamp = GetTickCount(); -} - // EOF diff --git a/GUI/host/textthread.h b/GUI/host/textthread.h index 8cdbd89..e4f273d 100644 --- a/GUI/host/textthread.h +++ b/GUI/host/textthread.h @@ -23,8 +23,8 @@ public: ~TextThread(); std::wstring GetStorage(); - void AddText(const BYTE* data, int len); void AddSentence(std::wstring sentence); + void Push(const BYTE* data, int len); const int64_t handle; const std::wstring name; @@ -41,7 +41,7 @@ private: HANDLE deletionEvent = CreateEventW(nullptr, FALSE, FALSE, NULL); std::thread flushThread = std::thread([&] { while (WaitForSingleObject(deletionEvent, 10) == WAIT_TIMEOUT) Flush(); }); - DWORD timestamp = GetTickCount(); + DWORD lastPushTime = GetTickCount(); }; // EOF