/// <summary> /// 添加观察数据。 /// </summary> /// <param name="data"></param> public void AddDataObserver(HostBroadcast data) { if (data != null && !this.htcCache.ContainsKey(data.SName)) { this.htcCache.Add(data.SName, data); this.OnDataObserver(); } }
/// <summary> /// 触发数据观察事件。 /// </summary> /// <param name="data"></param> protected void OnDataObserver() { DataObserverHandler handler = this.Observer; int len = 0; if (handler != null && ((len = this.htcCache.Count) > 0)) { HostBroadcast[] array = new HostBroadcast[len]; this.htcCache.Values.CopyTo(array, 0); List<HostBroadcast> list = new List<HostBroadcast>(); for (int i = 0; i < len; i++) list.Add(array[i]); if (list.Count > 0) handler(list); } }
/// <summary> /// /// </summary> /// <param name="sci"></param> /// <param name="hostAddress"></param> /// <param name="ports"></param> public void Start(StartClassInfo sci, HostAddress hostAddress, PortSettings ports) { try { if (sci == null || ports == null || hostAddress == null) return; int broadcastPort = ports.HostBroadcast; Thread thread = HostBroadcastService.PortThreadCache[broadcastPort] as Thread; #region 如果当前广播未关闭,强制关闭。 if (thread != null) { try { this.Stop(); thread.Abort(); } finally { thread = null; } } #endregion this.RaiseChanged("开启主机广播..."); IPEndPoint broadcastAddr = new IPEndPoint(hostAddress.BroadcastAddress, broadcastPort); HostBroadcast data = new HostBroadcast(); data.SName = string.Format("{0}[{1}]{2}({3})", this.info.UserName, sci.ClassInfo.ClassName, sci.CatalogInfo.CatalogName, sci.CatalogInfo.TypeName); data.Ports = ports; data.UID = this.info.UserID; data.Time = DateTime.Now; int interval = ports.BroadcastInterval; thread = new Thread(new ThreadStart(delegate() { this.isStart = true; //主机广播。 this.sendHostBroadcast(data, broadcastAddr, interval); //发送主机关闭广播。 this.sendHostCloseBroadcast(broadcastAddr); //移除缓存。 HostBroadcastService.PortThreadCache[broadcastPort] = null; })); thread.IsBackground = true; HostBroadcastService.PortThreadCache[broadcastPort] = thread; thread.Start(); } catch (Exception x) { this.OnExceptionRecord(x, this.GetType()); this.RaiseChanged(x.Message); MessageBox.Show(x.Message, "循环广播时发生异常:", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void sendHostBroadcast(HostBroadcast data, IPEndPoint broadcastAddr, int interval) { try { if (data == null || broadcastAddr == null) return; if (interval < 0) interval = 1; while (this.isStart) { try { data.Time = DateTime.Now; using (UdpClient udpClient = new UdpClient()) { byte[] buffer = this.Serialize(data); if (buffer != null && buffer.Length > 0) { udpClient.Send(buffer, buffer.Length, broadcastAddr); } } } catch (Exception e) { this.OnExceptionRecord(e, this.GetType()); this.RaiseChanged("广播主机位置发生异常:" + e.Message); } finally { Thread.Sleep(interval * 1000 + 1600); } } } catch (Exception e) { this.OnExceptionRecord(new Exception("循环广播时发生异常[" + data + "]", e), this.GetType()); MessageBox.Show(e.Message, "循环广播时发生异常:", MessageBoxButtons.OK, MessageBoxIcon.Error); } }