コード例 #1
0
        }//end sub

        /// <summary>
        /// 处理用户的请求消息
        /// </summary>
        /// <param name="e"></param>
        public void Execute(MessageRequestEventArgs e)
        {
            string message = e.MessageText;

            if (ServiceRequest.IsServiceUrl(message))
            {
                string identity = e.Listener.Identity;

                string   processMesssage = string.Empty;
                DateTime beginTime       = DateTime.Now;
                processMesssage = string.Format("[{0}]正在处理服务请求--From: {1}:{2},Identity:{3}\r\n>>[RMID:{4}]{5}",
                                                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                                                e.Listener.FromIP, e.Listener.FromPort, identity, e.Listener.MessageID,
                                                message.Length > 256 ? message.Substring(0, 256) : message);
                Console.WriteLine(processMesssage);

                int msgId = e.Listener.MessageID;
                //执行服务方法的时候,由服务方法指名是否需要维持会话状态
                ServiceContext context = new ServiceContext(message);
                context.ServiceErrorEvent     += new EventHandler <ServiceErrorEventArgs>(currentProcess_ServiceErrorEvent);
                context.Request.ClientIP       = e.Listener.FromIP;
                context.Request.ClientPort     = e.Listener.FromPort;
                context.Request.ClientIdentity = identity;
                context.InitRequestParameters();
                context.User = MessageProcessBase.GetServiceIdentity(e.Listener);


                context.ProcessService(e.Listener.SessionID);

                string   result   = context.Response.AllText;
                bool     noResult = context.NoResultRecord(result);
                DateTime endTime  = DateTime.Now;
                processMesssage = string.Format("[{0}]请求处理完毕({1}ms)--To: {2}:{3},Identity:{4}\r\n>>[RMID:{5}]消息长度:{6} -------",
                                                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                                                endTime.Subtract(beginTime).TotalMilliseconds,
                                                e.Listener.FromIP, e.Listener.FromPort, identity, e.Listener.MessageID,
                                                noResult ? "[Empty Result]" : result.Length.ToString("###,###") + "字节");
                Console.WriteLine(processMesssage);

                e.ResultText = result;
                //此处内容可能很大,不能全程输出
                if (context.Response.ResultType == typeof(byte[]))
                {
                    Console.WriteLine("[byte Content]");
                }
                else
                {
                    Console.WriteLine("result:{0}", result.Length > 100 ? result.Substring(0, 100) + " ..." : result);
                }
            }
            else
            {
                e.ResultText = "OK";
            }
        }
コード例 #2
0
        /// <summary>
        /// 判断当前消息是否属于自己处理的类型,如果是,执行消息处理,如果不是,调用下一个消息处理类
        /// </summary>
        public void Work(MessageContext context)
        {
            SetState(context);

            if (this.IsCurrentType())
            {
                Process();
            }
            else
            {
                MessageProcessBase nextProcess = GetNext();
                if (nextProcess != null)
                {
                    nextProcess.Work(context);
                }
            }
        }
コード例 #3
0
 public MessageProcesser(SubscriberInfo subInfo, string message)
 {
     this.context        = new MessageContext(subInfo, message);
     this.currentProcess = new ServiceMessageProcess();
     this.currentProcess.ServiceErrorEvent += new EventHandler <ServiceErrorEventArgs>(currentProcess_ServiceErrorEvent);
 }
