189 lines
4.8 KiB
JavaScript
189 lines
4.8 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', []]
|
|
});
|
|
} 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(4096);
|
|
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(4096);
|
|
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;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
loginLib,
|
|
monitorLib,
|
|
performCleanup,
|
|
stringToBuffer,
|
|
initializeMonitor,
|
|
cleanupMonitor,
|
|
startMonitorSystemInfo,
|
|
getSystemInfo,
|
|
getAllThreadInfo,
|
|
stopMonitorSystemInfo
|
|
};
|