- 数据格式
- BK.Buffer 缓冲区
- writeUint8Buffer / readUint8Buffer
- writeUint16Buffer / readUint16Buffer
- writeUint32Buffer / readUint32Buffer
- writeUint64Buffer / readUint64Buffer
- writeInt8Buffer / readInt8Buffer
- writeInt16Buffer / readInt16Buffer
- writeInt32Buffer / readInt32Buffer
- writeInt64Buffer / readInt64Buffer
- writeFloatBuffer / readFloatBuffer
- writeDoubleBuffer / readDoubleBuffer
- writeStringBuffer / readStringBuffer
- writeAsString / readAsString
- writeBuffer / readBuffer
- bufferLength
- rewind
- jumpBytes
- truncateBytes
- expandToBytes
- releaseBuffer
- BK.TLV [Tag][Length][Value]
- BK.Buffer 缓冲区
数据格式
bricks引擎提供了几种适用于网络传输的数据格式。
BK.Buffer 缓冲区
BK.Buffer支持多种数据同时写入与读取。读写操作相当于堆操作,写入的操作相当于压栈,读取的操作相当于出栈。
writeUint8Buffer / readUint8Buffer
以无符号8位整型格式 写入 / 读取 当前buffer:
var buff = new BK.Buffer(lenUint8);
buff.writeUint8Buffer(numUint8);
var read = buff.readUint8Buffer();
writeUint16Buffer / readUint16Buffer
以无符号16位整型格式 写入 / 读取 当前buffer:
var buff = new BK.Buffer(lenUint16);
buff.writeUint16Buffer(numUint16);
var read = buff.readUint16Buffer();
writeUint32Buffer / readUint32Buffer
以无符号32位整型格式 写入 / 读取 当前buffer:
var buff = new BK.Buffer(lenUint32);
buff.writeUint32Buffer(numUint32);
var read = buff.readUint32Buffer();
writeUint64Buffer / readUint64Buffer
以无符号64位整型格式 写入 / 读取 当前buffer:
var buff = new BK.Buffer(lenUint64);
buff.writeUint64Buffer(numUint64);
var read = buff.readUint64Buffer();
writeInt8Buffer / readInt8Buffer
以8位整型格式 写入 / 读取 当前buffer:
var buff = new BK.Buffer(lenInt8);
buff.writeInt8Buffer(numInt8);
var read = buff.readInt8Buffer();
writeInt16Buffer / readInt16Buffer
以16位整型格式 写入 / 读取 当前buffer:
var buff = new BK.Buffer(lenInt16);
buff.writeInt16Buffer(numInt16);
var read = buff.readInt16Buffer();
writeInt32Buffer / readInt32Buffer
以32位整型格式 写入 / 读取 当前buffer:
var buff = new BK.Buffer(lenInt32);
buff.writeInt32Buffer(numInt32);
var read = buff.readInt32Buffer();
writeInt64Buffer / readInt64Buffer
以64位整型格式 写入 / 读取 当前buffer:
var buff = new BK.Buffer(lenInt64);
buff.writeInt64Buffer(numInt64);
var read = buff.readInt64Buffer();
writeFloatBuffer / readFloatBuffer
以单精度浮点数格式 写入 / 读取 当前buffer:
var buff = new BK.Buffer(lenFloat);
buff.writeFloatBuffer(numFloat);
var read = buff.readFloatBuffer();
writeDoubleBuffer / readDoubleBuffer
以双精度浮点数格式 写入 / 读取 当前buffer:
var buff = new BK.Buffer(lenDouble);
buff.writeDoubleBuffer(numDouble);
var read = buff.readDoubleBuffer();
writeStringBuffer / readStringBuffer
以String字符串格式 写入 / 读取 当前buffer:
var buff = new BK.Buffer(str.length+3);
buff.writeStringBuffer(str);
var read = buff.readStringBuffer();
writeAsString / readAsString
将buffer整段以字符串格式进行 写入 / 读取:
var buff = new BK.Buffer(str.length+3);
buff.writeAsString(str);
var read = buff.readAsString();
writeBuffer / readBuffer
写入 / 读取 一定长度的buffer:
var buff = new BK.Buffer(str.length+3);
buff.writeBuffer(str);
var read = buff.readBuffer(8);// 读取8个字节
bufferLength
获取buffer长度:
var buff = new BK.Buffer(10);
var len = buff.bufferLength();//len = 10
rewind
将游标pointer重置为0:
buff.rewind();
jumpBytes
将游标pointer移动n字节:
buff.jumpBytes(10);
truncateBytes
截断已写入的数据缓存:
buff.length = 512;
buff.truncateBytes(50);// buff剩余已写入数据量为462
expandToBytes
扩展buffer缓冲:
buff.expandToBytes(num);
releaseBuffer
释放buffer缓冲:
buff.releaseBuffer();
接口说明:BK.Buffer 缓冲区
示例代码:script/demo/basics/buffer_demo.js
BK.TLV [Tag][Length][Value]
[Tag][Length][Value],其中Tag占2字节 length占2个字节,当使用构造函数申请空间时,则需申请 数据个数 *(4+各个数据类型长度)个字节。
TLV支持的类型:
TLVType = {
Int8 : 0x21,
Uint8 : 0x22,
Int16 : 0x21,
Uint16 : 0x24,
Int32 : 0x25,
Uint32 : 0x26,
Int64 : 0x27,
Uint64 : 0x28,
Byte : 0x29,
Double : 0x2a,
Float : 0x2b,
Int8Repeated : 0x31,
Uint8Repeated : 0x32,
Int16Repeated : 0x33,
Uint16Repeated : 0x34,
Int32Repeated : 0x35,
Uint32Repeated : 0x36,
Int64Repeated : 0x37,
Uint64Repeated : 0x38,
ByteRepeated : 0x39,
DoubleRepeated : 0x3a,
FloatRepeated : 0x3b
}
TLV作为数据发送 / 接收:
// ------- 作为数据发送 -------
var sendStr = "message";
var sendTlv = new BK.TLV(sendStr.length+4);
//写入一个String,标记为2
var sendBuf = new BK.Buffer(sendStr.length);
sendBuf.writeAsString(sendStr);
sendTlv.bkJSTLVWriteBuffer(sendBuf,TLVType.Byte,2);
// 假装经过了网络传输
var recvBuffer = sendTlv.bkJSTLVGetBuffer();
// ------- 作为数据接收 -------
var recvTlv = new BK.TLV(recvBuffer);
//解析
var result = recvTlv.bkJSParseTLV();
for (var key in result) {
if (result.hasOwnProperty(key)) {
var element = result[key];
BK.Script.log(0,0,"key = " + key + " element = "+element);
}
}
//根据标记读取数据
var value = result.tag2;
var recvStr = value.readAsString();
接口说明:BK.TLV
示例代码:script/demo/basics/tlv_demo.js
原文: https://hudong.qq.com/docs/engine/engine/native/func/funcs/data.html