Form1.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using XdCxRhDW.Dto;
  15. namespace XdCxRhDW.Sender
  16. {
  17. public partial class Form1 : DevExpress.XtraEditors.XtraForm
  18. {
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23. CancellationTokenSource cts1;
  24. TcpClient client1;
  25. private async void btn1_Click(object sender, EventArgs e)
  26. {
  27. layoutControlItem1.Enabled = false;
  28. layoutControlItem7.Enabled = false;
  29. if (btn1.Text == "推送")
  30. {
  31. try
  32. {
  33. cts1 = new CancellationTokenSource();
  34. btn1.Text = "停止";
  35. var addrArr = txtAddr1.Text.Trim().Replace(":", ":").Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  36. string ip = addrArr[0];
  37. int port = Convert.ToInt32(addrArr[1]);
  38. S:
  39. if (string.IsNullOrWhiteSpace(localPort1.Text))
  40. client1 = new TcpClient();
  41. else
  42. client1 = new TcpClient(new IPEndPoint(IPAddress.Any, Convert.ToInt32(localPort1.Text)));
  43. client1.NoDelay = true;
  44. client1.SendTimeout = 5000;
  45. client1.ReceiveTimeout = 5000;
  46. while (!cts1.IsCancellationRequested)
  47. {
  48. try
  49. {
  50. await client1.ConnectAsync(IPAddress.Parse(ip), port);
  51. break;
  52. }
  53. catch (Exception ex)
  54. {
  55. Log(ex);
  56. await Task.Delay(5000, cts1.Token);
  57. }
  58. }
  59. Log($"已成功连接到[{txtAddr1.Text}]");
  60. var lines = File.ReadAllLines("Simulation_Data2023.dat");
  61. while (!cts1.IsCancellationRequested)
  62. {
  63. try
  64. {
  65. int idx = 1;
  66. foreach (var line in lines)
  67. {
  68. if (cts1.IsCancellationRequested) break;
  69. if (string.IsNullOrWhiteSpace(line)) continue;
  70. var items = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  71. SendDto dto = new SendDto()
  72. {
  73. SxDto = Convert.ToDouble(items[0]),
  74. XdDto = Convert.ToDouble(items[1]),
  75. MainYbDto = Convert.ToDouble(items[2]),
  76. AdjaYbDto = Convert.ToDouble(items[3]),
  77. CxRes = Convert.ToDouble(items[4]),
  78. MainX = Convert.ToDouble(items[7]),
  79. MainY = Convert.ToDouble(items[8]),
  80. MainZ = Convert.ToDouble(items[9]),
  81. AdjaX = Convert.ToDouble(items[10]),
  82. AdjaY = Convert.ToDouble(items[11]),
  83. AdjaZ = Convert.ToDouble(items[12]),
  84. };
  85. var msg = Newtonsoft.Json.JsonConvert.SerializeObject(dto);
  86. var data = Encoding.UTF8.GetBytes(msg);
  87. var dataWithHeader = BitConverter.GetBytes(data.Length).Concat(data).ToArray();
  88. await client1.GetStream().WriteAsync(dataWithHeader, 0, dataWithHeader.Length);
  89. Log($"已向[{txtAddr1.Text}]发送第{idx++}条仿真结果");
  90. await Task.Delay(3000, cts1.Token);
  91. }
  92. }
  93. catch (InvalidOperationException)
  94. {
  95. goto S;
  96. }
  97. catch (Exception ex)
  98. {
  99. Log($"向[{txtAddr1.Text}]发送结果失败.{ex.Message}");
  100. await Task.Delay(5000, cts1.Token);
  101. }
  102. }
  103. }
  104. catch (Exception ex)
  105. {
  106. Log(ex);
  107. }
  108. finally
  109. {
  110. btn1.Text = "推送";
  111. layoutControlItem1.Enabled = true;
  112. layoutControlItem7.Enabled = true;
  113. }
  114. }
  115. else
  116. {
  117. cts1?.Cancel();
  118. client1?.Dispose();
  119. }
  120. }
  121. private void Log(string msg)
  122. {
  123. try
  124. {
  125. if (this.InvokeRequired)
  126. {
  127. this.Invoke(new Action(() =>
  128. {
  129. if (listBoxControl1.ItemCount > 5000)
  130. listBoxControl1.Items.Clear();
  131. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{msg}");
  132. listBoxControl1.SelectedIndex = 0;
  133. }));
  134. }
  135. else
  136. {
  137. if (listBoxControl1.ItemCount > 5000)
  138. listBoxControl1.Items.Clear();
  139. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{msg}");
  140. listBoxControl1.SelectedIndex = 0;
  141. }
  142. }
  143. catch
  144. { }
  145. }
  146. private void Log(Exception ex)
  147. {
  148. try
  149. {
  150. if (this.InvokeRequired)
  151. {
  152. this.Invoke(new Action(() =>
  153. {
  154. if (listBoxControl1.ItemCount > 5000)
  155. listBoxControl1.Items.Clear();
  156. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{ex.Message}");
  157. listBoxControl1.SelectedIndex = 0;
  158. }));
  159. }
  160. else
  161. {
  162. if (listBoxControl1.ItemCount > 5000)
  163. listBoxControl1.Items.Clear();
  164. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{ex.Message}");
  165. listBoxControl1.SelectedIndex = 0;
  166. }
  167. }
  168. catch
  169. { }
  170. }
  171. private void listBoxControl1_MouseClick(object sender, MouseEventArgs e)
  172. {
  173. if (e.Button == MouseButtons.Right)
  174. {
  175. popupMenu1.ShowPopup(MousePosition);
  176. }
  177. }
  178. private void btnClear_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  179. {
  180. listBoxControl1.Items.Clear();
  181. }
  182. private void btnCopyAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  183. {
  184. StringBuilder sb = new StringBuilder();
  185. foreach (var item in listBoxControl1.Items)
  186. {
  187. sb.AppendLine(item.ToString());
  188. }
  189. var data = sb.ToString();
  190. if (string.IsNullOrWhiteSpace(data)) return;
  191. Clipboard.SetText(sb.ToString());
  192. }
  193. }
  194. }