TcpClientManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. public class TcpClientManager
  10. {
  11. private int HostNumber { get; set; }
  12. private List<TcpClientInfo> TcpClientList { get; set; }
  13. /// <summary>
  14. /// 构造方法(初始化标号起始标记6位,初始化客户端列表)
  15. /// </summary>
  16. public TcpClientManager()
  17. {
  18. HostNumber = 100000;
  19. TcpClientList = new List<TcpClientInfo>();
  20. }
  21. #region 统计项
  22. /// <summary>
  23. /// 当前连接客户端总数
  24. /// </summary>
  25. /// <returns></returns>
  26. public int CountClient()
  27. {
  28. return TcpClientList.Count();
  29. }
  30. #endregion
  31. #region 判定项
  32. public bool IsExistByNumber(int number)
  33. {
  34. if (Ls.Ok(TcpClientList))
  35. {
  36. return TcpClientList.Any(x => x.Number == number);
  37. }
  38. return false;
  39. }
  40. public bool IsExistByHost(string host)
  41. {
  42. if (Ls.Ok(TcpClientList))
  43. {
  44. return TcpClientList.Any(x => x.Host == host);
  45. }
  46. return false;
  47. }
  48. public bool IsConnectKey(string host, string key)
  49. {
  50. var item = GetInfoByHost(host);
  51. if (item != null) return item.ConnectKey == key;
  52. return false;
  53. }
  54. #endregion
  55. #region 查询项
  56. public List<TcpClientInfo> GetAll()
  57. {
  58. return TcpClientList;
  59. }
  60. public TcpClientInfo GetInfoByNumber(int number)
  61. {
  62. if (IsExistByNumber(number))
  63. {
  64. return TcpClientList.FirstOrDefault(x => x.Number == number);
  65. }
  66. return null;
  67. }
  68. public TcpClientInfo GetInfoByHost(string host)
  69. {
  70. TcpClientInfo client = null;
  71. try
  72. {
  73. if (IsExistByHost(host))
  74. {
  75. client = TcpClientList.FirstOrDefault(x => x.Host == host);
  76. }
  77. }
  78. catch { }
  79. return client;
  80. }
  81. #endregion
  82. #region 添加项
  83. /// <summary>
  84. /// 添加或更新到客户端列表
  85. /// </summary>
  86. public int AddOrUpdate(string host, TcpClient client)
  87. {
  88. try
  89. {
  90. HostNumber++;
  91. var item = TcpClientList.FirstOrDefault(x => x.Host == host);
  92. if (item == null)
  93. {
  94. var model = new TcpClientInfo()
  95. {
  96. Number = HostNumber,
  97. Host = host,
  98. Client = client,
  99. IsConnect = true,
  100. ConnectTime = DateTime.Now
  101. };
  102. TcpClientList.Add(model);
  103. return model.Number;
  104. }
  105. else
  106. {
  107. item.Client = client;
  108. }
  109. }
  110. catch { }
  111. return 0;
  112. }
  113. #endregion
  114. #region 更新项
  115. public bool UpdateConnectKey(string host, string s)
  116. {
  117. if (IsExistByHost(host))
  118. {
  119. for (var i = 0; i < TcpClientList.Count; i++)
  120. {
  121. if (TcpClientList[i].Host == host)
  122. {
  123. TcpClientList[i].ConnectKey = s;
  124. return true;
  125. }
  126. }
  127. }
  128. return false;
  129. }
  130. public bool UpdateUploadFlowCount(string host, long flow)
  131. {
  132. if (IsExistByHost(host))
  133. {
  134. for (var i = 0; i < TcpClientList.Count; i++)
  135. {
  136. if (TcpClientList[i].Host == host)
  137. {
  138. TcpClientList[i].UploadFlowCount += flow;
  139. TcpClientList[i].LastUploadTime = DateTime.Now;
  140. return true;
  141. }
  142. }
  143. }
  144. return false;
  145. }
  146. public bool UpdateDownloadFlowCount(string host, long flow)
  147. {
  148. if (IsExistByHost(host))
  149. {
  150. for (var i = 0; i < TcpClientList.Count; i++)
  151. {
  152. if (TcpClientList[i].Host == host)
  153. {
  154. TcpClientList[i].DownloadFlowCount += flow;
  155. TcpClientList[i].LastDownloadTime = DateTime.Now;
  156. return true;
  157. }
  158. }
  159. }
  160. return false;
  161. }
  162. #endregion
  163. #region 删除项
  164. public int RemoveByNumber(int number)
  165. {
  166. try
  167. {
  168. return TcpClientList.RemoveAll(x => x.Number == number);
  169. }
  170. catch { }
  171. return 0;
  172. }
  173. public int RemoveByHost(string host)
  174. {
  175. try
  176. {
  177. return TcpClientList.RemoveAll(x => x.Host == host);
  178. }
  179. catch { }
  180. return 0;
  181. }
  182. #endregion
  183. }
  184. }