123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- using Vinno.IUS.Common.Log;
- using Vinno.vCloud.Common.FIS.Helper;
- using Vinno.vCloud.FIS.CrossPlatform.Common;
- using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
- using WingInterfaceLibrary.DTO.ServerInfo;
- using WingInterfaceLibrary.Interface;
- namespace Vinno.vCloud.Common.FIS
- {
- internal class ConnectionCheckerV2
- {
- private readonly IVinnoServerService _vinnoServerService;
- private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
- /// <summary>
- ///get or set connection check cycle
- /// </summary>
- private readonly int _connectionCheckCycle;
- private bool _defaultStatus;
- private bool _checkLoginStatus;
- public event EventHandler Offlined;
- public ConnectionCheckerV2(IVinnoServerService vinnoServerService, int connectionCheckCycle)
- {
- _vinnoServerService = vinnoServerService;
- _connectionCheckCycle = connectionCheckCycle;
- _defaultStatus = false;
- _checkLoginStatus = false;
- }
- /// <summary>
- /// Start the Heartrate to keep the session available.
- /// </summary>
- public void Start(bool defaultStatus)
- {
- _defaultStatus = defaultStatus;
- _checkLoginStatus = defaultStatus;
- Task.Run(() =>
- {
- while (!_waitEvent.WaitOne(TimeSpan.FromSeconds(_connectionCheckCycle)))
- {
- DoCheck();
- }
- });
- }
- /// <summary>
- /// Stop the Heartrate
- /// </summary>
- public void Stop()
- {
- _waitEvent.Set();
- }
- private void DoCheck()
- {
- try
- {
- if (_defaultStatus)
- {
- if (!Check(true))
- {
- OnOfflined();
- }
- }
- else
- {
- OnOfflined();
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"ConnectionCheckerV2 do check failed:{ex}");
- }
- }
- public bool Check(bool doubleCheck = true)
- {
- try
- {
- if (_checkLoginStatus)
- {
- if (CommonParameter.Instance.DeviceStatus == EnumDeviceStatus.Offline || CommonParameter.Instance.DeviceStatus == EnumDeviceStatus.LoginFailed)//说明连接失败
- {
- return false;
- }
- else if (CommonParameter.Instance.DeviceStatus != EnumDeviceStatus.Logoning && CommonParameter.Instance.DeviceStatus != EnumDeviceStatus.Reconnecting && CommonParameter.Instance.DeviceStatus != EnumDeviceStatus.Online)//说明还在连接,未失败也未成功,下次仍需检查
- {
- _checkLoginStatus = false;
- }
- }
- EchoResult result = JsonRpcHelper.Echo(_vinnoServerService);
- if (result == null || result.Code != 0)
- {
- if (doubleCheck)
- {
- result = JsonRpcHelper.Echo(_vinnoServerService);
- if (result == null || result.Code != 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else
- {
- return false;
- }
- }
- else
- {
- return true;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"ConnectionCheckerV2 check error:{ex}");
- return false;
- }
- }
- private void OnOfflined()
- {
- Offlined?.Invoke(this, EventArgs.Empty);
- }
- }
- }
|