예제 #1
0
 public ResponseBase StopMine([FromBody] SignRequest request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         Logger.InfoDebugLine("停止挖矿");
         ResponseBase response;
         if (!IsNTMinerOpened())
         {
             return(ResponseBase.Ok());
         }
         try {
             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);
             }
         }
         catch (Exception e) {
             Logger.ErrorDebugLine(e);
         }
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
예제 #2
0
        private void CloseNTMiner()
        {
            Logger.InfoDebugLine("退出挖矿端");
            bool isClosed = false;

            try {
                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();
                }
            }
            catch (Exception e) {
                Logger.ErrorDebugLine(e);
            }
            if (!isClosed)
            {
                try {
                    string location = NTMinerRegistry.GetLocation();
                    if (!string.IsNullOrEmpty(location) && File.Exists(location))
                    {
                        string processName = Path.GetFileNameWithoutExtension(location);
                        Windows.TaskKill.Kill(processName);
                    }
                }
                catch (Exception e) {
                    Logger.ErrorDebugLine(e);
                }
            }
        }
예제 #3
0
 /// <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 static 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()) {
                 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($"http://{host}:{port.ToString()}/api/{controller}/{action}{query.ToQueryString()}", content);
                 getHttpResponse.Result.Content.ReadAsAsync <TResponse>().ContinueWith(t => {
                     callback?.Invoke(t.Result, null);
                 });
             }
         }
         catch (Exception e) {
             callback?.Invoke(default, e);
예제 #4
0
 /// <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 static 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()) {
                 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 NTMinerException($"{action} http response {getHttpResponse.Result.StatusCode.ToString()} {getHttpResponse.Result.ReasonPhrase}"));
예제 #5
0
 /// <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 static 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()) {
                 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();
         }
     });
 }
예제 #6
0
        public void TaskTest()
        {
            HttpClient client = RpcRoot.CreateHttpClient();

            client.GetAsync($"http://{RpcRoot.OfficialServerAddress}/api/{RpcRoot.GetControllerName<IAppSettingController>()}/{nameof(IAppSettingController.GetTime)}")
            .ContinueWith(t => {
                Console.WriteLine(t.Result.Content.ReadAsAsync <DateTime>().Result);
            }).Wait();
        }
예제 #7
0
 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);
     }
 }
예제 #8
0
 public void SetAutoBootStart([FromUri] bool autoBoot, [FromUri] bool autoStart)
 {
     Logger.InfoDebugLine($"开机启动{(autoBoot ? "√" : "×")},自动挖矿{(autoStart ? "√" : "×")}");
     MinerProfileUtil.SetAutoStart(autoBoot, autoStart);
     if (IsNTMinerOpened())
     {
         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}");
         }
     }
 }
예제 #9
0
 private static async Task <byte[]> GetHtmlAsync(string url)
 {
     try {
         using (HttpClient client = RpcRoot.CreateHttpClient()) {
             client.Timeout = TimeSpan.FromSeconds(20);
             return(await client.GetByteArrayAsync(url));
         }
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(new byte[0]);
     }
 }
예제 #10
0
 public void TestMethod1()
 {
     using (HttpClient client = new HttpClient()) {
         Assert.AreEqual(100, client.Timeout.TotalSeconds);
     }
     using (HttpClient client = RpcRoot.CreateHttpClient()) {
         Assert.AreEqual(10, client.Timeout.TotalSeconds);
         client.SetTimeout(5000);
         Assert.AreEqual(5000, client.Timeout.TotalMilliseconds);
     }
     using (HttpClient client = RpcRoot.CreateHttpClient()) {
         // 0不起作用
         client.SetTimeout(0);
         Assert.AreEqual(10, client.Timeout.TotalSeconds);
     }
 }
