public void DispatchMessage <TMessage>(TMessage message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } var messageType = typeof(TMessage); MessageTypeAttribute messageTypeDescription = MessageTypeAttribute.GetMessageTypeDescription(messageType); if (_handlers.ContainsKey(messageType)) { var messageHandlers = _handlers[messageType].ToArray(); foreach (var messageHandler in messageHandlers) { var tMessageHandler = (DelegateHandler <TMessage>)messageHandler; if (!tMessageHandler.IsEnabled) { continue; } var evtArgs = new MessageDispatchEventArgs(message, messageHandler.GetType(), messageHandler); switch (tMessageHandler.HandlerId.LogType) { case LogEnum.DevConsole: if (DevMode.IsDevMode) { Write.DevDebug($"({messageType.Name})->({tMessageHandler.HandlerId.Location.Name}){tMessageHandler.HandlerId.Description}"); } break; case LogEnum.UserConsole: Write.UserInfo($"({messageType.Name})->({tMessageHandler.HandlerId.Location.Name}){tMessageHandler.HandlerId.Description}"); break; case LogEnum.Log: Logger.InfoDebugLine($"({messageType.Name})->({tMessageHandler.HandlerId.Location.Name}){tMessageHandler.HandlerId.Description}"); break; case LogEnum.None: default: break; } tMessageHandler.Handle(message); } } else if (!messageTypeDescription.IsCanNoHandler) { Write.DevWarn(messageType.FullName + "类型的消息没有对应的处理器"); } }
public void DispatchMessage <TMessage>(TMessage message) { if (message == null) { throw new ArgumentNullException("message"); } var messageType = typeof(TMessage); MessageTypeAttribute messageTypeDescription = MessageTypeAttribute.GetMessageTypeDescription(messageType); if (_handlers.ContainsKey(messageType)) { var messageHandlers = _handlers[messageType].ToArray(); foreach (var messageHandler in messageHandlers) { var tMessageHandler = (DelegateHandler <TMessage>)messageHandler; var evtArgs = new MessageDispatchEventArgs(message, messageHandler.GetType(), messageHandler); if (tMessageHandler.HandlerId.LogType == LogEnum.Log) { Global.Logger.InfoDebugLine($"{messageTypeDescription.Description}({messageType.Name}) -> ({tMessageHandler.HandlerId.Location.Name}){tMessageHandler.HandlerId.Description}"); } if (tMessageHandler.HandlerId.LogType == LogEnum.Console) { Global.DebugLine($"{messageTypeDescription.Description}({messageType.Name}) -> ({tMessageHandler.HandlerId.Location.Name}){tMessageHandler.HandlerId.Description}"); } this.Dispatching?.Invoke(this, evtArgs); try { tMessageHandler.Handle(message); this.Dispatched?.Invoke(this, evtArgs); } catch (Exception e) { this.DispatchFailed?.Invoke(this, evtArgs); Global.Logger.ErrorDebugLine(tMessageHandler.GetType().FullName + ":" + messageType.FullName + ":" + e.Message, e); throw; } } } else if (!messageTypeDescription.IsCanNoHandler) { Global.Logger.WarnDebugLine(messageType.FullName + "类型的消息没有对应的处理器"); } }
public static MessageTypeAttribute GetMessageTypeAttribute(Type messageType) { if (_messageTypeAttributeDic.ContainsKey(messageType)) { return(_messageTypeAttributeDic[messageType]); } object atrrObj = messageType.GetCustomAttributes(typeof(MessageTypeAttribute), false).FirstOrDefault(); MessageTypeAttribute attr; if (atrrObj == null) { attr = new MessageTypeAttribute(messageType.Name); } else { attr = (MessageTypeAttribute)atrrObj; } _messageTypeAttributeDic.Add(messageType, attr); return(attr); }
public static MessageTypeAttribute GetMessageTypeDescription(Type messageType) { if (_messageTypeDescriptionDic.ContainsKey(messageType)) { return(_messageTypeDescriptionDic[messageType]); } object atrrObj = messageType.GetCustomAttributes(typeof(MessageTypeAttribute), false).FirstOrDefault(); MessageTypeAttribute messageTypeDescription; if (atrrObj == null) { messageTypeDescription = new MessageTypeAttribute(messageType, messageType.Name); } else { messageTypeDescription = (MessageTypeAttribute)atrrObj; } _messageTypeDescriptionDic.Add(messageType, messageTypeDescription); return(messageTypeDescription); }
public void Dispatch <TMessage>(TMessage message) where TMessage : IMessage { if (message == null) { throw new ArgumentNullException(nameof(message)); } var messageType = typeof(TMessage); if (_handlers.TryGetValue(messageType, out List <object> list)) { var messagePaths = list.ToArray(); foreach (var messagePath in messagePaths) { var tMessagePath = (MessagePath <TMessage>)messagePath; // isMatch表示该处路径是否可以通过该消息,因为有些路径的PathId属性不为Guid.Empty,非空PathId的路径只允许特定标识造型的消息通过 // PathId可以认为是路径的形状,唯一的PathId表明该路径具有唯一的形状从而只允许和路径的形状一样的消息结构体穿过 bool isMatch = tMessagePath.PathId == Guid.Empty || message is ICmd; if (!isMatch && message is IEvent evt) { isMatch = tMessagePath.PathId == evt.BornPathId; } if (isMatch) { // ViaLimite小于0表示是不限定通过的次数的路径,不限定通过的次数的路径不需要消息每通过一次递减一次ViaLimit计数 if (tMessagePath.ViaLimit > 0) { lock (tMessagePath.Locker) { if (tMessagePath.ViaLimit > 0) { tMessagePath.ViaLimit--; if (tMessagePath.ViaLimit == 0) { // ViaLimit递减到0从路径列表中移除该路径 Disconnect(tMessagePath); } } } } } if (!tMessagePath.IsEnabled) { continue; } if (isMatch) { switch (tMessagePath.LogType) { case LogEnum.DevConsole: if (DevMode.IsDevMode) { Write.DevDebug($"({messageType.Name})->({tMessagePath.Location.Name}){tMessagePath.Description}"); } break; case LogEnum.Log: Logger.InfoDebugLine($"({messageType.Name})->({tMessagePath.Location.Name}){tMessagePath.Description}"); break; case LogEnum.None: default: break; } tMessagePath.Go(message); } } } else { MessageTypeAttribute messageTypeAttr = MessageTypeAttribute.GetMessageTypeAttribute(messageType); if (!messageTypeAttr.IsCanNoHandler) { Write.DevWarn(messageType.FullName + "类型的消息没有对应的处理器"); } } }