123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- using Vinno.IUS.Common.Log;
- using Vinno.IUS.Common.Network;
- using Vinno.IUS.Common.Network.Leaf;
- using Vinno.IUS.Common.Network.Tcp;
- using Vinno.IUS.Common.Network.Transfer;
- using Vinno.vCloud.Protocol.Messages.Client;
- using Vinno.vCloud.Protocol.Messages.Live;
- namespace Vinno.vCloud.Common.FIS.LiveVideos
- {
- internal class PushHeartRateKeeper
- {
- private readonly int _checkInterval = 4;
- private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
- private readonly string _terminalId;
- private readonly string _liveServiceUrl;
- private ClientLeaf _clientLeaf;
- private volatile bool _stopped;
- private ClientLeaf _connectedLeaf;
- private bool _isConnectionTimeout;
- /// <summary>
- /// The envent connection off line when a wrong eche result happened
- /// </summary>
- public event EventHandler Offlined;
- public PushHeartRateKeeper(string terminalId, string liveServiceUrl, ClientLeaf connectedLeaf)
- {
- _liveServiceUrl = liveServiceUrl;
- _connectedLeaf = connectedLeaf;
- var args = liveServiceUrl.Split(':');
- var host = args[0];
- var port = Convert.ToInt32(args[1]);
- var tcpCreator = new TcpCreator(host, port);
- var rtyCount = 0;
- Logger.WriteLineInfo("Start To Create Client Leaf for PusherHeartRateKeeper");
- _clientLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, tcpCreator, "Terminal.PushConnection:");
- Logger.WriteLineInfo("Create Client Leaf End for PusherHeartRateKeeper");
- while (!_clientLeaf.Online && rtyCount < 1)
- {
- _clientLeaf.Close();
- Logger.WriteLineError($"Push HeartRateKeeper create leaf offline! url:{_liveServiceUrl}, Retry Count {rtyCount}");
- rtyCount++;
- Thread.Sleep(10);
- Logger.WriteLineInfo("Start To Create Client Leaf for PusherHeartRateKeeper");
- _clientLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, tcpCreator, "Terminal.PushConnection:");
- Logger.WriteLineInfo("Create Client Leaf End for PusherHeartRateKeeper");
- }
- if (!_clientLeaf.Online)
- {
- Logger.WriteLineError($"Push HeartRateKeeper create leaf offline! url:{_liveServiceUrl}, Retry Count {rtyCount}");
- }
- else
- {
- Logger.WriteLineInfo($"Push HeartRateKeeper leaf online! url:{_liveServiceUrl}, Retry Count {rtyCount}");
- }
- _terminalId = terminalId;
- }
- /// <summary>
- /// Start the Heartrate to keep the session available.
- /// </summary>
- public void Start()
- {
- _stopped = false;
- Logger.WriteLineInfo($"Push HeartRateKeeper is started url:{_liveServiceUrl}");
- Task.Run(() =>
- {
- DoCheck();
- });
- }
- /// <summary>
- /// Stop the Heartrate
- /// </summary>
- public void Stop()
- {
- _stopped = true;
- _waitEvent.Set();
- Logger.WriteLineInfo($"Push HeartRateKeeper is stopped url:{_liveServiceUrl}");
- if (_clientLeaf != null)
- {
- _clientLeaf.Close();
- _clientLeaf = null;
- }
- }
- private void ExecuteDoCheck()
- {
- if (_stopped)
- {
- return;
- }
- var shouldStop = false;
- try
- {
- using (var message = new LiveHeartRateRequest())
- {
- message.RoomId = _terminalId;
- message.SubscriberId = _terminalId;
- var result = _clientLeaf?.Send(message, 5000);
- if (result == null)
- {
- shouldStop = true;
- Logger.WriteLineError($"Push HeartRateKeeper Offlined occurred since result null");
- }
- else
- {
- Logger.WriteLineInfo($"Push HeartRateKeeper url:{_liveServiceUrl}");
- var messageResult = ResultMessage.Convert(result);
- if (messageResult != CCR.OK)
- {
- shouldStop = true;
- }
- else
- {
- shouldStop = false; // send live rate successed
- }
- }
- }
- }
- catch (ConnectionTimeoutException ex)
- {
- if (_isConnectionTimeout)
- {
- Logger.WriteLineError($"Push HeartRateKeeper error,Exception Timeout Connection 2 ,retry timeout , to stop keeper, url:{_liveServiceUrl},ex:{ex}");
- _stopped = true;//stop keeper
- }
- else
- {
- //first conncetion timeout ,retry to send LiveHeartRateRequest
- shouldStop = true;
- _isConnectionTimeout = true;
- Logger.WriteLineError($"Push HeartRateKeeper error,Exception Timeout Connection 1,retry to send connection url:{_liveServiceUrl},ex:{ex}");
- DoCheck();//retry to send LiveHeartRateRequest
- return;
- }
- }
- catch (Exception ex)
- {
- shouldStop = true;
- Logger.WriteLineError($"Push HeartRateKeeper error,ConnectedLeaf Online:{_connectedLeaf.Online}, url:{_liveServiceUrl}, {ex}");
- }
- _isConnectionTimeout = false;
- if (!_stopped)
- {
- if (shouldStop) // send live heart rate failed
- {
- if (!_connectedLeaf.Online)
- {
- if (_clientLeaf != null && _clientLeaf.Online)
- {
- _clientLeaf.Close();
- }
- OnOfflined();
- }
- }
- _waitEvent.WaitOne(TimeSpan.FromSeconds(_checkInterval));
- if (!_stopped)
- {
- DoCheck();
- }
- }
- }
- /// <summary>
- /// Do HartRate
- /// </summary>
- /// <returns></returns>
- private void DoCheck()
- {
- Task.Run(new Action(ExecuteDoCheck));
- }
- private void OnOfflined()
- {
- Offlined?.Invoke(this, EventArgs.Empty);
- }
- }
- }
|