177 lines
4.7 KiB
JavaScript
177 lines
4.7 KiB
JavaScript
const protobuf = require('protobufjs');
|
|
const ws = require('nodejs-websocket');
|
|
|
|
function prettyJsonEncode(obj) {
|
|
return JSON.stringify(obj, "", " ");
|
|
}
|
|
|
|
class ClientNet {
|
|
|
|
constructor(url, protoPbFile, msgIdPbFile) {
|
|
this.url = url;
|
|
this.conn = null;
|
|
this.protoPbFile = protoPbFile;
|
|
this.msgIdPbFile = msgIdPbFile;
|
|
this.protoPb = null;
|
|
this.msgIdPb = null;
|
|
this.cmMsgId = null;
|
|
this.smMsgId = null;
|
|
this.recvBuf = Buffer.alloc(0);
|
|
this.uniqId = 1000;
|
|
this.msgHandlerMap = new Map();
|
|
}
|
|
|
|
selfRegisterMsgHandle(msgName) {
|
|
this.registerMsgHandle(
|
|
msgName,
|
|
(msg) => {
|
|
this[msgName](msg);
|
|
});
|
|
}
|
|
|
|
async init() {
|
|
{
|
|
this.protoPb = await (new protobuf.Root()).load(this.protoPbFile,
|
|
{
|
|
'keepCase': true
|
|
}
|
|
);
|
|
}
|
|
{
|
|
this.msgIdPb = await (new protobuf.Root()).load(this.msgIdPbFile,
|
|
{
|
|
'keepCase': true
|
|
}
|
|
);
|
|
this.cmMsgId = this.msgIdPb.lookup('CMMessageId_e');
|
|
this.smMsgId = this.msgIdPb.lookup('SMMessageId_e');
|
|
}
|
|
{
|
|
this.selfRegisterMsgHandle('SMLogin');
|
|
this.selfRegisterMsgHandle('SMPing');
|
|
}
|
|
}
|
|
|
|
async connect() {
|
|
this.conn = await ws.connect(this.url);
|
|
this.on('binary', this.#onReceive.bind(this));
|
|
}
|
|
|
|
on(eventName, ...args) {
|
|
this.conn.on(eventName, ...args);
|
|
}
|
|
|
|
async #onReceive(inStream) {
|
|
inStream.on('readable', async () => {
|
|
//console.log('inStream.readable');
|
|
const newData = inStream.read();
|
|
if (newData) {
|
|
this.recvBuf = Buffer.concat([this.recvBuf, newData]);
|
|
await this.#onParsePacket();
|
|
}
|
|
});
|
|
inStream.on('end', () => {
|
|
//console.log('inStream.end', this.recvBuf.length);
|
|
});
|
|
inStream.on('close', () => {
|
|
//console.log('inStream.close');
|
|
});
|
|
}
|
|
|
|
async #onParsePacket() {
|
|
let offset = 0;
|
|
while (this.recvBuf.length > offset + 12) {
|
|
const msgSize = this.recvBuf.readUInt32LE(offset + 0);
|
|
const msgId = this.recvBuf.readUInt16LE(offset + 4);
|
|
const magicCode = this.recvBuf.readUInt16LE(offset + 6);
|
|
const seqId = this.recvBuf.readUInt32LE(offset + 8);
|
|
if (this.recvBuf.length >= offset + 12 + msgSize) {
|
|
await this.#processMsg(msgId,
|
|
this.recvBuf.slice
|
|
(
|
|
offset + 12,
|
|
offset + 12 + msgSize)
|
|
);
|
|
offset += 12 + msgSize;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
this.recvBuf = this.recvBuf.slice(offset);
|
|
}
|
|
|
|
async #processMsg(msgId, buff) {
|
|
const handlers = this.msgHandlerMap.get(msgId);
|
|
if (handlers) {
|
|
let msg = null;
|
|
handlers.forEach((value, key) => {
|
|
if (!msg) {
|
|
msg = value.msgType.decode(buff);
|
|
}
|
|
console.log(value.msgType['name'], prettyJsonEncode(msg));
|
|
value.cb(msg);
|
|
});
|
|
}
|
|
}
|
|
|
|
registerMsgHandle(msgName, cb) {
|
|
const msgType = this.protoPb.lookupType(msgName);
|
|
const msgId = this.smMsgId.values['_' + msgName];
|
|
if (!msgId) {
|
|
return null;
|
|
}
|
|
const handle = {
|
|
msgId: msgId,
|
|
msgName: msgName,
|
|
msgType: msgType,
|
|
cb: cb,
|
|
uniqId: ++this.uniqId}
|
|
;
|
|
if (!this.msgHandlerMap.has(msgId)) {
|
|
this.msgHandlerMap.set(msgId, new Map([
|
|
[handle.uniqId, handle]
|
|
]));
|
|
} else {
|
|
this.msgHandlerMap.get(msgId).set(handle.uniqId, handle);
|
|
}
|
|
return handle;
|
|
}
|
|
|
|
unRegisterMsgHandle(handle) {
|
|
if (this.msgHandlerMap.has(handle.msgId)) {
|
|
if (this.msgHandlerMap[handle.msgId].has(handle.uniqId)) {
|
|
this.msgHandlerMap[handle.msgId].delete(handle.uniqId);
|
|
}
|
|
}
|
|
}
|
|
|
|
async sendMsg(name, msg) {
|
|
const msgType = this.protoPb.lookupType(name);
|
|
const msgId = this.cmMsgId.values['_' + name];
|
|
const msgPb = msgType.create(msg);
|
|
|
|
const msgBuf = msgType.encode(msg).finish();
|
|
let buf = Buffer.alloc(12);
|
|
buf.writeUInt32LE(msgBuf.length, 0);
|
|
buf.writeUInt16LE(msgId, 4);
|
|
buf.writeUInt8('K'.charCodeAt(0), 6);
|
|
buf.writeUInt8('S'.charCodeAt(0), 7);
|
|
buf.writeInt32LE(0, 8);
|
|
this.conn.sendBinary(Buffer.concat([buf, msgBuf]));
|
|
console.log(name, msg, (Buffer.concat([buf, msgBuf])).length);
|
|
}
|
|
|
|
SMLogin(msg) {
|
|
if (msg.errcode) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
SMPing(msg) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = ClientNet;
|