SimpleSocketServerModel.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 
  2. using System;
  3. using System.Text;
  4. namespace Curtain.Net.Sockets.PLC.Model
  5. {
  6. /// <summary>
  7. /// 基本Socket服务模型
  8. /// </summary>
  9. public class SimpleSocketServerModel : ServerModel
  10. {
  11. /// <summary>
  12. /// 响应报文-头长度
  13. /// </summary>
  14. public override int HeadLength
  15. {
  16. get
  17. {
  18. return (FormatType == CommandFormatType.PLCCommand ? 4 : 1);
  19. }
  20. }
  21. /// <summary>
  22. /// 根据响应报文-头,计算响应报文-文本长度
  23. /// </summary>
  24. /// <param name="rs">报文</param>
  25. /// <returns>响应报文-文本长度</returns>
  26. public override int GetContentLength(ReceiveSession rs)
  27. {
  28. try
  29. {
  30. return Convert.ToInt32(Encoding.ASCII.GetString(rs.HeadBytes, 2, 2), 16);
  31. }
  32. catch
  33. {
  34. return -1;
  35. }
  36. }
  37. /// <summary>
  38. /// 验证响应报文-头
  39. /// </summary>
  40. /// <param name="rs">报文</param>
  41. /// <returns>是否通过</returns>
  42. public override bool CheckHead(ReceiveSession rs)
  43. {
  44. if (rs == null || rs.HeadBytes == null || rs.HeadBytes.Length != HeadLength)
  45. {
  46. return false;
  47. }
  48. if (FormatType == CommandFormatType.StartStopChar)
  49. {
  50. return CheckHeadChar(rs.HeadBytes);
  51. }
  52. rs.Command = ToCommandFromReceive(rs.HeadBytes);
  53. rs.Head = ToHeadFromReceive(rs.HeadBytes);
  54. return true;
  55. }
  56. /// <summary>
  57. /// 接收字节转换为命令
  58. /// </summary>
  59. /// <param name="receive">接收字节</param>
  60. /// <returns>响应报文</returns>
  61. public override string ToCommandFromReceive(byte[] receive)
  62. {
  63. if (receive == null || receive.Length == 0)
  64. {
  65. return null;
  66. }
  67. return Encoding.ASCII.GetString(receive, 0, 2);
  68. }
  69. /// <summary>
  70. /// 接收字节转换为报文头
  71. /// </summary>
  72. /// <param name="receive">接收字节</param>
  73. /// <returns>响应报文</returns>
  74. public override string ToHeadFromReceive(byte[] receive)
  75. {
  76. if (receive == null || receive.Length == 0)
  77. {
  78. return null;
  79. }
  80. //return Encoding.ASCII.GetString(receive, 0, HeadLength);
  81. return Encoding.ASCII.GetString(receive);
  82. }
  83. /// <summary>
  84. /// 接收字节转换为报文正文
  85. /// </summary>
  86. /// <param name="receive">接收字节</param>
  87. /// <returns>响应报文</returns>
  88. public override string ToContentFromReceive(byte[] receive)
  89. {
  90. if (receive == null || receive.Length == 0)
  91. {
  92. return null;
  93. }
  94. return Encoding.ASCII.GetString(receive);
  95. }
  96. /// <summary>
  97. /// 命令报文转换为发送字节
  98. /// </summary>
  99. /// <param name="command">命令报文</param>
  100. /// <returns>发送字节</returns>
  101. public override byte[] ToSendFromCommand(string command)
  102. {
  103. if (string.IsNullOrEmpty(command))
  104. {
  105. return null;
  106. }
  107. return Encoding.ASCII.GetBytes(command);
  108. }
  109. }
  110. }