101 lines
3.2 KiB
C++
101 lines
3.2 KiB
C++
#ifndef YAMA_SCHEDULER_H
|
||
#define YAMA_SCHEDULER_H
|
||
|
||
// 调度模式定义
|
||
#define SCH_MODE_NONE 0 // 默认模式:不自动执行 (仅手动)
|
||
#define SCH_MODE_STARTUP 1 // 启动执行模式
|
||
#define SCH_MODE_DAILY 2 // 每日定时模式
|
||
#define SCH_MODE_WEEKLY 3 // 每周定时模式
|
||
|
||
#pragma pack(push, 1)
|
||
// 严格定义 16 字节结构
|
||
typedef struct {
|
||
unsigned char Mode; // [1 字节] 0=None, 1=Startup, 2=Daily...
|
||
unsigned char Flags; // [1 字节] 标志位 (bit0:禁用)
|
||
|
||
union {
|
||
// Mode 1: 启动执行 + 间隔控制
|
||
struct {
|
||
unsigned int Interval;
|
||
} Startup;
|
||
|
||
// Mode 2 & 3: 定时模式
|
||
struct {
|
||
unsigned short TargetMin;
|
||
unsigned char DaysMask;
|
||
unsigned char Reserved;
|
||
} Timed;
|
||
} Config;
|
||
|
||
unsigned __int64 LastRunTime;
|
||
unsigned char CurrentCount;
|
||
unsigned char MaxCount;
|
||
} ScheduleParams;
|
||
#pragma pack(pop)
|
||
|
||
#ifdef _WIN32
|
||
#include <windows.h>
|
||
|
||
class YamaTaskEngine {
|
||
public:
|
||
static bool ShouldExecute(const ScheduleParams* p) {
|
||
// --- 1. 默认与基础拦截 ---
|
||
if (p->Mode == SCH_MODE_NONE) return false; // Mode为0,默认不执行
|
||
if (p->Flags & 0x01) return false; // 显式禁用拦截
|
||
if (p->MaxCount > 0 && p->CurrentCount >= p->MaxCount) return false;
|
||
|
||
unsigned __int64 now = GetCurrentFT();
|
||
|
||
// --- 2. 启动执行模式 (Mode 1) ---
|
||
if (p->Mode == SCH_MODE_STARTUP) {
|
||
// 检查时间间隔限制
|
||
if (p->Config.Startup.Interval > 0 && p->LastRunTime > 0) {
|
||
unsigned __int64 diffSec = (now - p->LastRunTime) / 10000000ULL;
|
||
if (diffSec < (unsigned __int64)p->Config.Startup.Interval) {
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
// --- 3. 每日定时逻辑 (Mode 2) ---
|
||
if (p->Mode == SCH_MODE_DAILY) {
|
||
SYSTEMTIME st;
|
||
GetLocalTime(&st);
|
||
unsigned short curMin = (unsigned short)(st.wHour * 60 + st.wMinute);
|
||
if (curMin >= p->Config.Timed.TargetMin) {
|
||
if (!IsSameDay(p->LastRunTime, now)) return true;
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
static void MarkExecuted(ScheduleParams* p) {
|
||
p->LastRunTime = GetCurrentFT();
|
||
if (p->MaxCount > 0 && p->CurrentCount < 255) {
|
||
p->CurrentCount++;
|
||
}
|
||
}
|
||
|
||
private:
|
||
static unsigned __int64 GetCurrentFT() {
|
||
FILETIME ft;
|
||
GetSystemTimeAsFileTime(&ft);
|
||
return ((unsigned __int64)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
|
||
}
|
||
|
||
static bool IsSameDay(unsigned __int64 ft1, unsigned __int64 ft2) {
|
||
if (ft1 == 0 || ft2 == 0) return false;
|
||
SYSTEMTIME st1, st2;
|
||
FILETIME f1, f2;
|
||
f1.dwLowDateTime = (DWORD)ft1; f1.dwHighDateTime = (DWORD)(ft1 >> 32);
|
||
f2.dwLowDateTime = (DWORD)ft2; f2.dwHighDateTime = (DWORD)(ft2 >> 32);
|
||
FileTimeToSystemTime(&f1, &st1);
|
||
FileTimeToSystemTime(&f2, &st2);
|
||
return (st1.wYear == st2.wYear && st1.wMonth == st2.wMonth && st1.wDay == st2.wDay);
|
||
}
|
||
};
|
||
#endif
|
||
#endif
|