/// <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);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 构造函数。
 /// </summary>
 private HostNetService(ICoreService service, StartClassInfo sci)
 {
     this.info = service["userinfo"] as UserInfo;
     this.ports = service["portsettings"] as PortSettings;
     this.hostAddress = service["hostaddress"] as HostAddress;
     //主机广播。
     this.hostBroadcast = new HostBroadcastService(info);
     this.hostBroadcast.Changed += this.OnRaiseChanged;
     //主机侦听。
     this.hostListenService = new HostListenService(this.sci = sci, this.info, this.ports);
     this.hostListenService.Changed += this.OnRaiseChanged;
     this.hostListenService.UpdateControls += this.OnUpdateControls;
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string strBroadcast = this.txtBroadcast.Text;
                if (string.IsNullOrEmpty(strBroadcast))
                {
                    this.OnMessageEvent(MessageType.PopupInfo, "请配置广播地址!");
                    return;
                }

                HostAddress host = new HostAddress();
                host.HostIP = IPAddress.Parse(this.ddlHostIP.Text);
                host.BroadcastAddress = IPAddress.Parse(strBroadcast);
                this.CoreService.Add("hostaddress", host);

                if (!this.isPluginLoad)
                {
                    TeaSyncData teaSyncData = null;

                    try
                    {
                        teaSyncData = TeaSyncData.DeSerialize(FolderStructure.UserSyncDataFile(userInfo.UserID));
                    }
                    catch (Exception x)
                    {
                        this.OnMessageEvent(MessageType.PopupWarn, string.Format("同步的数据发生异常:{0}", x.Message));
                    }

                    if (teaSyncData != null)
                    {
                        this.CoreService.Add("teasyncdata", teaSyncData);
                        this.CoreService.AddForm(new MonitorStudentsWindow(this.CoreService));
                    }
                    else
                    {
                        this.CoreService.AddForm(new SyncDataWindow(this.CoreService, userInfo, true));
                    }

                    this.CoreService.ForceQuit = true;
                }
                this.btnClose_Click(this.btnClose, e);
            }
            catch (Exception ex)
            {
                this.OnMessageEvent(MessageType.PopupInfo, ex.Message);
            }
        }