Hibok
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

53 righe
1.2 KiB

  1. import 'package:buffer/buffer.dart';
  2. import 'dart:typed_data';
  3. class MsgPacketModule {
  4. int comId;
  5. int msgId;
  6. List<int> msg;
  7. MsgPacketModule({this.comId, this.msgId, this.msg});
  8. }
  9. class MsgPacketUtil {
  10. static Uint8List encode(MsgPacketModule msgModule) {
  11. int msgLen = msgModule.msg.length;
  12. msgLen += 9; //修改为整体长度
  13. var buffWriter = ByteDataWriter(bufferLength: msgLen, endian: Endian.big);
  14. buffWriter.writeInt16(msgModule.comId);
  15. buffWriter.writeInt16(msgModule.msgId);
  16. buffWriter.writeInt8(0x24);
  17. buffWriter.writeUint32(msgLen);
  18. buffWriter.write(msgModule.msg);
  19. return buffWriter.toBytes();
  20. }
  21. static decode(Uint8List msg) {
  22. var decodeMsg = new MsgPacketModule();
  23. var buffReader = ByteDataReader(endian: Endian.big);
  24. buffReader.add(msg);
  25. int cmdId = buffReader.readInt16();
  26. decodeMsg.comId = cmdId;
  27. int msgId = buffReader.readInt16();
  28. decodeMsg.msgId = msgId;
  29. buffReader.readInt8();
  30. int msgLen = buffReader.readUint32();
  31. if (msgLen > 9) {
  32. decodeMsg.msg = buffReader.read(msgLen - 9);
  33. }
  34. return decodeMsg;
  35. }
  36. }