NetSimplifyServer.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using HslCommunication.Core.Net;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. namespace HslCommunication.Enthernet
  8. {
  9. /// <summary>
  10. /// 同步消息处理服务器,主要用来实现接收客户端信息并进行消息反馈的操作
  11. /// </summary>
  12. public class NetSimplifyServer : NetworkServerBase
  13. {
  14. #region Constructor
  15. /// <summary>
  16. /// 实例化一个服务器消息请求的信息
  17. /// </summary>
  18. public NetSimplifyServer()
  19. {
  20. // cxy
  21. HslProtocol.HeadByteLength = 8;
  22. }
  23. #endregion
  24. #region 事件通知块
  25. /// <summary>
  26. /// 接收字符串信息的事件
  27. /// </summary>
  28. public event Action<AppSession, NetHandle, string> ReceiveStringEvent;
  29. /// <summary>
  30. /// 接收字节信息的事件
  31. /// </summary>
  32. public event Action<AppSession, NetHandle, byte[]> ReceivedBytesEvent;
  33. private void OnReceiveStringEvent( AppSession session, int customer, string str )
  34. {
  35. ReceiveStringEvent?.Invoke( session, customer, str );
  36. }
  37. private void OnReceivedBytesEvent( AppSession session, int customer, byte[] temp )
  38. {
  39. ReceivedBytesEvent?.Invoke( session, customer, temp );
  40. }
  41. #endregion
  42. #region 启动停止块
  43. /// <summary>
  44. /// 关闭网络的操作
  45. /// </summary>
  46. protected override void CloseAction()
  47. {
  48. ReceivedBytesEvent = null;
  49. ReceiveStringEvent = null;
  50. base.CloseAction( );
  51. }
  52. // cxy
  53. public void SendMessage(AppSession session, string customer)
  54. {
  55. SendBytesAsync(session, Encoding.ASCII.GetBytes(customer));
  56. }
  57. /// <summary>
  58. /// 向指定的通信对象发送字符串数据
  59. /// </summary>
  60. /// <param name="session">通信对象</param>
  61. /// <param name="customer">用户的指令头</param>
  62. /// <param name="str">实际发送的字符串数据</param>
  63. public void SendMessage( AppSession session, int customer, string str )
  64. {
  65. SendBytesAsync( session, HslProtocol.CommandBytes( customer, Token, str ) );
  66. }
  67. /// <summary>
  68. /// 向指定的通信对象发送字节数据
  69. /// </summary>
  70. /// <param name="session">连接对象</param>
  71. /// <param name="customer">用户的指令头</param>
  72. /// <param name="bytes">实际的数据</param>
  73. public void SendMessage( AppSession session, int customer, byte[] bytes )
  74. {
  75. SendBytesAsync( session, HslProtocol.CommandBytes( customer, Token, bytes ) );
  76. }
  77. /// <summary>
  78. /// 处理请求接收连接后的方法
  79. /// </summary>
  80. /// <param name="obj"></param>
  81. protected override void ThreadPoolLogin( object obj )
  82. {
  83. if (obj is Socket)
  84. {
  85. Socket socket = obj as Socket;
  86. AppSession session = new AppSession( );
  87. session.WorkSocket = socket;
  88. try
  89. {
  90. session.IpEndPoint = (System.Net.IPEndPoint)socket.RemoteEndPoint;
  91. session.IpAddress = session.IpEndPoint.Address.ToString( );
  92. }
  93. catch(Exception ex)
  94. {
  95. LogNet?.WriteException( ToString( ), "Ip信息获取失败", ex );
  96. }
  97. LogNet?.WriteDebug( ToString( ), $"客户端 [ {session.IpEndPoint} ] 上线" );
  98. ReBeginReceiveHead( session, false );
  99. }
  100. }
  101. /// <summary>
  102. /// 处理异常的方法
  103. /// </summary>
  104. /// <param name="session"></param>
  105. /// <param name="ex">异常信息</param>
  106. internal override void SocketReceiveException( AppSession session, Exception ex )
  107. {
  108. session.WorkSocket?.Close( );
  109. LogNet?.WriteDebug( ToString( ), $"客户端 [ {session.IpEndPoint} ] 异常下线" );
  110. }
  111. /// <summary>
  112. /// 正常下线
  113. /// </summary>
  114. /// <param name="session"></param>
  115. internal override void AppSessionRemoteClose( AppSession session )
  116. {
  117. session.WorkSocket?.Close( );
  118. LogNet?.WriteDebug( ToString( ), $"客户端 [ {session.IpEndPoint} ] 下线" );
  119. }
  120. /// <summary>
  121. /// 数据处理中心
  122. /// </summary>
  123. /// <param name="session">当前的会话</param>
  124. /// <param name="protocol">协议指令头</param>
  125. /// <param name="customer">客户端信号</param>
  126. /// <param name="content">触发的消息内容</param>
  127. internal override void DataProcessingCenter( AppSession session, int protocol, int customer, byte[] content )
  128. {
  129. //接收数据完成,进行事件通知,优先进行解密操作
  130. if (protocol == HslProtocol.ProtocolCheckSecends)
  131. {
  132. // 初始化时候的测试消息
  133. session.HeartTime = DateTime.Now;
  134. SendMessage( session, customer, content );
  135. }
  136. else if (protocol == HslProtocol.ProtocolUserBytes)
  137. {
  138. // 字节数据
  139. OnReceivedBytesEvent( session, customer, content );
  140. }
  141. else if (protocol == HslProtocol.ProtocolUserString)
  142. {
  143. // 字符串数据
  144. string ccc = Encoding.Unicode.GetString(content);
  145. OnReceiveStringEvent( session, customer, ccc);
  146. }
  147. else
  148. {
  149. // 数据异常
  150. session?.WorkSocket?.Close( );
  151. }
  152. }
  153. #endregion
  154. #region Object Override
  155. /// <summary>
  156. /// 获取本对象的字符串表示形式
  157. /// </summary>
  158. /// <returns></returns>
  159. public override string ToString( )
  160. {
  161. return "NetSimplifyServer";
  162. }
  163. #endregion
  164. /// <summary>
  165. /// 检查当前的头子节信息的令牌是否是正确的
  166. /// </summary>
  167. /// <param name="headBytes">头子节数据</param>
  168. /// <returns>令牌是验证成功</returns>
  169. protected override bool CheckRemoteToken(byte[] headBytes)
  170. {
  171. try
  172. {
  173. string hh = Encoding.ASCII.GetString(headBytes, 0, 2);
  174. //if (!hh.StartsWith("00"))
  175. if (hh != "00")
  176. {
  177. return false;
  178. }
  179. hh = Encoding.ASCII.GetString(headBytes, 2, 2);
  180. int c = Convert.ToInt32(hh, 16);
  181. if (c == 0)
  182. {
  183. return false;
  184. }
  185. //return true;
  186. }
  187. catch
  188. {
  189. return false;
  190. }
  191. try
  192. {
  193. string hh = Encoding.ASCII.GetString(headBytes, 4, 2);
  194. //if (!hh.StartsWith("00"))
  195. if (hh != "00")
  196. {
  197. return false;
  198. }
  199. hh = Encoding.ASCII.GetString(headBytes, 6, 2);
  200. int cb = Convert.ToInt32(hh, 16);
  201. if (cb == 1 || cb == 11)
  202. {
  203. return true;
  204. }
  205. return false;
  206. }
  207. catch
  208. {
  209. return false;
  210. }
  211. }
  212. protected override bool CheckRemoteContent(byte[] content)
  213. {
  214. try
  215. {
  216. string ccc = Encoding.ASCII.GetString(content);
  217. if (ccc == "0" || ccc.StartsWith("#") || long.TryParse(ccc, out long r))
  218. {
  219. return true;
  220. }
  221. return false;
  222. }
  223. catch
  224. {
  225. return false;
  226. }
  227. }
  228. protected override int GetReceiveLength(AppSession session)
  229. {
  230. try
  231. {
  232. return Convert.ToInt32(Encoding.ASCII.GetString(session.BytesHead, 4, 4), 16);
  233. }
  234. catch
  235. {
  236. return 0;
  237. }
  238. }
  239. protected override int GetCustomer(byte[] head)
  240. {
  241. try
  242. {
  243. return Convert.ToInt32(Encoding.ASCII.GetString(head, 0, 4), 16);
  244. }
  245. catch
  246. {
  247. return 0;
  248. }
  249. }
  250. }
  251. }