From 4660be2efb78ee05870af6f955e56f9c065b7080 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Mon, 27 Aug 2018 20:49:33 -0400 Subject: [PATCH] add repetition filter and refactor host --- GUI/host/host.cc | 2 ++ GUI/host/textthread.cc | 14 +++++--------- GUI/host/textthread.h | 8 ++++---- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/GUI/host/host.cc b/GUI/host/host.cc index 6f54e53..4f4c38b 100644 --- a/GUI/host/host.cc +++ b/GUI/host/host.cc @@ -119,6 +119,8 @@ namespace default: { ThreadParam tp = *(ThreadParam*)buffer; + buffer[bytesRead] = 0; + buffer[bytesRead + 1] = 0; DispatchText(tp, buffer + sizeof(tp), bytesRead - sizeof(tp)); } break; diff --git a/GUI/host/textthread.cc b/GUI/host/textthread.cc index 32fb3a7..8c665c1 100644 --- a/GUI/host/textthread.cc +++ b/GUI/host/textthread.cc @@ -5,14 +5,7 @@ #include "textthread.h" #include "const.h" -TextThread::TextThread(ThreadParam tp, DWORD status) : - deletionEvent(CreateEventW(nullptr, FALSE, FALSE, NULL)), - flushThread([&]() { while (WaitForSingleObject(deletionEvent, 100) == WAIT_TIMEOUT) Flush(); }), - timestamp(GetTickCount()), - Output(nullptr), - tp(tp), - status(status) -{} +TextThread::TextThread(ThreadParam tp, DWORD status) : tp(tp), status(status) {} TextThread::~TextThread() { @@ -30,7 +23,7 @@ std::wstring TextThread::GetStore() void TextThread::Flush() { LOCK ttLock(ttMutex); - if (timestamp - GetTickCount() < 250 || buffer.size() == 0) return; // TODO: let user change delay before sentence is flushed + 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) { @@ -43,6 +36,7 @@ void TextThread::Flush() delete[] converted; } AddSentence(sentence); + memset(buffer.data(), 0, buffer.size()); buffer.clear(); } @@ -56,6 +50,8 @@ void TextThread::AddSentence(std::wstring sentence) void TextThread::AddText(const BYTE *con, int len) { LOCK ttLock(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); timestamp = GetTickCount(); } diff --git a/GUI/host/textthread.h b/GUI/host/textthread.h index cba1090..c9974f3 100644 --- a/GUI/host/textthread.h +++ b/GUI/host/textthread.h @@ -28,11 +28,11 @@ private: std::vector buffer; std::wstring storage; - std::recursive_mutex ttMutex; - HANDLE deletionEvent; - std::thread flushThread; - DWORD timestamp; + + HANDLE deletionEvent = CreateEventW(nullptr, FALSE, FALSE, NULL); + std::thread flushThread = std::thread([&]() { while (WaitForSingleObject(deletionEvent, 100) == WAIT_TIMEOUT) Flush(); }); + DWORD timestamp = GetTickCount(); ThreadOutputCallback Output; ThreadParam tp;