Ejemplo n.º 1
0
 public byte[] GetAllMsgBuff(AsyncUserToken toKen)
 {
     lock (locker1)
     {
         foreach (AsyncUserToken x in connList)
         {
             if (CompApInfo(x, toKen))
             {
                 byte[] rev = x.Buffer.GetRange(0, x.Buffer.Count).ToArray();
                 return(rev);
             }
         }
     }
     return(null);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 增加一条消息Id与App信息对应关系
        /// </summary>
        /// <param name="toKen">Ap信息</param>
        /// <param name="msgId2App">id与App对应关系</param>
        /// <returns>成功:true ;失败:false</returns>
        public bool AddMsgId2App(AsyncUserToken toKen, MsgId2App msgId2App)
        {
            lock (locker1)
            {
                DeviceDicKey dicKey = new DeviceDicKey(toKen.IPAddress.ToString(), toKen.Port);

                if (connList.ContainsKey(dicKey))
                {
                    msgId2App.AddTime = DateTime.Now;
                    connList[dicKey].msgId2App.Add(msgId2App);
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 更改最后收到消息的时间为当前时间
 /// </summary>
 /// <param name="toKen"></param>
 /// <returns>成功:true ;失败:false</returns>
 public bool ChangeEndMsgTime(AsyncUserToken toKen)
 {
     lock (locker1)
     {
         foreach (AsyncUserToken x in connList)
         {
             if (CompApInfo(x, toKen))
             {
                 x.EndMsgTime = DateTime.Now;
                 return(true);
             }
         }
     }
     return(false);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取AP在MainController模块的在线状态
        /// </summary>
        /// <param name="toKen">AP信息</param>
        /// <returns>状态</returns>
        public string GetMainControllerStatus(AsyncUserToken toKen)
        {
            string str = "unknown";

            lock (locker1)
            {
                DeviceDicKey dicKey = new DeviceDicKey(toKen.IPAddress.ToString(), toKen.Port);

                if (connList.ContainsKey(dicKey))
                {
                    str = connList[dicKey].MainControllerStatus;
                }
            }
            return(str);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 设置AP在MainController模块的在线状态
 /// </summary>
 /// <param name="status">状态</param>
 /// <param name="toKen">AP信息</param>
 /// <returns>修改成功返回true,否则返回false</returns>
 public bool SetMainControllerStatus(string status, AsyncUserToken toKen)
 {
     lock (locker1)
     {
         foreach (AsyncUserToken x in connList)
         {
             if (CompApInfo(x, toKen))
             {
                 x.MainControllerStatus = status;
                 return(true);
             }
         }
     }
     return(false);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 向设备列表中添加收到的消息
 /// </summary>
 /// <param name="toKen">设备信息</param>
 /// <param name="buff">收到的消息</param>
 /// <returns></returns>
 public bool AddMsgBuff(AsyncUserToken toKen, byte[] buff)
 {
     lock (locker1)
     {
         foreach (AsyncUserToken x in connList)
         {
             if (CompApInfo(x, toKen))
             {
                 x.Buffer.AddRange(buff);
                 x.EndMsgTime = DateTime.Now;
                 return(true);
             }
         }
     }
     return(false);
 }
Ejemplo n.º 7
0
        /// <summary>
        /// 根据设备SN,查找设备信息(不区分大小写)
        /// </summary>
        /// <param name="sn">设备sn</param>
        /// <returns>设备信息,未找到返回null</returns>
        public AsyncUserToken FindBySN(string sn)
        {
            AsyncUserToken toKen = null;

            lock (locker1)
            {
                foreach (AsyncUserToken x in connList)
                {
                    if (String.Compare(x.Sn, sn, true) == 0)
                    {
                        return(x);
                    }
                }
            }
            return(toKen);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 根据设备全名,查找设备信息(不区分大小写)
        /// </summary>
        /// <param name="fullname">设备全名</param>
        /// <returns>设备信息,未找到返回null</returns>
        public AsyncUserToken FindByFullname(string fullname)
        {
            AsyncUserToken toKen = null;

            lock (locker1)
            {
                foreach (AsyncUserToken x in connList)
                {
                    if (String.Compare(x.FullName, fullname, true) == 0)
                    {
                        return(x);
                    }
                }
            }
            return(toKen);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// 增加一条消息Id与App信息对应关系
 /// </summary>
 /// <param name="toKen">Ap信息</param>
 /// <param name="msgId2App">id与App对应关系</param>
 /// <returns>成功:true ;失败:false</returns>
 public bool AddMsgId2App(AsyncUserToken toKen, MsgId2App msgId2App)
 {
     lock (locker1)
     {
         foreach (AsyncUserToken x in connList)
         {
             if (CompApInfo(x, toKen))
             {
                 msgId2App.AddTime = DateTime.Now;
                 x.msgId2App.Add(msgId2App);
                 return(true);
             }
         }
     }
     return(false);
 }
Ejemplo n.º 10
0
        /// <summary>
        /// 透传MainController模块过来的消息给设备
        /// </summary>
        /// <param name="MainMsg"></param>
        private void Send2ap_TransparentMsg(InterModuleMsgStruct MainMsg)
        {
            AsyncUserToken apToKen = MyDeviceList.FindByApInfo(MainMsg.ApInfo);

            if (apToKen == null)
            {
                OnOutputLog(LogInfoType.WARN, string.Format("在线AP列表中找不到Ap[{0}:{1}]设备({2})!",
                                                            MainMsg.ApInfo.IP, MainMsg.ApInfo.Port.ToString(), MainMsg.ApInfo.Fullname));
                return;
            }

            string sendMsg = GetMsgStringValueInList("transparent_msg", MainMsg.Body);

            if (string.IsNullOrEmpty(sendMsg))
            {
                OnOutputLog(LogInfoType.EROR, string.Format("透传XML消息为空!"));
                Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type,
                                      string.Format("透传XML消息为空!"));
                return;
            }

            if (!Regex.IsMatch(sendMsg, "<\\s*id\\s*>.+<\\s*/\\s*id\\s*>"))
            {
                OnOutputLog(LogInfoType.EROR, string.Format("透传的XML消息中没有id项!"));
                Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type,
                                      string.Format("透传的XML消息中没有id项!"));
                return;
            }
            UInt16 id = ApMsgIdClass.addTransparentMsgId();

            sendMsg = Regex.Replace(sendMsg, "<\\s*id\\s*>.+<\\s*/\\s*id\\s*>", string.Format("<id>{0}</id>", id));

            MsgId2App msgId2App = new MsgId2App();

            msgId2App.id      = id;
            msgId2App.AppInfo = MainMsg.AppInfo;

            if (!MyDeviceList.AddMsgId2App(apToKen, msgId2App))
            {
                OnOutputLog(LogInfoType.EROR, string.Format("添加消息Id到设备列表出错!"));
                Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type,
                                      string.Format("添加消息Id到设备列表出错!"));
                return;
            }

            SendMsg2Ap(apToKen, MainMsg.Body.type, sendMsg);
        }
Ejemplo n.º 11
0
        public void CloseClientSocket(AsyncUserToken token)
        {
            lock (m_clients) { m_clients.Remove(token); }
            //如果有事件,则调用事件,发送客户端数量变化通知
            //log(LogInfoType.EROR, string.Format("设备[{0}:{1}]TCP连接断开!当前连接数:{2}!",
            //        token.IPAddress.ToString(), token.Port, m_clients.Count));

            try
            {
                token.Socket.Shutdown(SocketShutdown.Send);
            }
            catch (Exception) { }
            token.Socket.Close();
            // decrement the counter keeping track of the total number of clients connected to the server
            Interlocked.Decrement(ref m_clientCount);
            m_maxNumberAcceptedClients.Release();
        }
Ejemplo n.º 12
0
 // This method is invoked when an asynchronous send operation completes.
 // The method issues another receive on the socket to read any additional
 // data sent from the client
 //
 // <param name="e"></param>
 private void ProcessSend(SocketAsyncEventArgs e)
 {
     if (e.SocketError == SocketError.Success)
     {
         // done echoing data back to the client
         AsyncUserToken token = (AsyncUserToken)e.UserToken;
         // read the next block of data send from the client
         bool willRaiseEvent = token.Socket.ReceiveAsync(e);
         if (!willRaiseEvent)
         {
             ProcessReceive(e);
         }
     }
     else
     {
         CloseClientSocket(e);
     }
 }
Ejemplo n.º 13
0
        /// <summary>
        /// 向MainController模块发送状态改变消息(LTE/WCDMA状态)
        /// </summary>
        /// <param name="apToKen">AP信息,包含改变后的状态</param>
        /// <param name="OldDetail">改变前的状态</param>
        protected void Send2ap_ApStatusChange_LTE(AsyncUserToken apToKen, UInt32 detail, Byte ApReadySt)
        {
            UInt32 oldDetail    = apToKen.Detail;
            byte   oldApReadySt = apToKen.ApReadySt;

            OnOutputLog(LogInfoType.DEBG, "oldDetail=" + oldDetail + ";detail=" + detail +
                        ";oldApReadySt=" + oldApReadySt + ";ApReadySt=" + ApReadySt);
            //状态改变时才发送消息
            //需去掉上下线状态,再比较
            if (((detail | AP_STATUS_LTE.OnLine) == (oldDetail | AP_STATUS_LTE.OnLine)) && (oldApReadySt == ApReadySt))
            {
                return;
            }

            string st = Enum.GetName(typeof(ApReadyStEnum), ApReadySt);

            if (string.IsNullOrEmpty(st))
            {
                OnOutputLog(LogInfoType.EROR, string.Format("收到设备[{0}:{1}]的addStatu为{2}错误。",
                                                            apToKen.IPAddress.ToString(), apToKen.Port, ApReadySt));
                return;
            }

            st = st.Replace("_", "-");

            Msg_Body_Struct TypeKeyValue =
                new Msg_Body_Struct(Main2ApControllerMsgType.ApStatusChange,
                                    "carry", 0,
                                    "detail", string.Format("0x{0:X}", detail),
                                    "SCTP", ((detail & AP_STATUS_LTE.SCTP) > 0) ? 1 : 0,
                                    "S1", ((detail & AP_STATUS_LTE.S1) > 0) ? 1 : 0,
                                    "GPS", ((detail & AP_STATUS_LTE.GPS) > 0) ? 1 : 0,
                                    "CELL", ((detail & AP_STATUS_LTE.CELL) > 0) ? 1 : 0,
                                    "SYNC", ((detail & AP_STATUS_LTE.SYNC) > 0) ? 1 : 0,
                                    "LICENSE", ((detail & AP_STATUS_LTE.LICENSE) > 0) ? 1 : 0,
                                    "RADIO", ((detail & AP_STATUS_LTE.RADIO) > 0) ? 1 : 0,
                                    "wSelfStudy", ((detail & AP_STATUS_LTE.wSelfStudy) > 0) ? 1 : 0,
                                    "ALIGN", ((detail & AP_STATUS_LTE.ALIGN) > 0) ? 1 : 0,
                                    "ApReadySt", st,
                                    "timestamp", DateTime.Now.ToLocalTime().ToString());

            //向Main模块发消息
            OnSendMsg2Main(0, MsgStruct.MsgType.NOTICE, apToKen, TypeKeyValue);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 向APP返回通用错误消息
        /// </summary>
        /// <param name="apToKen">ap信息</param>
        /// <param name="appToken">app信息</param>
        /// <param name="type">app发送过来的消息类型</param>
        /// <param name="str">错误描述</param>
        protected void Send2APP_GeneralError(AsyncUserToken apToKen, AsyncUserToken appToken, string type, string str)
        {
            MessageType mt = MessageType.MSG_JSON;
            MessageBody mb = new MessageBody();

            InterModuleMsgStruct msg = new InterModuleMsgStruct();

            msg.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            msg.MsgType = MsgStruct.MsgType.CONFIG.ToString();

            if (apToKen == null)
            {
                msg.ApInfo = null;
            }
            else
            {
                msg.ApInfo.Type     = this.DeviceType;
                msg.ApInfo.IP       = apToKen.IPAddress.ToString();
                msg.ApInfo.Port     = apToKen.Port;
                msg.ApInfo.SN       = apToKen.Sn;
                msg.ApInfo.Fullname = apToKen.FullName;
            }

            msg.AppInfo.Type   = string.Empty;
            msg.AppInfo.Ip     = appToken.IPAddress.ToString();
            msg.AppInfo.Port   = appToken.Port;
            msg.AppInfo.Group  = appToken.Group;
            msg.AppInfo.User   = appToken.User;
            msg.AppInfo.Domain = appToken.Domain;

            Msg_Body_Struct TypeKeyValue =
                new Msg_Body_Struct(AppMsgType.general_error_result,
                                    "ErrStr", str,
                                    "RecvType", type);

            msg.Body = TypeKeyValue;
            mb.bJson = JsonConvert.SerializeObject(msg);

            OnOutputLog(LogInfoType.INFO, string.Format("发送消息({0})给MainController模块!", TypeKeyValue.type), LogCategory.S);
            OnOutputLog(LogInfoType.DEBG, string.Format("消息内容:\n{0}", mb.bJson), LogCategory.S);

            ApManager.sendMsg_2_MainController(mt, mb);
            //OnReceiveMainMsg(mt, mb);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 删除一条id与App对应关系
        /// </summary>
        /// <param name="toKen">Ap信息</param>
        /// <param name="msgId">消息Id</param>
        /// <returns>成功:true ;失败:false</returns>
        public bool RemoveMsgId2App(AsyncUserToken toKen, UInt16 msgId)
        {
            bool re = false;

            lock (locker1)
            {
                foreach (AsyncUserToken x in connList)
                {
                    if (CompApInfo(x, toKen))
                    {
                        HashSet <MsgId2App> RemoveList = new HashSet <MsgId2App>();
                        foreach (MsgId2App y in x.msgId2App)
                        {
                            if (y.id == msgId)
                            {
                                RemoveList.Add(y);
                                re = true;
                            }
                            else
                            {
                                TimeSpan timeSpan = DateTime.Now - y.AddTime;
                                if (timeSpan.TotalSeconds > 120) //大于120秒认为设备不会再回消息了
                                {
                                    RemoveList.Add(y);
                                }
                            }
                        }

                        foreach (MsgId2App y in RemoveList)
                        {
                            x.msgId2App.Remove(y);
                        }
                        x.msgId2App.TrimExcess();

                        RemoveList.Clear();
                        RemoveList.TrimExcess();

                        break;
                    }
                }
            }
            return(re);
        }
Ejemplo n.º 16
0
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            try
            {
                Interlocked.Increment(ref m_clientCount);
                // Get the socket for the accepted client connection and put it into the
                //ReadEventArg object user token
                SocketAsyncEventArgs readEventArgs = m_pool.Pop();
                AsyncUserToken       userToken     = (AsyncUserToken)readEventArgs.UserToken;
                userToken.Socket      = e.AcceptSocket;
                userToken.ConnectTime = DateTime.Now;
                userToken.Remote      = e.AcceptSocket.RemoteEndPoint;
                userToken.IPAddress   = ((IPEndPoint)(e.AcceptSocket.RemoteEndPoint)).Address;
                userToken.Port        = ((IPEndPoint)(e.AcceptSocket.RemoteEndPoint)).Port;

                lock (m_clients) { m_clients.Add(userToken); }

                //log(LogInfoType.EROR, string.Format("设备[{0}:{1}]建立TCP连接。当前连接数:{2}!",
                //    userToken.IPAddress.ToString(), userToken.Port,m_clients.Count));

                if (ClientNumberChange != null)
                {
                    ClientNumberChange(1, userToken);
                }

                if (!e.AcceptSocket.ReceiveAsync(readEventArgs))
                {
                    ProcessReceive(readEventArgs);
                }
            }
            catch (Exception me)
            {
                log(LogInfoType.EROR, me.Message + "\r\n" + me.StackTrace);
            }

            // Accept the next connection request
            if (e.SocketError == SocketError.OperationAborted)
            {
                return;
            }
            StartAccept(e);
        }
Ejemplo n.º 17
0
        //public void add(List<String> snList)
        //{
        //    lock (locker1)
        //    {
        //        foreach (String sn in snList)
        //        {
        //            foreach (AsyncUserToken x in ConnList)
        //            {
        //                if (String.Compare(x.Sn, sn, true) == 0)
        //                {
        //                    ConnList.Remove(x);
        //                    break;
        //                }
        //            }
        //            AsyncUserToken connInfo = new AsyncUserToken(sn);
        //            ConnList.Add(connInfo);
        //        }
        //    }
        //}

        //public void add(String sn)
        //{
        //    lock (locker1)
        //    {
        //        foreach (AsyncUserToken x in ConnList)
        //        {
        //            if (String.Compare(x.Sn, sn, true) == 0)
        //            {
        //                ConnList.Remove(x);
        //                break;
        //            }
        //        }
        //        AsyncUserToken connInfo = new AsyncUserToken(sn);
        //        ConnList.Add(connInfo);
        //    }
        //}

        public int add(AsyncUserToken toKen)
        {
            lock (locker1)
            {
                DeviceDicKey dicKey = new DeviceDicKey(toKen.IPAddress.ToString(), toKen.Port);
                if (connList.ContainsKey(dicKey))
                {
                    connList[dicKey].EndMsgTime = DateTime.Now;
                }
                else
                {
                    toKen.EndMsgTime           = DateTime.Now;
                    toKen.MainControllerStatus = "un";
                    toKen.ConnectTime          = DateTime.Now;

                    connList.Add(dicKey, toKen);
                }

                return(connList.Count);
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// 获取AP在MainController模块的在线状态
        /// </summary>
        /// <param name="toKen">AP信息</param>
        /// <returns>状态</returns>
        public string GetMainControllerStatus(AsyncUserToken toKen)
        {
            string str = "";

            lock (locker1)
            {
                foreach (AsyncUserToken x in connList)
                {
                    if (CompApInfo(x, toKen))
                    {
                        str = x.MainControllerStatus;
                        break;
                    }
                }
            }
            if (string.IsNullOrEmpty(str))
            {
                str = "unknown";
            }
            return(str);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// 发送AP上下线消息
        /// </summary>
        /// <param name="status">状态(OnLine:上线;OffLine:下线)</param>
        /// <param name="allNum">当前在线的AP总数</param>
        /// <param name="apToKen">Ap信息</param>
        /// <param name="MainControllerStatus">发送给数据库上、下线状态</param>
        protected void Send2main_OnOffLine(string status, int allNum, AsyncUserToken apToKen, string MainControllerStatus)
        {
            //保存的状态有更改时才上报
            //string MainControllerStatus = MyDeviceList.GetMainControllerStatus(token);
            //if (string.IsNullOrEmpty(MainControllerStatus)) MainControllerStatus = "unknown";

            OnOutputLog(LogInfoType.DEBG, "旧状态为:" + MainControllerStatus + "新状态为:" + status);

            if ((String.Compare(status, OffLine, true) == 0) && (MainControllerStatus.Equals("unknown")))
            {
                OnOutputLog(LogInfoType.WARN, "未向MainController模块上报过上线消息,该下线消息不上报!");
                return;
            }

            if ((String.Compare(status, OffLine, true) == 0) && (null != MyDeviceList.FindByFullname(apToKen.FullName)))
            {
                OnOutputLog(LogInfoType.WARN, "该设备有另外一个TCP连接在线,不向MainController模块上报下线消息!");
                return;
            }

            if (String.Compare(MainControllerStatus, status, true) != 0)
            {
                Msg_Body_Struct TypeKeyValue =
                    new Msg_Body_Struct(Main2ApControllerMsgType.OnOffLine,
                                        "AllOnLineNum", allNum.ToString(),
                                        "Status", status,
                                        "mode", apToKen.Mode.Replace("-", "_"),
                                        "version", apToKen.version,
                                        "timestamp", DateTime.Now.ToLocalTime().ToString());

                //向Main模块发消息
                OnSendMsg2Main(0, MsgStruct.MsgType.NOTICE, apToKen, TypeKeyValue);

                //修改状态----测试版本中使用,正式版本中在收到Ack后更改状态。
                //if (DebugMode)
                //{
                //    MyDeviceList.SetMainControllerStatus(status, token);
                //}
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// 删除一条id与App对应关系
        /// </summary>
        /// <param name="toKen">Ap信息</param>
        /// <param name="msgId">消息Id</param>
        /// <returns>成功:true ;失败:false</returns>
        public bool RemoveMsgId2App(AsyncUserToken toKen, UInt16 msgId)
        {
            bool re = false;

            lock (locker1)
            {
                DeviceDicKey dicKey = new DeviceDicKey(toKen.IPAddress.ToString(), toKen.Port);

                if (connList.ContainsKey(dicKey))
                {
                    HashSet <MsgId2App> RemoveList = new HashSet <MsgId2App>();
                    foreach (MsgId2App y in connList[dicKey].msgId2App)
                    {
                        if (y.id == msgId)
                        {
                            RemoveList.Add(y);
                            re = true;
                        }
                        else
                        {
                            TimeSpan timeSpan = DateTime.Now - y.AddTime;
                            if (timeSpan.TotalSeconds > 30) //大于30秒认为设备不会再回消息了
                            {
                                RemoveList.Add(y);
                            }
                        }
                    }

                    foreach (MsgId2App y in RemoveList)
                    {
                        connList[dicKey].msgId2App.Remove(y);
                    }
                    connList[dicKey].msgId2App.TrimExcess();

                    RemoveList.Clear();
                    RemoveList.TrimExcess();
                }
            }
            return(re);
        }
Ejemplo n.º 21
0
        /// <summary>
        /// 对数据进行打包,然后再发送
        /// </summary>
        /// <param name="token"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public void SendMessage(AsyncUserToken token, byte[] message)
        {
            if (token == null || token.Socket == null || !token.Socket.Connected)
            {
                log(LogInfoType.WARN, "未找到要发送的AP信息!");
                return;
            }
            if (message == null)
            {
                log(LogInfoType.EROR, "要发送的消息为NULL!");
                return;
            }

            try
            {
                //新建异步发送对象, 发送消息
                //SocketAsyncEventArgs sendArg = new SocketAsyncEventArgs();
                //sendArg.SetBuffer(message, 0, message.Length);  //将数据放置进去.
                //token.Socket.SendAsync(sendArg);
                string gbk_str  = System.Text.Encoding.Default.GetString(message);
                byte[] utf8_byt = System.Text.Encoding.UTF8.GetBytes(gbk_str);
                //string gbk_str1 = System.Text.Encoding.UTF8.GetString(utf8_byt);
                SendStruct sendStruct;
                sendStruct.token   = token;
                sendStruct.message = utf8_byt;
                ThreadPool.QueueUserWorkItem(new WaitCallback(BeginInvoke_SendMsg), sendStruct);

                //string str = string.Format("时间:[{0}] 客户端({1}:{2}),发送消息给设备成功!",
                //        token.ConnectTime.ToString(), token.IPAddress,token.Port.ToString(),
                //        System.Text.Encoding.Default.GetString(buff));
                //log(LogInfoType.INFO,str);

                //log(LogInfoType.DEBG, string.Format("消息内容:\n({0})",System.Text.Encoding.Default.GetString(buff)));
            }
            catch (Exception e)
            {
                log(LogInfoType.EROR, "SendMessage - Error:" + e.Message);
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// 根据apinfo查询apToKen信息。首先通过ip+Prot查询,若查不到,再通过fullname查询
        /// </summary>
        /// <param name="ApInfo">apinfo信息</param>
        /// <returns>apToKen信息,若未找到,返回null</returns>
        public AsyncUserToken FindByApInfo(Ap_Info_Struct ApInfo)
        {
            AsyncUserToken apToKen = this.FindByIpPort(ApInfo.IP, ApInfo.Port);

            if (apToKen == null)
            {
                Xml_codec.StaticOutputLog(LogInfoType.DEBG,
                                          string.Format("在线AP列表中找不到Ap[{0}:{1}]设备,通过FullName重新查询设备!",
                                                        ApInfo.IP, ApInfo.Port.ToString()),
                                          "DeviceList");
                apToKen = this.FindByFullname(ApInfo.Fullname);
            }

            if (apToKen == null)
            {
                Xml_codec.StaticOutputLog(LogInfoType.WARN,
                                          string.Format("在线AP列表中找不到Ap[{0}:{1}],FullName:{2}。无法向AP发送消息!",
                                                        ApInfo.IP, ApInfo.Port.ToString(), ApInfo.Fullname),
                                          "DeviceList");
            }

            return(apToKen);
        }
Ejemplo n.º 23
0
        /// <summary>
        /// 删除设备收到的消息
        /// </summary>
        /// <param name="toKen">设置信息</param>
        /// <param name="startIndex">起始标识</param>
        /// <param name="endIndex">结束标志</param>
        /// <returns></returns>
        public bool DelMsgBuff(AsyncUserToken toKen, int startIndex, int endIndex)
        {
            lock (locker1)
            {
                DeviceDicKey dicKey = new DeviceDicKey(toKen.IPAddress.ToString(), toKen.Port);
                if (connList.ContainsKey(dicKey))
                {
                    if (startIndex < 0)
                    {
                        startIndex = 0;
                    }
                    if (endIndex > connList[dicKey].Buffer.Count)
                    {
                        endIndex = connList[dicKey].Buffer.Count;
                    }

                    connList[dicKey].Buffer.RemoveRange(startIndex, endIndex);
                    connList[dicKey].Buffer.TrimExcess();
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 24
0
        /// <summary>
        /// 重写设备连接更改事件
        /// </summary>
        /// <param name="num">num大于0表示设备上线,否则设备下线</param>
        /// <param name="apToKen">设备信息</param>
        public override void OnDeviceNumberChange(int num, AsyncUserToken apToKen)
        {
            if (num > 0)
            {
                MyDeviceList.add(apToKen);
                //在收到心跳消息时上报
                //send2main_OnOffLine(OnLine,token);
            }
            else  //AP下线,删除设备列表中的AP信息
            {
                string MainControllerStatus = MyDeviceList.GetMainControllerStatus(apToKen);
                if (string.IsNullOrEmpty(MainControllerStatus))
                {
                    MainControllerStatus = "unknown";
                }

                int i = MyDeviceList.remov(apToKen);
                if (i != -1)
                {
                    Send2main_OnOffLine(OffLine, i, apToKen, MainControllerStatus);
                }
            }
        }
Ejemplo n.º 25
0
        /// <summary>
        /// 获取设备当前收到的消息
        /// </summary>
        /// <param name="toKen">设备信息</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="len">长度</param>
        /// <returns>收到的消息</returns>
        public byte[] GetMsgBuff(AsyncUserToken toKen, int startIndex, int len)
        {
            lock (locker1)
            {
                foreach (AsyncUserToken x in connList)
                {
                    if (CompApInfo(x, toKen))
                    {
                        if (startIndex < 0)
                        {
                            startIndex = 0;
                        }
                        if ((startIndex + len) > x.Buffer.Count)
                        {
                            len = x.Buffer.Count - startIndex;
                        }

                        byte[] rev = x.Buffer.GetRange(startIndex, len).ToArray();
                        return(rev);// System.Text.Encoding.Default.GetString(rev);
                    }
                }
            }
            return(null);// string.Empty;
        }
Ejemplo n.º 26
0
 public int remov(AsyncUserToken connInfo)
 {
     return(this.remov(connInfo.IPAddress.ToString(), connInfo.Port));
 }
Ejemplo n.º 27
0
 public RecvDeviceStruct()
 {
     this.token = new AsyncUserToken();
 }
Ejemplo n.º 28
0
        /// <summary>
        /// 处理收到的Main模块的消息
        /// </summary>
        /// <param name="MainMsg">消息内容</param>
        private void HandleMainMsg(MsgStruct.InterModuleMsgStruct MainMsg)
        {
            OnOutputLog(LogInfoType.INFO, string.Format("处理MainController消息。消息类型:{0}。", MainMsg.Body.type));

            //上、下线消息回复
            if (MainMsg.Body.type == Main2ApControllerMsgType.OnOffLine_Ack)
            {
                //所有在线AP数与数据库不一至,回复所有在线AP
                if (GetMsgIntValueInList("ReturnCode", MainMsg.Body) != 0)
                {
                    OnOutputLog(LogInfoType.EROR,
                                "[OnOffLine_Ack]Main模块返回错误:" + GetMsgStringValueInList("ReturnStr", MainMsg.Body));
                    //暂时不发送,等待后续定义
                    //Send2main_OnLineList();
                }
                else
                {
                    string status = GetMsgStringValueInList("Status", MainMsg.Body);
                    if (status.Equals(OnLine) || status.Equals(OffLine))
                    {
                        //修改状态
                        MyDeviceList.SetMainControllerStatus(status, MainMsg.ApInfo.IP, MainMsg.ApInfo.Port);
                    }
                    else
                    {
                        OnOutputLog(LogInfoType.EROR, "Main模块返回消息中,Status字段错误!");
                    }
                }
            }
            else if (MainMsg.Body.type == Main2ApControllerMsgType.ApDelete)
            {
                //修改状态为下线状态
                MyDeviceList.SetMainControllerStatus(OffLine, MainMsg.ApInfo.IP, MainMsg.ApInfo.Port);
            }
            else if (MainMsg.Body.type == Main2ApControllerMsgType.ApSetRadio)
            {
                AsyncUserToken apToKen = MyDeviceList.FindByApInfo(MainMsg.ApInfo);
                if (apToKen == null)
                {
                    OnOutputLog(LogInfoType.WARN, string.Format("在线AP列表中找不到Ap[{0}:{1}]设备({2})!",
                                                                MainMsg.ApInfo.IP, MainMsg.ApInfo.Port.ToString(), MainMsg.ApInfo.Fullname));
                    return;
                }

                //byte carry = GetMsgByteValueInList("carry", MainMsg.Body.dic, Byte.MaxValue);
                //if (carry != 0)
                //{
                //    OnOutputLog(LogInfoType.EROR, string.Format("Main模块发送消息[{0}]中,carry字段非法!",
                //        Main2ApControllerMsgType.ApSetRadio));
                //    return;
                //}

                Byte active_mode = GetMsgByteValueInList("active_mode", MainMsg.Body.dic, Byte.MaxValue);
                if (active_mode != 1 && active_mode != 2)
                {
                    OnOutputLog(LogInfoType.EROR, string.Format("Main模块发送消息[{0}]中,active_mode字段非法!",
                                                                Main2ApControllerMsgType.ApSetRadio));
                    return;
                }

                Byte mode = GetMsgByteValueInList("mode", MainMsg.Body.dic, Byte.MaxValue);
                if (mode != 1 && mode != 2)
                {
                    OnOutputLog(LogInfoType.EROR, string.Format("Main模块发送消息[{0}]中,mode字段非法!",
                                                                Main2ApControllerMsgType.ApSetRadio));
                    return;
                }

                Send2ap_activate_nodeb_request(apToKen, active_mode, mode);
            }
            //状态改变回复
            else if (MainMsg.Body.type == Main2ApControllerMsgType.ApStatusChange_Ack)
            {
                RecvAckSaveApStatus(MainMsg);
            }
            else if (MainMsg.Body.type == Main2ApControllerMsgType.OnOffLineCheck)
            {
                string status = GetMsgStringValueInList("Status", MainMsg.Body);
                Send2main_OnOffLineCheck(status, MainMsg.ApInfo);
            }
            else if (MainMsg.Body.type == Main2ApControllerMsgType.ReportGenParaAck)
            {
                //返回错误
                if (GetMsgIntValueInList("ReturnCode", MainMsg.Body) != 0)
                {
                    OnOutputLog(LogInfoType.EROR,
                                "[ReportGenParaAck]Main模块返回错误:" + GetMsgStringValueInList("ReturnStr", MainMsg.Body));
                }
                return;
            }
            else if (MainMsg.Body.type == Main2ApControllerMsgType.app_add_bwlist_request)
            {
                //添加黑白名单
                Send2ap_imsi_list_setconfig(MainMsg);
                return;
            }
            else if (MainMsg.Body.type == Main2ApControllerMsgType.app_del_bwlist_request)
            {
                //删除黑白名单
                Send2ap_imsi_list_delconfig(MainMsg);
                return;
            }
            else if (MainMsg.Body.type == Main2ApControllerMsgType.SetGenParaReq)
            {
                MainMsg.Body.type = ApMsgType.set_general_para_request;
                Send2ap_RecvMainMsg(MainMsg);
            }
            else //其它消息
            {
                if ((string.IsNullOrEmpty(MainMsg.ApInfo.IP)) || (MainMsg.ApInfo.IP == MsgStruct.NullDevice))
                {
                    OnOutputLog(LogInfoType.WARN, string.Format("目的设备为Null,不向Ap发送信息!"));
                    Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type,
                                          string.Format("目的设备为Null,不向Ap发送信息!"));
                }
                else
                {
                    Send2ap_RecvMainMsg(MainMsg);
                }
            }
            return;
        }
Ejemplo n.º 29
0
        /// <summary>
        /// 向AP删除黑白名单
        /// </summary>
        /// <param name="token">AP信息</param>
        private void Send2ap_imsi_list_delconfig(InterModuleMsgStruct MainMsg)
        {
            AsyncUserToken apToKen = MyDeviceList.FindByApInfo(MainMsg.ApInfo);

            if (apToKen == null)
            {
                OnOutputLog(LogInfoType.WARN, string.Format("在线AP列表中找不到Ap[{0}:{1}]设备({2})!",
                                                            MainMsg.ApInfo.IP, MainMsg.ApInfo.Port.ToString(), MainMsg.ApInfo.Fullname));
                return;
            }

            Msg_Body_Struct TypeKeyValue = new Msg_Body_Struct(ApMsgType.imsi_list_delconfig);
            int             i            = 0;

            foreach (Name_DIC_Struct n_dicList in MainMsg.Body.n_dic)
            {
                Dictionary <string, object> dic = n_dicList.dic;

                String bwFlag = GetMsgStringValueInList("bwFlag", dic);
                if (!bwFlag.Equals("black") && !bwFlag.Equals("white"))
                {
                    OnOutputLog(LogInfoType.WARN, "删除黑白名单参数错误。bwFlag字段错误!");
                    Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type,
                                          "删除黑白名单参数错误。bwFlag字段错误!");
                    continue;
                }

                if (bwFlag.Equals("white"))
                {
                    String imsi = GetMsgStringValueInList("imsi", dic);
                    if (imsi.Equals(String.Empty))
                    {
                        OnOutputLog(LogInfoType.WARN, "删除白名单参数错误。imsi字段错误!");
                        Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type,
                                              "删除白名单参数错误。imsi字段错误!");
                        continue;
                    }

                    Name_DIC_Struct n_dic = new Name_DIC_Struct("name" + i++);
                    n_dic.dic.Add("whiteimsi/imsi", imsi);
                    TypeKeyValue.n_dic.Add(n_dic);
                }
                if (bwFlag.Equals("black"))
                {
                    String imsi = GetMsgStringValueInList("imsi", dic);
                    if (imsi.Equals(String.Empty))
                    {
                        OnOutputLog(LogInfoType.WARN, "删除黑名单参数错误。imsi字段错误!");
                        Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type,
                                              "删除黑名单参数错误。imsi字段错误!");
                        continue;
                    }

                    Name_DIC_Struct n_dic = new Name_DIC_Struct("name" + i++);
                    n_dic.dic.Add("blackimsi/imsi", imsi);
                    TypeKeyValue.n_dic.Add(n_dic);
                }
            }

            if (i == 0)
            {
                OnOutputLog(LogInfoType.EROR, "删除黑名单参数错误。没有要发送的名单!");
                Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type,
                                      "删除黑名单参数错误。没有要发送的名单!");
                return;
            }

            if (!SendMsg2Ap(apToKen, 0, TypeKeyValue))
            {
                Send2APP_GeneralError(MainMsg.ApInfo, MainMsg.AppInfo, MainMsg.Body.type,
                                      "封装XML消息(imsi_list_delconfig)出错!");
            }
        }
Ejemplo n.º 30
0
        /// <summary>
        /// 处理收到的AP消息
        /// </summary>
        /// <param name="apToKen"></param>
        /// <param name="msg"></param>
        private void HandleApMsg(AsyncUserToken apToKen, string msg)
        {
            //解析AP发过来的消息
            UInt16          msgId   = 0;
            Msg_Body_Struct msgBody = null;

            try
            {
                msgBody = Xml_codec.DecodeApXmlMessage(msg, ref msgId);
            }
            catch (Exception)
            {
                OnOutputLog(LogInfoType.EROR, string.Format("解析收到的Ap[{0}:{1}]消息出错!",
                                                            apToKen.IPAddress.ToString(), apToKen.Port.ToString()));
                return;
            }

            if (msgBody == null)
            {
                OnOutputLog(LogInfoType.EROR, "收到消息格式错误!");
                OnOutputLog(LogInfoType.DEBG, "出错消息内容:" + msg);
                return;
            }

            if (msgBody.type == ApMsgType.device_test_request)
            {
                Send2ap_device_test_response(apToKen);
                return;
            }

            if (msgBody.type != ApMsgType.status_response)
            {
                OnOutputLog(LogInfoType.INFO, string.Format("处理AP[{0}:{1}]消息({2}),消息Id={3}!",
                                                            apToKen.IPAddress.ToString(), apToKen.Port, msgBody.type, msgId));
            }

            //处理透传消息
            if (msgId >= ApMsgIdClass.MIN_TRANSPARENT_MSG_ID && msgId <= ApMsgIdClass.MAX_TRANSPARENT_MSG_ID)
            {
                if (msgBody.type != ApMsgType.get_general_para_response) //参数上报消息不透传
                {
                    Msg_Body_Struct body = new Msg_Body_Struct(Main2ApControllerMsgType.transparent_msg_response, "transparent_msg", msg);
                    OnSendMsg2Main(msgId, MsgStruct.MsgType.TRANSPARENT, apToKen, body);
                    return;
                }
            }

            //心跳消息处理
            if (msgBody.type == ApMsgType.status_response)
            {
                //OnOutputLog(LogInfoType.INFO, "收到心跳消息");
                heartbeatMsgNum++;

                //UInt32 oldDetail = apToKen.Detail;
                UInt32 detail  = 0;
                string sDetail = GetMsgStringValueInList("detail", msgBody);
                if (!string.IsNullOrEmpty(sDetail))
                {
                    detail = Convert.ToUInt32(sDetail, 16);
                }

                //Byte oldApReadySt = apToKen.ApReadySt;
                Byte   ApReadySt  = 5;
                string sApReadySt = GetMsgStringValueInList("addStatus", msgBody);
                if (!string.IsNullOrEmpty(sApReadySt))
                {
                    ApReadySt = Convert.ToByte(sApReadySt);
                }

                apToKen.version  = GetMsgStringValueInList("version", msgBody);
                apToKen.Mode     = GetMsgStringValueInList("mode", msgBody);
                apToKen.Sn       = GetMsgStringValueInList("sn", msgBody);
                apToKen.FullName = GetMsgStringValueInList("fullname", msgBody);
                if (string.IsNullOrEmpty(apToKen.FullName))   //兼容老Agent拼写错误
                {
                    apToKen.FullName = GetMsgStringValueInList("fullaname", msgBody);
                }
                apToKen.Id = GetMsgStringValueInList("id", msgBody);
                //apToKen.Detail = detail;

                int i = MyDeviceList.add(apToKen);
                Send2main_OnOffLine(OnLine, i, apToKen);

                //判断是周期心跳,还是上线心跳
                if ((detail & (int)AP_STATUS_LTE.OnLine) > 0) //上线
                {
                    //OnOutputLog(LogInfoType.DEBG, "上线消息");
                    if (OnLine.Equals(MyDeviceList.GetMainControllerStatus(apToKen)))
                    {
                        Send2ap_status_request(apToKen);
                        Thread.Sleep(1000);
                        Send2ap_get_general_para_request(apToKen);
                    }
                    else
                    {
                        OnOutputLog(LogInfoType.DEBG, "MainController未回复上线成功消息!");
                    }
                }
                else //周期心跳
                {
                    //OnOutputLog(LogInfoType.DEBG, "周期心跳消息");
                }
                //发送状态改变
                Send2ap_ApStatusChange_LTE(apToKen, detail, ApReadySt);
            }
            else if (msgBody.type == ApMsgType.get_general_para_response)
            {
                Msg_Body_Struct SendMsgBody = msgBody;
                SendMsgBody.type = Main2ApControllerMsgType.ReportGenPara;
                // 向Main模块发消息
                OnSendMsg2Main(0, MsgStruct.MsgType.NOTICE, apToKen, SendMsgBody);
            }
            else if (msgBody.type == ApMsgType.set_general_para_response)
            {
                Msg_Body_Struct SendMsgBody = msgBody;
                SendMsgBody.type = Main2ApControllerMsgType.SetGenParaRsp;
                // 向Main模块发消息
                OnSendMsg2Main(0, MsgStruct.MsgType.NOTICE, apToKen, SendMsgBody);
            }
            else if (msgBody.type == ApMsgType.imsi_list_config_result)
            {
                Msg_Body_Struct SendMsgBody = msgBody;
                SendMsgBody.type = Main2ApControllerMsgType.app_add_bwlist_response;
                // 向Main模块发消息
                OnSendMsg2Main(msgId, MsgStruct.MsgType.NOTICE, apToKen, SendMsgBody);
            }
            else if (msgBody.type == ApMsgType.imsi_list_delconfig_result)
            {
                Msg_Body_Struct SendMsgBody = msgBody;
                SendMsgBody.type = Main2ApControllerMsgType.app_del_bwlist_response;
                // 向Main模块发消息
                OnSendMsg2Main(msgId, MsgStruct.MsgType.NOTICE, apToKen, SendMsgBody);
            }
            else if (msgBody.type == ApMsgType.scanner)
            {
                imsiMsgNum++;
                recvApImsiMsgNum++;

                string imsi = GetMsgStringValueInList("imsi", msgBody);
                if (!string.IsNullOrEmpty(imsi))
                {
                    //IMSI去重处理
                    if (DataController.RemoveDupMode == 1)
                    {
                        //有该IMSI,去重,不用上报
                        if (ImsiRemoveDup.isExist(imsi))
                        {
                            OnOutputLog(LogInfoType.INFO,
                                        string.Format("Imsi[{0}]在{1}时被抓到过,不再上报!",
                                                      imsi, ImsiRemoveDup.GetTime(imsi)));
                            return;
                        }
                        ImsiRemoveDup.add(imsi);
                    }

                    Msg_Body_Struct body = new Msg_Body_Struct(msgBody.type, msgBody.dic);

                    OnOutputLog(LogInfoType.DEBG, string.Format("上报imsi:{0}", imsi));
                    OnSendMsg2Main(0, MsgStruct.MsgType.NOTICE, apToKen, body);
                }
            }
            else
            {
                Msg_Body_Struct body = new Msg_Body_Struct(msgBody.type, msgBody.dic);
                OnSendMsg2Main(msgId, MsgStruct.MsgType.CONFIG, apToKen, body);
            }

            msgBody = null;
        }