CommunicationProtocol.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using HslCommunication.BasicFramework;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace HslCommunication
  7. {
  8. /*******************************************************************************
  9. *
  10. * 一个通信辅助类,对通信中的数据做了信号区分
  11. *
  12. * Communications helper classes, the data signal in a communications distinction
  13. *
  14. *******************************************************************************/
  15. /// <summary>
  16. /// 用于本程序集访问通信的暗号说明
  17. /// </summary>
  18. public class HslProtocol
  19. {
  20. /// <summary>
  21. /// 规定所有的网络传输指令头都为32字节
  22. /// </summary>
  23. //internal const int HeadByteLength = 32;
  24. public static int HeadByteLength = 8;// cxy
  25. /// <summary>
  26. /// 所有网络通信中的缓冲池数据信息
  27. /// </summary>
  28. internal const int ProtocolBufferSize = 1024;
  29. /// <summary>
  30. /// 用于心跳程序的暗号信息
  31. /// </summary>
  32. internal const int ProtocolCheckSecends = 1;
  33. /// <summary>
  34. /// 客户端退出消息
  35. /// </summary>
  36. internal const int ProtocolClientQuit = 2;
  37. /// <summary>
  38. /// 因为客户端达到上限而拒绝登录
  39. /// </summary>
  40. internal const int ProtocolClientRefuseLogin = 3;
  41. /// <summary>
  42. /// 允许客户端登录到服务器
  43. /// </summary>
  44. internal const int ProtocolClientAllowLogin = 4;
  45. /// <summary>
  46. /// 说明发送的只是文本信息
  47. /// </summary>
  48. internal const int ProtocolUserString = 1001;
  49. /// <summary>
  50. /// 发送的数据就是普通的字节数组
  51. /// </summary>
  52. internal const int ProtocolUserBytes = 1002;
  53. /// <summary>
  54. /// 发送的数据就是普通的图片数据
  55. /// </summary>
  56. internal const int ProtocolUserBitmap = 1003;
  57. /// <summary>
  58. /// 发送的数据是一条异常的数据,字符串为异常消息
  59. /// </summary>
  60. internal const int ProtocolUserException = 1004;
  61. /// <summary>
  62. /// 请求文件下载的暗号
  63. /// </summary>
  64. internal const int ProtocolFileDownload = 2001;
  65. /// <summary>
  66. /// 请求文件上传的暗号
  67. /// </summary>
  68. internal const int ProtocolFileUpload = 2002;
  69. /// <summary>
  70. /// 请求删除文件的暗号
  71. /// </summary>
  72. internal const int ProtocolFileDelete = 2003;
  73. /// <summary>
  74. /// 文件校验成功
  75. /// </summary>
  76. internal const int ProtocolFileCheckRight = 2004;
  77. /// <summary>
  78. /// 文件校验失败
  79. /// </summary>
  80. internal const int ProtocolFileCheckError = 2005;
  81. /// <summary>
  82. /// 文件保存失败
  83. /// </summary>
  84. internal const int ProtocolFileSaveError = 2006;
  85. /// <summary>
  86. /// 请求文件列表的暗号
  87. /// </summary>
  88. internal const int ProtocolFileDirectoryFiles = 2007;
  89. /// <summary>
  90. /// 请求子文件的列表暗号
  91. /// </summary>
  92. internal const int ProtocolFileDirectories = 2008;
  93. /// <summary>
  94. /// 进度返回暗号
  95. /// </summary>
  96. internal const int ProtocolProgressReport = 2009;
  97. /// <summary>
  98. /// 不压缩数据字节
  99. /// </summary>
  100. internal const int ProtocolNoZipped = 3001;
  101. /// <summary>
  102. /// 压缩数据字节
  103. /// </summary>
  104. internal const int ProtocolZipped = 3002;
  105. /// <summary>
  106. /// 生成终极传送指令的方法,所有的数据均通过该方法出来
  107. /// </summary>
  108. /// <param name="command">命令头</param>
  109. /// <param name="customer">自用自定义</param>
  110. /// <param name="token">令牌</param>
  111. /// <param name="data">字节数据</param>
  112. /// <returns></returns>
  113. internal static byte[] CommandBytes( int command, int customer, Guid token, byte[] data )
  114. {
  115. byte[] _temp = null;
  116. int _zipped = ProtocolNoZipped;
  117. int _sendLength = 0;
  118. if (data == null)
  119. {
  120. _temp = new byte[HeadByteLength];
  121. }
  122. else
  123. {
  124. // 加密
  125. data = HslSecurity.ByteEncrypt( data );
  126. if (data.Length > 102400)
  127. {
  128. // 100K以上的数据,进行数据压缩
  129. data = SoftZipped.CompressBytes( data );
  130. _zipped = ProtocolZipped;
  131. }
  132. _temp = new byte[HeadByteLength + data.Length];
  133. _sendLength = data.Length;
  134. }
  135. BitConverter.GetBytes( command ).CopyTo( _temp, 0 );
  136. BitConverter.GetBytes( customer ).CopyTo( _temp, 4 );
  137. BitConverter.GetBytes( _zipped ).CopyTo( _temp, 8 );
  138. token.ToByteArray( ).CopyTo( _temp, 12 );
  139. BitConverter.GetBytes( _sendLength ).CopyTo( _temp, 28 );
  140. if (_sendLength > 0)
  141. {
  142. Array.Copy( data, 0, _temp, 32, _sendLength );
  143. }
  144. return _temp;
  145. }
  146. /// <summary>
  147. /// 解析接收到数据,先解压缩后进行解密
  148. /// </summary>
  149. /// <param name="head"></param>
  150. /// <param name="content"></param>
  151. internal static byte[] CommandAnalysis( byte[] head, byte[] content )
  152. {
  153. if (content != null)
  154. {
  155. int _zipped = BitConverter.ToInt32( head, 8 );
  156. // 先进行解压
  157. if (_zipped == ProtocolZipped)
  158. {
  159. content = SoftZipped.Decompress( content );
  160. }
  161. // 进行解密
  162. return HslSecurity.ByteDecrypt( content );
  163. }
  164. else
  165. {
  166. return null;
  167. }
  168. }
  169. /// <summary>
  170. /// 获取发送字节数据的实际数据,带指令头
  171. /// </summary>
  172. /// <param name="customer"></param>
  173. /// <param name="token"></param>
  174. /// <param name="data"></param>
  175. /// <returns></returns>
  176. internal static byte[] CommandBytes( int customer, Guid token, byte[] data )
  177. {
  178. return CommandBytes( ProtocolUserBytes, customer, token, data );
  179. }
  180. /// <summary>
  181. /// 获取发送字节数据的实际数据,带指令头
  182. /// </summary>
  183. /// <param name="customer"></param>
  184. /// <param name="token"></param>
  185. /// <param name="data"></param>
  186. /// <returns></returns>
  187. internal static byte[] CommandBytes( int customer, Guid token, string data )
  188. {
  189. if (data == null) return CommandBytes( ProtocolUserString, customer, token, null );
  190. else return CommandBytes( ProtocolUserString, customer, token, Encoding.Unicode.GetBytes( data ) );
  191. }
  192. }
  193. }