void SendFunc() { Sendclient = new UdpClient(); //发送信息的端口一定要和接受的端口号一样 SendPort = new IPEndPoint(groupAddress, tport); while (true) { //判断侦听器注册数量 if (DiscoveryLogServer.Count == 0) { ManagerLog("未设置侦听器,无法分拣消息"); //LogFile("UDPManager", "未设置侦听器,无法分拣消息"); continue; } try { Queue <DataTransfer> datas = new Queue <DataTransfer>(); List <string> HandleKeys = new List <string>(DiscoveryLogServer.Keys); foreach (string key in HandleKeys)//构建消息队列 { List <UDPHandle> tHandle = DiscoveryLogServer[key]; if (tHandle == null || tHandle.Count == 0) { //Handle类别为空 continue; } DataTransfer transfer = DataTransfer.GetInstance(ID, tHandle[0].HandleType); tHandle.ForEach(delegate(UDPHandle cHandle) { Fields[] tField = cHandle.OnSendData(); if (tField == null || tField.Length == 0) { return; } for (int i = 0; i < tField.Length; i++) { tField[i].name = cHandle.HandleName; } transfer.FieldsJoining(tField); }); if (transfer.data.Count != 0) { datas.Enqueue(transfer); dataCollections = transfer.ToString(); } /* * Fields[] tField = tHandle.OnSendData(); * if(tField == null || tField.Length == 0) { continue; } * DataTransfer transfer = DataTransfer.GetInstance(ID, tHandle.HandleType(), tField); * datas.Enqueue(transfer); */ } if (datas.Count > 0) { while (datas.Count > 0) { DataTransfer SendingData = datas.Dequeue();//前面已经确保了DataTransfer不为空 try { string json = Newtonsoft.Json.JsonConvert.SerializeObject(SendingData); byte[] bufs = Encoding.UTF8.GetBytes(json); Sendclient.Send(bufs, bufs.Length, SendPort); } catch (Exception e) { DiscoveryLogServer[SendingData.type].ForEach(handle => handle.Log(e.ToString())); } } } else { //ManagerLog("空转"); Thread.Sleep(sendMillSec); } }catch (Exception e) { ManagerLog(e.ToString()); } } }
public Fields(string tName, DataTransfer tTransfer) { FieldList = new List <Field>(); name = tName; AttachTransfer = tTransfer; }