zhuguoqing ff550d5d6a init
2022-05-22 10:32:02 +08:00

147 lines
4.4 KiB
JavaScript

// Learn cc.Class:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
const BUFF_SIZE = 1024;
const MSGHEAD_SIZE = 12;
var MO = require('msgobj');
var MsgBuffer = cc.Class({
name: "MsgBuffer",
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
start () {
},
// update (dt) {},
init(){
this.BUFF_SZ = BUFF_SIZE;
this.buffer = new ArrayBuffer(BUFF_SIZE);
this.handrecvbuf = new Uint8Array(this.buffer);
this.usedlen = 0;
},
pushData(buf){
if(!buf || !buf.byteLength || buf.byteLength <= 0){
return;
}
var len = buf.byteLength;
while (this.usedlen + len >= this.BUFF_SZ){
console.log("msgbuffer enlarge *2!" + this.BUFF_SZ + "|" + this.usedlen);
this.BUFF_SZ *= 2;
var newBuffer = new ArrayBuffer(this.BUFF_SZ);
if(this.usedlen>0){
var oldbuf = new Uint8Array(this.buffer);
var newbuf = new Uint8Array(newBuffer);
var subold = oldbuf.subarray(this.usedlen);
newbuf.set(subold);
}
this.buffer = newBuffer;
this.handrecvbuf = new Uint8Array(this.buffer);
}
var subbuf = this.handrecvbuf.subarray(this.usedlen);
var hbuf = new Uint8Array(buf);
subbuf.set(hbuf);
this.usedlen += len;
},
hasPackMsg(){
if(this.usedlen < MSGHEAD_SIZE){//len(2byte) + id(2byte) + code(4byte)
return false;
}
var handbuf = this.handrecvbuf;
var num = handbuf[0] | ((handbuf[1] << 8));
if(this.usedlen < num + MSGHEAD_SIZE){
return false;
}
return true;
},
popPackMsg(){
var handbuf = this.handrecvbuf;
var num = handbuf[0] | ((handbuf[1] << 8));
var id = handbuf[2] | ((handbuf[3] << 8));
var msg = new MO(handbuf.buffer, num);
// msg.errcode = handbuf[10]>> 8+handbuf[11]
// console.log("[popPackMsg]:"+num+","+id);
//start update param
var n = num + MSGHEAD_SIZE;
if(this.usedlen > n){
this.buffer.copyWithin(0, n, this.usedlen);
}
this.usedlen -= n;
return msg;
},
buildPackMsg(msgid, arrbuf){
var len = arrbuf? arrbuf.byteLength: 0;
var num = len + MSGHEAD_SIZE;
var bytebuf = new Uint8Array(num);
bytebuf[0] = len > 0? len & 0xFF: 0;
bytebuf[1] = len > 0? (len >> 8) & 0xFF: 0;
bytebuf[2] = msgid & 0xFF;
bytebuf[3] = (msgid >> 8) & 0xFF;
bytebuf[4] = 0x00;
bytebuf[5] = 0x00;
bytebuf[6] = 0x00;
bytebuf[7] = 0x00;
bytebuf[8] = 75;
bytebuf[9] = 83;
bytebuf[10] = 0x00;
bytebuf[11] = 0x00;
//var handbuf = new Uint16Array(bytebuf.buffer);
//handbuf[0] = num;
//handbuf[1] = msgid;
//var dwordbuf = new Uint32Array(handbuf.buffer);
//dwordbuf[1] = 0xFFCCAABB;
if(arrbuf){
var oldbuf = new Uint8Array(arrbuf);
bytebuf.set(oldbuf, 12);
}
return bytebuf.buffer;
}
});
module.exports = MsgBuffer;