Esempio n. 1
0
 //static object lookObj = new object();
 public void StartServer(string ip, int port)
 {
     if (hySever == null)
     {
         hySever = new TCPSyncSocketServer(port, ip, 1024 * 1024 * 6);
         try
         {
             hySever.OnReceviceByte += (o, a, c) =>
             {
                 try
                 {
                     ////if(c>0)
                     //try
                     //{
                     //    if (c > 0)
                     //    {
                     //        lock (lookObj)
                     //        {
                     //            datas.Add(new ZDPowerData.PowerData() { o = o, data = a, lenght = c });
                     //        }
                     //    }
                     //}
                     //catch (Exception) { }
                     DataHandle(o, a, c);
                     //SendData(((IPEndPoint)o.RemoteEndPoint).Address.ToString(), ((IPEndPoint)o.RemoteEndPoint).Port, "Zzzdgyjk000210Zzzdgyjk");
                 }
                 catch (Exception) { }
             };
             hySever.OnOfflineClient += (c) =>
             {
                 try
                 {
                     //clientInfo.RemoveAt(clientInfo.FindIndex(o => o.Ip == ((IPEndPoint)c.RemoteEndPoint).Address.ToString()));
                     OfflineClient?.Invoke(c);
                     ClientInfo pd = clientInfo.Find(a => a.Ip == ((IPEndPoint)c.RemoteEndPoint).Address.ToString());
                     if (pd != null)
                     {
                         pd.OnlineTime = DateTime.Now;
                         pd.Status     = "离线中";
                     }
                 }
                 catch (Exception) { }
             };
             hySever.OnOnlineClient += (o) =>
             {
                 OnlineClient?.Invoke(o);
                 //SendData(((IPEndPoint)o.RemoteEndPoint).Address.ToString(), ((IPEndPoint)o.RemoteEndPoint).Port, "stxzjzdstart1002");
                 //SendData(((IPEndPoint)o.RemoteEndPoint).Address.ToString(), ((IPEndPoint)o.RemoteEndPoint).Port, "zzzdgyjk000210Zzzzdgyjk" + Environment.NewLine);
                 ClientInfo pd = clientInfo.Find(a => a.Ip == ((IPEndPoint)o.RemoteEndPoint).Address.ToString());
                 if (pd != null)
                 {
                     pd.OnlineTime = DateTime.Now;
                     pd.Status     = "在线上";
                 }
             };
             hySever.OnStateInfo += (o, a) =>
             {
                 StateInfo?.Invoke(o, a);
             };
             //hySever.OnExceptionMsg += (o) =>
             //{
             //    //try
             //    //{
             //    //    txtFxMsg.Dispatcher.BeginInvoke(new Action(() =>
             //    //    {
             //    //        txtFxMsg.AppendText(Environment.NewLine + AppLogHelper.GetLogStr("----->>>" + string.Format("{0} {1}\r\n", o, DateTime.Now.ToString()), "错误消息"));
             //    //    }));
             //    //}
             //    //catch (Exception) { }
             //};
         }
         catch (InvalidOperationException) { }
         catch (Exception) { }
     }
     hySever.StartListen();
     (new Thread(SendHert)
     {
         IsBackground = true
     }).Start();
 }