MessageProtocol.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. namespace UnityCommon
  7. {
  8. // 消息协议类由 【协议命令】+【协议参数】+【实际消息长度】+【实际消息内容】+【多余的消息内容】 组成
  9. public sealed class MessageProtocol
  10. {
  11. public const int HEADLENGTH = 6; // 协议首部长度(命令1字节+参数1字节+数据长度4字节=6字节)
  12. private byte _Command = 0;
  13. public byte Command { get => this._Command; } // 协议信令标志 (值范围0~255)
  14. private byte _Param = 0;
  15. public byte Param { get => this._Param; } // 信令参数标志(范围0~255)
  16. private int _DataLength = 0;
  17. public int DataLength { get => this._DataLength; } // 有效载荷数据长度
  18. private byte[] _MessageData = new byte[0];
  19. public byte[] MessageData { get => this._MessageData; } // 完整消息体
  20. private byte[] _MoreData = new byte[0];
  21. public byte[] MoreData { get => this._MoreData; } // 完整消息体多余的数据
  22. public MessageProtocol(byte[] buffer) // 通过字节数组创建消息协议对象(相当于拆包过程)
  23. {
  24. if (buffer == null || buffer.Length < HEADLENGTH) // 当要拆包的数据长度小于协议首部长度,就不进行拆包,因为根本无法得出需要的信息,比如有效数据长度
  25. {
  26. return;
  27. }
  28. MemoryStream ms = new MemoryStream(buffer);
  29. BinaryReader br = new BinaryReader(ms);
  30. try
  31. {
  32. this._Command = br.ReadByte();
  33. this._Param = br.ReadByte();
  34. this._DataLength = br.ReadInt32();
  35. if (buffer.Length - HEADLENGTH >= this._DataLength) // 如果要拆包的数据长度减去协议首部长度大于等于需要的数据长度就可以提取出实际的数据
  36. {
  37. this._MessageData = br.ReadBytes(this._DataLength); // 读取DataLength长度的数据
  38. }
  39. if (buffer.Length - HEADLENGTH - this.DataLength > 0) // 如果要拆包的数据长度减去协议头长度减去实际数据的长度大于0就说明还有多余数据
  40. {
  41. this._MoreData = br.ReadBytes(buffer.Length - HEADLENGTH - this._DataLength); // 读取多余数据
  42. }
  43. }
  44. catch (Exception)
  45. {
  46. // 异常处理
  47. }
  48. br.Close();
  49. ms.Close();
  50. }
  51. public MessageProtocol(byte command, byte param, byte[] messageData) // 通过传入协议参数构造一个协议对象
  52. {
  53. this._Command = command;
  54. this._Param = param;
  55. this._MessageData = messageData;
  56. this._DataLength = messageData.Length;
  57. }
  58. public byte[] GetBytes() // 将协议对象转换成二进制数据包(相当于封包过程)
  59. {
  60. try
  61. {
  62. byte[] bytes = null;
  63. MemoryStream ms = new MemoryStream();
  64. BinaryWriter bw = new BinaryWriter(ms);
  65. bw.Write(this._Command);
  66. bw.Write(this._Param);
  67. bw.Write(this._DataLength);
  68. bw.Write(this._MessageData);
  69. bytes = ms.ToArray();
  70. bw.Close();
  71. ms.Close();
  72. return (bytes);
  73. }
  74. catch (Exception)
  75. {
  76. return (new byte[0]);
  77. }
  78. }
  79. public (byte Command, byte Param, int DataLength, byte[] MessageData) GetMessage() // 获取完整的一条消息(忽略多余字节部分)
  80. {
  81. (byte Command, byte Param, int DataLength, byte[] MessageData) returnValue = (this._Command, this._Param, this._MessageData.Length, this._MessageData);
  82. return (returnValue);
  83. }
  84. public static (byte Command, byte Param, int DataLength) GetHeadInfo(byte[] buffer) // 读取协议头部分
  85. {
  86. (byte Command, byte Param, int DataLength) returnValue = (0, 0, 0);
  87. if (buffer == null || buffer.Length < HEADLENGTH) // 如果数据长度小于协议头根本无法读出完整的协议头内容
  88. {
  89. return (returnValue);
  90. }
  91. returnValue.Command = buffer[0];
  92. returnValue.Param = buffer[1];
  93. returnValue.DataLength = BitConverter.ToInt32(buffer, 2);
  94. return (returnValue);
  95. }
  96. }
  97. }