예제 #11
0
 public ResponseBase StartMine([FromBody] WorkRequest request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         Logger.InfoDebugLine("开始挖矿");
         ResponseBase response;
         if (request.WorkId != Guid.Empty)
         {
             File.WriteAllText(SpecialPath.NTMinerLocalJsonFileFullName, request.LocalJson);
             File.WriteAllText(SpecialPath.NTMinerServerJsonFileFullName, request.ServerJson);
         }
         string location = NTMinerRegistry.GetLocation();
         if (IsNTMinerOpened())
         {
             using (HttpClient client = RpcRoot.CreateHttpClient()) {
                 WorkRequest innerRequest = new WorkRequest {
                     WorkId = request.WorkId
                 };
                 Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://localhost:{NTKeyword.MinerClientPort.ToString()}/api/MinerClient/StartMine", innerRequest);
                 response = getHttpResponse.Result.Content.ReadAsAsync <ResponseBase>().Result;
                 return(response);
             }
         }
         else
         {
             if (!string.IsNullOrEmpty(location) && File.Exists(location))
             {
                 string arguments = NTKeyword.AutoStartCmdParameterName;
                 if (request.WorkId != Guid.Empty)
                 {
                     arguments += " --work";
                 }
                 Windows.Cmd.RunClose(location, arguments);
                 return(ResponseBase.Ok());
             }
             return(ResponseBase.ServerError("挖矿端程序不存在"));
         }
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError(e.Message));
     }
 }
예제 #12
0
 public void SaveGpuProfilesJson()
 {
     try {
         Logger.InfoDebugLine("保存显卡参数");
         string json = Request.Content.ReadAsStringAsync().Result;
         SpecialPath.SaveGpuProfilesJsonFile(json);
         if (IsNTMinerOpened())
         {
             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}");
             }
         }
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
     }
 }
예제 #13
0
 /// <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 static 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()) {
                 client.SetTimeout(timeountMilliseconds);
                 Task <HttpResponseMessage> message = client.GetAsync($"http://{host}:{port.ToString()}/api/{controller}/{action}{query.ToQueryString()}");
                 message.Result.Content.ReadAsAsync <TResponse>().ContinueWith(t => {
                     callback?.Invoke(t.Result, null);
                 });
             }
         }
         catch (Exception e) {
             callback?.Invoke(default, e);
예제 #14
0
 /// <summary>
 /// 注意:Response时ReadAsByteArrayAsync后进行二进制反序列化。
 /// </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 static 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()) {
                 client.SetTimeout(timeountMilliseconds);
                 Task <HttpResponseMessage> message = client.GetAsync(RpcRoot.GetUrl(host, port, controller, action, query));
                 message.Result.Content.ReadAsByteArrayAsync().ContinueWith(t => {
                     callback?.Invoke(VirtualRoot.BinarySerializer.Deserialize <TResponse>(t.Result), null);
                 });
             }
         }
         catch (Exception e) {
             callback?.Invoke(default, e);
예제 #15
0
 /// <summary>
 /// 异步Post
 /// </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">post的数据</param>
 /// <param name="callback"></param>
 /// <param name="timeountMilliseconds"></param>
 public static 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()) {
                 client.SetTimeout(timeountMilliseconds);
                 Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync(RpcRoot.GetUrl(host, port, controller, action, query), data);
                 getHttpResponse.Result.Content.ReadAsAsync <TResponse>().ContinueWith(t => {
                     callback?.Invoke(t.Result, null);
                 });
             }
         }
         catch (Exception e) {
             callback?.Invoke(default, e);
예제 #16
0
 /// <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 static 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()) {
                 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}"));
예제 #17
0
 /// <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 static 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()) {
                 client.SetTimeout(timeountMilliseconds);
                 Task <HttpResponseMessage> getHttpResponse = client.PostAsync($"http://{host}:{port.ToString()}/api/{controller}/{action}{query.ToQueryString()}", content);
                 NTMinerConsole.DevDebug($"{action} {getHttpResponse.Result.ReasonPhrase}");
                 callback?.Invoke();
             }
         }
         catch {
             callback?.Invoke();
         }
     });
 }