Exemple #1
0
 private static bool ExistType(Type type, int netCommand)
 {
     if (!CmdTypes.TryGetValue(netCommand, out HashSet <Type> types))
     {
         return(false);
     }
     return(types.Contains(type));
 }
Exemple #2
0
        private static void LoadSubscriber()
        {
            var handlerTypes = ObjectTypeStorage.GetTypes <ISubscriber>();

            foreach (var type in handlerTypes)
            {
                var attributes = type.GetCustomAttributes <NetCommandAttribute>();
                if (type.IsAbstract)
                {
                    continue;
                }

                if (attributes == null || !attributes.Any())
                {
                    throw new Exception($"类型:{type}必须有SubscriberCMDAttribute特性器指定订阅消息类型.");
                }

                var cmds       = attributes.Select(a => a.MessageCmds).SelectMany(c => c).Distinct().ToList();
                var subscriber = (ISubscriber)Activator.CreateInstance(type);

                foreach (var cmd in cmds)
                {
                    //校验订阅的NetCommand是否相同,相同抛出一个异常。
                    Validate(subscriber, cmd);

                    if (!Subscribers.TryGetValue(cmd, out Dictionary <Type, ISubscriber> subscribers))
                    {
                        subscribers      = new Dictionary <Type, ISubscriber>();
                        Subscribers[cmd] = subscribers;
                    }

                    if (subscriber.MessageType.BaseType == typeof(Enum))
                    {
                        subscribers.Add(typeof(int), subscriber);
                    }
                    else
                    {
                        subscribers.Add(subscriber.MessageType, subscriber);
                    }

                    if (subscriber.MessageType == null)
                    {
                        continue;
                    }

                    if (!CmdTypes.TryGetValue(cmd, out HashSet <Type> types))
                    {
                        types         = new HashSet <Type>();
                        CmdTypes[cmd] = types;
                    }
                    types.Add(subscriber.MessageType);

                    if (!TypeCmds.TryGetValue(subscriber.MessageType, out HashSet <int> msgCmds))
                    {
                        msgCmds = new HashSet <int>();
                        TypeCmds[subscriber.MessageType] = msgCmds;
                    }
                    msgCmds.Add(cmd);
                }
            }
        }