HslMessage.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace HslCommunication.Core.IMessage
  6. {
  7. /// <summary>
  8. /// 本组件系统使用的默认的消息规则,说明解析和反解析规则的
  9. /// </summary>
  10. public class HslMessage : INetMessage
  11. {
  12. /// <summary>
  13. /// 本协议的消息头长度
  14. /// </summary>
  15. public int ProtocolHeadBytesLength
  16. {
  17. get { return 32; }
  18. }
  19. /// <summary>
  20. /// 头子节信息
  21. /// </summary>
  22. public byte[] HeadBytes { get ; set; }
  23. /// <summary>
  24. /// 内容字节信息
  25. /// </summary>
  26. public byte[] ContentBytes { get ; set ; }
  27. /// <summary>
  28. /// 检查接收的数据是否合法
  29. /// </summary>
  30. /// <param name="token">令牌</param>
  31. /// <returns>是否合法</returns>
  32. public bool CheckHeadBytesLegal(byte[] token)
  33. {
  34. if(HeadBytes?.Length>=32)
  35. {
  36. return BasicFramework.SoftBasic.IsTwoBytesEquel(HeadBytes, 12, token, 0, 16);
  37. }
  38. else
  39. {
  40. return false;
  41. }
  42. }
  43. /// <summary>
  44. /// 从头子节信息中解析出接下来需要接收的数据长度
  45. /// </summary>
  46. /// <returns>接下来的数据长度</returns>
  47. public int GetContentLengthByHeadBytes()
  48. {
  49. if (HeadBytes?.Length >= 32)
  50. {
  51. return BitConverter.ToInt32(HeadBytes, 28);
  52. }
  53. else
  54. {
  55. return 0;
  56. }
  57. }
  58. /// <summary>
  59. /// 获取头子节里的特殊标识
  60. /// </summary>
  61. /// <returns>标识信息</returns>
  62. public int GetHeadBytesIdentity()
  63. {
  64. if (HeadBytes?.Length >= 32)
  65. {
  66. return BitConverter.ToInt32(HeadBytes, 4);
  67. }
  68. else
  69. {
  70. return 0;
  71. }
  72. }
  73. /// <summary>
  74. /// 发送的字节信息
  75. /// </summary>
  76. public byte[] SendBytes { get; set; }
  77. }
  78. }