This commit is contained in:
Akash Mozumdar 2018-11-03 21:41:38 -04:00
parent 68cb554102
commit 0160578c2d
3 changed files with 25 additions and 26 deletions

View File

@ -62,7 +62,7 @@ namespace
if (textThreadsByParams.size() > MAX_THREAD_COUNT) return Host::AddConsoleOutput(L"too many text threads: can't create more"); if (textThreadsByParams.size() > MAX_THREAD_COUNT) return Host::AddConsoleOutput(L"too many text threads: can't create more");
OnCreate(textThreadsByParams[tp] = std::make_shared<TextThread>(tp, Host::GetHookParam(tp), Host::GetHookName(tp))); OnCreate(textThreadsByParams[tp] = std::make_shared<TextThread>(tp, Host::GetHookParam(tp), Host::GetHookName(tp)));
} }
textThreadsByParams[tp]->AddText(text, len); textThreadsByParams[tp]->Push(text, len);
} }
void RemoveThreads(std::function<bool(ThreadParam)> removeIf) void RemoveThreads(std::function<bool(ThreadParam)> removeIf)

View File

@ -6,7 +6,6 @@
#include "host.h" #include "host.h"
#include "const.h" #include "const.h"
#include <regex> #include <regex>
#include <algorithm>
TextThread::TextThread(ThreadParam tp, HookParam hp, std::wstring name) : handle(threadCounter++), name(name), tp(tp), hp(hp) {} 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; 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() void TextThread::Flush()
{ {
std::wstring sentence; std::wstring sentence;
{ {
LOCK(threadMutex); LOCK(threadMutex);
if (buffer.empty()) return; if (buffer.empty()) return;
if (buffer.size() < maxBufferSize && GetTickCount() - timestamp < flushDelay) return; if (buffer.size() < maxBufferSize && GetTickCount() - lastPushTime < flushDelay) return;
sentence = buffer; sentence = buffer;
buffer.clear(); buffer.clear();
@ -41,25 +61,4 @@ void TextThread::Flush()
AddSentence(sentence); 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 // EOF

View File

@ -23,8 +23,8 @@ public:
~TextThread(); ~TextThread();
std::wstring GetStorage(); std::wstring GetStorage();
void AddText(const BYTE* data, int len);
void AddSentence(std::wstring sentence); void AddSentence(std::wstring sentence);
void Push(const BYTE* data, int len);
const int64_t handle; const int64_t handle;
const std::wstring name; const std::wstring name;
@ -41,7 +41,7 @@ private:
HANDLE deletionEvent = CreateEventW(nullptr, FALSE, FALSE, NULL); HANDLE deletionEvent = CreateEventW(nullptr, FALSE, FALSE, NULL);
std::thread flushThread = std::thread([&] { while (WaitForSingleObject(deletionEvent, 10) == WAIT_TIMEOUT) Flush(); }); std::thread flushThread = std::thread([&] { while (WaitForSingleObject(deletionEvent, 10) == WAIT_TIMEOUT) Flush(); });
DWORD timestamp = GetTickCount(); DWORD lastPushTime = GetTickCount();
}; };
// EOF // EOF