feat: errorStack

This commit is contained in:
pk5ls20 committed 2024-11-13 16:52:03 +08:00
1 parent f07941685b
commit 75866b435e
11 files changed
+102 -35

No files matched your search

+5 -1
View File
@@ -2,6 +2,7 @@ import { LRUCache } from "@/common/lru-cache";
import crypto, { createHash } from "crypto";
import { PacketContext } from "@/core/packet/context/packetContext";
import { OidbPacket, PacketHexStr } from "@/core/packet/transformer/base";
import { LogStack } from "@/core/packet/context/clientContext";
export interface RecvPacket {
type: string, // 仅recv
@@ -24,13 +25,16 @@ function randText(len: number): string {
return text;
}
export abstract class IPacketClient {
protected readonly context: PacketContext;
protected readonly cb = new LRUCache<string, (json: RecvPacketData) => Promise<void>>(500); // trace_id-type callback
logStack: LogStack;
available: boolean = false;
protected constructor(context: PacketContext) {
protected constructor(context: PacketContext, logStack: LogStack) {
this.context = context;
this.logStack = logStack;
}
abstract check(): boolean;
+5 -4
View File
@@ -6,6 +6,7 @@ import { IPacketClient } from "@/core/packet/client/baseClient";
import { constants } from "node:os";
import { LRUCache } from "@/common/lru-cache";
import { PacketContext } from "@/core/packet/context/packetContext";
import { LogStack } from "@/core/packet/context/clientContext";
// 0 send 1 recv
export interface NativePacketExportType {
@@ -18,19 +19,19 @@ export class NativePacketClient extends IPacketClient {
private MoeHooExport: { exports: NativePacketExportType } = { exports: {} };
private sendEvent = new LRUCache<number, string>(500); // seq->trace_id
constructor(context: PacketContext) {
super(context);
constructor(context: PacketContext, logStack: LogStack) {
super(context, logStack);
}
check(): boolean {
const platform = process.platform + '.' + process.arch;
if (!this.supportedPlatforms.includes(platform)) {
this.context.logger.warn(`不支持的平台: ${platform}`);
this.logStack.pushLogWarn(`NativePacketClient: 不支持的平台: ${platform}`);
return false;
}
const moehoo_path = path.join(dirname(fileURLToPath(import.meta.url)), './moehoo/MoeHoo.' + platform + '.node');
if (!fs.existsSync(moehoo_path)) {
this.context.logger.warn(`[Core] [Packet:Native] 缺失运行时文件: ${moehoo_path}`);
this.logStack.pushLogWarn(`NativePacketClient: 缺失运行时文件: ${moehoo_path}`);
return false;
}
return true;
+7 -6
View File
@@ -1,6 +1,7 @@
import { Data, WebSocket } from "ws";
import { IPacketClient, RecvPacket } from "@/core/packet/client/baseClient";
import { PacketContext } from "@/core/packet/context/packetContext";
import { LogStack } from "@/core/packet/context/clientContext";
export class wsPacketClient extends IPacketClient {
private websocket: WebSocket | null = null;
@@ -12,8 +13,8 @@ export class wsPacketClient extends IPacketClient {
private isInitialized: boolean = false;
private initPayload: { pid: number, recv: string, send: string } | null = null;
constructor(context: PacketContext) {
super(context);
constructor(context: PacketContext, logStack: LogStack) {
super(context, logStack);
this.clientUrl = this.context.napcore.config.packetServer
? this.clientUrlWrap(this.context.napcore.config.packetServer)
: this.clientUrlWrap('127.0.0.1:8083');
@@ -21,7 +22,7 @@ export class wsPacketClient extends IPacketClient {
check(): boolean {
if (!this.context.napcore.config.packetServer) {
this.context.logger.warn(`wsPacketClient 未配置服务器地址`);
this.logStack.pushLogWarn(`wsPacketClient 未配置服务器地址`);
return false;
}
return true;
@@ -41,7 +42,7 @@ export class wsPacketClient extends IPacketClient {
trace_id
}));
} else {
this.context.logger.warn(`WebSocket 未连接,无法发送命令: ${cmd}`);
this.logStack.pushLogWarn(`WebSocket 未连接,无法发送命令: ${cmd}`);
}
}
@@ -52,11 +53,11 @@ export class wsPacketClient extends IPacketClient {
return;
} catch (error) {
this.reconnectAttempts++;
this.context.logger.warn(`第 ${this.reconnectAttempts}/${this.maxReconnectAttempts} 次尝试重连失败!`);
this.logStack.pushLogWarn(`第 ${this.reconnectAttempts}/${this.maxReconnectAttempts} 次尝试重连失败!`);
await this.delay(5000);
}
}
this.context.logger.error(`wsPacketClient 在 ${this.clientUrl} 达到最大重连次数 (${this.maxReconnectAttempts})!`);
this.logStack.pushLogError(`wsPacketClient 在 ${this.clientUrl} 达到最大重连次数 (${this.maxReconnectAttempts})!`);
throw new Error(`无法连接到 WebSocket 服务器:${this.clientUrl}`);
}