SocketServerManager.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Collections.Concurrent;
  10. using System.Net;
  11. using HPSocket;
  12. using HPSocket.Tcp;
  13. using HPSocket.Ssl;
  14. using HPSocket.WebSocket;
  15. using HXX.Scanner.Common;
  16. namespace HXX.Scanner.Socket
  17. {
  18. /// <summary>
  19. /// 对socket的统一调度
  20. /// </summary>
  21. public class SocketServerManager
  22. {
  23. static SocketServerManager()
  24. {
  25. dic_client = new ConcurrentDictionary<IntPtr, ws_client>();
  26. }
  27. /// <summary>
  28. /// 外部调用注册 当连接到达
  29. /// </summary>
  30. private static on_connect_delegate on_connect_method;
  31. /// <summary>
  32. /// 外部调用注册 当信息到达(byte)
  33. /// </summary>
  34. private static on_message_delegate1 on_message_byte;
  35. /// <summary>
  36. /// 外部调用注册 当信息到达(string)
  37. /// </summary>
  38. private static on_message_delegate2 on_message_string;
  39. /// <summary>
  40. /// 外部调用注册 当连接关闭
  41. /// </summary>
  42. private static on_close_delegate on_close_method;
  43. /// <summary>
  44. /// 所有连接列表
  45. /// </summary>
  46. private static ConcurrentDictionary<IntPtr, ws_client> dic_client { get; set; }
  47. /// <summary>
  48. /// 启动一个socket服务器
  49. /// </summary>
  50. /// <param name="url">监听地址</param>
  51. /// <param name="method1">消息到达(byte)处理方法</param>
  52. /// <param name="method2">消息到达(string)处理方法</param>
  53. /// <param name="method3">连接关闭处理方法</param>
  54. /// <param name="method4">连接到达处理方法</param>
  55. /// <returns></returns>
  56. public static bool Start(string url, on_message_delegate1 method1, on_message_delegate2 method2, on_close_delegate method3, on_connect_delegate method4)
  57. {
  58. on_message_byte = method1;
  59. on_message_string = method2;
  60. on_close_method = method3;
  61. on_connect_method = method4;
  62. return SocketServer.Start(url);
  63. }
  64. /// <summary>
  65. /// 连接到达
  66. /// </summary>
  67. /// <param name="connId"></param>
  68. public static void on_connect(IntPtr connId)
  69. {
  70. if (on_connect_method != null)
  71. {
  72. on_connect_method(connId);
  73. }
  74. }
  75. /// <summary>
  76. /// 消息到达
  77. /// </summary>
  78. /// <param name="connId"></param>
  79. /// <param name="data"></param>
  80. public static void on_message(IntPtr connId, string data)
  81. {
  82. if (on_message_string != null)
  83. {
  84. on_message_string(connId, data);
  85. }
  86. }
  87. /// <summary>
  88. /// 消息到达
  89. /// </summary>
  90. /// <param name="connId"></param>
  91. /// <param name="data"></param>
  92. public static void on_message(IntPtr connId, byte[] data)
  93. {
  94. if (on_message_byte != null)
  95. {
  96. on_message_byte(connId, data);
  97. }
  98. }
  99. /// <summary>
  100. /// 发送byte给同一个token的所有连接
  101. /// </summary>
  102. /// <param name="connId"></param>
  103. /// <param name="data"></param>
  104. public static void Send_Family(IntPtr connId, byte[] data)
  105. {
  106. foreach (var f in get_same_token_client_list(connId))
  107. {
  108. try
  109. {
  110. SocketServer.Send(f.connId, data);
  111. }
  112. catch (Exception ee)
  113. {
  114. LogManager.WriteLog(ee);
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// 发送string给同一个token的所有连接
  120. /// </summary>
  121. /// <param name="connId"></param>
  122. /// <param name="data"></param>
  123. public static void Send_Family(IntPtr connId, string data)
  124. {
  125. foreach (var f in get_same_token_client_list(connId))
  126. {
  127. try
  128. {
  129. SocketServer.Send(f.connId, data);
  130. }
  131. catch (Exception ee)
  132. {
  133. LogManager.WriteLog(ee);
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// 发送给单个连接
  139. /// </summary>
  140. /// <param name="connId"></param>
  141. /// <param name="data"></param>
  142. public static void Send_Single(IntPtr connId, string data)
  143. {
  144. try
  145. {
  146. SocketServer.Send(connId, data);
  147. }
  148. catch (Exception ee)
  149. {
  150. LogManager.WriteLog(ee);
  151. }
  152. }
  153. /// <summary>
  154. /// 发送给所有连接
  155. /// </summary>
  156. /// <param name="data"></param>
  157. //public static void Send_All(string data)
  158. //{
  159. // try
  160. // {
  161. // if (SocketServer.server != null)
  162. // {
  163. // foreach (var connId in SocketServer.server.GetAllConnectionIds())
  164. // {
  165. // SocketServer.Send(connId, data);
  166. // }
  167. // }
  168. // }
  169. // catch (Exception ee)
  170. // {
  171. // LogManager.WriteLog(ee);
  172. // }
  173. //}
  174. /// <summary>
  175. /// 获取某连接的token所属的所有连接
  176. /// </summary>
  177. /// <param name="connId"></param>
  178. /// <returns></returns>
  179. private static List<ws_client> get_same_token_client_list(IntPtr connId)
  180. {
  181. List<ws_client> result = new List<ws_client>();
  182. var t = dic_client.FirstOrDefault(x => x.Key == connId);
  183. if (t.Key != null && t.Value != null)
  184. {
  185. var family = dic_client.Where(x => x.Value.token == t.Value.token).Select(x => x.Value);
  186. var current = SocketServer.server.GetAllConnectionIds();
  187. foreach (var f in family)
  188. {
  189. if (current.Any(x => x == f.connId))
  190. {
  191. result.Add(f);
  192. }
  193. else
  194. {
  195. dic_client.TryRemove(f.connId, out ws_client temp);
  196. }
  197. }
  198. if (t.Value != null && string.IsNullOrEmpty(t.Value.token))
  199. {
  200. result = result.Where(x => x.connId == connId).ToList();
  201. }
  202. }
  203. else
  204. {
  205. dic_client.TryRemove(connId, out ws_client temp);
  206. }
  207. return result;
  208. }
  209. /// <summary>
  210. /// 添加一个连接到连接列表
  211. /// </summary>
  212. /// <param name="_connId"></param>
  213. /// <param name="_token"></param>
  214. public static void add_client(IntPtr _connId, string _token)
  215. {
  216. dic_client[_connId] = new ws_client() { connId = _connId, token = _token };
  217. }
  218. /// <summary>
  219. /// 连接关闭
  220. /// </summary>
  221. /// <param name="connId"></param>
  222. /// <param name="connection_count"></param>
  223. public static void on_close(IntPtr connId, int connection_count)
  224. {
  225. if (on_close_method != null)
  226. {
  227. on_close_method(connId, connection_count);
  228. }
  229. }
  230. }
  231. }