/// <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()); } } } }
/// <summary> /// 向服务器发送数据 /// </summary> /// <param name="message"></param> /// <param name="isAsync">如过为同步调用,则在回传的是后需要释放同步信号</param> public void SendData(CometMessageAction action, String message, Boolean isAsync = true) { if (m_SocketClient == null || !m_SocketClient.Connected) { throw new Exception("无法连接服务器!"); } try { CometMessage regMessage = new CometMessage() { Action = action, IsAsync = isAsync, ClientType = m_ClientType, ClientVersion = ESBProxy.GetInstance().Version.Replace("MB.ESB.Core ", ""), ProcessorID = Process.GetCurrentProcess().Id, DotNetFramworkVersion = Environment.Version.ToString(), OSVersion = Environment.OSVersion.ToString(), MessageBody = message }; String dataMessage = XmlUtil.SaveXmlFromObj <CometMessage>(regMessage); Console.WriteLine("发送数据:{0}", dataMessage); Byte[] data = Encoding.UTF8.GetBytes(dataMessage); m_SocketClient.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), m_SocketClient); } catch (Exception ex) { OnLostConnection(ex.ToString()); } }
/// <summary> /// 向单个客户端发送数据 /// </summary> /// <param name="rsClient"></param> /// <param name="data"></param> /// <param name="isAsync">默认为异步调用</param> public void SendData(RegistryClient registryClient, CometMessageAction action, String data, Boolean isAsync = true) { try { CometMessage rm = new CometMessage() { Action = action, MessageBody = data, IsAsync = isAsync }; String messageData = XmlUtil.SaveXmlFromObj<CometMessage>(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(); } } }
/// <summary> /// 向服务器发送数据 /// </summary> /// <param name="message"></param> /// <param name="isAsync">如过为同步调用,则在回传的是后需要释放同步信号</param> public void SendData(CometMessageAction action, String message, Boolean isAsync = true) { if (m_SocketClient == null || !m_SocketClient.Connected) { throw new Exception("无法连接服务器!"); } try { CometMessage regMessage = new CometMessage() { Action = action, IsAsync = isAsync, ClientType = m_ClientType, ClientVersion = ESBProxy.GetInstance().Version.Replace("MB.ESB.Core ", ""), ProcessorID = Process.GetCurrentProcess().Id, DotNetFramworkVersion = Environment.Version.ToString(), OSVersion = Environment.OSVersion.ToString(), MessageBody = message }; String dataMessage = XmlUtil.SaveXmlFromObj<CometMessage>(regMessage); Console.WriteLine("发送数据:{0}", dataMessage); Byte[] data = Encoding.UTF8.GetBytes(dataMessage); m_SocketClient.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), m_SocketClient); } catch (Exception ex) { OnLostConnection(ex.ToString()); } }
/// <summary> /// 接收到注册中心的连接客户端列表 /// </summary> /// <param name="rm"></param> private void OnListRegistryClient(CometMessage rm) { m_RegistryClientList = XmlUtil.LoadObjFromXML<List<RegistryClient>>(rm.MessageBody); //--如果是同步调用,则需要在消息返回时释放信号 if (!rm.IsAsync) m_AutoResetEvent.Set(); }
/// <summary> /// 接收到服务配置信息 /// </summary> private void OnReceiveServiceConfig(CometMessage rm) { //--如果ESBConfig为NULL,则说明注册中心还没连上 if (m_ESBProxy.ESBConfig == null) { m_ESBProxy.ESBConfig = XmlUtil.LoadObjFromXML<ESBConfig>(rm.MessageBody); m_ESBProxy.MessageQueueClient.ConnectAsync(); } else { //--从返回消息中加载ESBConfig配置文件 m_ESBProxy.ESBConfig = XmlUtil.LoadObjFromXML<ESBConfig>(rm.MessageBody); if (m_ESBProxy.ESBConfig != null) m_ESBProxy.Status = ESBProxy.ESBProxyStatus.Ready; } //--如果是同步调用,则需要在消息返回时释放信号 if (!rm.IsAsync) m_AutoResetEvent.Set(); //--异步将配置文件序列化到本地存储 ThreadPoolX.QueueUserWorkItem(x => { m_ConfigurationManager.SaveESBConfig(m_ESBProxy.ESBConfig); }); //--如果客户端订阅了这个事件,则触发并送上最新的ESBConfig if (OnServiceConfigChange != null) { OnServiceConfigChange(this, new RegistryEventArgs() { ESBConfig = m_ESBProxy.ESBConfig }); } }