123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using Vinno.IUS.Common.Network.Leaf;
- using Vinno.IUS.Common.Network.Tcp;
- using Vinno.IUS.Common.Network.Transfer;
- using Vinno.IUS.Common.Network;
- using Vinno.vCloud.Protocol.Messages.Client;
- using Vinno.vCloud.Protocol.Messages.Live;
- using Vinno.IUS.Common.Log;
- namespace Vinno.vCloud.Client.Proxy.Interfaces
- {
- public class ConsultationConnectionKeeper
- {
- private ClientLeaf _clientLeaf;
- private volatile bool _stopped;
- private int _chechInterminal = 3;
- private int _heartBeatInterval = 2;
- private bool _heartBeatAlive;
- public event EventHandler ClientLeafClosed;
- private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
- private readonly string _accountId;
- private readonly string _roomId;
- /// <summary>
- /// The envent connection off line when a wrong eche result happened
- /// </summary>
- public event EventHandler Offlined;
- public ConsultationConnectionKeeper(string roomId, string accountId,string url)
- {
- if (string.IsNullOrEmpty(url))
- {
- throw new ArgumentNullException("Url");
- }
- var args = url.Split(':');
- var host = args[0];
- var port = Convert.ToInt32(args[1]);
- var tcpCreator = new TcpCreator(host, port);
- _clientLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, tcpCreator);
- if (!_clientLeaf.Online)
- {
- _clientLeaf.Close();
- _clientLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, tcpCreator);
- // If client leaf is off line, try once more.
- if (!_clientLeaf.Online)
- {
- throw new Exception("RtcClient not connect");
- }
- }
- _roomId = roomId;
- _accountId = accountId;
- }
- /// <summary>
- /// Start the Heartrate to keep the session available.
- /// </summary>
- public async void Start(bool isDelay = true)
- {
- _stopped = false;
- Logger.WriteLineInfo($"Chat connection keeper is started for room: {_roomId}, isDelay{isDelay}");
- if (isDelay)
- {
- await Task.Delay(5000);
- }
- DoCheck();
- }
- /// <summary>
- /// Stop the Heartrate
- /// </summary>
- public void Stop()
- {
- _stopped = true;
- _waitEvent.Set();
- Logger.WriteLineInfo($"Chat connection keeper is stopped for room: {_roomId}");
- if (_clientLeaf != null)
- {
- _clientLeaf.Close();
- _clientLeaf = null;
- }
- }
- public bool HeartBeatAlive => _heartBeatAlive;
- private void ExecuteDoCheck()
- {
- try
- {
- using (var message = new LiveHeartRateRequest())
- {
- message.RoomId = _roomId;
- message.SubscriberId = _accountId;
- var result = _clientLeaf.Send(message);
- var shouldStop = false;
- if (result == null)
- {
- Logger.WriteLineInfo($"OnOfflined called since result null");
- shouldStop = true;
- }
- else
- {
- var messageResult = ResultMessage.Convert(result);
- if (messageResult == CCR.OK)
- {
- _heartBeatInterval = 2;
- _heartBeatAlive = true;
- }
- else
- {
- Logger.WriteLineInfo($"Offline occurred sinc none ok result.roomid: {_roomId}, subscriberid:{_accountId}, " +
- $"message result: {result?.ToString()}, {result?.GetType()}");
- shouldStop = true;
- }
- }
- if (shouldStop)
- {
- if (_clientLeaf.Online)
- {
- _clientLeaf.Close();
- }
- if (!_stopped)
- {
- Logger.WriteLineInfo($"OnOfflined called since shouldStop");
- //Do offline when server has no current chat room
- OnOfflined();
- }
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"ChatConnectionKeeper ExecuteDoCheck error:{ex}");
- _heartBeatAlive = false;
- if (!_stopped && _clientLeaf != null)
- {
- if (_clientLeaf.IsClosed || !_clientLeaf.Online)
- {
- if (_heartBeatInterval == 2)
- {
- ClientLeafClosed.Invoke(this, null);
- return;
- }
- }
- else
- {
- if (_heartBeatInterval <= 0)
- {
- OnOfflined();
- return;
- }
- }
- _heartBeatInterval--;
- }
- }
- if (!_stopped)
- {
- _waitEvent.WaitOne(TimeSpan.FromSeconds(_chechInterminal));
- if (!_stopped)
- {
- if (_chechInterminal == 0)
- {
- _chechInterminal = 3;
- }
- DoCheck();
- }
- }
- }
- /// <summary>
- /// Do HartRate
- /// </summary>
- /// <returns></returns>
- private void DoCheck()
- {
- Task.Run(new Action(ExecuteDoCheck));
- }
- private void OnOfflined()
- {
- Logger.WriteLineInfo($"Connection keeper is offline");
- Offlined?.Invoke(this, EventArgs.Empty);
- }
- }
- }
|