Exemplo n.º 1
0
 private static void SwitchRadeonGpuMode(bool on)
 {
     SwitchRadeonGpu.SwitchRadeonGpu.Run(on, (isSuccess, e) => {
         if (isSuccess)
         {
             if (on)
             {
                 VirtualRoot.ThisWorkerInfo(nameof(App), "开启A卡计算模式成功", OutEnum.Success);
             }
             else
             {
                 VirtualRoot.ThisWorkerInfo(nameof(App), "关闭A卡计算模式成功", OutEnum.Success);
             }
         }
         else if (e != null)
         {
             VirtualRoot.Out.ShowError(e.Message, delaySeconds: 4);
         }
         else
         {
             if (on)
             {
                 VirtualRoot.ThisWorkerError(nameof(App), "开启A卡计算模式失败", OutEnum.Warn);
             }
             else
             {
                 VirtualRoot.ThisWorkerError(nameof(App), "关闭A卡计算模式失败", OutEnum.Warn);
             }
         }
     });
 }
Exemplo n.º 2
0
            private static void KernelProcessDaemon(IMineContext mineContext, Action clear)
            {
                if (_kernelProcessDaemon != null)
                {
                    VirtualRoot.DeletePath(_kernelProcessDaemon);
                    _kernelProcessDaemon = null;
                    clear?.Invoke();
                }
                string processName = mineContext.Kernel.GetProcessName();

                _kernelProcessDaemon = VirtualRoot.BuildEventPath <Per1MinuteEvent>("周期性检查挖矿内核是否消失,如果消失尝试重启", LogEnum.DevConsole,
                                                                                    action: message => {
                    if (mineContext == Instance.CurrentMineContext)
                    {
                        if (!string.IsNullOrEmpty(processName))
                        {
                            Process[] processes = Process.GetProcessesByName(processName);
                            if (processes.Length == 0)
                            {
                                mineContext.AutoRestartKernelCount += 1;
                                VirtualRoot.ThisWorkerWarn(nameof(NTMinerRoot), processName + $"挖矿内核进程消失", toConsole: true);
                                if (Instance.MinerProfile.IsAutoRestartKernel && mineContext.AutoRestartKernelCount <= Instance.MinerProfile.AutoRestartKernelTimes)
                                {
                                    VirtualRoot.ThisWorkerInfo(nameof(NTMinerRoot), $"尝试第{mineContext.AutoRestartKernelCount}次重启,共{Instance.MinerProfile.AutoRestartKernelTimes}次", toConsole: true);
                                    Instance.RestartMine();
                                    Instance.CurrentMineContext.AutoRestartKernelCount = mineContext.AutoRestartKernelCount;
                                }
                                else
                                {
                                    Instance.StopMineAsync(StopMineReason.KernelProcessLost);
                                }
                                if (_kernelProcessDaemon != null)
                                {
                                    VirtualRoot.DeletePath(_kernelProcessDaemon);
                                    clear?.Invoke();
                                }
                            }
                        }
                    }
                    else
                    {
                        if (_kernelProcessDaemon != null)
                        {
                            VirtualRoot.DeletePath(_kernelProcessDaemon);
                            _kernelProcessDaemon = null;
                            clear?.Invoke();
                        }
                    }
                });
            }
