MainForm.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using DotNettyFrom.common;
  12. using DotNettyFrom.config;
  13. using DotNettyFrom.dialog;
  14. using DotNettyFrom.netty;
  15. namespace DotNettyFrom
  16. {
  17. public partial class MainForm : Form
  18. {
  19. #region 属性
  20. private NettyClient _nettyClient;
  21. private string host;
  22. private int port;
  23. #endregion
  24. public MainForm()
  25. {
  26. InitializeComponent();
  27. }
  28. private void MainForm_Load(object sender, EventArgs e)
  29. {
  30. updateUIComponent();
  31. var consumer = Task.Factory.StartNew(() =>
  32. {
  33. foreach (UIInfoModel value in DataModel.ReceCollection.GetConsumingEnumerable())
  34. {
  35. Invoke(new ThreadStart(delegate { CallUI(value); }));
  36. }
  37. });
  38. }
  39. private void CallUI(UIInfoModel value)
  40. {
  41. LBLogShow.Items.Add(value);
  42. switch (value.action)
  43. {
  44. case -1: //连接不上,服务器没开启
  45. break;
  46. case 1: //连接成功.ChannelActive
  47. ConnectionActive();
  48. break;
  49. case 2: //连接断开 ChannelInactive
  50. ConnectionInActive();
  51. break;
  52. case 3: //异常. ExceptionCaught
  53. break;
  54. }
  55. }
  56. private void ConnectionActive()
  57. {
  58. TbHost.Enabled = false;
  59. TbPort.Enabled = false;
  60. TbDeviceId.Enabled = false;
  61. TbCCID.Enabled = false;
  62. BtnConnection.Enabled = false;
  63. BtnLogin.Enabled = false;
  64. BtnDisConn.Enabled = true;
  65. BtnSendData.Enabled = true;
  66. }
  67. private void ConnectionInActive()
  68. {
  69. TbHost.Enabled = true;
  70. TbPort.Enabled = true;
  71. TbDeviceId.Enabled = true;
  72. TbCCID.Enabled = true;
  73. BtnConnection.Enabled = true;
  74. BtnLogin.Enabled = true;
  75. BtnDisConn.Enabled = false;
  76. BtnSendData.Enabled = false;
  77. }
  78. private void beforeConnection()
  79. {
  80. this.host = TbHost.Text.Trim();
  81. this.port = int.Parse(TbPort.Text.Trim());
  82. updateUIComponent();
  83. }
  84. private void updateUIComponent()
  85. {
  86. StatusItemHbTime.Text = "R:" + SystemConfig.ReaderIdleTimeSeconds + "/s,W:" +
  87. SystemConfig.WriterIdleTimeSeconds + "/s,A:" + SystemConfig.AllIdleTimeSeconds +
  88. "/s";
  89. }
  90. #region Component Event
  91. private void BtnConnection_Click(object sender, EventArgs e)
  92. {
  93. /* var producer = Task.Factory.StartNew(() =>
  94. {
  95. for (int count = 0; count < 1000; count++)
  96. {
  97. }
  98. // blockingCollection.CompleteAdding();
  99. });*/
  100. /*_nettyClient = new NettyClient();
  101. t = new Thread(() => { _nettyClient.StartNetty("127.0.0.1", 4001); });
  102. t.Start();*/
  103. beforeConnection();
  104. Task startNew = Task.Factory.StartNew(() =>
  105. {
  106. _nettyClient = new NettyClient();
  107. _nettyClient.StartNetty(host, port);
  108. });
  109. }
  110. private void BtnLogin_Click(object sender, EventArgs e)
  111. {
  112. beforeConnection();
  113. Task startNew = Task.Factory.StartNew(() =>
  114. {
  115. _nettyClient = new NettyClient();
  116. _nettyClient.StartNetty(host, port);
  117. });
  118. }
  119. private void BtnDisConn_Click(object sender, EventArgs e)
  120. {
  121. _nettyClient?.StopNetty();
  122. //t?.Abort();
  123. }
  124. private void BtnSendData_Click(object sender, EventArgs e)
  125. {
  126. string cmd = TbSendData.Text.Trim();
  127. _nettyClient?.SendData(cmd);
  128. }
  129. private void RT_HB_Time_Click(object sender, EventArgs e)
  130. {
  131. HeartBeatTime hbt = new HeartBeatTime();
  132. hbt.ShowDialog();
  133. }
  134. private void TbPort_KeyPress(object sender, KeyPressEventArgs e)
  135. {
  136. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
  137. {
  138. e.Handled = true;
  139. }
  140. }
  141. #endregion
  142. }
  143. }