/// <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 void ReportStateAsync(Guid clientId, bool isMining) { ReportState request = new ReportState { ClientId = clientId, IsMining = isMining }; RpcRoot.FirePostAsync(RpcRoot.OfficialServerHost, RpcRoot.OfficialServerPort, _controllerName, nameof(IReportController.ReportState), null, request, null, 5000); }
public bool SaveGpuProfilesJson(string json) { bool isSuccess = false; string description = "超频"; try { SpecialPath.SaveGpuProfilesJsonFile(json); if (IsNTMinerOpened()) { RpcRoot.FirePostAsync(NTKeyword.Localhost, NTKeyword.MinerClientPort, _minerClientControllerName, nameof(IMinerClientController.OverClock), null, null); } else { description = "超频,挖矿端未启动,下次启动时生效"; } isSuccess = true; } catch (Exception e) { Logger.ErrorDebugLine(e); } VirtualRoot.OperationResultSet.Add(new OperationResultData { Timestamp = Timestamp.GetTimestamp(), StateCode = isSuccess ? 200 : 500, ReasonPhrase = isSuccess ? "Ok" : "Fail", Description = description }); return(isSuccess); }
public void QueryClientsAsync( int pageIndex, int pageSize, Guid?groupId, Guid?workId, string minerIp, string minerName, MineStatus mineState, string coin, string pool, string wallet, string version, string kernel, Action <QueryClientsResponse, Exception> callback) { var request = new QueryClientsRequest { PageIndex = pageIndex, PageSize = pageSize, GroupId = groupId, WorkId = workId, MinerIp = minerIp, MinerName = minerName, MineState = mineState, Coin = coin, Pool = pool, Wallet = wallet, Version = version, Kernel = kernel }; RpcRoot.PostAsync(NTMinerRegistry.GetControlCenterHost(), NTKeyword.ControlCenterPort, _controllerName, nameof(IClientController.QueryClients), request, request, callback); }
public void GetGpuProfilesJsonAsync(IMinerData client) { RpcRoot.PostAsync <string>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.GetGpuProfilesJson), null, (json, e) => { GpuProfilesJsonDb data = VirtualRoot.JsonSerializer.Deserialize <GpuProfilesJsonDb>(json) ?? new GpuProfilesJsonDb(); VirtualRoot.RaiseEvent(new GetGpuProfilesResponsedEvent(client.ClientId, data)); }, timeountMilliseconds: 3000); }
public void GetAliyunServerJson(Action <string> callback) { string serverJsonFileUrl = $"https://minerjson.{NTKeyword.CloudFileDomain}/{HomePath.ExportServerJsonFileName}"; string fileUrl = serverJsonFileUrl + "?t=" + DateTime.Now.Ticks; Task.Factory.StartNew(() => { try { var webRequest = WebRequest.Create(fileUrl); webRequest.Timeout = 20 * 1000; webRequest.Method = "GET"; // 因为有压缩和解压缩,server.json的尺寸已经不是问题 webRequest.Headers.Add("Accept-Encoding", "gzip, deflate, br"); var response = webRequest.GetResponse(); using (Stream ms = new MemoryStream(), stream = response.GetResponseStream()) { byte[] buffer = new byte[NTKeyword.IntK]; int n = stream.Read(buffer, 0, buffer.Length); while (n > 0) { ms.Write(buffer, 0, n); n = stream.Read(buffer, 0, buffer.Length); } byte[] data = new byte[ms.Length]; ms.Position = 0; ms.Read(data, 0, data.Length); data = RpcRoot.ZipDecompress(data); callback?.Invoke(Encoding.UTF8.GetString(data)); } } catch (Exception e) { Logger.ErrorDebugLine(e); callback?.Invoke(string.Empty); } }); }
/// <summary> /// 本机同步网络调用 /// </summary> public void CloseNTMinerAsync(Action callback) { string location = NTMinerRegistry.GetLocation(NTMinerAppType.MinerClient); if (string.IsNullOrEmpty(location) || !File.Exists(location)) { callback?.Invoke(); return; } string processName = Path.GetFileNameWithoutExtension(location); if (Process.GetProcessesByName(processName).Length == 0) { callback?.Invoke(); return; } RpcRoot.PostAsync(NTKeyword.Localhost, NTKeyword.MinerClientPort, _controllerName, nameof(IMinerClientController.CloseNTMiner), new SignRequest { }, (ResponseBase response, Exception e) => { if (!response.IsSuccess()) { try { Windows.TaskKill.Kill(processName, waitForExit: true); } catch (Exception ex) { Logger.ErrorDebugLine(ex); } } callback?.Invoke(); }, timeountMilliseconds: 2000); }
public void SetAutoBootStartAsync(IMinerData client, SetAutoBootStartRequest request) { RpcRoot.FirePostAsync(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.SetAutoBootStart), new Dictionary <string, string> { { "autoBoot", request.AutoBoot.ToString() }, { "autoStart", request.AutoStart.ToString() } }, null, callback: null, timeountMilliseconds: 3000); }
/// <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}")
public void GetGpuProfilesJsonAsync(string clientIp, Action <GpuProfilesJsonDb, Exception> callback) { RpcRoot.PostAsync(clientIp, NTKeyword.NTMinerDaemonPort, _controllerName, nameof(INTMinerDaemonController.GetGpuProfilesJson), null, (string json, Exception e) => { GpuProfilesJsonDb data = VirtualRoot.JsonSerializer.Deserialize <GpuProfilesJsonDb>(json) ?? new GpuProfilesJsonDb(); callback?.Invoke(data, null); }, timeountMilliseconds: 3000); }
public void GetMineWorksAsync(Action <DataResponse <List <UserMineWorkData> >, Exception> callback) { SignRequest request = new SignRequest { }; RpcRoot.SignPostAsync(RpcRoot.OfficialServerHost, RpcRoot.OfficialServerPort, _controllerName, nameof(IUserMineWorkController.MineWorks), data: request, callback, timeountMilliseconds: 2000); }
/// <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(); } }); }
// ReSharper disable once InconsistentNaming public void GetNTMinerUrlAsync(string fileName, Action <string, Exception> callback) { NTMinerUrlRequest request = new NTMinerUrlRequest { FileName = fileName }; RpcRoot.PostAsync(_host, _port, _controllerName, nameof(IFileUrlController.NTMinerUrl), request, callback); }
public void GetServerMessagesAsync(DateTime timestamp, Action <DataResponse <List <ServerMessageData> >, Exception> callback) { ServerMessagesRequest request = new ServerMessagesRequest { Timestamp = Timestamp.GetTimestamp(timestamp) }; RpcRoot.PostAsync(RpcRoot.OfficialServerHost, RpcRoot.OfficialServerPort, _controllerName, nameof(IServerMessageController.ServerMessages), request, callback); }
public void RemoveClientsAsync(List <string> objectIds, Action <ResponseBase, Exception> callback) { var request = new MinerIdsRequest { ObjectIds = objectIds }; RpcRoot.SignPostAsync(RpcRoot.OfficialServerHost, RpcRoot.OfficialServerPort, _controllerName, nameof(IClientDataController.RemoveClients), data: request, callback); }
public void GetLatestSnapshotsAsync(int limit, Action <GetCoinSnapshotsResponse, Exception> callback) { GetCoinSnapshotsRequest request = new GetCoinSnapshotsRequest { Limit = limit }; RpcRoot.SignPostAsync(RpcRoot.OfficialServerHost, RpcRoot.OfficialServerPort, _controllerName, nameof(ICoinSnapshotController.LatestSnapshots), data: request, callback); }
public void RemoveMineWorkAsync(Guid id, Action <ResponseBase, Exception> callback) { DataRequest <Guid> request = new DataRequest <Guid> { Data = id }; RpcRoot.PostAsync(NTMinerRegistry.GetControlCenterHost(), NTKeyword.ControlCenterPort, _controllerName, nameof(IMineWorkController.RemoveMineWork), request, request, callback); }
public void GetPackageUrlAsync(string package, Action <string, Exception> callback) { PackageUrlRequest request = new PackageUrlRequest { Package = package }; RpcRoot.PostAsync(_host, _port, _controllerName, nameof(IFileUrlController.PackageUrl), request, callback); }
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 void AddOrUpdatePoolAsync(PoolData entity, Action <ResponseBase, Exception> callback) { DataRequest <PoolData> request = new DataRequest <PoolData> { Data = entity }; RpcRoot.PostAsync(NTMinerRegistry.GetControlCenterHost(), NTKeyword.ControlCenterPort, _controllerName, nameof(IPoolController.AddOrUpdatePool), request, request, callback); }
public void IsLoginNameExistAsync(string loginName, Action <bool> callback) { RpcRoot.GetAsync(RpcRoot.OfficialServerHost, RpcRoot.OfficialServerPort, _controllerName, nameof(IUserController.IsLoginNameExist), new Dictionary <string, string> { { "loginName", loginName } }, (DataResponse <bool> response, Exception e) => { callback?.Invoke(response.IsSuccess() && response.Data); }, timeountMilliseconds: 5000); }
public void RemoveMineWorkAsync(Guid id, Action <ResponseBase, Exception> callback) { DataRequest <Guid> request = new DataRequest <Guid> { Data = id }; RpcRoot.SignPostAsync(RpcRoot.OfficialServerHost, RpcRoot.OfficialServerPort, _controllerName, nameof(IUserMineWorkController.RemoveMineWork), data: request, callback); }
// ReSharper disable once InconsistentNaming public void UpgradeNTMinerAsync(IMinerData client, string ntminerFileName) { UpgradeNTMinerRequest request = new UpgradeNTMinerRequest { NTMinerFileName = ntminerFileName }; RpcRoot.PostAsync <ResponseBase>(client.GetLocalIp(), NTKeyword.NTMinerDaemonPort, _daemonControllerName, nameof(INTMinerDaemonController.UpgradeNTMiner), request, null, timeountMilliseconds: 3000); }
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 void LoginAsync(string loginName, string password, Action <ResponseBase, Exception> callback) { VirtualRoot.SetRpcUser(new User.RpcUser(loginName, password)); SignRequest request = new SignRequest() { }; RpcRoot.PostAsync(NTMinerRegistry.GetControlCenterHost(), NTKeyword.ControlCenterPort, _controllerName, nameof(IControlCenterController.LoginControlCenter), request, request, callback); }
public void AddOrUpdateMinerGroupAsync(MinerGroupData entity, Action <ResponseBase, Exception> callback) { entity.ModifiedOn = DateTime.Now; DataRequest <MinerGroupData> request = new DataRequest <MinerGroupData> { Data = entity }; RpcRoot.PostAsync(NTMinerRegistry.GetControlCenterHost(), NTKeyword.ControlCenterPort, _controllerName, nameof(IMinerGroupController.AddOrUpdateMinerGroup), request, request, callback); }
public void TestMethod1() { using (HttpClient client = new HttpClient()) { Assert.AreEqual(100, client.Timeout.TotalSeconds); } using (HttpClient client = RpcRoot.Create()) { Assert.AreEqual(60, client.Timeout.TotalSeconds); } }
public void TaskTest() { HttpClient client = RpcRoot.Create(); client.GetAsync($"http://{NTKeyword.OfficialServerHost}:{NTKeyword.ControlCenterPort.ToString()}/api/AppSetting/GetTime") .ContinueWith(t => { Console.WriteLine(t.Result.Content.ReadAsAsync <DateTime>().Result); }).Wait(); }
public void AddOrUpdateServerMessageAsync(ServerMessageData entity, Action <ResponseBase, Exception> callback) { DataRequest <ServerMessageData> request = new DataRequest <ServerMessageData>() { Data = entity }; RpcRoot.SignPostAsync(RpcRoot.OfficialServerHost, RpcRoot.OfficialServerPort, _controllerName, nameof(IServerMessageController.AddOrUpdateServerMessage), data: request, callback); }
public void MarkDeleteServerMessageAsync(Guid id, Action <ResponseBase, Exception> callback) { DataRequest <Guid> request = new DataRequest <Guid>() { Data = id }; RpcRoot.SignPostAsync(RpcRoot.OfficialServerHost, RpcRoot.OfficialServerPort, _controllerName, nameof(IServerMessageController.MarkDeleteServerMessage), data: request, callback); }