public static void Load() { var msgTypes = ObjectTypeStorage.GetTypes <IMessage>(); foreach (var type in msgTypes) { var attribute = type.GetCustomAttribute <NetMessageTypeAttribute>(); if (attribute == null) { throw new Exception($"类型:{type}必须有MessageTypeAttribute特性器指定消息类型."); } if (attribute.TypeCode == (int)BasicMessageType.Ignore) { continue; } MsgCodes[type] = attribute.TypeCode; CodeMsgTypes[attribute.TypeCode] = type; } var valTypes = GetValueTypeCode(); foreach (var val in valTypes) { MsgCodes[val.Key] = (int)val.Value; CodeMsgTypes[(int)val.Value] = val.Key; } MsgCodes[typeof(H6Null)] = 0; CodeMsgTypes[0] = typeof(H6Null); }
public override void Awake() { var types = ObjectTypeStorage.GetTypes <BaseActor>(); foreach (var type in types) { using (var component = ObjectStorage.Fetch(type) as BaseActorComponent) { ActorTypes[component.ActorType] = type; } } this.Distributions = Game.Scene.AddComponent <NetDistributionsComponent>(); this.Distributions.OnDisconnected += network => { if (!NetIdActors.TryGetValue(network.Id, out Dictionary <int, BaseActorComponent> dicVal)) { return; } this.OnDisconnected?.Invoke(network); var acotrs = dicVal.Values.ToList(); foreach (var acotr in acotrs) { acotr.Dispose(); } }; this.Distributions.OnConnected += network => { this.OnConnected?.Invoke(network); network.Send((ushort)MSGCommand.SyncActorInfoCmd); }; }
private void AddRpositoryComponents() { var sysConfig = new DBConfig().Config; var sysDbClient = new MongoClient(sysConfig.ConnectionString); var sysDb = sysDbClient.GetDatabase(sysConfig.DatabaseName); var logConfig = new LoggerConfig().Config.DBConfig; var logDbClient = new MongoClient(logConfig.ConnectionString); var logDb = logDbClient.GetDatabase(logConfig.DatabaseName); var types = ObjectTypeStorage.GetTypes <IRpository>(); foreach (var type in types) { AddComponent(type, sysConfig, logConfig, sysDbClient, logDbClient, sysDb, logDb); } }
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); } } }