ChatConnectionKeeper.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using System.Threading;
  6. using Vinno.IUS.Common.Network.Leaf;
  7. using Vinno.IUS.Common.Network.Tcp;
  8. using Vinno.IUS.Common.Network.Transfer;
  9. using Vinno.IUS.Common.Network;
  10. using Vinno.vCloud.Protocol.Messages.Client;
  11. using Vinno.vCloud.Protocol.Messages.Live;
  12. using Vinno.IUS.Common.Log;
  13. namespace Vinno.vCloud.Client.Proxy.Interfaces
  14. {
  15. public class ConsultationConnectionKeeper
  16. {
  17. private ClientLeaf _clientLeaf;
  18. private volatile bool _stopped;
  19. private int _chechInterminal = 3;
  20. private int _heartBeatInterval = 2;
  21. private bool _heartBeatAlive;
  22. public event EventHandler ClientLeafClosed;
  23. private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
  24. private readonly string _accountId;
  25. private readonly string _roomId;
  26. /// <summary>
  27. /// The envent connection off line when a wrong eche result happened
  28. /// </summary>
  29. public event EventHandler Offlined;
  30. public ConsultationConnectionKeeper(string roomId, string accountId,string url)
  31. {
  32. if (string.IsNullOrEmpty(url))
  33. {
  34. throw new ArgumentNullException("Url");
  35. }
  36. var args = url.Split(':');
  37. var host = args[0];
  38. var port = Convert.ToInt32(args[1]);
  39. var tcpCreator = new TcpCreator(host, port);
  40. _clientLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, tcpCreator);
  41. if (!_clientLeaf.Online)
  42. {
  43. _clientLeaf.Close();
  44. _clientLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, tcpCreator);
  45. // If client leaf is off line, try once more.
  46. if (!_clientLeaf.Online)
  47. {
  48. throw new Exception("RtcClient not connect");
  49. }
  50. }
  51. _roomId = roomId;
  52. _accountId = accountId;
  53. }
  54. /// <summary>
  55. /// Start the Heartrate to keep the session available.
  56. /// </summary>
  57. public async void Start(bool isDelay = true)
  58. {
  59. _stopped = false;
  60. Logger.WriteLineInfo($"Chat connection keeper is started for room: {_roomId}, isDelay{isDelay}");
  61. if (isDelay)
  62. {
  63. await Task.Delay(5000);
  64. }
  65. DoCheck();
  66. }
  67. /// <summary>
  68. /// Stop the Heartrate
  69. /// </summary>
  70. public void Stop()
  71. {
  72. _stopped = true;
  73. _waitEvent.Set();
  74. Logger.WriteLineInfo($"Chat connection keeper is stopped for room: {_roomId}");
  75. if (_clientLeaf != null)
  76. {
  77. _clientLeaf.Close();
  78. _clientLeaf = null;
  79. }
  80. }
  81. public bool HeartBeatAlive => _heartBeatAlive;
  82. private void ExecuteDoCheck()
  83. {
  84. try
  85. {
  86. using (var message = new LiveHeartRateRequest())
  87. {
  88. message.RoomId = _roomId;
  89. message.SubscriberId = _accountId;
  90. var result = _clientLeaf.Send(message);
  91. var shouldStop = false;
  92. if (result == null)
  93. {
  94. Logger.WriteLineInfo($"OnOfflined called since result null");
  95. shouldStop = true;
  96. }
  97. else
  98. {
  99. var messageResult = ResultMessage.Convert(result);
  100. if (messageResult == CCR.OK)
  101. {
  102. _heartBeatInterval = 2;
  103. _heartBeatAlive = true;
  104. }
  105. else
  106. {
  107. Logger.WriteLineInfo($"Offline occurred sinc none ok result.roomid: {_roomId}, subscriberid:{_accountId}, " +
  108. $"message result: {result?.ToString()}, {result?.GetType()}");
  109. shouldStop = true;
  110. }
  111. }
  112. if (shouldStop)
  113. {
  114. if (_clientLeaf.Online)
  115. {
  116. _clientLeaf.Close();
  117. }
  118. if (!_stopped)
  119. {
  120. Logger.WriteLineInfo($"OnOfflined called since shouldStop");
  121. //Do offline when server has no current chat room
  122. OnOfflined();
  123. }
  124. }
  125. }
  126. }
  127. catch (Exception ex)
  128. {
  129. Logger.WriteLineError($"ChatConnectionKeeper ExecuteDoCheck error:{ex}");
  130. _heartBeatAlive = false;
  131. if (!_stopped && _clientLeaf != null)
  132. {
  133. if (_clientLeaf.IsClosed || !_clientLeaf.Online)
  134. {
  135. if (_heartBeatInterval == 2)
  136. {
  137. ClientLeafClosed.Invoke(this, null);
  138. return;
  139. }
  140. }
  141. else
  142. {
  143. if (_heartBeatInterval <= 0)
  144. {
  145. OnOfflined();
  146. return;
  147. }
  148. }
  149. _heartBeatInterval--;
  150. }
  151. }
  152. if (!_stopped)
  153. {
  154. _waitEvent.WaitOne(TimeSpan.FromSeconds(_chechInterminal));
  155. if (!_stopped)
  156. {
  157. if (_chechInterminal == 0)
  158. {
  159. _chechInterminal = 3;
  160. }
  161. DoCheck();
  162. }
  163. }
  164. }
  165. /// <summary>
  166. /// Do HartRate
  167. /// </summary>
  168. /// <returns></returns>
  169. private void DoCheck()
  170. {
  171. Task.Run(new Action(ExecuteDoCheck));
  172. }
  173. private void OnOfflined()
  174. {
  175. Logger.WriteLineInfo($"Connection keeper is offline");
  176. Offlined?.Invoke(this, EventArgs.Empty);
  177. }
  178. }
  179. }