import Application from '../application'; import * as define from '../util/define'; import { I_encodeDecodeConfig } from '../util/interfaceDefine'; let app: Application; export function init(_app: Application) { app = _app; } export const default_encodeDecode: Required = { protoDecode: function (data: Buffer) { return { cmd: data.readUInt16BE(1), msg: data.subarray(3) }; }, msgDecode: function (cmd: number, msg: Buffer) { return JSON.parse(msg.toString()); }, protoEncode: function (cmd: number, msg: any) { const msgBuf: Buffer = app.msgEncode(cmd, msg); const buf = Buffer.allocUnsafe(msgBuf.length + 7); buf.writeUInt32BE(msgBuf.length + 3, 0); buf.writeUInt8(define.Server_To_Client.msg, 4); buf.writeUInt16BE(cmd, 5); msgBuf.copy(buf, 7); return buf; }, msgEncode: function (cmd: number, msg: any) { return Buffer.from(JSON.stringify(msg)); } };