NetSimplifyServer.cs 8.6 KB

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