Skip to content

Commit

Permalink
UI: Use SetThreadExecutionState() to prevent OS sleep during events p…
Browse files Browse the repository at this point in the history
…rocessing
  • Loading branch information
tnodir committed Mar 18, 2022
1 parent a605864 commit 80710ac
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 48 deletions.
11 changes: 8 additions & 3 deletions src/ui/control/controlmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,17 @@ bool ControlManager::processRequest(Control::Command command, const QVariantList
if (Q_UNLIKELY(!w))
return false;

OsUtil::setThreadIsBusy(true);

QString errorMessage;
if (!processCommand({ w, command, args, errorMessage })) {
const bool success = processCommand({ w, command, args, errorMessage });
if (!success) {
qCWarning(LC) << "Bad command" << errorMessage << ':' << command << args;
return false;
}
return true;

OsUtil::setThreadIsBusy(false);

return success;
}

bool ControlManager::processCommand(const ProcessCommandArgs &p)
Expand Down
100 changes: 56 additions & 44 deletions src/ui/log/logmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void LogManager::processLogBuffer(LogBuffer *logBuffer, bool success, quint32 er
}

if (success) {
readLogEntries(logBuffer);
processLogEntries(logBuffer);
} else if (errorCode != 0) {
const auto errorMessage = OsUtil::errorMessage(errorCode);
setErrorMessage(errorMessage);
Expand All @@ -120,51 +120,63 @@ void LogManager::processLogBuffer(LogBuffer *logBuffer, bool success, quint32 er
addFreeBuffer(logBuffer);
}

void LogManager::readLogEntries(LogBuffer *logBuffer)
void LogManager::processLogEntries(LogBuffer *logBuffer)
{
OsUtil::setThreadIsBusy(true);

for (;;) {
const auto logType = logBuffer->peekEntryType();

switch (logType) {
case FORT_LOG_TYPE_BLOCKED:
case FORT_LOG_TYPE_ALLOWED: {
LogEntryBlocked blockedEntry;
logBuffer->readEntryBlocked(&blockedEntry);
IoC<ConfManager>()->logBlockedApp(blockedEntry);
} break;
case FORT_LOG_TYPE_BLOCKED_IP: {
LogEntryBlockedIp blockedIpEntry;
logBuffer->readEntryBlockedIp(&blockedIpEntry);
IoC<StatManager>()->logBlockedIp(blockedIpEntry, currentUnixTime());
} break;
case FORT_LOG_TYPE_PROC_NEW: {
LogEntryProcNew procNewEntry;
logBuffer->readEntryProcNew(&procNewEntry);
IoC<StatManager>()->logProcNew(procNewEntry, currentUnixTime());
} break;
case FORT_LOG_TYPE_STAT_TRAF: {
LogEntryStatTraf statTrafEntry;
logBuffer->readEntryStatTraf(&statTrafEntry);
IoC<StatManager>()->logStatTraf(statTrafEntry, currentUnixTime());
} break;
case FORT_LOG_TYPE_TIME: {
LogEntryTime timeEntry;
logBuffer->readEntryTime(&timeEntry);
setCurrentUnixTime(timeEntry.unixTime());
if (timeEntry.timeChanged()) {
emit systemTimeChanged();
}
} break;
default:
if (logBuffer->offset() < logBuffer->top()) {
const auto data = QByteArray::fromRawData(
logBuffer->array().constData() + logBuffer->offset(),
logBuffer->top() - logBuffer->offset());

qCCritical(LC) << "Unknown Log entry:" << logType << logBuffer->offset()
<< logBuffer->top() << data;
}
return;
const FortLogType logType = logBuffer->peekEntryType();

if (!processLogEntry(logBuffer, logType))
break;
}

OsUtil::setThreadIsBusy(false);
}

bool LogManager::processLogEntry(LogBuffer *logBuffer, FortLogType logType)
{
switch (logType) {
case FORT_LOG_TYPE_BLOCKED:
case FORT_LOG_TYPE_ALLOWED: {
LogEntryBlocked blockedEntry;
logBuffer->readEntryBlocked(&blockedEntry);
IoC<ConfManager>()->logBlockedApp(blockedEntry);
} break;
case FORT_LOG_TYPE_BLOCKED_IP: {
LogEntryBlockedIp blockedIpEntry;
logBuffer->readEntryBlockedIp(&blockedIpEntry);
IoC<StatManager>()->logBlockedIp(blockedIpEntry, currentUnixTime());
} break;
case FORT_LOG_TYPE_PROC_NEW: {
LogEntryProcNew procNewEntry;
logBuffer->readEntryProcNew(&procNewEntry);
IoC<StatManager>()->logProcNew(procNewEntry, currentUnixTime());
} break;
case FORT_LOG_TYPE_STAT_TRAF: {
LogEntryStatTraf statTrafEntry;
logBuffer->readEntryStatTraf(&statTrafEntry);
IoC<StatManager>()->logStatTraf(statTrafEntry, currentUnixTime());
} break;
case FORT_LOG_TYPE_TIME: {
LogEntryTime timeEntry;
logBuffer->readEntryTime(&timeEntry);
setCurrentUnixTime(timeEntry.unixTime());
if (timeEntry.timeChanged()) {
emit systemTimeChanged();
}
} break;
default:
if (logBuffer->offset() < logBuffer->top()) {
const auto data =
QByteArray::fromRawData(logBuffer->array().constData() + logBuffer->offset(),
logBuffer->top() - logBuffer->offset());

qCCritical(LC) << "Unknown Log entry:" << logType << logBuffer->offset()
<< logBuffer->top() << data;
}
return false;
}

return true;
}
4 changes: 3 additions & 1 deletion src/ui/log/logmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QObject>

#include <common/fortdef.h>
#include <util/ioc/iocservice.h>

class LogBuffer;
Expand Down Expand Up @@ -42,7 +43,8 @@ private slots:
LogBuffer *getFreeBuffer();
void addFreeBuffer(LogBuffer *logBuffer);

void readLogEntries(LogBuffer *logBuffer);
void processLogEntries(LogBuffer *logBuffer);
bool processLogEntry(LogBuffer *logBuffer, FortLogType logType);

private:
bool m_active = false;
Expand Down
5 changes: 5 additions & 0 deletions src/ui/util/osutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,8 @@ void OsUtil::writeToConsole(const char *category, const QString &message)
DWORD nw;
WriteFile(stdoutHandle, data.constData(), DWORD(data.size()), &nw, nullptr);
}

void OsUtil::setThreadIsBusy(bool on)
{
SetThreadExecutionState(ES_CONTINUOUS | (on ? ES_SYSTEM_REQUIRED : 0));
}
2 changes: 2 additions & 0 deletions src/ui/util/osutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class OsUtil

static void showConsole(bool visible);
static void writeToConsole(const char *category, const QString &message);

static void setThreadIsBusy(bool on);
};

#endif // OSUTIL_H

0 comments on commit 80710ac

Please sign in to comment.