TokenConnectionManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. namespace IFramework.Net
  6. {
  7. internal class TokenConnectionManager
  8. {
  9. LinkedList<NetConnectionToken> list = null;
  10. int period = 60;//s
  11. System.Threading.Timer timeoutThreading = null;
  12. LockParam lockParam = null;
  13. public int ConnectionTimeout { get; set; } = 60;//s
  14. public int Count { get { return list.Count; } }
  15. public TokenConnectionManager(int period)
  16. {
  17. if (period < 2) this.period = 2;
  18. else this.period = period;
  19. lockParam = new LockParam();
  20. int _period = GetPeriodSeconds();
  21. list = new LinkedList<NetConnectionToken>();
  22. timeoutThreading = new System.Threading.Timer(new TimerCallback(timeoutHandler), null, _period, _period);
  23. }
  24. private int GetPeriodSeconds()
  25. {
  26. return (period * 1000) >> 1;
  27. }
  28. public void TimerEnable(bool isContinue)
  29. {
  30. if (isContinue)
  31. {
  32. int _period = GetPeriodSeconds();
  33. timeoutThreading.Change(_period, _period);
  34. }
  35. else timeoutThreading.Change(-1, -1);
  36. }
  37. public void TimeoutChange(int period)
  38. {
  39. this.period = period;
  40. if (period < 2) this.period = 2;
  41. int _p = GetPeriodSeconds();
  42. timeoutThreading.Change(_p, _p);
  43. }
  44. public NetConnectionToken GetTopToken()
  45. {
  46. using(LockWait lwait=new LockWait(ref lockParam))
  47. {
  48. if (list.Count > 0)
  49. return list.First();
  50. return null;
  51. }
  52. }
  53. public IEnumerable<NetConnectionToken> ReadNext()
  54. {
  55. using(LockWait lwait=new LockWait(ref lockParam))
  56. {
  57. foreach (var l in list)
  58. {
  59. yield return l;
  60. }
  61. }
  62. }
  63. public void InsertToken(NetConnectionToken ncToken)
  64. {
  65. using (LockWait lwait = new LockWait(ref lockParam))
  66. {
  67. list.AddLast(ncToken);
  68. }
  69. }
  70. public bool RemoveToken(NetConnectionToken ncToken,bool isClose)
  71. {
  72. using (LockWait lwait = new LockWait(ref lockParam))
  73. {
  74. if (isClose) ncToken.Token.Close();
  75. return list.Remove(ncToken);
  76. }
  77. }
  78. public bool RemoveToken(SocketToken sToken)
  79. {
  80. using (LockWait lwait = new LockWait(ref lockParam))
  81. {
  82. var item = list.Where(x => x.Token.CompareTo(sToken) == 0).FirstOrDefault();
  83. if (item != null)
  84. {
  85. return list.Remove(item);
  86. }
  87. }
  88. return false;
  89. }
  90. public NetConnectionToken GetTokenById(int Id)
  91. {
  92. using (LockWait lwait = new LockWait(ref lockParam))
  93. {
  94. return list.Where(x => x.Token.TokenId == Id).FirstOrDefault();
  95. }
  96. }
  97. public NetConnectionToken GetTokenBySocketToken(SocketToken sToken)
  98. {
  99. using (LockWait lwait = new LockWait(ref lockParam))
  100. {
  101. return list.Where(x => x.Token.CompareTo(sToken) == 0).FirstOrDefault();
  102. }
  103. }
  104. public void Clear(bool isClose)
  105. {
  106. using (LockWait lwait = new LockWait(ref lockParam))
  107. {
  108. while (list.Count > 0)
  109. {
  110. var item = list.First();
  111. list.RemoveFirst();
  112. if (isClose)
  113. {
  114. if (item.Token != null)
  115. item.Token.Close();
  116. }
  117. }
  118. }
  119. }
  120. public bool RefreshConnectionToken(SocketToken sToken)
  121. {
  122. using (LockWait lwait = new LockWait(ref lockParam))
  123. {
  124. var rt = list.Find(new NetConnectionToken(sToken));
  125. if (rt == null) return false;
  126. rt.Value.ConnectionTime = DateTime.Now;
  127. return true;
  128. }
  129. }
  130. private void timeoutHandler(object obj)
  131. {
  132. using (LockWait lwait = new LockWait(ref lockParam))
  133. {
  134. var items= list.Where(x => x.Verification == false ||
  135. DateTime.Now.Subtract(x.ConnectionTime).TotalSeconds >= ConnectionTimeout)
  136. .ToArray();
  137. for (int i = 0; i < items.Length; ++i)
  138. {
  139. items[i].Token.Close();
  140. list.Remove(items[i]);
  141. }
  142. }
  143. }
  144. }
  145. public class NetConnectionToken:IComparable<NetConnectionToken>
  146. {
  147. public NetConnectionToken() { }
  148. public NetConnectionToken(SocketToken sToken)
  149. {
  150. this.Token = sToken;
  151. Verification = true;
  152. ConnectionTime = DateTime.Now;//兼容低版本语法
  153. }
  154. public SocketToken Token { get; set; }
  155. public DateTime ConnectionTime { get; set; }
  156. public bool Verification { get; set; }
  157. public int CompareTo(NetConnectionToken item)
  158. {
  159. return Token.CompareTo(item.Token);
  160. }
  161. public override bool Equals(object obj)
  162. {
  163. var nc = obj as NetConnectionToken;
  164. if (nc == null) return false;
  165. return this.CompareTo(nc) == 0;
  166. }
  167. public override int GetHashCode()
  168. {
  169. return Token.TokenId.GetHashCode()|Token.TokenSocket.GetHashCode();
  170. }
  171. }
  172. }