Ejemplo n.º 1
0
        /// <summary>
        /// 向单个客户端发送数据
        /// </summary>
        /// <param name="rsClient"></param>
        /// <param name="data"></param>
        public void SendData(RegistryClient registryClient, RegistryMessageAction action, String data)
        {
            try
            {
                RegistryMessage rm = new RegistryMessage()
                {
                    Action      = action,
                    MessageBody = data
                };

                String messageData = XmlUtil.SaveXmlFromObj <RegistryMessage>(rm);
                Console.WriteLine("发送数据:{0}", messageData);

                Byte[] msg = Encoding.UTF8.GetBytes(messageData);
                registryClient.Socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(SendCallback), registryClient);
            }
            catch (Exception ex)
            {
                XTrace.WriteLine("发送数据时发生异常:" + ex.ToString());
                lock (m_RegistryClients)
                {
                    m_RegistryClients.Remove(registryClient);
                    registryClient.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        private void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                TcpListener listener = ar.AsyncState as TcpListener;
                Socket      socket   = listener.EndAcceptSocket(ar);
                listener.BeginAcceptSocket(new AsyncCallback(AcceptCallback), listener);

                RegistryClient registryClient = new RegistryClient(socket);
                registryClient.ReceiveDateTime    = DateTime.Now;
                registryClient.RegistryClientType = RegistryClientType.AnKnown;
                registryClient.ClearBuffer();
                registryClient.Socket.BeginReceive(registryClient.ReceiveBuffer, 0, registryClient.ReceiveBuffer.Length
                                                   , SocketFlags.None, new AsyncCallback(ReceiveCallback), registryClient);

                Console.WriteLine("接收客户端:{0}", registryClient.Socket.RemoteEndPoint.ToString());

                lock (m_RegistryClients)
                {
                    m_RegistryClients.Add(registryClient);
                }
            }
            catch (Exception ex)
            {
                XTrace.WriteLine(ex.ToString());
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 增对不同的客户端和消息类型进行处理
        /// </summary>
        /// <param name="regClient"></param>
        /// <param name="regMessage"></param>
        public void Process(RegistryClient regClient, CometMessage regMessage)
        {
            if (regMessage.Action == CometMessageAction.Hello)
            {
                ConsumerConfig consumerConfig = XmlUtil.LoadObjFromXML <ConsumerConfig>(regMessage.MessageBody);
                regClient.ConsumerConfig        = consumerConfig;
                regClient.ProcessorID           = regMessage.ProcessorID;
                regClient.DotNetFramworkVersion = regMessage.DotNetFramworkVersion;
                regClient.OSVersion             = regMessage.OSVersion;
                regClient.ClientApplicationName = consumerConfig.ApplicationName;

                ESBConfig esbConfig = GetESBConfig(regClient);
                m_RegistryCenter.SendData(regClient, CometMessageAction.ServiceConfig, esbConfig.ToXml(), regMessage.IsAsync);
            }
            else if (regMessage.Action == CometMessageAction.ListRegistryClient)
            {
                String message = XmlUtil.SaveXmlFromObj <List <RegistryClient> >(m_RegistryCenter.RegistryClients);
                m_RegistryCenter.SendData(regClient, CometMessageAction.ListRegistryClient, message, regMessage.IsAsync);
            }
            else if (regMessage.Action == CometMessageAction.ResendConfig)//--管理中心向每个客户端发送配置文件
            {
                foreach (var item in m_RegistryCenter.RegistryClients)
                {
                    if (item.RegistryClientType == CometClientType.Consumer ||
                        item.RegistryClientType == CometClientType.CallCenter ||
                        item.RegistryClientType == CometClientType.Monitor ||
                        item.RegistryClientType == CometClientType.QueueCenter ||
                        item.RegistryClientType == CometClientType.Portal)
                    {
                        ESBConfig esbConfig = GetESBConfig(item);
                        m_RegistryCenter.SendData(item, CometMessageAction.ServiceConfig, esbConfig.ToXml());
                    }
                }
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 增对不同的客户端和消息类型进行处理
 /// </summary>
 /// <param name="regClient"></param>
 /// <param name="regMessage"></param>
 public void Process(RegistryClient regClient, RegistryMessage regMessage)
 {
     if (regMessage.ClientType == RegistryClientType.Consumer && regMessage.Action == RegistryMessageAction.Hello)
     {
         ConsumerConfig consumerConfig = XmlUtil.LoadObjFromXML <ConsumerConfig>(regMessage.MessageBody);
         ESBConfig      esbConfig      = GetESBConfig(consumerConfig);
         m_RegistryCenter.SendData(regClient, RegistryMessageAction.ServiceConfig, esbConfig.ToXml());
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 接收请求回调函数
        /// </summary>
        /// <param name="ar"></param>
        private void ReceiveCallback(IAsyncResult ar)
        {
            RegistryClient registryClient = (RegistryClient)ar.AsyncState;

            try
            {
                int dataLength = registryClient.Socket.EndReceive(ar);

                if (dataLength == 0)
                {
                    Console.WriteLine("接收客户端:{0}已经断开连接。", registryClient.ClientIP);
                    lock (m_RegistryClients){
                        m_RegistryClients.Remove(registryClient);
                        registryClient.Dispose();
                    }
                }
                else
                {
                    String data = Encoding.UTF8.GetString(registryClient.ReceiveBuffer, 0, dataLength);

                    Console.WriteLine("接收客户端:{0}发送的数据:{1}。", registryClient.ClientIP, data);

                    //--解析来自客户端的类型
                    CometMessage regMessage = XmlUtil.LoadObjFromXML <CometMessage>(data);
                    registryClient.RegistryClientType = regMessage.ClientType;
                    registryClient.ClientVersion      = regMessage.ClientVersion;

                    registryClient.ClearBuffer();
                    registryClient.Socket.BeginReceive(registryClient.ReceiveBuffer, 0, registryClient.ReceiveBuffer.Length
                                                       , SocketFlags.None, new AsyncCallback(ReceiveCallback), registryClient);

                    //--由消息处理器进行处理
                    m_MessageProcessor.Process(registryClient, regMessage);
                };
            }
            catch (Exception ex)
            {
                XTrace.WriteLine("接收数据时发生异常:" + ex.ToString());
                lock (m_RegistryClients)
                {
                    m_RegistryClients.Remove(registryClient);
                    registryClient.Dispose();
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 发送数据回调函数
        /// </summary>
        /// <param name="ar"></param>
        private void SendCallback(IAsyncResult ar)
        {
            RegistryClient registryClient = ar.AsyncState as RegistryClient;

            try
            {
                registryClient.Socket.EndSend(ar);
            }
            catch (Exception ex)
            {
                XTrace.WriteLine("发送数据时发生异常:" + ex.ToString());
                lock (m_RegistryClients)
                {
                    m_RegistryClients.Remove(registryClient);
                    registryClient.Dispose();
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 获取到ESBConfig
        /// </summary>
        /// <returns></returns>
        private ESBConfig GetESBConfig(RegistryClient regClient)
        {
            ConsumerConfig consumerConfig = regClient.ConsumerConfig;
            ESBConfig      esbConfig      = new ESBConfig();

            //--处理中心地址
            foreach (SettingUri uri in SettingUri.FindAll())
            {
                switch (uri.UriType)
                {
                case 0:     //--注册中心
                    esbConfig.Registry.Add(new RegistryItem()
                    {
                        Uri = String.Format("{0}:{1}", uri.Uri, uri.Port), Load = 1
                    });
                    break;

                case 1:     //--监控中心
                    esbConfig.Monitor.Add(new MonitorItem()
                    {
                        Uri = String.Format("{0}:{1}", uri.Uri, uri.Port), Load = 1
                    });
                    break;

                case 2:     //--调用中心
                    esbConfig.CallCenter.Add(new CallCenterItem()
                    {
                        Uri = uri.Uri, Load = 1
                    });
                    break;

                case 3:     //--队列中心
                    esbConfig.MessageQueue.Add(new MessageQueueItem()
                    {
                        Uri = String.Format("{0}:{1}:{2}:{3}", uri.Uri, uri.Port, uri.UserName, uri.PassWord), Load = 1
                    });
                    break;

                case 4:     //--缓存中心
                    esbConfig.Cache.Add(new CacheItem()
                    {
                        Uri = String.Format("{0}:{1}", uri.Uri, uri.Port), Load = 1
                    });
                    break;

                default:
                    break;
                }
            }

            //--处理服务
            if (regClient.RegistryClientType == CometClientType.Consumer ||
                regClient.RegistryClientType == CometClientType.Portal ||
                regClient.RegistryClientType == CometClientType.Monitor)
            {
                foreach (var refService in consumerConfig.Reference)
                {
                    List <EsbView_ServiceVersion> lstBS = EsbView_ServiceVersion.FindAllPublish();
                    foreach (EsbView_ServiceVersion bs in lstBS.Where(x => x.ServiceName == refService.ServiceName))
                    {
                        ServiceItem si = new ServiceItem();
                        si.ServiceName = bs.ServiceName;
                        si.Version     = bs.BigVer;
                        si.Binding     = bs.Binding;
                        si.IsDefault   = (bs.BigVer == bs.DefaultVersion);

                        esbConfig.Service.Add(si);
                    }
                }
            }
            else if (regClient.RegistryClientType == CometClientType.CallCenter ||
                     regClient.RegistryClientType == CometClientType.QueueCenter)
            {
                EntityList <EsbView_ServiceVersion> lstBS = EsbView_ServiceVersion.FindAllPublish();
                foreach (var bs in lstBS)
                {
                    ServiceItem si = new ServiceItem();
                    si.ServiceName = bs.ServiceName;
                    si.Version     = bs.BigVer;
                    si.Binding     = bs.Binding;
                    si.IsDefault   = (bs.BigVer == bs.DefaultVersion);

                    esbConfig.Service.Add(si);
                }
            }

            //--处理服务配置
            if (regClient.RegistryClientType == CometClientType.Consumer ||
                regClient.RegistryClientType == CometClientType.Portal ||
                regClient.RegistryClientType == CometClientType.Monitor)
            {
                List <EsbView_ServiceConfig> lstSC = EsbView_ServiceConfig.FindAll();

                foreach (var refService in consumerConfig.Reference)
                {
                    List <EsbView_ServiceConfig> scs = lstSC.FindAll(x => x.ServiceName == refService.ServiceName);
                    foreach (EsbView_ServiceConfig sc in scs)
                    {
                        esbConfig.ServiceConfig.Add(sc);
                    }
                }
            }
            else if (regClient.RegistryClientType == CometClientType.CallCenter ||
                     regClient.RegistryClientType == CometClientType.QueueCenter)
            {
                esbConfig.ServiceConfig = EsbView_ServiceConfig.FindAll();
            }


            return(esbConfig);
        }