数据格式

bricks引擎提供了几种适用于网络传输的数据格式。

BK.Buffer 缓冲区

BK.Buffer支持多种数据同时写入与读取。读写操作相当于堆操作,写入的操作相当于压栈,读取的操作相当于出栈。

writeUint8Buffer / readUint8Buffer

以无符号8位整型格式 写入 / 读取 当前buffer:

  1. var buff = new BK.Buffer(lenUint8);
  2. buff.writeUint8Buffer(numUint8);
  3. var read = buff.readUint8Buffer();

writeUint16Buffer / readUint16Buffer

以无符号16位整型格式 写入 / 读取 当前buffer:

  1. var buff = new BK.Buffer(lenUint16);
  2. buff.writeUint16Buffer(numUint16);
  3. var read = buff.readUint16Buffer();

writeUint32Buffer / readUint32Buffer

以无符号32位整型格式 写入 / 读取 当前buffer:

  1. var buff = new BK.Buffer(lenUint32);
  2. buff.writeUint32Buffer(numUint32);
  3. var read = buff.readUint32Buffer();

writeUint64Buffer / readUint64Buffer

以无符号64位整型格式 写入 / 读取 当前buffer:

  1. var buff = new BK.Buffer(lenUint64);
  2. buff.writeUint64Buffer(numUint64);
  3. var read = buff.readUint64Buffer();

writeInt8Buffer / readInt8Buffer

以8位整型格式 写入 / 读取 当前buffer:

  1. var buff = new BK.Buffer(lenInt8);
  2. buff.writeInt8Buffer(numInt8);
  3. var read = buff.readInt8Buffer();

writeInt16Buffer / readInt16Buffer

以16位整型格式 写入 / 读取 当前buffer:

  1. var buff = new BK.Buffer(lenInt16);
  2. buff.writeInt16Buffer(numInt16);
  3. var read = buff.readInt16Buffer();

writeInt32Buffer / readInt32Buffer

以32位整型格式 写入 / 读取 当前buffer:

  1. var buff = new BK.Buffer(lenInt32);
  2. buff.writeInt32Buffer(numInt32);
  3. var read = buff.readInt32Buffer();

writeInt64Buffer / readInt64Buffer

以64位整型格式 写入 / 读取 当前buffer:

  1. var buff = new BK.Buffer(lenInt64);
  2. buff.writeInt64Buffer(numInt64);
  3. var read = buff.readInt64Buffer();

writeFloatBuffer / readFloatBuffer

以单精度浮点数格式 写入 / 读取 当前buffer:

  1. var buff = new BK.Buffer(lenFloat);
  2. buff.writeFloatBuffer(numFloat);
  3. var read = buff.readFloatBuffer();

writeDoubleBuffer / readDoubleBuffer

以双精度浮点数格式 写入 / 读取 当前buffer:

  1. var buff = new BK.Buffer(lenDouble);
  2. buff.writeDoubleBuffer(numDouble);
  3. var read = buff.readDoubleBuffer();

writeStringBuffer / readStringBuffer

以String字符串格式 写入 / 读取 当前buffer:

  1. var buff = new BK.Buffer(str.length+3);
  2. buff.writeStringBuffer(str);
  3. var read = buff.readStringBuffer();

writeAsString / readAsString

将buffer整段以字符串格式进行 写入 / 读取

  1. var buff = new BK.Buffer(str.length+3);
  2. buff.writeAsString(str);
  3. var read = buff.readAsString();

writeBuffer / readBuffer

写入 / 读取 一定长度的buffer:

  1. var buff = new BK.Buffer(str.length+3);
  2. buff.writeBuffer(str);
  3. var read = buff.readBuffer(8);// 读取8个字节

bufferLength

获取buffer长度:

  1. var buff = new BK.Buffer(10);
  2. var len = buff.bufferLength();//len = 10

rewind

将游标pointer重置为0:

  1. buff.rewind();

jumpBytes

将游标pointer移动n字节:

  1. buff.jumpBytes(10);

truncateBytes

截断已写入的数据缓存:

  1. buff.length = 512;
  2. buff.truncateBytes(50);// buff剩余已写入数据量为462

expandToBytes

扩展buffer缓冲:

  1. buff.expandToBytes(num);

releaseBuffer

释放buffer缓冲:

  1. buff.releaseBuffer();

接口说明:BK.Buffer 缓冲区

示例代码:script/demo/basics/buffer_demo.js

BK.TLV [Tag][Length][Value]

[Tag][Length][Value],其中Tag占2字节 length占2个字节,当使用构造函数申请空间时,则需申请 数据个数 *(4+各个数据类型长度)个字节。

TLV支持的类型:

  1. TLVType = {
  2. Int8 : 0x21,
  3. Uint8 : 0x22,
  4. Int16 : 0x21,
  5. Uint16 : 0x24,
  6. Int32 : 0x25,
  7. Uint32 : 0x26,
  8. Int64 : 0x27,
  9. Uint64 : 0x28,
  10. Byte : 0x29,
  11. Double : 0x2a,
  12. Float : 0x2b,
  13. Int8Repeated : 0x31,
  14. Uint8Repeated : 0x32,
  15. Int16Repeated : 0x33,
  16. Uint16Repeated : 0x34,
  17. Int32Repeated : 0x35,
  18. Uint32Repeated : 0x36,
  19. Int64Repeated : 0x37,
  20. Uint64Repeated : 0x38,
  21. ByteRepeated : 0x39,
  22. DoubleRepeated : 0x3a,
  23. FloatRepeated : 0x3b
  24. }

TLV作为数据发送 / 接收:

  1. // ------- 作为数据发送 -------
  2. var sendStr = "message";
  3. var sendTlv = new BK.TLV(sendStr.length+4);
  4. //写入一个String,标记为2
  5. var sendBuf = new BK.Buffer(sendStr.length);
  6. sendBuf.writeAsString(sendStr);
  7. sendTlv.bkJSTLVWriteBuffer(sendBuf,TLVType.Byte,2);
  8. // 假装经过了网络传输
  9. var recvBuffer = sendTlv.bkJSTLVGetBuffer();
  10. // ------- 作为数据接收 -------
  11. var recvTlv = new BK.TLV(recvBuffer);
  12. //解析
  13. var result = recvTlv.bkJSParseTLV();
  14. for (var key in result) {
  15. if (result.hasOwnProperty(key)) {
  16. var element = result[key];
  17. BK.Script.log(0,0,"key = " + key + " element = "+element);
  18. }
  19. }
  20. //根据标记读取数据
  21. var value = result.tag2;
  22. var recvStr = value.readAsString();

接口说明:BK.TLV

示例代码:script/demo/basics/tlv_demo.js

原文: https://hudong.qq.com/docs/engine/engine/native/func/funcs/data.html