Example #1
0
        /// <summary>
        /// 发送指令
        /// </summary>
        /// <param name="instruct"></param>
        public void AppendInstruct(Instruct instruct)
        {
            if (IsStop) return; //完全终止
            instruct.ClientID = this.Client.ClientID;

            if (!instruct.IsSync)
                SendInstructAsync(instruct);
            else
                SendInstruct(new Instruct[] { instruct }, true);
        }
Example #2
0
        public void CreateInstruct(int userID, InstructType type, object datas, bool isSync)
        {
            ///检查是否开放Passport服务器
            if (!AllSettings.Current.PassportServerSettings.EnablePassportService)
                return;

            Instruct ins = new Instruct();

            string tempData = string.Empty;

            if (Serializer.IsSimpleDataTypes(datas))
                tempData = datas.ToString();
            else
                tempData = Serializer.GetXML(datas);

            ins.Datas = tempData;
            ins.InstructType = type;
            ins.TargetID = userID;
            ins.IsSync = isSync;

            InstructDriver driver;

            foreach (KeyValuePair<int, InstructDriver> item in this.DriverList)
            {
                driver = item.Value;
                if (driver.Client.InstructTypes.Count == 0 || driver.Client.InstructTypes.Contains(type))
                {
                    driver.AppendInstruct(ins);
                }
            }
        }
Example #3
0
        /// <summary>
        /// 发送异步指令
        /// </summary>
        /// <param name="instruct"></param>
        private void SendInstructAsync(Instruct instruct)
        {
            instruct.InstructID = WriteToDB(instruct);  //先入库

            if (IsDisposed) return;                      //对象已经销毁

            if (!IsOpenQueue || HasDbData)
            {
                HasDbData = true;
                return;
            }

            if (this.InstructList.Count < this.QueueSize)
            {
                this.InstructList.Enqueue(instruct); //入列
                if (thread == null || !thread.IsAlive)
                {
                    lock (locker)
                    {
                        if (thread == null || !thread.IsAlive)
                        {
                            this.thread = new Thread(ThreadStart);
                            thread.IsBackground = true;
                            thread.Start();
                        }
                    }
                }
            }
            else
            {
                this.IsOpenQueue = false;
                HasDbData = true;
            }
        }
Example #4
0
 private void DeleteFromDB(Instruct ins)
 {
     PassportBO.Instance.DeleteFromDB(new long[] { ins.InstructID });
 }
Example #5
0
 /// <summary>
 /// 日志记录
 /// </summary>
 /// <param name="ins"></param>
 /// <param name="error"></param>
 private long WriteToDB(Instruct ins)
 {
     return PassportBO.Instance.WriteInstruct(this.Client.ClientID, ins.TargetID, ins.CreateDate, ins.InstructType, ins.Datas);
 }
Example #6
0
        /// <summary>
        /// 发送指令
        /// </summary>
        /// <param name="ins"></param>
        private bool SendInstruct(Instruct[] ins, bool isSync)
        {
            if (IsDisposed)
                return false;

            watch.Reset();
            watch.Start();


            ClientInstruct[] instructs = new ClientInstruct[ins.Length];

            int i = 0;

            //转换
            foreach (Instruct instruct in ins)
            {
                ClientInstruct clientInstruct = new ClientInstruct();
                clientInstruct.InstructID = instruct.InstructID;
                if (instruct.Datas != null)
                {
                    clientInstruct.Datas = instruct.Datas.Trim('\0');
                }
                else
                {
                    clientInstruct.Datas = string.Empty;
                }
                clientInstruct.InstructType = (int)instruct.InstructType;
                clientInstruct.CreateDateTime = instruct.CreateDate;
                clientInstruct.TargetID = instruct.TargetID;
                instructs[i++] = clientInstruct;
            }

            try
            {
                ClientService.ReceiveInstruct(instructs);
            }
            catch (SoapException ex)//客户端异常, 跳过
            {
                LogHelper.CreateLog(ex, ex.Message+"(指令被跳过)", string.Format("passport_{0}_Exception_{1}.log", this.Client.ClientID,DateTime.Now.ToString("yyyyMMdd")));
                if (isSync)
                    throw;
            }
            catch (Exception ex)
            {
                LogHelper.CreateLog(ex, ex.Message, string.Format("passport_{0}_Exception_{1}.log", this.Client.ClientID, DateTime.Now.ToString("yyyyMMdd")));

                if (isSync)
                    throw;
                watch.Reset();
                return false;
            }

            SendCount++;
            watch.Stop();
            ElapsedTime += watch.Elapsed.TotalMilliseconds;
            if (watch.ElapsedMilliseconds > 500)
            {
                string msg =string.Concat( 
                    "InstructType = "+ins[0].InstructType,"\r\n"
                ,   "Time = ", watch.Elapsed.TotalSeconds, "\r\n"
                ,  " ClientID=", this.Client.ClientID,"\r\n");

                LogHelper.CreateLog(null, msg, string.Format("InstructCall_{0}.txt", DateTime.Now.ToString("yyyy-MM-dd")));
            }

            return true;
        }
Example #7
0
        /// <summary>
        /// 发送指令
        /// </summary>
        /// <param name="ins"></param>
        private bool SendInstruct(Instruct[] ins, bool isSync)
        {
            if (IsDisposed)
                return false;

            watch.Reset();
            watch.Start();


            InstructProxy[] instructs = new InstructProxy[ins.Length];

            int i = 0;

            //转换
            foreach (Instruct instruct in ins)
            {
                InstructProxy clientInstruct = new InstructProxy();
                clientInstruct.InstructID = instruct.InstructID;
                if (instruct.Datas != null)
                {
                    clientInstruct.Datas = instruct.Datas.Trim('\0');
                }
                else
                {
                    clientInstruct.Datas = string.Empty;
                }
                clientInstruct.InstructType = (int)instruct.InstructType;
                clientInstruct.CreateDateTime = instruct.CreateDate;
                clientInstruct.TargetID = instruct.TargetID;
                instructs[i++] = clientInstruct;
            }

            try
            {
                ClientService.ReceiveInstruct(this.Client.AccessKey,this.Client.APIUrl, instructs);
            }
            //catch (SoapException ex)//客户端异常, 跳过
            //{
            //   
            //    if (isSync)
            //        throw;
            //}
            catch (WebException ex)
            {
                bool skeepIns = false;
                HttpWebResponse response = ex.Response as HttpWebResponse;
            
                if (response != null)
                {
                    switch (response.StatusCode)
                    {
                        case HttpStatusCode.InternalServerError:  //500
                            skeepIns = true;
                            LogHelper.CreateLog(ex, ex.Message + "(指令被跳过)", string.Format("passport_{0}_Exception_{1}.log", this.Client.ClientID, DateTime.Now.ToString("yyyyMMdd")));
                            break;
                    }
                }

                if (isSync)
                    throw;
                if (!skeepIns)
                {
                    watch.Reset();
                    return false;
                }
            }

            SendCount++;
            watch.Stop();
            ElapsedTime += watch.Elapsed.TotalMilliseconds;

#if DEBUG  //客户端API延时监控记录部分
            if (watch.ElapsedMilliseconds > 500)
            {
                string msg =string.Concat( 
                    "InstructType = "+ins[0].InstructType,"\r\n"
                ,   "Time = ", watch.Elapsed.TotalSeconds, "\r\n"
                ,  " ClientID=", this.Client.ClientID,"\r\n");

                LogHelper.CreateLog(null, msg, string.Format("InstructCall_{0}.txt", DateTime.Now.ToString("yyyy-MM-dd")));
            }
#endif

            return true;
        }