12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using System;
- using System.IO;
- using System.Threading;
- using System.Threading.Tasks;
- using Vinno.IUS.Common.Log;
- using Vinno.IUS.Common.Network.Leaf;
- using Vinno.IUS.Common.Network.Transfer;
- using Vinno.vCloud.Protocol.Messages.Client.Login;
- using Vinno.vCloud.Protocol.Messages.Common;
- namespace Vinno.vCloud.Common.FIS
- {
- internal class HeartRateKeeper
- {
- private readonly string _sessionId;
- private readonly ClientLeaf _leaf;
- private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
- private readonly int _heartRate;
- public HeartRateKeeper(string sessionId, ClientLeaf leaf, int heartRateCycle)
- {
- _sessionId = sessionId;
- _leaf = leaf;
- _heartRate = heartRateCycle;
- }
- /// <summary>
- /// Start the Heartrate to keep the session available.
- /// </summary>
- public void Start()
- {
- DoHeartRate();
- }
- /// <summary>
- /// Stop the Heartrate
- /// </summary>
- public void Stop()
- {
- _waitEvent.Set();
- }
- /// <summary>
- /// Do HartRate
- /// </summary>
- /// <returns></returns>
- private void DoHeartRate()
- {
- Task.Run(() =>
- {
- while (!_waitEvent.WaitOne(TimeSpan.FromMinutes(_heartRate)))
- {
- try
- {
- using (var request = MessagePool.GetMessage<HeartRateRequest>())
- {
- request.AccountSessionId = _sessionId;
- var result = ResultMessage.Convert(_leaf.Send(request));
- if (result == null || result.ResultCode != OKResult.Code)
- {
- throw new InvalidDataException();
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"HeartRateKeeper Do Heart Rate Error:{ex}");
- }
- }
- });
- }
- }
- }
|