public void OnNewAlertClient(AlertClient alertClient) { if (this.NewAlertClient != null) //執行_listener_NewAlertClient方法 this.NewAlertClient(this, alertClient); }
void _listener_NewAlertClient(AlertListener sender, AlertClient alertClient) { Action action = () => { var checkClient = _manager.Clients.Where(i => i.IP.Split(':').First() == alertClient.IP.Split(':').First()).ToArray(); if (checkClient.Count() > 0) { checkClient[0].Close(); _manager.Remove(checkClient[0]); } else { txtMsg.AppendText(string.Format("{0} New Client: {1} {2}\r\n", DateTime.Now.ToString("MM-dd HH:mm"), alertClient.IP, alertClient.Location)); } _manager.Add(alertClient); alertClient.getClients(_manager); }; this.BeginInvoke(action); //當每次發生RawDataReceived事件時,即呼叫alertClient_DataReceived()方法 //alertClient.RawDataReceived += alertClient_DataReceived; alertClient.ReadyAckReceived += alertClient_ReadyAckReceived; //alertClient.ClientMsgReceived += alertClient_ClientMsgReceived; alertClient.HeartBeatReceived += alertClient_HeartBeatReceived; alertClient.TalkReceived += alertClient_TalkReceived; //alertClient.AlarmAckReceived += alertClient_AlarmAckReceived; alertClient.ServiceReadyReceived += alertClient_ServiceReadyReceived; alertClient.ServiceAlertReceived += alertClient_ServiceAlertReceived; alertClient.ServiceClose += alertClient_ServiceClose; }
void alertClient_TalkReceived(AlertClient sender, TalkMsg talkMsg) { Action action = () => { txtMsg.AppendText(string.Format("{0} {1} Talk: {2}\r\n", DateTime.Now.ToString("MM-dd HH:mm"), sender.Name, talkMsg.MsgContent)); }; this.BeginInvoke(action); }
void alertClient_ServiceClose(AlertClient sender) { Action action = () => { sender.Close(); _manager.Remove(sender); }; this.BeginInvoke(action); }
void alertClient_ServiceAlertReceived(AlertClient sender, ServiceAlertMsg ServiceAlertMsg) { Action action = () => { txtMsg.AppendText(string.Format("Service Send Alert: {0}\r\n", ServiceAlertMsg)); }; this.BeginInvoke(action); }
void alertClient_AlarmAckReceived(AlertClient sender, AlarmAckMsg AlarmAckMsg) { Action action = () => { txtMsg.AppendText(string.Format("{0} Alarm Ack: {1}\r\n", sender.Id, AlarmAckMsg)); }; this.BeginInvoke(action); }
// 顯示自client所傳回的是否存活的訊息 void alertClient_HeartBeatReceived(AlertClient sender) { Action action = () => { txtMsg.AppendText(string.Format("{0} HeartBeat: {1} {2}\r\n", DateTime.Now.ToString("MM-dd HH:mm"), sender.Name, sender.Location)); }; this.BeginInvoke(action); }
void alertClient_DataReceived(AlertClient sender, IEnumerable<byte> rawData) { var data = rawData.ToArray(); var msg = System.Text.Encoding.Default.GetString(data, 0, data.Length); Action action = () => { txtMsg.AppendText(string.Format("{0} RawData: {1}\r\n", sender.IP, msg)); }; this.BeginInvoke(action); // BeginInvoke() → 在建立控制項基礎控制代碼的執行緒上執行指定的非同步委派 }
void alertClient_ClientMsgReceived(AlertClient sender, ClientMsg clientMsg) { Action action = () => { txtMsg.AppendText(string.Format("{0} ClientMsg: {1} {2}\r\n{3}\r\n", DateTime.Now.ToString("MM-dd HH:mm"), sender.Name, sender.Location, clientMsg)); }; //[delegate].BeginInvoke 立即Return 新的背景 Thread this.BeginInvoke(action); }
public void Remove(AlertClient client) { Clients.Remove(client); this.OnListChanged(); }
public void Add(AlertClient client) { Clients.Add(client); this.OnListChanged(); }
void _bw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { //應用程式是否已經要求取消背景作業 while (_bw.CancellationPending == false) { System.Threading.Thread.Sleep(50); //是否有暫止連接要求存在 if (_listener.Pending() == false) continue; AlertClient client = new AlertClient(_listener.AcceptTcpClient()); this.AlertClients.Add(client); // 發生OnNewAlertClient事件,執行底下OnNewAlertClient() this.OnNewAlertClient(client); } }
void _listener_NewAlertClient(AlertListener sender, AlertClient alertClient) { Action action = () => { txtMsg.AppendText(string.Format("{0} New Client: {1}\r\n", DateTime.Now.ToString("MM-dd HH:mm"), alertClient.IP)); _manager.Add(alertClient); alertClient.getClients(_manager); }; this.BeginInvoke(action); //當每次發生RawDataReceived事件時,即呼叫alertClient_DataReceived()方法 alertClient.RawDataReceived += alertClient_DataReceived; alertClient.ReadyAckReceived += alertClient_ReadyAckReceived; //alertClient.ClientMsgReceived += alertClient_ClientMsgReceived; alertClient.TalkReceived += alertClient_TalkReceived; //alertClient.AlarmAckReceived += alertClient_AlarmAckReceived; alertClient.ServiceReadyReceived += alertClient_ServiceReadyReceived; alertClient.ServiceAlertReceived += alertClient_ServiceAlertReceived; alertClient.ServiceClose += alertClient_ServiceClose; }
//顯示client傳回的ReadyAck訊息 void alertClient_ReadyAckReceived(AlertClient sender, ReadyAckMsg readyAckMsg) { Action action = () => { var checkClient = _manager.Clients.Where(i => i.Id == sender.Id).ToArray(); if (checkClient.Length > 1 || sender.Id == null) { if (checkClient[0].TcpClient.Connected) { checkClient[0].Close(); _manager.Remove(checkClient[0]); } } else { txtMsg.AppendText(string.Format("{0} Ready Ack: {1}\r\n", sender.Id, readyAckMsg)); } }; this.BeginInvoke(action); }