/// <summary> /// 注意:Request时ByteArrayContent,Response时ReadAsAsync<TResponse>。 /// </summary> /// <typeparam name="TResponse">post的data的类型</typeparam> /// <param name="host">用于组装Url</param> /// <param name="port">用于组装Url</param> /// <param name="controller">用于组装Url</param> /// <param name="action">用于组装Url</param> /// <param name="query">Url上的查询参数,承载登录名、时间戳、签名</param> /// <param name="data">字节数组</param> /// <param name="callback"></param> /// <param name="timeountMilliseconds"></param> public void PostAsync <TResponse>( string host, int port, string controller, string action, Dictionary <string, string> query, object data, Action <TResponse, Exception> callback, int timeountMilliseconds = 0) { Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.CreateHttpClient()) { if (timeountMilliseconds > 0) { client.SetTimeout(timeountMilliseconds); } byte[] bytes = VirtualRoot.BinarySerializer.Serialize(data); if (bytes == null) { bytes = new byte[0]; } HttpContent content = new ByteArrayContent(bytes); Task <HttpResponseMessage> getHttpResponse = client.PostAsync(RpcRoot.GetUrl(host, port, controller, action, query), content); if (getHttpResponse.Result.IsSuccessStatusCode) { getHttpResponse.Result.Content.ReadAsAsync <TResponse>().ContinueWith(t => { callback?.Invoke(t.Result, null); }); } else { callback?.Invoke(default, new NTMinerHttpException($"{action} http response {getHttpResponse.Result.StatusCode.ToString()} {getHttpResponse.Result.ReasonPhrase}")
/// <summary> /// 注意:Request时原始HttpContent,Fire忽略Response /// </summary> /// <param name="host"></param> /// <param name="port"></param> /// <param name="controller"></param> /// <param name="action"></param> /// <param name="query"></param> /// <param name="content"></param> /// <param name="callback"></param> /// <param name="timeountMilliseconds"></param> public void FirePostAsync( string host, int port, string controller, string action, Dictionary <string, string> query, HttpContent content, Action callback = null, int timeountMilliseconds = 0) { Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.CreateHttpClient()) { if (timeountMilliseconds > 0) { client.SetTimeout(timeountMilliseconds); } Task <HttpResponseMessage> getHttpResponse = client.PostAsync(RpcRoot.GetUrl(host, port, controller, action, query), content); if (!getHttpResponse.Result.IsSuccessStatusCode) { NTMinerConsole.DevDebug($"{action} http response {getHttpResponse.Result.StatusCode.ToString()} {getHttpResponse.Result.ReasonPhrase}"); } callback?.Invoke(); } } catch { callback?.Invoke(); } }); }
/// <summary> /// /// </summary> /// <typeparam name="TResponse"></typeparam> /// <param name="host">用于组装Url</param> /// <param name="port">用于组装Url</param> /// <param name="controller">用于组装Url</param> /// <param name="action">用于组装Url</param> /// <param name="query">Url上的查询参数,承载登录名、时间戳、签名</param> /// <param name="callback"></param> public void GetAsync <TResponse>( string host, int port, string controller, string action, Dictionary <string, string> query, Action <TResponse, Exception> callback, int?timeountMilliseconds = null) { Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.CreateHttpClient()) { if (timeountMilliseconds > 0) { client.SetTimeout(timeountMilliseconds); } Task <HttpResponseMessage> getHttpResponse = client.GetAsync(RpcRoot.GetUrl(host, port, controller, action, query)); if (getHttpResponse.Result.IsSuccessStatusCode) { getHttpResponse.Result.Content.ReadAsAsync <TResponse>().ContinueWith(t => { callback?.Invoke(t.Result, null); }); } else { callback?.Invoke(default, new NTMinerException($"{action} http response {getHttpResponse.Result.StatusCode.ToString()} {getHttpResponse.Result.ReasonPhrase}"));
public ResponseBase StartMine(string clientIp, WorkRequest request) { using (HttpClient client = RpcRoot.CreateHttpClient()) { Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{clientIp}:{NTKeyword.MinerClientPort.ToString()}/api/{_controllerName}/{nameof(IMinerClientController.StartMine)}", request); ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync <ResponseBase>().Result; return(response); } }
public ResponseBase RestartWindows(string clientIp, SignRequest request) { using (HttpClient client = RpcRoot.CreateHttpClient()) { Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{clientIp}:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{_controllerName}/{nameof(INTMinerDaemonController.RestartWindows)}", request); ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync <ResponseBase>().Result; return(response); } }
public ResponseBase SetMinerProfileProperty(string clientIp, SetClientMinerProfilePropertyRequest request) { using (HttpClient client = RpcRoot.CreateHttpClient()) { client.Timeout = TimeSpan.FromSeconds(3); Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{clientIp}:{NTKeyword.MinerClientPort.ToString()}/api/{_controllerName}/{nameof(IMinerClientController.SetMinerProfileProperty)}", request); ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync <ResponseBase>().Result; return(response); } }
public void TestMethod1() { using (HttpClient client = new HttpClient()) { Assert.AreEqual(100, client.Timeout.TotalSeconds); } using (HttpClient client = RpcRoot.CreateHttpClient()) { Assert.AreEqual(60, client.Timeout.TotalSeconds); } }
public void TaskTest() { HttpClient client = RpcRoot.CreateHttpClient(); client.GetAsync($"http://{NTKeyword.OfficialServerHost}:{NTKeyword.ControlCenterPort.ToString()}/api/AppSetting/GetTime") .ContinueWith(t => { Console.WriteLine(t.Result.Content.ReadAsAsync <DateTime>().Result); }).Wait(); }
/// <summary> /// 本机网络调用 /// </summary> public void CloseDaemon() { try { using (HttpClient client = RpcRoot.CreateHttpClient()) { client.Timeout = TimeSpan.FromSeconds(2); Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{_controllerName}/{nameof(INTMinerDaemonController.CloseDaemon)}", null); Write.DevDebug($"{nameof(CloseDaemon)} {getHttpResponse.Result.ReasonPhrase}"); } } catch (Exception e) { Write.DevException(e); } }
/// <summary> /// 本机网络调用 /// </summary> public void RefreshAutoBootStartAsync() { Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.CreateHttpClient()) { client.Timeout = TimeSpan.FromSeconds(3); Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{NTKeyword.MinerClientPort.ToString()}/api/{_controllerName}/{nameof(IMinerClientController.RefreshAutoBootStart)}", null); Write.DevDebug($"{nameof(RefreshAutoBootStartAsync)} {getHttpResponse.Result.ReasonPhrase}"); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }); }
/// <summary> /// 本机网络调用 /// </summary> /// <param name="clientPort"></param> /// <param name="callback"></param> public void ShowMainWindowAsync(int clientPort, Action <bool, Exception> callback) { Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.CreateHttpClient()) { Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{clientPort.ToString()}/api/{_controllerName}/{nameof(IMinerStudioController.ShowMainWindow)}", null); bool response = getHttpResponse.Result.Content.ReadAsAsync <bool>().Result; callback?.Invoke(response, null); } } catch (Exception e) { callback?.Invoke(false, e); } }); }
public void SetAutoBootStartAsync(string clientIp, bool autoBoot, bool autoStart) { Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.CreateHttpClient()) { client.Timeout = TimeSpan.FromSeconds(3); Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://{clientIp}:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{_controllerName}/{nameof(INTMinerDaemonController.SetAutoBootStart)}?autoBoot={autoBoot}&autoStart={autoStart}", null); Write.DevDebug($"{nameof(SetAutoBootStartAsync)} {getHttpResponse.Result.ReasonPhrase}"); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }); }
public void SaveGpuProfilesJsonAsync(string clientIp, string json) { Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.CreateHttpClient()) { client.Timeout = TimeSpan.FromSeconds(3); HttpContent content = new StringContent(json); Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://{clientIp}:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{_controllerName}/{nameof(INTMinerDaemonController.SaveGpuProfilesJson)}", content); Write.DevDebug($"{nameof(SaveGpuProfilesJsonAsync)} {getHttpResponse.Result.ReasonPhrase}"); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }); }
/// <summary> /// 本机网络调用 /// </summary> public void GetSpeedAsync(Action <string, Exception> callback) { Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.CreateHttpClient()) { client.Timeout = TimeSpan.FromSeconds(3); Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{NTKeyword.MinerClientPort.ToString()}/api/{_controllerName}/{nameof(IMinerClientController.GetSpeed)}", null); string json = getHttpResponse.Result.Content.ReadAsStringAsync().Result; callback?.Invoke(json, null); } } catch (Exception e) { callback?.Invoke(null, e); } }); }
/// <summary> /// 本机同步网络调用 /// </summary> public void CloseServices() { try { Process[] processes = Process.GetProcessesByName(NTKeyword.NTMinerServicesProcessName); if (processes.Length == 0) { return; } using (HttpClient client = RpcRoot.CreateHttpClient()) { Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{NTKeyword.ControlCenterPort.ToString()}/api/{_controllerName}/{nameof(IControlCenterController.CloseServices)}", null); Write.DevDebug($"{nameof(CloseServices)} {getHttpResponse.Result.ReasonPhrase}"); } } catch (Exception e) { Logger.ErrorDebugLine(e); } }
public void ReportSpeedAsync(string host, SpeedData data, Action <ReportResponse> callback) { Task.Factory.StartNew(() => { TimeSpan timeSpan = TimeSpan.FromSeconds(3); try { using (HttpClient client = RpcRoot.CreateHttpClient()) { // 可能超过3秒钟,查查原因。因为我的网络不稳经常断线。 client.Timeout = timeSpan; Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{host}:{NTKeyword.ControlCenterPort.ToString()}/api/{_controllerName}/{nameof(IReportController.ReportSpeed)}", data); ReportResponse response = getHttpResponse.Result.Content.ReadAsAsync <ReportResponse>().Result; callback?.Invoke(response); } } catch (Exception e) { Write.DevException(e); } }); }
public Task <SpeedData> GetSpeedAsync(string clientIp, Action <SpeedData, Exception> callback) { return(Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.CreateHttpClient()) { client.Timeout = TimeSpan.FromSeconds(3); Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://{clientIp}:{NTKeyword.MinerClientPort.ToString()}/api/{_controllerName}/{nameof(IMinerClientController.GetSpeed)}", null); SpeedData data = getHttpResponse.Result.Content.ReadAsAsync <SpeedData>().Result; callback?.Invoke(data, null); return data; } } catch (Exception e) { callback?.Invoke(null, e); return null; } })); }
/// <summary> /// 本机网络调用 /// </summary> /// <param name="callback"></param> public void GetServicesVersionAsync(Action <string, Exception> callback) { Process[] processes = Process.GetProcessesByName(NTKeyword.NTMinerServicesProcessName); if (processes.Length == 0) { callback?.Invoke(string.Empty, null); } Task.Factory.StartNew(() => { try { using (HttpClient client = RpcRoot.CreateHttpClient()) { Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{NTKeyword.ControlCenterPort.ToString()}/api/{_controllerName}/{nameof(IControlCenterController.GetServicesVersion)}", null); string response = getHttpResponse.Result.Content.ReadAsAsync <string>().Result; callback?.Invoke(response, null); } } catch (Exception e) { callback?.Invoke(string.Empty, e); } }); }
public void ReportStateAsync(string host, Guid clientId, bool isMining) { Task.Factory.StartNew(() => { TimeSpan timeSpan = TimeSpan.FromSeconds(3); try { using (HttpClient client = RpcRoot.CreateHttpClient()) { client.Timeout = timeSpan; ReportState request = new ReportState { ClientId = clientId, IsMining = isMining }; Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{host}:{NTKeyword.ControlCenterPort.ToString()}/api/{_controllerName}/{nameof(IReportController.ReportState)}", request); Write.DevDebug($"{nameof(ReportStateAsync)} {getHttpResponse.Result.ReasonPhrase}"); } } catch (Exception e) { Write.DevException(e); } }); }
/// <summary> /// 同步方法 /// </summary> /// <param name="clientId"></param> /// <returns></returns> private static List <UserData> GetUsers() { try { DataRequest <Guid?> request = new DataRequest <Guid?> { Data = NTMinerRegistry.GetClientId() }; using (HttpClient client = RpcRoot.CreateHttpClient()) { client.Timeout = TimeSpan.FromSeconds(2); Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{NTMinerRegistry.GetControlCenterHost()}:{NTKeyword.ControlCenterPort.ToString()}/api/ControlCenter/Users", request); DataResponse <List <UserData> > response = getHttpResponse.Result.Content.ReadAsAsync <DataResponse <List <UserData> > >().Result; if (response != null && response.Data != null) { return(response.Data); } } } catch (Exception e) { Logger.ErrorDebugLine(e); } return(new List <UserData>()); }
/// <summary> /// 本机同步网络调用 /// </summary> public void CloseMinerStudio() { 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.CreateHttpClient()) { Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://localhost:{NTKeyword.MinerStudioPort.ToString()}/api/{_controllerName}/{nameof(IMinerStudioController.CloseMinerStudio)}", 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, waitForExit: true); } catch (Exception e) { Logger.ErrorDebugLine(e); } } }