123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using DotNettyFrom.common;
- using DotNettyFrom.config;
- using DotNettyFrom.dialog;
- using DotNettyFrom.netty;
-
- namespace DotNettyFrom
- {
- public partial class MainForm : Form
- {
- #region 属性
-
- private NettyClient _nettyClient;
- private string host;
- private int port;
-
- #endregion
-
-
- public MainForm()
- {
- InitializeComponent();
- }
-
- private void MainForm_Load(object sender, EventArgs e)
- {
- updateUIComponent();
-
- var consumer = Task.Factory.StartNew(() =>
- {
- foreach (UIInfoModel value in DataModel.ReceCollection.GetConsumingEnumerable())
- {
- Invoke(new ThreadStart(delegate { CallUI(value); }));
- }
- });
- }
-
-
- private void CallUI(UIInfoModel value)
- {
- LBLogShow.Items.Add(value);
- switch (value.action)
- {
- case -1: //连接不上,服务器没开启
- break;
- case 1: //连接成功.ChannelActive
- ConnectionActive();
- break;
- case 2: //连接断开 ChannelInactive
- ConnectionInActive();
- break;
- case 3: //异常. ExceptionCaught
- break;
- }
- }
-
- private void ConnectionActive()
- {
- TbHost.Enabled = false;
- TbPort.Enabled = false;
- TbDeviceId.Enabled = false;
- TbCCID.Enabled = false;
-
- BtnConnection.Enabled = false;
- BtnLogin.Enabled = false;
-
- BtnDisConn.Enabled = true;
- BtnSendData.Enabled = true;
- }
-
-
- private void ConnectionInActive()
- {
- TbHost.Enabled = true;
- TbPort.Enabled = true;
- TbDeviceId.Enabled = true;
- TbCCID.Enabled = true;
-
- BtnConnection.Enabled = true;
- BtnLogin.Enabled = true;
-
- BtnDisConn.Enabled = false;
- BtnSendData.Enabled = false;
- }
-
- private void beforeConnection()
- {
- this.host = TbHost.Text.Trim();
- this.port = int.Parse(TbPort.Text.Trim());
- updateUIComponent();
- }
-
- private void updateUIComponent()
- {
- StatusItemHbTime.Text = "R:" + SystemConfig.ReaderIdleTimeSeconds + "/s,W:" +
- SystemConfig.WriterIdleTimeSeconds + "/s,A:" + SystemConfig.AllIdleTimeSeconds +
- "/s";
- }
-
- #region Component Event
-
- private void BtnConnection_Click(object sender, EventArgs e)
- {
- /* var producer = Task.Factory.StartNew(() =>
- {
- for (int count = 0; count < 1000; count++)
- {
-
-
- }
- // blockingCollection.CompleteAdding();
- });*/
- /*_nettyClient = new NettyClient();
- t = new Thread(() => { _nettyClient.StartNetty("127.0.0.1", 4001); });
- t.Start();*/
-
- beforeConnection();
-
- Task startNew = Task.Factory.StartNew(() =>
- {
- _nettyClient = new NettyClient();
- _nettyClient.StartNetty(host, port);
- });
- }
-
- private void BtnLogin_Click(object sender, EventArgs e)
- {
- beforeConnection();
-
- Task startNew = Task.Factory.StartNew(() =>
- {
- _nettyClient = new NettyClient();
- _nettyClient.StartNetty(host, port);
-
- });
- }
-
- private void BtnDisConn_Click(object sender, EventArgs e)
- {
- _nettyClient?.StopNetty();
- //t?.Abort();
- }
-
- private void BtnSendData_Click(object sender, EventArgs e)
- {
- string cmd = TbSendData.Text.Trim();
- _nettyClient?.SendData(cmd);
- }
-
-
- private void RT_HB_Time_Click(object sender, EventArgs e)
- {
- HeartBeatTime hbt = new HeartBeatTime();
- hbt.ShowDialog();
- }
-
- private void TbPort_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
- {
- e.Handled = true;
- }
- }
-
- #endregion
- }
- }
|