PushHeartRateKeeper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Vinno.IUS.Common.Log;
  5. using Vinno.IUS.Common.Network;
  6. using Vinno.IUS.Common.Network.Leaf;
  7. using Vinno.IUS.Common.Network.Tcp;
  8. using Vinno.IUS.Common.Network.Transfer;
  9. using Vinno.vCloud.Protocol.Messages.Client;
  10. using Vinno.vCloud.Protocol.Messages.Live;
  11. namespace Vinno.vCloud.Common.FIS.LiveVideos
  12. {
  13. internal class PushHeartRateKeeper
  14. {
  15. private readonly int _checkInterval = 4;
  16. private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
  17. private readonly string _terminalId;
  18. private readonly string _liveServiceUrl;
  19. private ClientLeaf _clientLeaf;
  20. private volatile bool _stopped;
  21. private ClientLeaf _connectedLeaf;
  22. private bool _isConnectionTimeout;
  23. /// <summary>
  24. /// The envent connection off line when a wrong eche result happened
  25. /// </summary>
  26. public event EventHandler Offlined;
  27. public PushHeartRateKeeper(string terminalId, string liveServiceUrl, ClientLeaf connectedLeaf)
  28. {
  29. _liveServiceUrl = liveServiceUrl;
  30. _connectedLeaf = connectedLeaf;
  31. var args = liveServiceUrl.Split(':');
  32. var host = args[0];
  33. var port = Convert.ToInt32(args[1]);
  34. var tcpCreator = new TcpCreator(host, port);
  35. var rtyCount = 0;
  36. Logger.WriteLineInfo("Start To Create Client Leaf for PusherHeartRateKeeper");
  37. _clientLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, tcpCreator, "Terminal.PushConnection:");
  38. Logger.WriteLineInfo("Create Client Leaf End for PusherHeartRateKeeper");
  39. while (!_clientLeaf.Online && rtyCount < 1)
  40. {
  41. _clientLeaf.Close();
  42. Logger.WriteLineError($"Push HeartRateKeeper create leaf offline! url:{_liveServiceUrl}, Retry Count {rtyCount}");
  43. rtyCount++;
  44. Thread.Sleep(10);
  45. Logger.WriteLineInfo("Start To Create Client Leaf for PusherHeartRateKeeper");
  46. _clientLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, tcpCreator, "Terminal.PushConnection:");
  47. Logger.WriteLineInfo("Create Client Leaf End for PusherHeartRateKeeper");
  48. }
  49. if (!_clientLeaf.Online)
  50. {
  51. Logger.WriteLineError($"Push HeartRateKeeper create leaf offline! url:{_liveServiceUrl}, Retry Count {rtyCount}");
  52. }
  53. else
  54. {
  55. Logger.WriteLineInfo($"Push HeartRateKeeper leaf online! url:{_liveServiceUrl}, Retry Count {rtyCount}");
  56. }
  57. _terminalId = terminalId;
  58. }
  59. /// <summary>
  60. /// Start the Heartrate to keep the session available.
  61. /// </summary>
  62. public void Start()
  63. {
  64. _stopped = false;
  65. Logger.WriteLineInfo($"Push HeartRateKeeper is started url:{_liveServiceUrl}");
  66. Task.Run(() =>
  67. {
  68. DoCheck();
  69. });
  70. }
  71. /// <summary>
  72. /// Stop the Heartrate
  73. /// </summary>
  74. public void Stop()
  75. {
  76. _stopped = true;
  77. _waitEvent.Set();
  78. Logger.WriteLineInfo($"Push HeartRateKeeper is stopped url:{_liveServiceUrl}");
  79. if (_clientLeaf != null)
  80. {
  81. _clientLeaf.Close();
  82. _clientLeaf = null;
  83. }
  84. }
  85. private void ExecuteDoCheck()
  86. {
  87. if (_stopped)
  88. {
  89. return;
  90. }
  91. var shouldStop = false;
  92. try
  93. {
  94. using (var message = new LiveHeartRateRequest())
  95. {
  96. message.RoomId = _terminalId;
  97. message.SubscriberId = _terminalId;
  98. var result = _clientLeaf?.Send(message, 5000);
  99. if (result == null)
  100. {
  101. shouldStop = true;
  102. Logger.WriteLineError($"Push HeartRateKeeper Offlined occurred since result null");
  103. }
  104. else
  105. {
  106. Logger.WriteLineInfo($"Push HeartRateKeeper url:{_liveServiceUrl}");
  107. var messageResult = ResultMessage.Convert(result);
  108. if (messageResult != CCR.OK)
  109. {
  110. shouldStop = true;
  111. }
  112. else
  113. {
  114. shouldStop = false; // send live rate successed
  115. }
  116. }
  117. }
  118. }
  119. catch (ConnectionTimeoutException ex)
  120. {
  121. if (_isConnectionTimeout)
  122. {
  123. Logger.WriteLineError($"Push HeartRateKeeper error,Exception Timeout Connection 2 ,retry timeout , to stop keeper, url:{_liveServiceUrl},ex:{ex}");
  124. _stopped = true;//stop keeper
  125. }
  126. else
  127. {
  128. //first conncetion timeout ,retry to send LiveHeartRateRequest
  129. shouldStop = true;
  130. _isConnectionTimeout = true;
  131. Logger.WriteLineError($"Push HeartRateKeeper error,Exception Timeout Connection 1,retry to send connection url:{_liveServiceUrl},ex:{ex}");
  132. DoCheck();//retry to send LiveHeartRateRequest
  133. return;
  134. }
  135. }
  136. catch (Exception ex)
  137. {
  138. shouldStop = true;
  139. Logger.WriteLineError($"Push HeartRateKeeper error,ConnectedLeaf Online:{_connectedLeaf.Online}, url:{_liveServiceUrl}, {ex}");
  140. }
  141. _isConnectionTimeout = false;
  142. if (!_stopped)
  143. {
  144. if (shouldStop) // send live heart rate failed
  145. {
  146. if (!_connectedLeaf.Online)
  147. {
  148. if (_clientLeaf != null && _clientLeaf.Online)
  149. {
  150. _clientLeaf.Close();
  151. }
  152. OnOfflined();
  153. }
  154. }
  155. _waitEvent.WaitOne(TimeSpan.FromSeconds(_checkInterval));
  156. if (!_stopped)
  157. {
  158. DoCheck();
  159. }
  160. }
  161. }
  162. /// <summary>
  163. /// Do HartRate
  164. /// </summary>
  165. /// <returns></returns>
  166. private void DoCheck()
  167. {
  168. Task.Run(new Action(ExecuteDoCheck));
  169. }
  170. private void OnOfflined()
  171. {
  172. Offlined?.Invoke(this, EventArgs.Empty);
  173. }
  174. }
  175. }