TcpClientManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using Azylee.Core.DataUtils.CollectionUtils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. namespace Azylee.YeahWeb.SocketUtils.TcpUtils
  8. {
  9. /// <summary>
  10. /// 客户端信息管理器
  11. /// </summary>
  12. public class TcpClientManager
  13. {
  14. private int HostNumber { get; set; }
  15. private List<TcpClientInfo> TcpClientList { get; set; }
  16. /// <summary>
  17. /// 构造方法(初始化标号起始标记6位,初始化客户端列表)
  18. /// </summary>
  19. public TcpClientManager()
  20. {
  21. HostNumber = 100000;
  22. TcpClientList = new List<TcpClientInfo>();
  23. }
  24. #region 统计项
  25. /// <summary>
  26. /// 当前连接客户端总数
  27. /// </summary>
  28. /// <returns></returns>
  29. public int CountClient()
  30. {
  31. return TcpClientList.Count();
  32. }
  33. #endregion
  34. #region 判定项
  35. public bool IsExistByNumber(int number)
  36. {
  37. if (Ls.Ok(TcpClientList))
  38. {
  39. return TcpClientList.Any(x => x.Number == number);
  40. }
  41. return false;
  42. }
  43. public bool IsExistByHost(string host)
  44. {
  45. if (Ls.Ok(TcpClientList))
  46. {
  47. return TcpClientList.Any(x => x.Host == host);
  48. }
  49. return false;
  50. }
  51. public bool IsConnectKey(string host, string key)
  52. {
  53. var item = GetInfoByHost(host);
  54. if (item != null) return item.ConnectKey == key;
  55. return false;
  56. }
  57. #endregion
  58. #region 查询项
  59. public List<TcpClientInfo> GetAll()
  60. {
  61. return TcpClientList;
  62. }
  63. public TcpClientInfo GetInfoByNumber(int number)
  64. {
  65. if (IsExistByNumber(number))
  66. {
  67. return TcpClientList.FirstOrDefault(x => x.Number == number);
  68. }
  69. return null;
  70. }
  71. public TcpClientInfo GetInfoByHost(string host)
  72. {
  73. TcpClientInfo client = null;
  74. try
  75. {
  76. if (IsExistByHost(host))
  77. {
  78. client = TcpClientList.FirstOrDefault(x => x.Host == host);
  79. }
  80. }
  81. catch { }
  82. return client;
  83. }
  84. #endregion
  85. #region 添加项
  86. /// <summary>
  87. /// 添加或更新到客户端列表
  88. /// </summary>
  89. public int AddOrUpdate(string host, TcpClient client)
  90. {
  91. try
  92. {
  93. HostNumber++;
  94. var item = TcpClientList.FirstOrDefault(x => x.Host == host);
  95. if (item == null)
  96. {
  97. string ip = "";
  98. int ipFlagIndex = host.IndexOf(":");
  99. if (ipFlagIndex > 0) ip = host.Substring(0, ipFlagIndex);
  100. var model = new TcpClientInfo()
  101. {
  102. Number = HostNumber,
  103. IP = ip,
  104. Host = host,
  105. Client = client,
  106. IsConnect = true,
  107. ConnectTime = DateTime.Now
  108. };
  109. TcpClientList.Add(model);
  110. return model.Number;
  111. }
  112. else
  113. {
  114. item.Client = client;
  115. }
  116. }
  117. catch { }
  118. return 0;
  119. }
  120. #endregion
  121. #region 更新项
  122. /// <summary>
  123. /// 更新 ConnectKey 连接秘钥
  124. /// </summary>
  125. /// <param name="host"></param>
  126. /// <param name="s"></param>
  127. /// <returns></returns>
  128. public bool UpdateConnectKey(string host, string s)
  129. {
  130. if (IsExistByHost(host))
  131. {
  132. for (var i = 0; i < TcpClientList.Count; i++)
  133. {
  134. if (TcpClientList[i].Host == host)
  135. {
  136. TcpClientList[i].ConnectKey = s;
  137. return true;
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. /// <summary>
  144. /// 更新 UserEmail 用户邮箱
  145. /// </summary>
  146. /// <param name="host"></param>
  147. /// <param name="s"></param>
  148. /// <returns></returns>
  149. public bool UpdateUserEmail(string host, string s)
  150. {
  151. if (IsExistByHost(host))
  152. {
  153. for (var i = 0; i < TcpClientList.Count; i++)
  154. {
  155. if (TcpClientList[i].Host == host)
  156. {
  157. TcpClientList[i].UserEmail = s;
  158. return true;
  159. }
  160. }
  161. }
  162. return false;
  163. }
  164. /// <summary>
  165. /// 更新 AccessCode 权限编码
  166. /// </summary>
  167. /// <param name="host"></param>
  168. /// <param name="s"></param>
  169. /// <returns></returns>
  170. public bool UpdateAccessCode(string host, string s)
  171. {
  172. if (IsExistByHost(host))
  173. {
  174. for (var i = 0; i < TcpClientList.Count; i++)
  175. {
  176. if (TcpClientList[i].Host == host)
  177. {
  178. TcpClientList[i].AccessCode = s;
  179. return true;
  180. }
  181. }
  182. }
  183. return false;
  184. }
  185. /// <summary>
  186. /// 更新 MachineID 主机ID
  187. /// </summary>
  188. /// <param name="host"></param>
  189. /// <param name="s"></param>
  190. /// <returns></returns>
  191. public bool UpdateMachineID(string host, string s)
  192. {
  193. if (IsExistByHost(host))
  194. {
  195. for (var i = 0; i < TcpClientList.Count; i++)
  196. {
  197. if (TcpClientList[i].Host == host)
  198. {
  199. TcpClientList[i].MachineID = s;
  200. return true;
  201. }
  202. }
  203. }
  204. return false;
  205. }
  206. /// <summary>
  207. /// 更新 MachineName 主机名称
  208. /// </summary>
  209. /// <param name="host"></param>
  210. /// <param name="s"></param>
  211. /// <returns></returns>
  212. public bool UpdateMachineName(string host, string s)
  213. {
  214. if (IsExistByHost(host))
  215. {
  216. for (var i = 0; i < TcpClientList.Count; i++)
  217. {
  218. if (TcpClientList[i].Host == host)
  219. {
  220. TcpClientList[i].MachineName = s;
  221. return true;
  222. }
  223. }
  224. }
  225. return false;
  226. }
  227. /// <summary>
  228. /// 更新 UserName 用户名
  229. /// </summary>
  230. /// <param name="host"></param>
  231. /// <param name="s"></param>
  232. /// <returns></returns>
  233. public bool UpdateUserName(string host, string s)
  234. {
  235. if (IsExistByHost(host))
  236. {
  237. for (var i = 0; i < TcpClientList.Count; i++)
  238. {
  239. if (TcpClientList[i].Host == host)
  240. {
  241. TcpClientList[i].UserName = s;
  242. return true;
  243. }
  244. }
  245. }
  246. return false;
  247. }
  248. /// <summary>
  249. /// 更新 AppCode 应用程序编码
  250. /// </summary>
  251. /// <param name="host"></param>
  252. /// <param name="s"></param>
  253. /// <returns></returns>
  254. public bool UpdateAppCode(string host, string s)
  255. {
  256. if (IsExistByHost(host))
  257. {
  258. for (var i = 0; i < TcpClientList.Count; i++)
  259. {
  260. if (TcpClientList[i].Host == host)
  261. {
  262. TcpClientList[i].AppCode = s;
  263. return true;
  264. }
  265. }
  266. }
  267. return false;
  268. }
  269. /// <summary>
  270. /// 更新上行流量
  271. /// </summary>
  272. /// <param name="host"></param>
  273. /// <param name="flow"></param>
  274. /// <returns></returns>
  275. public bool UpdateUploadFlowCount(string host, long flow)
  276. {
  277. if (IsExistByHost(host))
  278. {
  279. for (var i = 0; i < TcpClientList.Count; i++)
  280. {
  281. if (TcpClientList[i].Host == host)
  282. {
  283. TcpClientList[i].UploadFlowCount += flow;
  284. TcpClientList[i].LastUploadTime = DateTime.Now;
  285. return true;
  286. }
  287. }
  288. }
  289. return false;
  290. }
  291. /// <summary>
  292. /// 更新下行流量
  293. /// </summary>
  294. /// <param name="host"></param>
  295. /// <param name="flow"></param>
  296. /// <returns></returns>
  297. public bool UpdateDownloadFlowCount(string host, long flow)
  298. {
  299. if (IsExistByHost(host))
  300. {
  301. for (var i = 0; i < TcpClientList.Count; i++)
  302. {
  303. if (TcpClientList[i].Host == host)
  304. {
  305. TcpClientList[i].DownloadFlowCount += flow;
  306. TcpClientList[i].LastDownloadTime = DateTime.Now;
  307. return true;
  308. }
  309. }
  310. }
  311. return false;
  312. }
  313. /// <summary>
  314. /// 更新心跳时间
  315. /// </summary>
  316. /// <param name="host"></param>
  317. /// <returns></returns>
  318. public bool UpdateHeartbeatTime(string host)
  319. {
  320. if (IsExistByHost(host))
  321. {
  322. for (var i = 0; i < TcpClientList.Count; i++)
  323. {
  324. if (TcpClientList[i].Host == host)
  325. {
  326. TcpClientList[i].HeartbeatTime = DateTime.Now;
  327. return true;
  328. }
  329. }
  330. }
  331. return false;
  332. }
  333. #endregion
  334. #region 删除项
  335. public int RemoveByNumber(int number)
  336. {
  337. try
  338. {
  339. return TcpClientList.RemoveAll(x => x.Number == number);
  340. }
  341. catch { }
  342. return 0;
  343. }
  344. public int RemoveByHost(string host)
  345. {
  346. try
  347. {
  348. return TcpClientList.RemoveAll(x => x.Host == host);
  349. }
  350. catch { }
  351. return 0;
  352. }
  353. #endregion
  354. }
  355. }