Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Artikash committed Dec 14, 2018
1 parent a3ac850 commit dfb45a3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
29 changes: 14 additions & 15 deletions GUI/host/textthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#include "host.h"
#include "util.h"

TextThread::TextThread(ThreadParam tp, HookParam hp, std::optional<std::wstring> name) :
handle(threadCounter++),
name(name.value_or(Util::StringToWideString(hp.name).value())),
tp(tp),
TextThread::TextThread(ThreadParam tp, HookParam hp, std::optional<std::wstring> name) :
handle(threadCounter++),
name(name.value_or(Util::StringToWideString(hp.name).value())),
tp(tp),
hp(hp)
{
CreateTimerQueueTimer(timer, NULL, Flush, this, 25, 25, WT_EXECUTELONGFUNCTION);
CreateTimerQueueTimer(timer, NULL, [](void* This, BOOLEAN) { ((TextThread*)This)->Flush(); }, this, 25, 25, WT_EXECUTELONGFUNCTION);
OnCreate(this);
}

Expand Down Expand Up @@ -40,19 +40,18 @@ void TextThread::Push(const BYTE* data, int len)
lastPushTime = GetTickCount();
}

void CALLBACK Flush(void* thread, BOOLEAN)
void TextThread::Flush()
{
auto This = (TextThread*)thread;
std::wstring sentence;
{
LOCK(This->bufferMutex);
if (This->buffer.empty()) return;
if (This->buffer.size() < This->maxBufferSize && GetTickCount() - This->lastPushTime < This->flushDelay) return;
sentence = This->buffer;
This->buffer.clear();
LOCK(bufferMutex);
if (buffer.empty()) return;
if (buffer.size() < maxBufferSize && GetTickCount() - lastPushTime < flushDelay) return;
sentence = buffer;
buffer.clear();

if (Util::RemoveRepetition(sentence)) This->repeatingChars = std::unordered_set(sentence.begin(), sentence.end());
else This->repeatingChars.clear();
if (Util::RemoveRepetition(sentence)) repeatingChars = std::unordered_set(sentence.begin(), sentence.end());
else repeatingChars.clear();
}
This->AddSentence(sentence);
AddSentence(sentence);
}
4 changes: 2 additions & 2 deletions GUI/host/textthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ class TextThread
std::wstring GetStorage();
void AddSentence(std::wstring sentence);
void Push(const BYTE* data, int len);
friend void CALLBACK Flush(void* thread, BOOLEAN);

const int64_t handle;
const std::wstring name;
const ThreadParam tp;
const HookParam hp;

private:
struct TimerDeleter { void operator()(void* h) { DeleteTimerQueueTimer(NULL, h, INVALID_HANDLE_VALUE); } };
void Flush();

struct TimerDeleter { void operator()(void* h) { DeleteTimerQueueTimer(NULL, h, INVALID_HANDLE_VALUE); } };
ThreadSafePtr<std::wstring> storage;
std::wstring buffer;
std::unordered_set<wchar_t> repeatingChars;
Expand Down

0 comments on commit dfb45a3

Please sign in to comment.