Tuple <long, long> DoSpeedTesting( string title, string testUrl, int testTimeout, int port, string config, EventHandler <VgcApis.Models.Datas.StrEvent> logDeliever) { void log(string content) => logDeliever?.Invoke(this, new VgcApis.Models.Datas.StrEvent(content)); log($"{I18N.SpeedtestPortNum}{port}"); if (string.IsNullOrEmpty(config)) { log(I18N.DecodeImportFail); return(new Tuple <long, long>(TIMEOUT, 0)); } var speedTester = new Libs.V2Ray.Core(setting) { title = title }; if (logDeliever != null) { speedTester.OnLog += logDeliever; } long latency = TIMEOUT; long len = 0; try { speedTester.RestartCore(config); if (WaitUntilCoreReady(speedTester)) { var expectedSizeInKib = setting.isUseCustomSpeedtestSettings ? setting.CustomSpeedtestExpectedSizeInKib : -1; var r = VgcApis.Misc.Utils.TimedDownloadTest(testUrl, port, expectedSizeInKib, testTimeout); latency = r.Item1; len = r.Item2; } speedTester.StopCore(); if (logDeliever != null) { speedTester.OnLog -= logDeliever; } } catch { } return(new Tuple <long, long>(latency, len)); }
long DoSpeedTesting( string title, string testUrl, int testTimeout, int port, string config, EventHandler <VgcApis.Models.Datas.StrEvent> logDeliever) { void log(string content) => logDeliever?.Invoke(this, new VgcApis.Models.Datas.StrEvent(content)); log($"{I18N.SpeedtestPortNum}{port}"); if (string.IsNullOrEmpty(config)) { log(I18N.DecodeImportFail); return(SpeedtestTimeout); } var speedTester = new Libs.V2Ray.Core(setting) { title = title }; if (logDeliever != null) { speedTester.OnLog += logDeliever; } long latency = VgcApis.Models.Consts.Core.SpeedtestTimeout; try { speedTester.RestartCore(config); var expectedSizeInKib = setting.isUseCustomSpeedtestSettings ? setting.CustomSpeedtestExpectedSizeInKib : -1; latency = VgcApis.Misc.Utils.TimedDownloadTesting(testUrl, port, expectedSizeInKib, testTimeout); speedTester.StopCore(); if (logDeliever != null) { speedTester.OnLog -= logDeliever; } } catch { } return(latency); }
void RestartCoreWorker(Action next) { try { var finalConfig = configer.GetFinalConfig(); if (finalConfig == null) { StopCore(); return; } v2rayCore.title = coreStates.GetTitle(); v2rayCore.RestartCore(finalConfig.ToString(), Misc.Utils.GetEnvVarsFromConfig(finalConfig)); bookKeeper?.Run(); } finally { next?.Invoke(); } }
long RunSpeedTestWorker( string rawConfig, string title, string testUrl, int testTimeout, bool isUseCache, bool isInjectSpeedTestTpl, bool isInjectActivateTpl, EventHandler <VgcApis.Models.Datas.StrEvent> logDeliever) { var port = VgcApis.Misc.Utils.GetFreeTcpPort(); // for debugging logDeliever?.Invoke(this, new VgcApis.Models.Datas.StrEvent( I18N.SpeedtestPortNum + port.ToString())); var speedTestConfig = CreateSpeedTestConfig( rawConfig, port, isUseCache, isInjectSpeedTestTpl, isInjectActivateTpl); if (string.IsNullOrEmpty(speedTestConfig)) { logDeliever?.Invoke(this, new VgcApis.Models.Datas.StrEvent(I18N.DecodeImportFail)); return(SpeedtestTimeout); } var speedTester = new Libs.V2Ray.Core(setting) { title = title }; speedTester.WaitForStartCoreToken(); if (setting.isSpeedtestCancelled) { speedTester.ReleaseToken(); return(VgcApis.Models.Consts.Core.SpeedtestAbort); } if (logDeliever != null) { speedTester.OnLog += logDeliever; } try { speedTester.RestartCore(speedTestConfig); } finally { speedTester.ReleaseToken(); } var expectedSizeInKib = setting.isUseCustomSpeedtestSettings ? setting.CustomSpeedtestExpectedSizeInKib : 0; long testResult = Misc.Utils.VisitWebPageSpeedTest(testUrl, port, expectedSizeInKib, testTimeout); speedTester.StopCore(); if (logDeliever != null) { speedTester.OnLog -= logDeliever; } return(testResult); }