Exemple #1
0
        /// <summary>
        /// 检测Ap在线状态
        /// </summary>
        private void CheckApStatusThread()
        {
            long upTime = 0;
            HashSet <AsyncUserToken> toKenList = new HashSet <AsyncUserToken>();
            HashSet <AsyncUserToken> RemovList = new HashSet <AsyncUserToken>();

            while (true)
            {
                try
                {
                    if (DeviceType != null && ((DateTime.Now.Ticks - upTime) / 10000000) > 60)
                    {
                        upTime = DateTime.Now.Ticks;
                        //报到线程状态
                        FrmMainController.write_monitor_status(DeviceType + "_STATUS");
                    }

                    MyDeviceList.CopyConnList(ref toKenList);
                    foreach (AsyncUserToken x in toKenList)
                    {
                        TimeSpan timeSpan = DateTime.Now - x.EndMsgTime;
                        if (timeSpan.TotalSeconds > DataController.ApOnLineTime) //大于180秒认为设备下线
                        {
                            RemovList.Add(x);
                        }
                    }
                    //删除已下线的AP
                    foreach (AsyncUserToken x in RemovList)
                    {
                        string MainControllerStatus = MyDeviceList.GetMainControllerStatus(x);
                        if (string.IsNullOrEmpty(MainControllerStatus))
                        {
                            MainControllerStatus = "unknown";
                        }

                        int i = MyDeviceList.remov(x);
                        if (i != -1)
                        {
                            CloseToken(x);
                            OnOutputLog(LogInfoType.INFO, string.Format("Ap[{0}:{1}]下线了!!!", x.IPAddress, x.Port.ToString()));
                            Send2main_OnOffLine(OffLine, i, x, MainControllerStatus);
                        }
                    }
                    //OnOutputLog(LogInfoType.DEBG, "当前在线Ap数量为:" + MyDeviceList.GetCount().ToString() + "台 !");

                    toKenList.Clear();
                    toKenList.TrimExcess();
                    RemovList.Clear();
                    RemovList.TrimExcess();

                    Thread.Sleep(3000);
                }
                catch (Exception e)
                {
                    OnOutputLog(LogInfoType.EROR, string.Format("线程[CheckApStatusThread]出错。错误码:{0}", e.Message));
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 接收到MainController模块消息处理线程
        /// </summary>
        /// <param name="o"></param>
        public void ReceiveMainMsgThread(object o)
        {
            bool   noMsg  = false;
            int    hNum   = 0;
            int    count  = 0;
            long   upTime = 0;
            string str    = string.Empty;

            while (true)
            {
                try
                {
                    if (DeviceType != null && ((DateTime.Now.Ticks - upTime) / 10000000) > 60)
                    {
                        upTime = DateTime.Now.Ticks;
                        //报到线程状态
                        FrmMainController.write_monitor_status("MAIN_2_" + DeviceType + "_HANDLE");
                    }

                    if (noMsg)
                    {
                        Thread.Sleep(10);
                    }
                    else
                    {
                        if (hNum >= 100)
                        {
                            hNum = 0;
                            //Thread.Sleep(10);
                        }
                    }

                    lock (AppManager.mutex_Main2App_Msg)
                    {
                        if (AppManager.rMain2AppMsgQueue.Count <= 0)
                        {
                            noMsg = true;
                            hNum  = 0;
                            continue;
                        }
                        else
                        {
                            noMsg = false;
                            hNum++;
                            str = AppManager.rMain2AppMsgQueue.Dequeue();
                        }
                        count = AppManager.rMain2AppMsgQueue.Count;
                    }

                    if (AppManager.handleMain2AppContrMsgNum == System.UInt32.MaxValue)
                    {
                        AppManager.handleMain2AppContrMsgNum = 0;
                    }
                    else
                    {
                        AppManager.handleMain2AppContrMsgNum++;
                    }

                    //解析收到的消息
                    InterModuleMsgStruct MainMsg = null;
                    try
                    {
                        MainMsg = JsonConvert.DeserializeObject <InterModuleMsgStruct>(str);
                    }
                    catch (Exception)
                    {
                        OnOutputLog(LogInfoType.EROR, "解析收到的Main模块消息出错!", LogCategory.I);
                        OnOutputLog(LogInfoType.INFO, string.Format("共处理Main2App消息条数:{0},当前队列消息条数:{1}!",
                                                                    AppManager.handleMain2AppContrMsgNum, count));
                        continue;
                    }

                    if ((MainMsg.AppInfo.Ip.Equals(MsgStruct.AllDevice)) || (MainMsg.AppInfo.Type.Equals(DeviceType)))
                    {
                        OnOutputLog(LogInfoType.INFO,
                                    string.Format("接收到MainController消息({0})。", MainMsg.Body.type));
                        OnOutputLog(LogInfoType.DEBG, string.Format("消息内容:\n{0}", str));
                    }

                    AppInnerType flag;
                    if ((!MainMsg.AppInfo.Ip.Equals(MsgStruct.AllDevice)) &&
                        (!(Enum.TryParse <AppInnerType>(MainMsg.AppInfo.Type, true, out flag))))
                    {
                        if (string.IsNullOrEmpty(MainMsg.AppInfo.Type))
                        {
                            MainMsg.AppInfo.Type = "空";
                        }
                        OnOutputLog(LogInfoType.EROR, "收到MainController模块消息中APP类型错误!收到类型为:" + MainMsg.AppInfo.Type);
                        continue;
                    }

                    if (MainMsg.Body.type == ApMsgType.scanner)
                    {
                        recvMainImsiMsgNum++;
                    }

                    if (ReceiveMainData != null && MainMsg != null)
                    {
                        ReceiveMainData(MainMsg);
                    }

                    OnOutputLog(LogInfoType.INFO, string.Format("共处理Main2App消息条数:{0},当前队列消息条数:{1}!",
                                                                AppManager.handleMain2AppContrMsgNum, count));
                }
                catch (Exception e)
                {
                    OnOutputLog(LogInfoType.EROR, string.Format("线程[ReceiveMainMsgThread]出错。错误码:{0}", e.Message));
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// 用于处理Logger的线程
        /// </summary>
        /// <param name="obj"></param>
        private static void thread_for_logger(object obj)
        {
            bool          noMsg   = false;
            string        info    = "";
            List <string> lstData = new List <string>();

            DateTime startTime = System.DateTime.Now;
            DateTime endTime   = System.DateTime.Now;
            TimeSpan ts        = endTime.Subtract(startTime);

            List <string> batchData = new List <string>();

            DateTime startTimeConn = System.DateTime.Now;
            DateTime endTimeConn   = System.DateTime.Now;
            TimeSpan tsConn        = endTimeConn.Subtract(startTimeConn);

            DateTime startTimeMonitor = System.DateTime.Now;
            DateTime endTimeMonitor   = System.DateTime.Now;
            TimeSpan tsMonitor        = endTimeMonitor.Subtract(startTimeMonitor);


            while (true)
            {
                if (noMsg)
                {
                    //没消息时Sleep一大点
                    Thread.Sleep(100);
                }
                else
                {
                    //有消息时Sleep一小点
                    Thread.Sleep(2);
                }

                try
                {
                    #region 报告线程状态

                    endTimeMonitor = System.DateTime.Now;
                    tsMonitor      = endTimeMonitor.Subtract(startTimeMonitor);

                    if (tsMonitor.TotalSeconds >= 60)
                    {
                        FrmMainController.write_monitor_status("Logger_Status");

                        //计时复位
                        startTimeMonitor = System.DateTime.Now;
                    }

                    #endregion
                }
                catch (Exception ee)
                {
                    Logger.Trace(LogInfoType.EROR, ee.Message, "Logger", LogCategory.I);
                    continue;
                }

                try
                {
                    #region keepAlive处理

                    if (logOutType == LogOutType.OT_Net || logOutType == LogOutType.OT_Both)
                    {
                        endTimeConn = System.DateTime.Now;
                        tsConn      = endTimeConn.Subtract(startTimeConn);

                        if (tsConn.TotalSeconds >= DataController.CheckFtpConnStatTime)
                        {
                            string str       = string.Format("[{0}]--keepAlive-UDP", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
                            byte[] sendBytes = System.Text.Encoding.Default.GetBytes(str);

                            udpSender.Connect(DataController.StrLogIpAddr, int.Parse(DataController.StrLogPort));

                            //udpSender.Send(sendBytes, sendBytes.Length);

                            Trace(LogInfoType.INFO, str, "Logger", LogCategory.S);
                            FrmMainController.add_log_info(LogInfoType.INFO, str, "Logger", LogCategory.S);

                            //计时复位
                            startTimeConn = System.DateTime.Now;
                        }
                    }

                    #endregion
                }
                catch (Exception ee)
                {
                    Logger.Trace(LogInfoType.EROR, ee.Message, "Logger", LogCategory.I);
                    continue;
                }

                try
                {
                    #region 保存Logger

                    lock (mutex_Logger)
                    {
                        // 动态计算批量更新的数量
                        BatchValue = (4 - (int)DataController.LogOutputLevel) * 10;
                        if (gQueueLogger.Count < BatchValue)
                        {
                            #region 数量不足

                            endTime = System.DateTime.Now;
                            ts      = endTime.Subtract(startTime);

                            if (ts.TotalSeconds < DataController.LogMaxIdleSeconds)
                            {
                                noMsg = true;
                                continue;
                            }
                            else
                            {
                                //清空数据
                                //lstData = new List<string>();
                                lstData.Clear();
                                lstData.TrimExcess();

                                //拷贝数据
                                while (gQueueLogger.Count > 0)
                                {
                                    lstData.Add(gQueueLogger.Dequeue());
                                }

                                //复位计时
                                startTime = System.DateTime.Now;
                            }

                            #endregion
                        }
                        else
                        {
                            #region 数量充足

                            //清空数据
                            //lstData = new List<string>();
                            lstData.Clear();
                            lstData.TrimExcess();

                            //拷贝数据
                            for (int i = 0; i < BatchValue; i++)
                            {
                                lstData.Add(gQueueLogger.Dequeue());
                            }

                            //复位起始时间
                            startTime = System.DateTime.Now;

                            #endregion
                        }
                    }

                    noMsg = false;
                    switch (logOutType)
                    {
                    case LogOutType.OT_File:
                    {
                        //检测日志日期
                        StrategyLog();

                        info = null;
                        foreach (string str in lstData)
                        {
                            info += str + "\r\n";
                        }

                        System.Diagnostics.Trace.WriteLine(info);
                        if (fileFlushType == FileFlushType.RightNow)
                        {
                            System.Diagnostics.Trace.Close();
                        }

                        info = null;
                        break;
                    }

                    case LogOutType.OT_Net:
                    {
                        foreach (string str in lstData)
                        {
                            byte[] sendBytes = Encoding.Default.GetBytes(str);
                            udpSender.Send(sendBytes, sendBytes.Length);
                        }

                        break;
                    }

                    case LogOutType.OT_Both:
                    {
                        //检测日志日期
                        StrategyLog();

                        info = null;
                        foreach (string str in lstData)
                        {
                            info += str + "\r\n";
                        }

                        System.Diagnostics.Trace.WriteLine(info);
                        if (fileFlushType == FileFlushType.RightNow)
                        {
                            System.Diagnostics.Trace.Close();
                        }

                        foreach (string str in lstData)
                        {
                            byte[] sendBytes = Encoding.Default.GetBytes(str);
                            udpSender.Send(sendBytes, sendBytes.Length);
                        }

                        info = null;
                        break;
                    }
                    }

                    #endregion
                }
                catch (Exception ee)
                {
                    Logger.Trace(LogInfoType.EROR, ee.Message, "Logger", LogCategory.I);
                    continue;
                }
            }
        }