Exemplo n.º 3
0
 public ResponseBase SetMinerProfileProperty([FromBody] SetMinerProfilePropertyRequest request)
 {
     if (request == null || string.IsNullOrEmpty(request.PropertyName))
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         VirtualRoot.ThisWorkerInfo(nameof(MinerClientController), $"设置挖矿参数");
         NTMinerRoot.Instance.MinerProfile.SetMinerProfileProperty(request.PropertyName, request.Value);
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
Exemplo n.º 4
0
 public ResponseBase StopMine([FromBody] SignRequest request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         VirtualRoot.ThisWorkerInfo(nameof(MinerClientController), $"停止挖矿", toConsole: true);
         NTMinerRoot.Instance.StopMineAsync(StopMineReason.RPCUserAction);
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
Exemplo n.º 5
0
 public ResponseBase StartMine([FromBody] WorkRequest request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         VirtualRoot.ThisWorkerInfo(nameof(MinerClientController), $"开始挖矿", toConsole: true);
         NTMinerRoot.Instance.RestartMine(isWork: request.WorkId != Guid.Empty);
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
Exemplo n.º 6
0
 public ResponseBase CloseNTMiner([FromBody] SignRequest request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         VirtualRoot.ThisWorkerInfo(nameof(MinerClientController), $"退出挖矿端");
         TimeSpan.FromMilliseconds(100).Delay().ContinueWith((t) => {
             VirtualRoot.Execute(new CloseNTMinerCommand());
         });
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
Exemplo n.º 7
0
 private void RefreshServerJsonFile()
 {
     OfficialServer.GetJsonFileVersionAsync(MainAssemblyInfo.ServerJsonFileName, (serverJsonFileVersion, minerClientVersion) => {
         AppVersionChangedEvent.PublishIfNewVersion(minerClientVersion);
         string localServerJsonFileVersion = GetServerJsonVersion();
         if (!string.IsNullOrEmpty(serverJsonFileVersion) && localServerJsonFileVersion != serverJsonFileVersion)
         {
             GetAliyunServerJson((data) => {
                 string rawJson = Encoding.UTF8.GetString(data);
                 SpecialPath.WriteServerJsonFile(rawJson);
                 SetServerJsonVersion(serverJsonFileVersion);
                 ReInitServerJson();
                 // 作业模式下界面是禁用的,所以这里的初始化isWork必然是false
                 ContextReInit(isWork: VirtualRoot.IsMinerStudio);
                 VirtualRoot.ThisWorkerInfo(nameof(NTMinerRoot), $"刷新server.json配置", toConsole: true);
             });
         }
         else
         {
             Write.DevDebug("server.json没有新版本");
         }
     });
 }
Exemplo n.º 8
0
 public void OverClock()
 {
     VirtualRoot.ThisWorkerInfo(nameof(MinerClientController), $"刷新超频", toConsole: true);
     NTMinerRoot.Instance.GpuProfileSet.Refresh();
 }
Exemplo n.º 9
0
 public void Init(Action callback)
 {
     Task.Factory.StartNew(() => {
         bool isWork = Environment.GetCommandLineArgs().Contains("--work", StringComparer.OrdinalIgnoreCase);
         if (isWork)   // 是作业
         {
             DoInit(isWork, callback);
             if (VirtualRoot.IsMinerClient)
             {
                 NTMinerRegistry.SetIsLastIsWork(true);
             }
         }
         else   // 不是作业
         {
             if (VirtualRoot.IsMinerClient)
             {
                 NTMinerRegistry.SetIsLastIsWork(false);
             }
             // 如果是Debug模式且不是群控客户端则使用本地数据库初始化
             bool useLocalDb = DevMode.IsDebugMode && !VirtualRoot.IsMinerStudio;
             if (useLocalDb)
             {
                 DoInit(isWork: false, callback: callback);
             }
             else
             {
                 Logger.InfoDebugLine("开始下载server.json");
                 GetAliyunServerJson((data) => {
                     // 如果server.json未下载成功则不覆写本地server.json
                     if (data != null && data.Length != 0)
                     {
                         Logger.InfoDebugLine("GetAliyunServerJson下载成功");
                         var serverJson = Encoding.UTF8.GetString(data);
                         if (!string.IsNullOrEmpty(serverJson))
                         {
                             SpecialPath.WriteServerJsonFile(serverJson);
                         }
                         OfficialServer.GetJsonFileVersionAsync(MainAssemblyInfo.ServerJsonFileName, (serverJsonFileVersion, minerClientVersion) => {
                             SetServerJsonVersion(serverJsonFileVersion);
                             AppVersionChangedEvent.PublishIfNewVersion(minerClientVersion);
                         });
                     }
                     else
                     {
                         if (!File.Exists(SpecialPath.ServerJsonFileFullName))
                         {
                             VirtualRoot.ThisWorkerError(nameof(NTMinerRoot), "配置文件下载失败,这是第一次运行开源矿工,配置文件至少需要成功下载一次,请检查网络是否可用", OutEnum.Warn);
                         }
                         else
                         {
                             VirtualRoot.ThisWorkerWarn(nameof(NTMinerRoot), "配置文件下载失败,使用最后一次成功下载的配置文件", OutEnum.Warn);
                         }
                     }
                     DoInit(isWork, callback);
                 });
                 #region 发生了用户活动时检查serverJson是否有新版本
                 VirtualRoot.BuildEventPath <UserActionEvent>("发生了用户活动时检查serverJson是否有新版本", LogEnum.DevConsole,
                                                              action: message => {
                     RefreshServerJsonFile();
                 });
                 #endregion
             }
         }
         VirtualRoot.ThisWorkerInfo(nameof(NTMinerRoot), "启动");
     });
 }
Exemplo n.º 10
0
        private void Link()
        {
            VirtualRoot.BuildCmdPath <RegCmdHereCommand>(action: message => {
                try {
                    RegCmdHere(); VirtualRoot.ThisWorkerInfo(nameof(NTMinerRoot), "windows右键命令行添加成功", OutEnum.Success);
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                    RegCmdHere(); VirtualRoot.ThisWorkerError(nameof(NTMinerRoot), "windows右键命令行添加失败", OutEnum.Warn);
                }
            });
            VirtualRoot.BuildEventPath <Per1MinuteEvent>("每1分钟阻止系统休眠", LogEnum.None,
                                                         action: message => {
                Windows.Power.PreventSleep();
            });
            #region 挖矿开始时将无份额内核重启份额计数置0
            int      shareCount = 0;
            DateTime shareOn    = DateTime.Now;
            VirtualRoot.BuildEventPath <MineStartedEvent>("挖矿开始后将无份额内核重启份额计数置0", LogEnum.DevConsole,
                                                          action: message => {
                // 将无份额内核重启份额计数置0
                shareCount = 0;
                if (!message.MineContext.IsRestart)
                {
                    shareOn = DateTime.Now;
                }
            });
            #endregion
            #region 每20秒钟检查是否需要重启
            VirtualRoot.BuildEventPath <Per20SecondEvent>("每20秒钟检查是否需要重启", LogEnum.None,
                                                          action: message => {
                #region 重启电脑
                try {
                    if (MinerProfile.IsPeriodicRestartComputer)
                    {
                        if ((DateTime.Now - this.CreatedOn).TotalMinutes > 60 * MinerProfile.PeriodicRestartComputerHours + MinerProfile.PeriodicRestartComputerMinutes)
                        {
                            Logger.WarnWriteLine($"每运行{MinerProfile.PeriodicRestartKernelHours}小时{MinerProfile.PeriodicRestartComputerMinutes}分钟重启电脑");
                            Windows.Power.Restart(60);
                            VirtualRoot.Execute(new CloseNTMinerCommand());
                            return;    // 退出
                        }
                    }
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                }
                #endregion

                #region 周期重启内核
                try {
                    if (IsMining && MinerProfile.IsPeriodicRestartKernel)
                    {
                        if ((DateTime.Now - CurrentMineContext.CreatedOn).TotalMinutes > 60 * MinerProfile.PeriodicRestartKernelHours + MinerProfile.PeriodicRestartKernelMinutes)
                        {
                            Logger.WarnWriteLine($"每运行{MinerProfile.PeriodicRestartKernelHours}小时{MinerProfile.PeriodicRestartKernelMinutes}分钟重启内核");
                            RestartMine();
                            return;    // 退出
                        }
                    }
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                }
                #endregion

                #region 无份额重启内核
                try {
                    if (IsMining && this.CurrentMineContext.MainCoin != null)
                    {
                        int totalShare       = 0;
                        bool restartComputer = MinerProfile.IsNoShareRestartComputer && (DateTime.Now - shareOn).TotalMinutes > MinerProfile.NoShareRestartComputerMinutes;
                        bool restartKernel   = MinerProfile.IsNoShareRestartKernel && (DateTime.Now - shareOn).TotalMinutes > MinerProfile.NoShareRestartKernelMinutes;
                        if (restartComputer || restartKernel)
                        {
                            ICoinShare mainCoinShare = this.CoinShareSet.GetOrCreate(this.CurrentMineContext.MainCoin.GetId());
                            totalShare = mainCoinShare.TotalShareCount;
                            if ((this.CurrentMineContext is IDualMineContext dualMineContext) && dualMineContext.DualCoin != null)
                            {
                                ICoinShare dualCoinShare = this.CoinShareSet.GetOrCreate(dualMineContext.DualCoin.GetId());
                                totalShare += dualCoinShare.TotalShareCount;
                            }
                            // 如果份额没有增加
                            if (shareCount == totalShare)
                            {
                                if (restartComputer)
                                {
                                    if (!MinerProfile.IsAutoBoot || !MinerProfile.IsAutoStart)
                                    {
                                        VirtualRoot.Execute(new SetAutoStartCommand(true, true));
                                    }
                                    Logger.WarnWriteLine($"{MinerProfile.NoShareRestartComputerMinutes}分钟无份额重启电脑");
                                    Windows.Power.Restart(60);
                                    VirtualRoot.Execute(new CloseNTMinerCommand());
                                    return;    // 退出
                                }
                                // 产生过份额或者已经两倍重启内核时间了
                                if (restartKernel && (totalShare > 0 || (DateTime.Now - shareOn).TotalMinutes > 2 * MinerProfile.NoShareRestartKernelMinutes))
                                {
                                    Logger.WarnWriteLine($"{MinerProfile.NoShareRestartKernelMinutes}分钟无份额重启内核");
                                    RestartMine();
                                    return;    // 退出
                                }
                            }
                            if (totalShare > shareCount)
                            {
                                shareCount = totalShare;
                                shareOn    = DateTime.Now;
                            }
                        }