VirtualRoot.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using LiteDB;
  2. using Microsoft.Win32;
  3. using NTMiner.Core;
  4. using NTMiner.Core.Impl;
  5. using NTMiner.Serialization;
  6. using NTMiner.Ws;
  7. using System;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading;
  12. namespace NTMiner {
  13. public partial class VirtualRoot {
  14. public static INTSerializer JsonSerializer { get; private set; } = new NTJsonSerializer();
  15. public static IOperationResultSet OperationResultSet { get; private set; } = new OperationResultSet();
  16. public static IDaemonOperation DaemonOperation = new DaemonOperation();
  17. private static string _sha1 = null;
  18. private static string Sha1 {
  19. get {
  20. if (_sha1 == null) {
  21. _sha1 = HashUtil.Sha1(File.ReadAllBytes(Process.GetCurrentProcess().MainModule.FileName));
  22. }
  23. return _sha1;
  24. }
  25. }
  26. public static DaemonWsClient DaemonWsClient;
  27. public static DateTime StartedOn { get; private set; } = DateTime.Now;
  28. static VirtualRoot() {
  29. }
  30. public static EventWaitHandle _waitHandle;
  31. private static Mutex _sMutexApp;
  32. // 注意:该程序编译成无界面的windows应用程序而不是控制台程序,从而随机自动启动时无界面
  33. [STAThread]
  34. static void Main(string[] args) {
  35. HomePath.SetHomeDirFullName(AppDomain.CurrentDomain.BaseDirectory);
  36. SetOut(new ConsoleOut());
  37. if (args.Length != 0) {
  38. if (args.Contains("--sha1", StringComparer.OrdinalIgnoreCase)) {
  39. File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sha1"), Sha1);
  40. return;
  41. }
  42. }
  43. try {
  44. SystemEvents.SessionEnding += SessionEndingEventHandler;
  45. StartTimer();
  46. _waitHandle = new AutoResetEvent(false);
  47. bool mutexCreated;
  48. try {
  49. _sMutexApp = new Mutex(true, "NTMinerDaemonAppMutex", out mutexCreated);
  50. }
  51. catch {
  52. mutexCreated = false;
  53. }
  54. if (mutexCreated) {
  55. if (!DevMode.IsDevMode) {
  56. Write.Disable();
  57. }
  58. NTMinerRegistry.SetDaemonVersion(Sha1);
  59. NTMinerRegistry.SetAutoBoot("NTMinerDaemon", true);
  60. #region 是否自动启动挖矿端
  61. bool isAutoBoot = MinerProfileUtil.GetIsAutoBoot();
  62. if (isAutoBoot) {
  63. string location = NTMinerRegistry.GetLocation(NTMinerAppType.MinerClient);
  64. if (!string.IsNullOrEmpty(location) && File.Exists(location)) {
  65. string processName = Path.GetFileName(location);
  66. Process[] processes = Process.GetProcessesByName(processName);
  67. if (processes.Length == 0) {
  68. string arguments = NTMinerRegistry.GetArguments(NTMinerAppType.MinerClient);
  69. if (NTMinerRegistry.GetIsLastIsWork()) {
  70. arguments = "--work " + arguments;
  71. }
  72. try {
  73. Process.Start(location, arguments);
  74. Write.DevOk(() => $"启动挖矿端 {location} {arguments}");
  75. }
  76. catch (Exception e) {
  77. Logger.ErrorDebugLine($"启动挖矿端失败因为异常 {location} {arguments}", e);
  78. }
  79. }
  80. else {
  81. Write.DevDebug($"挖矿端已经在运行中无需启动");
  82. }
  83. }
  84. }
  85. #endregion
  86. Run();
  87. }
  88. }
  89. catch (Exception e) {
  90. Logger.ErrorDebugLine(e);
  91. }
  92. }
  93. private static void Run() {
  94. try {
  95. Windows.ConsoleHandler.Register(Exit);
  96. HttpServer.Start($"http://{NTKeyword.Localhost}:{NTKeyword.NTMinerDaemonPort.ToString()}");
  97. DaemonWsClient = new DaemonWsClient();
  98. AddEventPath<Per2MinuteEvent>("每2分钟通过Ws通道上报一次算力", LogEnum.DevConsole, action: message => {
  99. if (!DaemonWsClient.IsOpen) {
  100. return;
  101. }
  102. RpcRoot.Client.MinerClientService.WsGetSpeedAsync((data, ex) => {
  103. if (!DaemonWsClient.IsOpen) {
  104. return;
  105. }
  106. DaemonWsClient.SendAsync(new WsMessage(Guid.NewGuid(), WsMessage.Speed) {
  107. Data = data
  108. });
  109. });
  110. }, typeof(VirtualRoot));
  111. _waitHandle.WaitOne();
  112. Exit();
  113. }
  114. catch (Exception e) {
  115. Logger.ErrorDebugLine(e);
  116. }
  117. finally {
  118. Exit();
  119. }
  120. }
  121. public static LiteDatabase CreateLocalDb() {
  122. return new LiteDatabase($"filename={Path.Combine(AppDomain.CurrentDomain.BaseDirectory, NTKeyword.LocalDbFileName)}");
  123. }
  124. private static bool _isClosed = false;
  125. public static void Exit() {
  126. if (!_isClosed) {
  127. _isClosed = true;
  128. RaiseEvent(new AppExitEvent());
  129. RpcRoot.RpcUser?.Logout();
  130. NTMinerConsole.Free();
  131. _sMutexApp?.Dispose();
  132. Environment.Exit(0);
  133. }
  134. }
  135. }
  136. }