ConnectionCheckerV2.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Vinno.IUS.Common.Log;
  5. using Vinno.vCloud.Common.FIS.Helper;
  6. using Vinno.vCloud.FIS.CrossPlatform.Common;
  7. using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
  8. using WingInterfaceLibrary.DTO.ServerInfo;
  9. using WingInterfaceLibrary.Interface;
  10. namespace Vinno.vCloud.Common.FIS
  11. {
  12. internal class ConnectionCheckerV2
  13. {
  14. private readonly IVinnoServerService _vinnoServerService;
  15. private readonly ManualResetEvent _waitEvent = new ManualResetEvent(false);
  16. /// <summary>
  17. ///get or set connection check cycle
  18. /// </summary>
  19. private readonly int _connectionCheckCycle;
  20. private bool _defaultStatus;
  21. private bool _checkLoginStatus;
  22. public event EventHandler Offlined;
  23. public ConnectionCheckerV2(IVinnoServerService vinnoServerService, int connectionCheckCycle)
  24. {
  25. _vinnoServerService = vinnoServerService;
  26. _connectionCheckCycle = connectionCheckCycle;
  27. _defaultStatus = false;
  28. _checkLoginStatus = false;
  29. }
  30. /// <summary>
  31. /// Start the Heartrate to keep the session available.
  32. /// </summary>
  33. public void Start(bool defaultStatus)
  34. {
  35. _defaultStatus = defaultStatus;
  36. _checkLoginStatus = defaultStatus;
  37. Task.Run(() =>
  38. {
  39. while (!_waitEvent.WaitOne(TimeSpan.FromSeconds(_connectionCheckCycle)))
  40. {
  41. DoCheck();
  42. }
  43. });
  44. }
  45. /// <summary>
  46. /// Stop the Heartrate
  47. /// </summary>
  48. public void Stop()
  49. {
  50. _waitEvent.Set();
  51. }
  52. private void DoCheck()
  53. {
  54. try
  55. {
  56. if (_defaultStatus)
  57. {
  58. if (!Check(true))
  59. {
  60. OnOfflined();
  61. }
  62. }
  63. else
  64. {
  65. OnOfflined();
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. Logger.WriteLineError($"ConnectionCheckerV2 do check failed:{ex}");
  71. }
  72. }
  73. public bool Check(bool doubleCheck = true)
  74. {
  75. try
  76. {
  77. if (_checkLoginStatus)
  78. {
  79. if (CommonParameter.Instance.DeviceStatus == EnumDeviceStatus.Offline || CommonParameter.Instance.DeviceStatus == EnumDeviceStatus.LoginFailed)//说明连接失败
  80. {
  81. return false;
  82. }
  83. else if (CommonParameter.Instance.DeviceStatus != EnumDeviceStatus.Logoning && CommonParameter.Instance.DeviceStatus != EnumDeviceStatus.Reconnecting && CommonParameter.Instance.DeviceStatus != EnumDeviceStatus.Online)//说明还在连接,未失败也未成功,下次仍需检查
  84. {
  85. _checkLoginStatus = false;
  86. }
  87. }
  88. EchoResult result = JsonRpcHelper.Echo(_vinnoServerService);
  89. if (result == null || result.Code != 0)
  90. {
  91. if (doubleCheck)
  92. {
  93. result = JsonRpcHelper.Echo(_vinnoServerService);
  94. if (result == null || result.Code != 0)
  95. {
  96. return false;
  97. }
  98. else
  99. {
  100. return true;
  101. }
  102. }
  103. else
  104. {
  105. return false;
  106. }
  107. }
  108. else
  109. {
  110. return true;
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. Logger.WriteLineError($"ConnectionCheckerV2 check error:{ex}");
  116. return false;
  117. }
  118. }
  119. private void OnOfflined()
  120. {
  121. Offlined?.Invoke(this, EventArgs.Empty);
  122. }
  123. }
  124. }