示例#1
0
        /// <summary>
        /// 处理登陆包
        /// </summary>
        /// <param name="jsText"></param>
        /// <returns></returns>
        public LoginBeatRec ProcessJsonLogin(string jsText)
        {
            LoginBeatRec jsObject = default(LoginBeatRec);

            if (!string.IsNullOrEmpty(jsText))
            {
                try
                {
                    jsObject = JObject.Parse(jsText.Insert(2, "l")).ToObject <LoginBeatRec>();
                    return(jsObject);
                }
                catch (Exception ex) { log.Error(ex); }
            }
            return(null);
        }
示例#2
0
        public void Start()
        {
            try
            {
                UdpServer = new UdpClient(30000);
            }
            catch (Exception ex)
            {
                throw new Exception("UdpServer初始化失败,原因:" + ex.Message);
            }
            SendLoginBeatSend();
            IPEndPoint tbip = null;

            if (IsLoad)
            {
                try
                {
                    UdpServer.Client.ReceiveTimeout = 5 * 1000;
                    byte[]       dataLogin = UdpServer.Receive(ref tbip);
                    LoginBeatRec BeatRec   = ProcessJsonLogin(dataLogin.ToASCIIString());
                    if (BeatRec != null && BeatRec.las != null && BeatRec.las.login_success)
                    {
                    }
                    else
                    {
                        throw new Exception("服务器拒绝登陆,登陆失败");
                    }
                }
                catch { throw new Exception("服务器无响应登陆失败,Time=" + UdpServer.Client.ReceiveTimeout + "MS"); }
            }
            UdpServer.Client.ReceiveTimeout = 5 * 60 * 1000;
            IsRuning = true;
            Task.Run(() =>
            {
                while (IsRuning)
                {
                    try
                    {
                        IPEndPoint ip = null;
                        byte[] data   = UdpServer.Receive(ref ip);
                        if (!IsLoad)
                        {
                            Ep = ip.ToDnsEndPoint();
                        }
                        if (data.Length > 0)
                        {
                            if (data[2] == 'a' && data[3] == 's')
                            {
                                LoginBeatRec nas = ProcessJsonLogin(data.ToASCIIString());
                                if (nas?.las != null && nas.las.login_success)
                                {
                                    BeatRcvTime = DateTime.Now;
                                }
                            }
                            else
                            {
                                ProcessLoraPackage(data);
                            }
                        }
                    }
                    catch (Exception ex) { log.Error(ex); }
                }
            });
        }