コード例 #4
0
        protected override void Process()
        {
            string message  = this.Message;
            string identity = this.SubscriberInfo.Identity;

            string   processMesssage = string.Empty;
            DateTime beginTime       = DateTime.Now;

            processMesssage = string.Format("[{0}]正在处理服务请求--From: {1}:{2},Identity:{3}\r\n>>[PMID:{4}]{5}",
                                            beginTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                                            this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort, identity,
                                            this.SubscriberInfo.MessageID,
                                            this.Message.Length > 256 ? this.Message.Substring(0, 256) : this.Message);
            Console.WriteLine(processMesssage);

            int msgId = this.SubscriberInfo.MessageID;//执行完服务方法后,MessageID 可能被另外一个线程改变
            //执行服务方法的时候,由服务方法指名是否需要维持会话状态
            ServiceContext context = new ServiceContext(message);

            //如果当前请求是 发布-订阅模式,多个订阅端使用一个发布对象实例
            if (context.Request.RequestModel == RequestModel.Publish || context.Request.RequestModel == RequestModel.ServiceEvent)
            {
                if (PublisherFactory.Instance.Contains(context))  //已经订阅过
                {
                    ServicePublisher publisher = PublisherFactory.Instance.GetPublisher(context);
                    if (publisher.TaskIsRunning)
                    {
                        publisher.SubscriberInfoList.Add(this.SubscriberInfo);
                        processMesssage = string.Format("\r\n[{0}]当前监听器已经加入消息发布线程, {1}:{2},Identity:{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort, this.SubscriberInfo.Identity);
                        Console.WriteLine(processMesssage);
                        return;
                    }
                }
            }

            context.Host = Program.Host;
            context.ServiceErrorEvent     += new EventHandler <ServiceErrorEventArgs>(context_ServiceErrorEvent);
            context.Request.ClientIP       = this.SubscriberInfo.FromIP;
            context.Request.ClientPort     = this.SubscriberInfo.FromPort;
            context.Request.ClientIdentity = this.SubscriberInfo.Identity;

            context.User = MessageProcessBase.GetServiceIdentity(this.SubscriberInfo._innerListener);

            context.GetMessageFun = strPara =>
            {
                MessageListener currLst = MessageCenter.Instance.GetListener(this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort);
                if (currLst == null)
                {
                    throw new NullReferenceException("监听器不存在!");
                }
                return(currLst.CallBackFunction(msgId, strPara));
            };

            context.PreGetMessageFun = strPara =>
            {
                MessageListener currLst = MessageCenter.Instance.GetListener(this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort);
                if (currLst == null)
                {
                    throw new NullReferenceException("监听器不存在!");
                }
                return(currLst.PreCallBackFunction(msgId, strPara));
            };

            string result   = string.Empty;
            bool   noResult = false;

            context.InitRequestParameters();
            if (!context.HasError)
            {
                //Console.WriteLine("Process Service begin...");
                context.ProcessService(this.SubscriberInfo.SessionID);
                //Console.WriteLine("Process Service ok...");
                result   = context.Response.AllText;
                noResult = context.NoResultRecord(result);
            }
            else
            {
                result = ServiceConst.CreateServiceErrorMessage(context.ErrorMessage);
                //base.OnServiceError(context, new ServiceErrorEventArgs(context.ErrorMessage));
            }

            DateTime endTime = DateTime.Now;

            processMesssage = string.Format("[{0}]请求处理完毕({1}ms)--To: {2}:{3},Identity:{4}\r\n>>[PMID:{5}]消息长度:{6} -------",
                                            endTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                                            endTime.Subtract(beginTime).TotalMilliseconds,
                                            this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort, identity,
                                            this.SubscriberInfo.MessageID,
                                            noResult ? "[Empty Result]" : result.Length.ToString("###,###") + "字节");
            Console.WriteLine(processMesssage);
            //此处内容可能很大,不能全程输出
            if (context.Response.ResultType == typeof(byte[]))
            {
                Console.WriteLine("[byte Content]");
            }
            else
            {
                Console.WriteLine("result:{0}", result.Length > 100 ? result.Substring(0, 100) + " ..." : result);
            }

            MessageListener currLstn = MessageCenter.Instance.GetListener(this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort);

            if (currLstn == null)
            {
                processMesssage = "Error:监听器未找到,已取消发送消息。请求源:" + this.SubscriberInfo.Message;
                Console.WriteLine(processMesssage);
                return;
            }
            if (context.Request.RequestModel == RequestModel.GetService)
            {
                //对于请求-响应模式,处理完服务以后,始终会回调客户端的方法(如果提供的话)
                if (MessageCenter.Instance.ResponseMessage(currLstn, msgId, result))
                {
                    Console.WriteLine("Reponse Message OK.");
                }
            }
            else
            {
                //订阅模式,仅在服务处理有结果的情况下,才给客户端发布数据。

                if (!noResult)
                {
                    if (MessageCenter.Instance.NotifyOneMessage(currLstn, msgId, result))
                    {
                        Console.WriteLine("Publish Message OK.");
                    }
                    else
                    {
                        Console.WriteLine("Publish no result.");
                    }
                }
                if (!context.HasError)
                {
                    StartPublishWorker(context);//把Host传递进去
                    processMesssage = string.Format("\r\n[{0}]当前监听器已经加入工作线程, {1}:{2},Identity:{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), this.SubscriberInfo.FromIP, this.SubscriberInfo.FromPort, this.SubscriberInfo.Identity);
                    Console.WriteLine(processMesssage);
                }
            }
        }