public void GetGpuProfilesJsonAsync(string clientIp, Action <GpuProfilesJsonDb, Exception> callback) { Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.Create()) { client.Timeout = TimeSpan.FromSeconds(3); Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://{clientIp}:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{s_controllerName}/{nameof(INTMinerDaemonController.GetGpuProfilesJson)}", null); string json = getHttpResponse.Result.Content.ReadAsAsync <string>().Result; GpuProfilesJsonDb data = VirtualRoot.JsonSerializer.Deserialize <GpuProfilesJsonDb>(json); callback?.Invoke(data, null); return(data); } } catch (Exception e) { callback?.Invoke(null, e); return(null); } }); }
public void ReportStateAsync(string host, Guid clientId, bool isMining) { Task.Factory.StartNew(() => { TimeSpan timeSpan = TimeSpan.FromSeconds(3); try { using (HttpClient client = RpcRoot.Create()) { client.Timeout = timeSpan; ReportState request = new ReportState { ClientId = clientId, IsMining = isMining }; Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{host}:{NTKeyword.ControlCenterPort.ToString()}/api/{SControllerName}/{nameof(IReportController.ReportState)}", request); Write.DevDebug($"{nameof(ReportStateAsync)} {getHttpResponse.Result.ReasonPhrase}"); } } catch (Exception e) { Write.DevException(e); } }); }
private static void GetAsync <T>(string controller, string action, Dictionary <string, string> param, Action <T, Exception> callback) { Task.Factory.StartNew(() => { try { string serverHost = NTMinerRegistry.GetControlCenterHost(); using (HttpClient client = RpcRoot.Create()) { string queryString = string.Empty; if (param != null && param.Count != 0) { queryString = "?" + string.Join("&", param.Select(a => a.Key + "=" + a.Value)); } Task <HttpResponseMessage> message = client.GetAsync($"http://{serverHost}:{NTKeyword.ControlCenterPort.ToString()}/api/{controller}/{action}{queryString}"); T response = message.Result.Content.ReadAsAsync <T>().Result; callback?.Invoke(response, null); } } catch (Exception e) { callback?.Invoke(default, e);
public void GetServicesVersionAsync(Action <string, Exception> callback) { Process[] processes = Process.GetProcessesByName("NTMinerServices"); if (processes.Length == 0) { callback?.Invoke(string.Empty, null); } Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.Create()) { Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{NTKeyword.ControlCenterPort.ToString()}/api/{SControllerName}/{nameof(IControlCenterController.GetServicesVersion)}", null); string response = getHttpResponse.Result.Content.ReadAsAsync <string>().Result; callback?.Invoke(response, null); } } catch (Exception e) { callback?.Invoke(string.Empty, e); } }); }
// ReSharper disable once InconsistentNaming public void CloseNTMiner() { string location = NTMinerRegistry.GetLocation(); if (string.IsNullOrEmpty(location) || !File.Exists(location)) { return; } string processName = Path.GetFileNameWithoutExtension(location); if (Process.GetProcessesByName(processName).Length == 0) { return; } bool isClosed = false; try { using (HttpClient client = RpcRoot.Create()) { client.Timeout = TimeSpan.FromSeconds(2); Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://localhost:{NTKeyword.MinerClientPort.ToString()}/api/{s_controllerName}/{nameof(IMinerClientController.CloseNTMiner)}", new SignRequest { }); ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync <ResponseBase>().Result; isClosed = response.IsSuccess(); } } catch (Exception e) { Logger.ErrorDebugLine(e); } if (!isClosed) { try { Windows.TaskKill.Kill(processName); } catch (Exception e) { Logger.ErrorDebugLine(e); } } }
private static T Post <T>(string controller, string action, Dictionary <string, string> query, object param, int?timeout = null) where T : class { try { string queryString = string.Empty; if (query != null && query.Count != 0) { queryString = "?" + string.Join("&", query.Select(a => a.Key + "=" + a.Value)); } string serverHost = NTMinerRegistry.GetControlCenterHost(); using (HttpClient client = RpcRoot.Create()) { if (timeout.HasValue) { client.Timeout = TimeSpan.FromMilliseconds(timeout.Value); } Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{serverHost}:{NTKeyword.ControlCenterPort.ToString()}/api/{controller}/{action}{queryString}", param); T response = getHttpResponse.Result.Content.ReadAsAsync <T>().Result; return(response); } } catch (Exception e) { Logger.ErrorDebugLine(e); return(null); } }
private static void PostAsync <T>(string controller, string action, Dictionary <string, string> query, object param, Action <T, Exception> callback, int timeountMilliseconds = 0) where T : class { Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.Create()) { if (timeountMilliseconds != 0) { client.Timeout = TimeSpan.FromMilliseconds(timeountMilliseconds); } string queryString = string.Empty; if (query != null && query.Count != 0) { queryString = "?" + string.Join("&", query.Select(a => a.Key + "=" + a.Value)); } Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{NTKeyword.OfficialServerHost}:{NTKeyword.ControlCenterPort.ToString()}/api/{controller}/{action}{queryString}", param); T response = getHttpResponse.Result.Content.ReadAsAsync <T>().Result; callback?.Invoke(response, null); } } catch (Exception e) { callback?.Invoke(null, e); } }); }