Form1.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.OleDb;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Runtime.Serialization.Json;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. using GWSocketClient.db;
  16. using GWSocketClient.model;
  17. using GWSocketClient.util;
  18. using GWSocketClient.xml;
  19. using Newtonsoft.Json;
  20. using Newtonsoft.Json.Linq;
  21. using XuanJiSocket;
  22. namespace GWSocketClient
  23. {
  24. public partial class Form1 : Form
  25. {
  26. public Form1()
  27. {
  28. InitializeComponent();
  29. }
  30. private string ip;
  31. private string port;
  32. private SocketHelper.TcpClients sockeTcpClients;
  33. private VcarecityPD vcarecityProtocolBean = null;
  34. private void Form1_Load(object sender, EventArgs e)
  35. {
  36. XmlLoader xmlLoader = new XmlLoader();
  37. vcarecityProtocolBean = xmlLoader.loadXml();
  38. if (vcarecityProtocolBean != null)
  39. {
  40. this.Text = vcarecityProtocolBean.ProtocolName + " - " + vcarecityProtocolBean.ProtocolNo + " - " +
  41. vcarecityProtocolBean.ProtocolStartSymbol;
  42. }
  43. SocketHelper.pushSockets = new SocketHelper.PushSockets(receiveData); //注册推送器
  44. sockeTcpClients = new SocketHelper.TcpClients();
  45. ip = tbIpaddress.Text.Trim();
  46. port = tbPort.Text.Trim();
  47. }
  48. private bool showTopForNoLoadXML()
  49. {
  50. if (vcarecityProtocolBean == null)
  51. {
  52. MessageBox.Show("请先加载XML协议文件");
  53. return false;
  54. }
  55. return true;
  56. }
  57. private void receiveData(SocketHelper.Sockets sks)
  58. {
  59. this.Invoke(new ThreadStart(delegate
  60. {
  61. if (sks.ex != null)
  62. {
  63. switch (sks.ErrorCode)
  64. {
  65. case SocketHelper.Sockets.ErrorCodes.objectNull:
  66. break;
  67. case SocketHelper.Sockets.ErrorCodes.ConnectError:
  68. break;
  69. case SocketHelper.Sockets.ErrorCodes.ConnectSuccess:
  70. addToListboxItem("状态信息:连接成功.!");
  71. break;
  72. case SocketHelper.Sockets.ErrorCodes.TrySendData:
  73. break;
  74. default:
  75. break;
  76. }
  77. addToListboxItem(string.Format("客户端信息{0}", sks.ex));
  78. }
  79. else
  80. {
  81. byte[] buffer = new byte[sks.Offset];
  82. Array.Copy(sks.RecBuffer, buffer, sks.Offset);
  83. string byteToHexStr = HexUtil.byteToHexStr(buffer);
  84. if (byteToHexStr.Equals("ServerOff"))
  85. {
  86. addToListboxItem("状态信息:服务端主动关闭!");
  87. }
  88. else
  89. {
  90. if (!byteToHexStr.Equals(""))
  91. {
  92. addToListboxItem(string.Format("收到服务端{0}发来消息:[{1}]", sks.Ip, byteToHexStr));
  93. handleResponse(sks, byteToHexStr);
  94. }
  95. else
  96. {
  97. addToListboxItem("状态信息:服务端主动关闭!");
  98. sockeTcpClients.Stop();
  99. }
  100. // listBoxShow.Text += "\r\n";
  101. // listBoxShow.Text += string.Format("服务端{0}发来消息:{1}", sks.Ip, byteToHexStr);
  102. }
  103. }
  104. }));
  105. }
  106. //处理回复信息
  107. private void handleResponse(SocketHelper.Sockets sks, string reqData)
  108. {
  109. string resp = tfTmpResponse.Text.Trim();
  110. if (resp.Equals(""))
  111. {
  112. resp = "00";
  113. }
  114. string functionWord = reqData.Substring(18, 2);
  115. int funcode = Utils.hexToTen(functionWord);
  116. //16 表示要读数据,返回数据库中的数据
  117. //32 表示返回结果 成功:00 失败:01
  118. //数据长度
  119. int dataLen = Utils.hexToTen(reqData.Substring(20, 4));
  120. string readlData = "";
  121. try
  122. {
  123. readlData = reqData.Substring(24, dataLen);
  124. }
  125. catch (Exception e)
  126. {
  127. Console.WriteLine(e.Message);
  128. addToListboxItem("数据长度与帧长度对不上!!");
  129. return;
  130. }
  131. string myId = readlData.Substring(6, 4);
  132. string idInfo = readlData.Substring(10);
  133. if (funcode == 32)
  134. {
  135. //设置数据
  136. int res = AccsessDbLoader.getInstance().setReceiveData(myId, idInfo);
  137. if (res > 0)
  138. {
  139. //00
  140. }
  141. else
  142. {
  143. //01
  144. }
  145. }
  146. else if (funcode == 16)
  147. {
  148. //返回数据库的数据
  149. }
  150. if (funcode == 16 || funcode == 32)
  151. {
  152. funcode = funcode + 128;
  153. functionWord = Utils.stringWith0(Utils.tenToHex(funcode), 2);
  154. string cmd = reqData.Substring(0, 18) + functionWord;
  155. string data = reqData.Substring(24, 10) + resp;
  156. cmd = cmd + Utils.stringWith0(Utils.tenToHex(data.Length / 2), 4) + data;
  157. cmd = Utils.calcCrcCode(cmd);
  158. addToListboxItem(string.Format("回复服务端{0}回应消息:[{1}]", sks.Ip, cmd));
  159. byte[] ts = HexUtil.hexToBytes(cmd);
  160. sockeTcpClients.SendData(ts);
  161. }
  162. }
  163. private void tbEquipmentAddress_TextChanged(object sender, EventArgs e)
  164. {
  165. string equipmentAddress = tbEquipmentAddress.Text.Trim();
  166. tsslEquipmentAddress.Text = equipmentAddress.Length / 2.0 + "";
  167. }
  168. private void btnLogin_Click(object sender, EventArgs e)
  169. {
  170. try
  171. {
  172. if (!showTopForNoLoadXML())
  173. return;
  174. ip = tbIpaddress.Text.Trim();
  175. port = tbPort.Text.Trim();
  176. sockeTcpClients.InitSocket(ip, int.Parse(port));
  177. sockeTcpClients.Start();
  178. // string loginCmd = "7e7e7e4442832e833c8200143839383630324231313931353530343739383737f7";
  179. string loginCmd = vcarecityProtocolBean.ProtocolStartSymbol;
  180. loginCmd += tbEquipmentAddress.Text.Trim() + "82" +
  181. Utils.stringWith0(Utils.tenToHex(tbCCID.Text.Trim().Length / 2), 4);
  182. loginCmd += tbCCID.Text.Trim();
  183. loginCmd = Utils.calcCrcCode(loginCmd);
  184. //发送登录命令
  185. byte[] ts = HexUtil.hexToBytes(loginCmd);
  186. addToListboxItem(string.Format("发送到服务端:[{0}]", loginCmd));
  187. sockeTcpClients.SendData(ts);
  188. }
  189. catch (Exception ex)
  190. {
  191. // statuslist.Items.Add(string.Format("连接失败!原因:{0}", ex.Message
  192. Console.WriteLine(ex.Message);
  193. }
  194. }
  195. private void tbCCID_TextChanged(object sender, EventArgs e)
  196. {
  197. string ccid = tbCCID.Text.Trim();
  198. tsslCCIDLEN.Text = ccid.Length / 2.0 + "";
  199. }
  200. private void btnQuickLogout_Click(object sender, EventArgs e)
  201. {
  202. try
  203. {
  204. sockeTcpClients.Stop();
  205. }
  206. catch (Exception exception)
  207. {
  208. addToListboxItem("你还没有登录!!" + exception.Message);
  209. }
  210. }
  211. private void tsmiLoadXml_Click(object sender, EventArgs e)
  212. {
  213. OpenFileDialog ofd = new OpenFileDialog();
  214. ofd.Filter = "XML文件|*.xml|所有文件|*.*";
  215. if (ofd.ShowDialog() == DialogResult.OK)
  216. {
  217. string xmlFilepath = ofd.FileName;
  218. vcarecityProtocolBean = new XmlLoader().load(xmlFilepath).getVcarecityPD();
  219. }
  220. }
  221. private void btnSendData_Click(object sender, EventArgs e)
  222. {
  223. string trim = tfTmpSendData.Text.Trim();
  224. trim = trim.Replace("\n", "").Replace("\r", "").Replace("\t", "").Replace(" ", "").Replace("-", "");
  225. if (!trim.Equals(""))
  226. {
  227. byte[] ts = HexUtil.hexToBytes(trim);
  228. addToListboxItem(string.Format("发送到服务端:[{0}]", trim));
  229. sockeTcpClients.SendData(ts);
  230. }
  231. }
  232. private void rightMenuCopyCmd_Click(object sender, EventArgs e)
  233. {
  234. int index = listBoxShow.SelectedIndex;
  235. if (index >= 0 && index < listBoxShow.Items.Count)
  236. {
  237. string info = listBoxShow.SelectedItem.ToString();
  238. if (!info.Equals(""))
  239. {
  240. if (info.Contains("[") || info.Contains("]"))
  241. {
  242. Regex reg = new Regex(@"\[\w+\]"); //注意里面的引号 要用双引号表示,而不是用反斜
  243. Match match = reg.Match(info);
  244. string ifs = match.ToString();
  245. ifs = ifs.Replace("[", "").Replace("]", "");
  246. if (!info.Equals(""))
  247. {
  248. Clipboard.SetDataObject(ifs);
  249. }
  250. else
  251. {
  252. Clipboard.SetDataObject(info);
  253. }
  254. }
  255. else
  256. {
  257. Clipboard.SetDataObject(info);
  258. }
  259. }
  260. }
  261. }
  262. private void addToListboxItem(object info)
  263. {
  264. if (listBoxShow.Items.Count > 1000)
  265. {
  266. listBoxShow.Items.Remove(0);
  267. // listBoxShow.Items.Clear();
  268. }
  269. Console.WriteLine(info);
  270. listBoxShow.Items.Add(info);
  271. listBoxShow.TopIndex = listBoxShow.Items.Count - 1;
  272. }
  273. private void rightMenuClearInfo_Click(object sender, EventArgs e)
  274. {
  275. listBoxShow.Items.Clear();
  276. }
  277. private void listBoxShow_DrawItem(object sender, DrawItemEventArgs e)
  278. {
  279. if (e.Index >= 0 && e.Index < listBoxShow.Items.Count)
  280. {
  281. e.DrawBackground();
  282. //
  283. string res = listBoxShow.Items[e.Index].ToString().ToLower();
  284. if (res.Contains("收到服务端"))
  285. {
  286. Font f = new Font("微软雅黑", e.Font.Size, FontStyle.Bold);
  287. e.Graphics.DrawString(((ListBox) sender).Items[e.Index].ToString(), f, new SolidBrush(Color.Blue),
  288. e.Bounds);
  289. }
  290. else if (res.Contains("发送到服务端") || res.Contains("回复"))
  291. {
  292. Font f = new Font("微软雅黑", e.Font.Size, FontStyle.Italic);
  293. e.Graphics.DrawString(((ListBox) sender).Items[e.Index].ToString(), f,
  294. new SolidBrush(Color.BlueViolet),
  295. e.Bounds);
  296. }
  297. else
  298. {
  299. e.Graphics.DrawString(((ListBox) sender).Items[e.Index].ToString(), e.Font,
  300. new SolidBrush(e.ForeColor),
  301. e.Bounds);
  302. }
  303. e.DrawFocusRectangle();
  304. }
  305. }
  306. /// <summary>
  307. /// 压力测试
  308. /// </summary>
  309. private void btnPressureTest_Click(object sender, EventArgs e)
  310. {
  311. ip = tbIpaddress.Text.Trim();
  312. port = tbPort.Text.Trim();
  313. int count = int.Parse(tbPresureTestCount.Text);
  314. ThreadPool.QueueUserWorkItem(o =>
  315. {
  316. for (int i = 1024; i < 1024 + count; i++)
  317. {
  318. SocketHelper.TcpClients clientx = new SocketHelper.TcpClients(); //初始化类库
  319. clientx.InitSocket(ip, int.Parse(port));
  320. clientx.Start();
  321. string loginCmd = "7e7e7e";
  322. loginCmd += "4442832e" + i;
  323. loginCmd += "820014383938363032423131393135353034373938" + i;
  324. loginCmd = Utils.calcCrcCode(loginCmd);
  325. sockeTcpClients.SendData(HexUtil.hexToBytes(loginCmd));
  326. Thread.Sleep(200);
  327. }
  328. });
  329. }
  330. private void listBoxShow_DoubleClick(object sender, EventArgs e)
  331. {
  332. int index = listBoxShow.SelectedIndex;
  333. if (index >= 0 && index < listBoxShow.Items.Count)
  334. {
  335. string info = listBoxShow.SelectedItem.ToString();
  336. if (!info.Equals(""))
  337. {
  338. if (info.Contains("[") || info.Contains("]"))
  339. {
  340. Regex reg = new Regex(@"\[\w+\]"); //注意里面的引号 要用双引号表示,而不是用反斜
  341. Match match = reg.Match(info);
  342. string ifs = match.ToString();
  343. ifs = ifs.Replace("[", "").Replace("]", "");
  344. if (!info.Equals(""))
  345. {
  346. MessageBox.Show(ifs);
  347. }
  348. }
  349. }
  350. }
  351. }
  352. private void buttonCmdManager_Click(object sender, EventArgs e)
  353. {
  354. InfoEditor ie = new InfoEditor();
  355. ie.ShowDialog();
  356. }
  357. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  358. {
  359. AccsessDbLoader.getInstance().closeConnection();
  360. }
  361. private void btnTextCmd_Click(object sender, EventArgs e)
  362. {
  363. try
  364. {
  365. Dictionary<string, string> abc = AccsessDbLoader.getInstance().getUserIdInfo();
  366. MessageBox.Show(abc.Count + "");
  367. string ac = abc["0400"];
  368. MessageBox.Show(ac);
  369. }
  370. catch (Exception exception)
  371. {
  372. Console.WriteLine(exception);
  373. }
  374. }
  375. }
  376. }