TcpClientManager.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. var model = new TcpClientInfo()
  98. {
  99. Number = HostNumber,
  100. Host = host,
  101. Client = client,
  102. IsConnect = true,
  103. ConnectTime = DateTime.Now
  104. };
  105. TcpClientList.Add(model);
  106. return model.Number;
  107. }
  108. else
  109. {
  110. item.Client = client;
  111. }
  112. }
  113. catch { }
  114. return 0;
  115. }
  116. #endregion
  117. #region 更新项
  118. public bool UpdateConnectKey(string host, string s)
  119. {
  120. if (IsExistByHost(host))
  121. {
  122. for (var i = 0; i < TcpClientList.Count; i++)
  123. {
  124. if (TcpClientList[i].Host == host)
  125. {
  126. TcpClientList[i].ConnectKey = s;
  127. return true;
  128. }
  129. }
  130. }
  131. return false;
  132. }
  133. public bool UpdateUserEmail(string host, string s)
  134. {
  135. if (IsExistByHost(host))
  136. {
  137. for (var i = 0; i < TcpClientList.Count; i++)
  138. {
  139. if (TcpClientList[i].Host == host)
  140. {
  141. TcpClientList[i].UserEmail = s;
  142. return true;
  143. }
  144. }
  145. }
  146. return false;
  147. }
  148. public bool UpdateUserName(string host, string s)
  149. {
  150. if (IsExistByHost(host))
  151. {
  152. for (var i = 0; i < TcpClientList.Count; i++)
  153. {
  154. if (TcpClientList[i].Host == host)
  155. {
  156. TcpClientList[i].UserName = s;
  157. return true;
  158. }
  159. }
  160. }
  161. return false;
  162. }
  163. public bool UpdateUploadFlowCount(string host, long flow)
  164. {
  165. if (IsExistByHost(host))
  166. {
  167. for (var i = 0; i < TcpClientList.Count; i++)
  168. {
  169. if (TcpClientList[i].Host == host)
  170. {
  171. TcpClientList[i].UploadFlowCount += flow;
  172. TcpClientList[i].LastUploadTime = DateTime.Now;
  173. return true;
  174. }
  175. }
  176. }
  177. return false;
  178. }
  179. public bool UpdateDownloadFlowCount(string host, long flow)
  180. {
  181. if (IsExistByHost(host))
  182. {
  183. for (var i = 0; i < TcpClientList.Count; i++)
  184. {
  185. if (TcpClientList[i].Host == host)
  186. {
  187. TcpClientList[i].DownloadFlowCount += flow;
  188. TcpClientList[i].LastDownloadTime = DateTime.Now;
  189. return true;
  190. }
  191. }
  192. }
  193. return false;
  194. }
  195. #endregion
  196. #region 删除项
  197. public int RemoveByNumber(int number)
  198. {
  199. try
  200. {
  201. return TcpClientList.RemoveAll(x => x.Number == number);
  202. }
  203. catch { }
  204. return 0;
  205. }
  206. public int RemoveByHost(string host)
  207. {
  208. try
  209. {
  210. return TcpClientList.RemoveAll(x => x.Host == host);
  211. }
  212. catch { }
  213. return 0;
  214. }
  215. #endregion
  216. }
  217. }