DirectXForm1.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using DevExpress.CodeParser;
  2. using Ips.Library.Basic;
  3. using Ips.Library.DxpLib;
  4. using Ips.Library.Entity;
  5. using Ips.Library.WebApi;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Reflection;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Forms;
  18. namespace Ips.Service.GpuServer
  19. {
  20. public partial class DirectXForm1 : DevExpress.XtraEditors.DirectXForm
  21. {
  22. private bool apiRuning = false;
  23. private CancellationTokenSource cts;
  24. public DirectXForm1()
  25. {
  26. InitializeComponent();
  27. this.Size = new Size(1024, 768);
  28. this.Text = "GPU服务";
  29. }
  30. protected override void OnLoad(EventArgs e)
  31. {
  32. base.OnLoad(e);
  33. new Library.DxpLib.LogCtrl() { Parent = groupControl1 };
  34. LoadParams();
  35. }
  36. private async void RegistrySvr(CancellationToken token)
  37. {
  38. while (!this.Disposing && apiRuning)
  39. {
  40. string getIpUrl = null;
  41. try
  42. {
  43. getIpUrl = AppConst.RegistryUri.AppendUrlSuffix("api/getclientip");
  44. var rsp = await HttpHelper.GetRequestAsync<string>(getIpUrl);
  45. if (rsp.code == 0)
  46. {
  47. IpsLogger.Error(rsp.msg);
  48. return;
  49. }
  50. else
  51. {
  52. AppConst.LocalIp = rsp.data;
  53. }
  54. break;
  55. }
  56. catch (Exception ex)
  57. {
  58. IpsLogger.Error($"无法注册到{getIpUrl},请检测地址是否正确并确保远程设备设备防火墙入栈规则允许TCP端口{getIpUrl.GetUrlPort()}通过", ex);
  59. await Task.Delay(10000, token);
  60. }
  61. }
  62. bool preSucceed = false;
  63. while (!this.Disposing && apiRuning)
  64. {
  65. string url = AppConst.RegistryUri.AppendUrlSuffix("api/SvrStateRpt/Report");
  66. try
  67. {
  68. var dto = new ModelSvrRpt()
  69. {
  70. SvrType = EnumSvrType.GpuSvr,
  71. SvrNo = AppConst.SvrNo,
  72. SvrRptType = EnumSvrRptType.Online,
  73. BaseHttpAddr = $"http://{AppConst.LocalIp}:{AppConst.LocalPort}",
  74. SwaggerAddr = $"http://{AppConst.LocalIp}:{AppConst.LocalPort}/Swagger",
  75. };
  76. List<string> list = new List<string>();
  77. if (AppConst.UseCg)
  78. {
  79. list.Add("参数估计");
  80. }
  81. if (AppConst.UseTpdx)
  82. {
  83. list.Add("同频对消");
  84. }
  85. dto.Features = string.Join(" ", list);
  86. var res = await HttpHelper.PostRequestAsync<object>(url, dto);
  87. if (res.code != 200)
  88. {
  89. IpsLogger.Error($"服务注册异常[url={url}].{res.msg}");
  90. preSucceed = false;
  91. }
  92. else
  93. {
  94. if (!preSucceed)
  95. IpsLogger.Info($"服务注册成功![url={url}]");
  96. preSucceed = true;
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. IpsLogger.Error($"服务注册异常[url={url}]", ex);
  102. preSucceed = false;
  103. }
  104. await Task.Delay(10000, token);
  105. }
  106. }
  107. private void BtnShowApi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  108. {
  109. string addr = $"http://127.0.0.1:{AppConst.LocalPort}/swagger";
  110. try
  111. {
  112. //System.Diagnostics.Process.Start(addr);//NetFramework可用,NetCore使用CMD启动
  113. ProcessStartInfo info = new ProcessStartInfo();
  114. info.Arguments = $"/C start {addr}";
  115. info.FileName = "cmd";
  116. info.UseShellExecute = false;
  117. info.CreateNoWindow = true;
  118. Process.Start(info);
  119. }
  120. catch (Exception ex)
  121. {
  122. IpsLogger.Error($"无法打开默认浏览器,请手动打开浏览器查看接口文档.地址{addr}", ex);
  123. }
  124. }
  125. private void btnStart_Click(object sender, EventArgs e)
  126. {
  127. try
  128. {
  129. if (btnStart.Text == "启动服务")
  130. {
  131. if (!int.TryParse(txtLocalPort.Text.Trim(), out int localPort))
  132. {
  133. throw new Exception("本地端口输入错误");
  134. }
  135. if (localPort < 1024 || localPort > 49151)
  136. {
  137. //1024以下给vip程序预留的,49152-65535用于客户端随机分配的
  138. throw new Exception("本地端口范围[1024,49151]");
  139. }
  140. if (txtSvrNo.Text.Trim().Length < 3 || txtSvrNo.Text.Trim().Length > 15)
  141. {
  142. throw new Exception("服务编号长度范围[3,15]");
  143. }
  144. try
  145. {
  146. var u = new Uri(txtRegistryAddr.Text.Trim());
  147. }
  148. catch
  149. {
  150. throw new Exception("服务注册地址不是有效的URI");
  151. }
  152. AppConst.LocalPort = localPort;
  153. AppConst.SvrNo = txtSvrNo.Text.Trim();
  154. AppConst.RegistryUri = txtRegistryAddr.Text.Trim();
  155. AppConst.UseTpdx = txtUseTpdx.Checked;
  156. AppConst.UseCg = txtUseCg.Checked;
  157. WebApiHelper.Start(AppConst.LocalPort, "Ips.Library.Entity.xml");
  158. btnStart.Text = "停止服务";
  159. stackPanel1.DisabledChildWithout(btnStart);
  160. IpsLogger.Info($"服务启动成功.接口地址http://+:{localPort}/swagger");
  161. IpsLogger.Info($"服务启动成功.静态文件预览地址http://+:{localPort}/upload");
  162. IpsLogger.Info($"服务启动成功.静态文件预览地址http://+:{localPort}/download");
  163. IpsLogger.Info($"服务启动成功.静态文件预览地址http://+:{localPort}/logs");
  164. apiRuning = true;
  165. cts = new CancellationTokenSource();
  166. RegistrySvr(cts.Token);
  167. }
  168. else
  169. {
  170. apiRuning = false;
  171. btnStart.Text = "启动服务";
  172. WebApiHelper.Stop();
  173. cts?.Cancel();
  174. stackPanel1.EnableChild();
  175. }
  176. SaveParams();
  177. }
  178. catch (Exception ex)
  179. {
  180. IpsLogger.Error(ex.Message, ex);
  181. }
  182. }
  183. void SaveParams()
  184. {
  185. try
  186. {
  187. Directory.CreateDirectory("Params");
  188. StringBuilder sb = new StringBuilder();
  189. sb.AppendLine(txtLocalPort.Text);
  190. sb.AppendLine(txtSvrNo.Text);
  191. sb.AppendLine(txtRegistryAddr.Text);
  192. sb.AppendLine(txtUseTpdx.Checked.ToString());
  193. sb.AppendLine(txtUseCg.Checked.ToString());
  194. sb.AppendLine(btnStart.Text);
  195. File.WriteAllText("Params\\Params.txt", sb.ToString());
  196. }
  197. catch (Exception ex)
  198. {
  199. IpsLogger.Error("保存Params.txt文件异常", ex);
  200. }
  201. }
  202. void LoadParams()
  203. {
  204. if (File.Exists("Params\\Params.txt"))
  205. {
  206. try
  207. {
  208. var lines = File.ReadAllLines("Params\\Params.txt");
  209. txtLocalPort.Text = lines[0];
  210. txtSvrNo.Text = lines[1];
  211. txtRegistryAddr.Text = lines[2];
  212. txtUseTpdx.Checked = lines[3].ToLower() == "true";
  213. txtUseCg.Checked = lines[4].ToLower() == "true";
  214. if (lines[5] == "停止服务")//上次状态是启动状态,加载后直接启动
  215. {
  216. btnStart_Click(null, null);
  217. }
  218. }
  219. catch (Exception ex)
  220. {
  221. IpsLogger.Error("加载Params.txt文件异常", ex);
  222. }
  223. }
  224. }
  225. }
  226. }