449 lines
13 KiB
JavaScript
449 lines
13 KiB
JavaScript
const ffi = require('ffi-napi');
|
|
const ref = require('ref-napi');
|
|
const path = require('path');
|
|
const os = require('os');
|
|
|
|
// 获取XNCore环境变量
|
|
const xnCorePath = process.env.XNCore || '';
|
|
|
|
// 根据操作系统类型确定动态库扩展名和路径
|
|
const isWindows = os.platform() === 'win32';
|
|
const libExtension = isWindows ? '.dll' : '.so';
|
|
const libPrefix = isWindows ? '' : 'lib';
|
|
|
|
// Login库配置
|
|
const loginLibName = `${libPrefix}Login${libExtension}`;
|
|
const loginLibPath = path.join(xnCorePath, 'lib', loginLibName);
|
|
|
|
// MonitorServer库配置
|
|
const monitorLibName = `${libPrefix}XNMonitorServer${libExtension}`;
|
|
const monitorLibPath = path.join(xnCorePath, 'lib', monitorLibName);
|
|
|
|
// 定义Buffer类型
|
|
const BufferType = ref.refType(ref.types.void);
|
|
const StringType = ref.types.CString;
|
|
const IntType = ref.types.int;
|
|
|
|
// 定义动态库函数接口
|
|
let loginLib;
|
|
let monitorLib;
|
|
|
|
try {
|
|
loginLib = ffi.Library(loginLibPath, {
|
|
'validateUser': ['int', [BufferType, 'size_t', BufferType, 'size_t']],
|
|
'getUserInfo': [StringType, ['int']],
|
|
'cleanup': ['void', []],
|
|
'registerUser': ['int', [BufferType, 'size_t', BufferType, 'size_t', BufferType, 'size_t']]
|
|
});
|
|
} catch (error) {
|
|
console.error(`加载 ${loginLibName} 失败:`, error);
|
|
}
|
|
|
|
try {
|
|
monitorLib = ffi.Library(monitorLibPath, {
|
|
'XN_Initialize': ['int', [StringType, 'int', StringType, 'int']],
|
|
'XN_Cleanup': ['void', []],
|
|
'XN_StartMonitorSystemInfo': ['int', [StringType, 'int']],
|
|
'XN_GetSystemInfo': ['int', [StringType, 'int']],
|
|
'XN_GetAllThreadInfo': ['int', [StringType, 'int']],
|
|
'XN_StopMonitorSystemInfo': ['void', []],
|
|
'XN_StartMonitorModelInfo': ['int', [StringType, 'int']],
|
|
'XN_GetModelInfo': ['int', [StringType, 'int']],
|
|
'XN_StopMonitorModelInfo': ['void', []],
|
|
'XN_InitializeEngineControl': ['int', [StringType, 'int']],
|
|
'XN_PauseEngine': ['void', [StringType, 'int']],
|
|
'XN_ResumeEngine': ['void', [StringType, 'int']],
|
|
'XN_StopEngine': ['void', [StringType, 'int']],
|
|
'XN_StartDataMonitor': ['int', [StringType, 'int', StringType, 'int']],
|
|
'XN_StopDataMonitor': ['void', [StringType, 'int', StringType, 'int']],
|
|
'XN_GetDataMonitorInfo': ['int', [StringType, 'int', StringType, 'int', StringType, 'int', StringType, 'int']],
|
|
'XN_InjectDataInterface': ['int', [StringType, 'int', StringType, 'int', StringType, 'int']],
|
|
'XN_StartInjectContinuous': ['int', [StringType, 'int', StringType, 'int', 'double', StringType, 'int']],
|
|
'XN_StopInjectContinuous': ['int', [StringType, 'int', StringType, 'int']]
|
|
});
|
|
} catch (error) {
|
|
console.error(`加载 ${monitorLibName} 失败:`, error);
|
|
}
|
|
|
|
// 注册进程退出时的清理函数
|
|
function performCleanup() {
|
|
console.log('正在执行清理操作...');
|
|
if (loginLib) {
|
|
try {
|
|
loginLib.cleanup();
|
|
console.log('清理操作完成');
|
|
} catch (error) {
|
|
console.error('清理操作失败:', error);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 转换字符串为Buffer的函数
|
|
function stringToBuffer(str) {
|
|
try {
|
|
if (!str) {
|
|
return { buffer: Buffer.alloc(0), length: 0 };
|
|
}
|
|
const buffer = Buffer.from(str, 'utf8');
|
|
return { buffer: buffer, length: buffer.length };
|
|
} catch (error) {
|
|
console.error('字符串转Buffer失败:', error);
|
|
return { buffer: Buffer.alloc(0), length: 0 };
|
|
}
|
|
}
|
|
|
|
// 初始化监控服务器
|
|
function initializeMonitor(domainId) {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
// 创建错误消息缓冲区
|
|
const errorMsg = Buffer.alloc(1024);
|
|
const result = monitorLib.XN_Initialize(domainId, domainId.length, errorMsg, errorMsg.length);
|
|
|
|
if (result !== 0) {
|
|
return `初始化失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
|
}
|
|
return '初始化成功';
|
|
} catch (error) {
|
|
return `初始化失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 清理监控服务器资源
|
|
function cleanupMonitor() {
|
|
if (!monitorLib) {
|
|
return;
|
|
}
|
|
try {
|
|
monitorLib.XN_Cleanup();
|
|
} catch (error) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 启动监控系统信息
|
|
function startMonitorSystemInfo() {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const errorMsg = Buffer.alloc(1024);
|
|
const result = monitorLib.XN_StartMonitorSystemInfo(errorMsg, errorMsg.length);
|
|
if (result !== 0) {
|
|
return `启动失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
|
}
|
|
return '启动成功';
|
|
} catch (error) {
|
|
return `启动失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 获取系统信息
|
|
function getSystemInfo() {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const infoMsg = Buffer.alloc(8192);
|
|
const result = monitorLib.XN_GetSystemInfo(infoMsg, infoMsg.length);
|
|
|
|
if (result !== 0) {
|
|
return `获取失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
|
}
|
|
return infoMsg.toString('utf8').replace(/\0/g, '');
|
|
} catch (error) {
|
|
return `获取失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 获取所有线程信息
|
|
function getAllThreadInfo() {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const infoMsg = Buffer.alloc(8192);
|
|
const result = monitorLib.XN_GetAllThreadInfo(infoMsg, infoMsg.length);
|
|
|
|
if (result !== 0) {
|
|
return `获取失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
|
}
|
|
return infoMsg.toString('utf8').replace(/\0/g, '');
|
|
} catch (error) {
|
|
return `获取失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 停止监控系统信息
|
|
function stopMonitorSystemInfo() {
|
|
if (!monitorLib) {
|
|
return;
|
|
}
|
|
try {
|
|
monitorLib.XN_StopMonitorSystemInfo();
|
|
} catch (error) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 启动监控模型信息
|
|
function startMonitorModelInfo() {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const errorMsg = Buffer.alloc(1024);
|
|
const result = monitorLib.XN_StartMonitorModelInfo(errorMsg, errorMsg.length);
|
|
if (result !== 0) {
|
|
return `启动失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
|
}
|
|
return '启动成功';
|
|
} catch (error) {
|
|
return `启动失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 获取模型信息
|
|
function getModelInfo() {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const infoMsg = Buffer.alloc(16384);
|
|
const result = monitorLib.XN_GetModelInfo(infoMsg, infoMsg.length);
|
|
|
|
if (result !== 0) {
|
|
return `获取失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
|
}
|
|
return infoMsg.toString('utf8').replace(/\0/g, '');
|
|
} catch (error) {
|
|
return `获取失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 停止监控模型信息
|
|
function stopMonitorModelInfo() {
|
|
if (!monitorLib) {
|
|
return;
|
|
}
|
|
try {
|
|
monitorLib.XN_StopMonitorModelInfo();
|
|
} catch (error) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 初始化引擎控制
|
|
function initializeEngineControl() {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const errorMsg = Buffer.alloc(1024);
|
|
const result = monitorLib.XN_InitializeEngineControl(errorMsg, errorMsg.length);
|
|
if (result !== 0) {
|
|
return `初始化引擎控制失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
|
}
|
|
return '初始化引擎控制成功';
|
|
} catch (error) {
|
|
return `初始化引擎控制失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 暂停引擎
|
|
function pauseEngine() {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const errorMsg = Buffer.alloc(1024);
|
|
monitorLib.XN_PauseEngine(errorMsg, errorMsg.length);
|
|
const error = errorMsg.toString('utf8').replace(/\0/g, '');
|
|
return error ? `暂停引擎失败: ${error}` : '暂停引擎成功';
|
|
} catch (error) {
|
|
return `暂停引擎失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 恢复引擎
|
|
function resumeEngine() {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const errorMsg = Buffer.alloc(1024);
|
|
monitorLib.XN_ResumeEngine(errorMsg, errorMsg.length);
|
|
const error = errorMsg.toString('utf8').replace(/\0/g, '');
|
|
return error ? `恢复引擎失败: ${error}` : '恢复引擎成功';
|
|
} catch (error) {
|
|
return `恢复引擎失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 停止引擎
|
|
function stopEngine() {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const errorMsg = Buffer.alloc(1024);
|
|
monitorLib.XN_StopEngine(errorMsg, errorMsg.length);
|
|
const error = errorMsg.toString('utf8').replace(/\0/g, '');
|
|
return error ? `停止引擎失败: ${error}` : '停止引擎成功';
|
|
} catch (error) {
|
|
return `停止引擎失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 启动数据监控
|
|
function startDataMonitor(structName) {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const errorMsg = Buffer.alloc(1024);
|
|
const result = monitorLib.XN_StartDataMonitor(structName, structName.length, errorMsg, errorMsg.length);
|
|
if (result !== 0) {
|
|
return `启动数据监控失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
|
}
|
|
return '启动数据监控成功';
|
|
} catch (error) {
|
|
return `启动数据监控失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 停止数据监控
|
|
function stopDataMonitor(structName) {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const errorMsg = Buffer.alloc(1024);
|
|
monitorLib.XN_StopDataMonitor(structName, structName.length, errorMsg, errorMsg.length);
|
|
const error = errorMsg.toString('utf8').replace(/\0/g, '');
|
|
return error ? `停止数据监控失败: ${error}` : '停止数据监控成功';
|
|
} catch (error) {
|
|
return `停止数据监控失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 获取数据监控信息
|
|
function getDataMonitorInfo(structName, interfaceName, bufferSize = 8192) {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const infoMsg = Buffer.alloc(1024);
|
|
const dataBuffer = Buffer.alloc(bufferSize); // 使用指定的缓冲区大小
|
|
const result = monitorLib.XN_GetDataMonitorInfo(
|
|
structName, structName.length,
|
|
interfaceName, interfaceName.length,
|
|
dataBuffer, dataBuffer.length,
|
|
infoMsg, infoMsg.length
|
|
);
|
|
|
|
if (result !== 0) {
|
|
return `获取数据监控信息失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
|
}
|
|
return {
|
|
data: dataBuffer.toString('utf8').replace(/\0/g, ''),
|
|
info: infoMsg.toString('utf8').replace(/\0/g, '')
|
|
};
|
|
} catch (error) {
|
|
return `获取数据监控信息失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 注入数据接口
|
|
function injectDataInterface(structName, interfaceNameAndData) {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const infoMsg = Buffer.alloc(1024);
|
|
const result = monitorLib.XN_InjectDataInterface(
|
|
structName, structName.length,
|
|
interfaceNameAndData, interfaceNameAndData.length,
|
|
infoMsg, infoMsg.length
|
|
);
|
|
|
|
if (result !== 0) {
|
|
return `注入数据接口失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
|
}
|
|
return '注入数据接口成功';
|
|
} catch (error) {
|
|
return `注入数据接口失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 持续注入数据接口
|
|
function startInjectContinuous(structName, interfaceNameAndData, frequency) {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const infoMsg = Buffer.alloc(1024);
|
|
const result = monitorLib.XN_StartInjectContinuous(
|
|
structName, structName.length,
|
|
interfaceNameAndData, interfaceNameAndData.length,
|
|
frequency,
|
|
infoMsg, infoMsg.length
|
|
);
|
|
|
|
if (result !== 0) {
|
|
return `启动持续注入数据失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
|
}
|
|
return '启动持续注入数据成功';
|
|
} catch (error) {
|
|
return `启动持续注入数据失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
// 停止持续注入数据接口
|
|
function stopInjectContinuous(structName) {
|
|
if (!monitorLib) {
|
|
return '监控服务器库未加载';
|
|
}
|
|
try {
|
|
const infoMsg = Buffer.alloc(1024);
|
|
const result = monitorLib.XN_StopInjectContinuous(
|
|
structName, structName.length,
|
|
infoMsg, infoMsg.length
|
|
);
|
|
|
|
if (result !== 0) {
|
|
return `停止持续注入数据失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
|
}
|
|
return '停止持续注入数据成功';
|
|
} catch (error) {
|
|
return `停止持续注入数据失败: ${error.message}`;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
loginLib,
|
|
monitorLib,
|
|
performCleanup,
|
|
stringToBuffer,
|
|
initializeMonitor,
|
|
cleanupMonitor,
|
|
startMonitorSystemInfo,
|
|
getSystemInfo,
|
|
getAllThreadInfo,
|
|
stopMonitorSystemInfo,
|
|
startMonitorModelInfo,
|
|
getModelInfo,
|
|
stopMonitorModelInfo,
|
|
initializeEngineControl,
|
|
pauseEngine,
|
|
resumeEngine,
|
|
stopEngine,
|
|
startDataMonitor,
|
|
stopDataMonitor,
|
|
getDataMonitorInfo,
|
|
injectDataInterface,
|
|
startInjectContinuous,
|
|
stopInjectContinuous
|
|
}; |