ntminer 5 years ago
parent
commit
8a370da5b6

+ 1 - 1
src/CalcConfigUpdater/Program.cs

@@ -320,7 +320,7 @@ namespace NTMiner {
 
         private static async Task<byte[]> GetHtmlAsync(string url) {
             try {
-                using (HttpClient client = RpcRoot.Create()) {
+                using (HttpClient client = RpcRoot.CreateHttpClient()) {
                     client.Timeout = TimeSpan.FromSeconds(20);
                     return await client.GetByteArrayAsync(url);
                 }

+ 5 - 5
src/NTMinerDaemon/NTMinerDaemonController.cs

@@ -58,7 +58,7 @@ namespace NTMiner {
                 string json = Request.Content.ReadAsStringAsync().Result;
                 SpecialPath.SaveGpuProfilesJsonFile(json);
                 if (IsNTMinerOpened()) {
-                    using (HttpClient client = RpcRoot.Create()) {
+                    using (HttpClient client = RpcRoot.CreateHttpClient()) {
                         Task<HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{NTKeyword.MinerClientPort.ToString()}/api/MinerClient/OverClock", null);
                         Write.DevDebug($"{nameof(SaveGpuProfilesJson)} {getHttpResponse.Result.ReasonPhrase}");
                     }
@@ -75,7 +75,7 @@ namespace NTMiner {
             Logger.InfoDebugLine($"开机启动{(autoBoot ? "√" : "×")},自动挖矿{(autoStart ? "√" : "×")}");
             MinerProfileUtil.SetAutoStart(autoBoot, autoStart);
             if (IsNTMinerOpened()) {
-                using (HttpClient client = RpcRoot.Create()) {
+                using (HttpClient client = RpcRoot.CreateHttpClient()) {
                     Task<HttpResponseMessage> getHttpResponse = client.PostAsync($"http://localhost:{NTKeyword.MinerClientPort.ToString()}/api/MinerClient/RefreshAutoBootStart", null);
                     Write.DevDebug($"{nameof(SetAutoBootStart)} {getHttpResponse.Result.ReasonPhrase}");
                 }
@@ -140,7 +140,7 @@ namespace NTMiner {
                 }
                 string location = NTMinerRegistry.GetLocation();
                 if (IsNTMinerOpened()) {
-                    using (HttpClient client = RpcRoot.Create()) {
+                    using (HttpClient client = RpcRoot.CreateHttpClient()) {
                         WorkRequest innerRequest = new WorkRequest {
                             WorkId = request.WorkId
                         };
@@ -179,7 +179,7 @@ namespace NTMiner {
                     return ResponseBase.Ok();
                 }
                 try {
-                    using (HttpClient client = RpcRoot.Create()) {
+                    using (HttpClient client = RpcRoot.CreateHttpClient()) {
                         Task<HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://localhost:{NTKeyword.MinerClientPort.ToString()}/api/MinerClient/StopMine", request);
                         response = getHttpResponse.Result.Content.ReadAsAsync<ResponseBase>().Result;
                         return response;
@@ -232,7 +232,7 @@ namespace NTMiner {
             Logger.InfoDebugLine("退出挖矿端");
             bool isClosed = false;
             try {
-                using (HttpClient client = RpcRoot.Create()) {
+                using (HttpClient client = RpcRoot.CreateHttpClient()) {
                     Task<HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://localhost:{NTKeyword.MinerClientPort.ToString()}/api/MinerClient/CloseNTMiner", new SignRequest { });
                     ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync<ResponseBase>().Result;
                     isClosed = response.IsSuccess();

+ 1 - 1
src/NTMinerDaemon/User/Impl/UserSet.cs

@@ -45,7 +45,7 @@ namespace NTMiner.User.Impl {
                 DataRequest<Guid?> request = new DataRequest<Guid?> {
                     Data = NTMinerRegistry.GetClientId()
                 };
-                using (HttpClient client = RpcRoot.Create()) {
+                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;

+ 1 - 1
src/NTMinerRpcClient/RpcRoot.cs

@@ -3,7 +3,7 @@ using System.Net.Http;
 
 namespace NTMiner {
     public static partial class RpcRoot {
-        public static HttpClient Create() {
+        public static HttpClient CreateHttpClient() {
             return new HttpClient {
                 Timeout = TimeSpan.FromSeconds(60)
             };

+ 3 - 3
src/NTMinerRpcClient/RpcRoot.partial.cs

@@ -35,7 +35,7 @@ namespace NTMiner {
         public static void GetAsync<T>(string host, int port, string controller, string action, Dictionary<string, string> data, Action<T, Exception> callback) {
             Task.Factory.StartNew(() => {
                 try {
-                    using (HttpClient client = Create()) {
+                    using (HttpClient client = CreateHttpClient()) {
                         string queryString = string.Empty;
                         if (data != null && data.Count != 0) {
                             queryString = "?" + string.Join("&", data.Select(a => a.Key + "=" + a.Value));
@@ -76,7 +76,7 @@ namespace NTMiner {
                 if (query != null && query.Count != 0) {
                     queryString = "?" + string.Join("&", query.Select(a => a.Key + "=" + a.Value));
                 }
-                using (HttpClient client = Create()) {
+                using (HttpClient client = CreateHttpClient()) {
                     if (timeout.HasValue) {
                         client.Timeout = TimeSpan.FromMilliseconds(timeout.Value);
                     }
@@ -94,7 +94,7 @@ namespace NTMiner {
         private static void PostAsync<T>(string host, int port, string controller, string action, Dictionary<string, string> query, object data, Action<T, Exception> callback, int timeountMilliseconds = 0) {
             Task.Factory.StartNew(() => {
                 try {
-                    using (HttpClient client = Create()) {
+                    using (HttpClient client = CreateHttpClient()) {
                         if (timeountMilliseconds != 0) {
                             client.Timeout = TimeSpan.FromMilliseconds(timeountMilliseconds);
                         }

+ 2 - 2
src/NTMinerRpcClient/Services/Client/MinerStudioService.cs

@@ -21,7 +21,7 @@ namespace NTMiner.Services.Client {
         public void ShowMainWindowAsync(int clientPort, Action<bool, Exception> callback) {
             Task.Factory.StartNew(() => {
                 try {
-                    using (HttpClient client = RpcRoot.Create()) {
+                    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);
@@ -47,7 +47,7 @@ namespace NTMiner.Services.Client {
             }
             bool isClosed = false;
             try {
-                using (HttpClient client = RpcRoot.Create()) {
+                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();

+ 4 - 4
src/NTMinerRpcClient/Services/Client/NTMinerClientService.cs

@@ -53,7 +53,7 @@ namespace NTMiner.Services.Client {
         public void RefreshAutoBootStartAsync() {
             Task.Factory.StartNew(() => {
                 try {
-                    using (HttpClient client = RpcRoot.Create()) {
+                    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}");
@@ -80,7 +80,7 @@ namespace NTMiner.Services.Client {
         }
 
         public ResponseBase StartMine(string clientIp, WorkRequest request) {
-            using (HttpClient client = RpcRoot.Create()) {
+            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;
@@ -104,7 +104,7 @@ namespace NTMiner.Services.Client {
         }
 
         public ResponseBase SetMinerProfileProperty(string clientIp, SetClientMinerProfilePropertyRequest request) {
-            using (HttpClient client = RpcRoot.Create()) {
+            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;
@@ -115,7 +115,7 @@ namespace NTMiner.Services.Client {
         public Task<SpeedData> GetSpeedAsync(string clientIp, Action<SpeedData, Exception> callback) {
             return Task.Factory.StartNew(() => {
                 try {
-                    using (HttpClient client = RpcRoot.Create()) {
+                    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;

+ 10 - 10
src/NTMinerRpcClient/Services/Client/NTMinerDaemonService.cs

@@ -18,7 +18,7 @@ namespace NTMiner.Services.Client {
         /// </summary>
         public void CloseDaemon() {
             try {
-                using (HttpClient client = RpcRoot.Create()) {
+                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}");
@@ -41,7 +41,7 @@ namespace NTMiner.Services.Client {
 
         #region ClientIp
         public ResponseBase EnableWindowsRemoteDesktop(string clientIp, SignRequest request) {
-            using (HttpClient client = RpcRoot.Create()) {
+            using (HttpClient client = RpcRoot.CreateHttpClient()) {
                 Task<HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{clientIp}:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{_controllerName}/{nameof(INTMinerDaemonController.EnableWindowsRemoteDesktop)}", request);
                 ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync<ResponseBase>().Result;
                 return response;
@@ -58,7 +58,7 @@ namespace NTMiner.Services.Client {
         public void SaveGpuProfilesJsonAsync(string clientIp, string json) {
             Task.Factory.StartNew(() => {
                 try {
-                    using (HttpClient client = RpcRoot.Create()) {
+                    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);
@@ -74,7 +74,7 @@ namespace NTMiner.Services.Client {
         public void SetAutoBootStartAsync(string clientIp, bool autoBoot, bool autoStart) {
             Task.Factory.StartNew(() => {
                 try {
-                    using (HttpClient client = RpcRoot.Create()) {
+                    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}");
@@ -87,7 +87,7 @@ namespace NTMiner.Services.Client {
         }
 
         public ResponseBase RestartWindows(string clientIp, SignRequest request) {
-            using (HttpClient client = RpcRoot.Create()) {
+            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;
@@ -95,7 +95,7 @@ namespace NTMiner.Services.Client {
         }
 
         public ResponseBase ShutdownWindows(string clientIp, SignRequest request) {
-            using (HttpClient client = RpcRoot.Create()) {
+            using (HttpClient client = RpcRoot.CreateHttpClient()) {
                 Task<HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{clientIp}:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{_controllerName}/{nameof(INTMinerDaemonController.ShutdownWindows)}", request);
                 ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync<ResponseBase>().Result;
                 return response;
@@ -103,7 +103,7 @@ namespace NTMiner.Services.Client {
         }
 
         public ResponseBase RestartNTMiner(string clientIp, WorkRequest request) {
-            using (HttpClient client = RpcRoot.Create()) {
+            using (HttpClient client = RpcRoot.CreateHttpClient()) {
                 Task<HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{clientIp}:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{_controllerName}/{nameof(INTMinerDaemonController.RestartNTMiner)}", request);
                 ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync<ResponseBase>().Result;
                 return response;
@@ -111,7 +111,7 @@ namespace NTMiner.Services.Client {
         }
 
         public ResponseBase UpgradeNTMiner(string clientIp, UpgradeNTMinerRequest request) {
-            using (HttpClient client = RpcRoot.Create()) {
+            using (HttpClient client = RpcRoot.CreateHttpClient()) {
                 Task<HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{clientIp}:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{_controllerName}/{nameof(INTMinerDaemonController.UpgradeNTMiner)}", request);
                 ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync<ResponseBase>().Result;
                 return response;
@@ -119,7 +119,7 @@ namespace NTMiner.Services.Client {
         }
 
         public ResponseBase StartMine(string clientIp, WorkRequest request) {
-            using (HttpClient client = RpcRoot.Create()) {
+            using (HttpClient client = RpcRoot.CreateHttpClient()) {
                 Task<HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{clientIp}:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{_controllerName}/{nameof(INTMinerDaemonController.StartMine)}", request);
                 ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync<ResponseBase>().Result;
                 return response;
@@ -127,7 +127,7 @@ namespace NTMiner.Services.Client {
         }
 
         public ResponseBase StopMine(string clientIp, SignRequest request) {
-            using (HttpClient client = RpcRoot.Create()) {
+            using (HttpClient client = RpcRoot.CreateHttpClient()) {
                 Task<HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{clientIp}:{NTKeyword.NTMinerDaemonPort.ToString()}/api/{_controllerName}/{nameof(INTMinerDaemonController.StopMine)}", request);
                 ResponseBase response = getHttpResponse.Result.Content.ReadAsAsync<ResponseBase>().Result;
                 return response;

+ 2 - 2
src/NTMinerRpcClient/Services/Server/ControlCenterService.cs

@@ -24,7 +24,7 @@ namespace NTMiner.Services.Server {
             }
             Task.Factory.StartNew(() => {
                 try {
-                    using (HttpClient client = RpcRoot.Create()) {
+                    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);
@@ -47,7 +47,7 @@ namespace NTMiner.Services.Server {
                 if (processes.Length == 0) {
                     return;
                 }
-                using (HttpClient client = RpcRoot.Create()) {
+                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}");
                 }

+ 2 - 2
src/NTMinerRpcClient/Services/Server/ReportService.cs

@@ -16,7 +16,7 @@ namespace NTMiner.Services.Server {
             Task.Factory.StartNew(() => {
                 TimeSpan timeSpan = TimeSpan.FromSeconds(3);
                 try {
-                    using (HttpClient client = RpcRoot.Create()) {
+                    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);
@@ -34,7 +34,7 @@ namespace NTMiner.Services.Server {
             Task.Factory.StartNew(() => {
                 TimeSpan timeSpan = TimeSpan.FromSeconds(3);
                 try {
-                    using (HttpClient client = RpcRoot.Create()) {
+                    using (HttpClient client = RpcRoot.CreateHttpClient()) {
                         client.Timeout = timeSpan;
                         ReportState request = new ReportState {
                             ClientId = clientId,

+ 2 - 2
src/UnitTests/HttpClientTests.cs

@@ -11,14 +11,14 @@ namespace UnitTests {
             using (HttpClient client = new HttpClient()) {
                 Assert.AreEqual(100, client.Timeout.TotalSeconds);
             }
-            using (HttpClient client = RpcRoot.Create()) {
+            using (HttpClient client = RpcRoot.CreateHttpClient()) {
                 Assert.AreEqual(60, client.Timeout.TotalSeconds);
             }
         }
 
         [TestMethod]
         public void TaskTest() {
-            HttpClient client = RpcRoot.Create();
+            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);