Example #1
0
        static void Instance_ListenerAcceptMessage(object sender, MessageListenerEventArgs e)
        {
            ChangeConsoleOut();

#if (PrivateUse)
            string ip = e.Listener.FromIP;
            if (ip.StartsWith("192.168.") || ip.StartsWith("127.0.0.1")) //测试,仅限于局域网使用
            {
                ip = string.Empty;
            }
            else
            {
                Console.WriteLine("错误,{0}", UseDescrition);
                return;
            }
#endif
            //下面整个处理过程应该放到一个动态实例对象的方法中,否则,多线程问题难以避免
            SubscriberInfo   subInfo   = new SubscriberInfo(e.Listener);
            MessageProcesser processer = new MessageProcesser(subInfo, e.Listener.FromMessage);
            processer.ServiceErrorEvent += new EventHandler <ServiceErrorEventArgs>(Processer_ServiceErrorEvent);
            //Console.WriteLine("process message begin.");
            try
            {
                processer.Process();
            }
            catch (Exception ex)
            {
                Processer_ServiceErrorEvent(processer, new ServiceErrorEventArgs(ex));
            }
            //Console.WriteLine("process message end.");
        }
Example #2
0
 /// <summary>
 /// 取消注册(但不关闭当前连接)
 /// </summary>
 /// <param name="subInfo"></param>
 public void DeSubscribe(SubscriberInfo subInfo)
 {
     if (SubscriberInfoList.Contains(subInfo))
     {
         SubscriberInfoList.Remove(subInfo);
     }
 }
Example #3
0
 protected Func <string, string> PreGetMessageFun(SubscriberInfo info)
 {
     return(strPara =>
     {
         MessageListener currLst = MessageCenter.Instance.GetListener(info.FromIP, info.FromPort);
         if (currLst != null)
         {
             return currLst.PreCallBackFunction(info.MessageID, strPara);
         }
         else
         {
             return "";
         }
     });
 }
Example #4
0
        /// <summary>
        /// 根据订阅者信息,查找它所在的发布者
        /// </summary>
        /// <param name="sourceSubInfo"></param>
        /// <returns></returns>
        public ServicePublisher Find(SubscriberInfo sourceSubInfo, out SubscriberInfo objSubInfo)
        {
            objSubInfo = null;

            foreach (string key in dict.Keys)
            {
                ServicePublisher item = dict[key];
                var tempSubinfo       = item.SubscriberInfoList.ToArray().FirstOrDefault(p => p.FromIP == sourceSubInfo.FromIP &&
                                                                                         p.FromPort == sourceSubInfo.FromPort &&
                                                                                         p.MessageID == sourceSubInfo.MessageID);
                if (tempSubinfo != null)
                {
                    objSubInfo = tempSubinfo;
                    return(item);
                }
            }
            return(null);
        }
Example #5
0
        /// <summary>
        /// 取消注册(但不关闭当前连接)
        /// </summary>
        /// <param name="subInfo"></param>
        public void DeSubscribe(SubscriberInfo subInfo)
        {
            List <SubscriberInfo> list = new List <SubscriberInfo>();
            SubscriberInfo        item;

            while (!SubscriberInfoList.IsEmpty)
            {
                if (SubscriberInfoList.TryTake(out item))
                {
                    if (item != subInfo)
                    {
                        list.Add(item);
                    }
                }
            }
            foreach (SubscriberInfo sub in list)
            {
                SubscriberInfoList.Add(sub);
            }
        }
Example #6
0
        private void PublishItem(SubscriberInfo info, System.Diagnostics.Stopwatch sw, ref string tempMsg, ref int index)
        {
            string         publishResult = null;
            ServiceContext context       = new ServiceContext(info.Request);

            context.Host             = this.Host;
            context.SessionRequired  = true;
            context.GetMessageFun    = base.GetMessageFun(info);
            context.PreGetMessageFun = base.PreGetMessageFun(info);
            //根据每个会话来计算服务结果
            publishResult = CallService(info.SessionID, context);
            tempMsg      += string.Format("\r\nPub2 No.{0},have used {1}ms.\r\n", index++, sw.ElapsedMilliseconds);
            if (context.SendEmptyResult || (!context.SendEmptyResult && !context.NoResultRecord(publishResult)))
            {
                //可能执行完服务后,监听器又断开了,因此需要再次获取
                MessageListener currLst = MessageCenter.Instance.GetListener(info.FromIP, info.FromPort);

                if (currLst != null)
                {
                    MessageCenter.Instance.NotifyOneMessage(currLst, info.MessageID, publishResult);
                    string text = string.Format("\r\n[{0}]Publish Required Session,ID: {1} \r\n>>[ID:{2}]消息长度:{3} 字节 ,消息内容摘要:\r\n{4}",
                                                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                                                info.SessionID, info.MessageID, publishResult.Length.ToString("###,###"),
                                                DataConverter.DeEncrypt8bitString(publishResult.Length > 256 ? publishResult.Substring(0, 256) : publishResult));
                    tempMsg += text;
                    //System.Diagnostics.Debug.WriteLine(text);
                }
                else
                {
                    string text = string.Format("\r\n[{0}]未找到监听器, Session,ID: {1} \r\n>>[ID:{2}]消息长度:{3} 字节 ,消息内容摘要:\r\n{4}",
                                                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                                                info.SessionID, info.MessageID, publishResult.Length.ToString("###,###"),
                                                DataConverter.DeEncrypt8bitString(publishResult.Length > 256 ? publishResult.Substring(0, 256) : publishResult));
                    tempMsg += text;
                    //System.Diagnostics.Debug.WriteLine(text);
                    DeSubscribe(info);
                }
            }
        }
Example #7
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);
 }
Example #8
0
 public MessageContext(SubscriberInfo subInfo, string message)
 {
     this.Message        = message;
     this.SubscriberInfo = subInfo;
 }