internal void AddActor(BaseActorComponent component) { if (component.IsLocalActor) { this.AddLocal(component); return; } this.AddRemote(component); }
/// <summary> /// 添加一个组件 /// </summary> /// <param name="component"></param> internal void AddComponent(BaseActorComponent component) { var type = component.GetType(); IdComponents[component.Id] = component; if (!ActorTypeComponents.TryGetValue(component.GetType(), out HashSet <BaseActorComponent> components)) { components = new HashSet <BaseActorComponent>(); ActorTypeComponents[type] = components; } components.Add(component); }
private void RemoveLocal(BaseActorComponent component) { if (!this.LocalActors.TryRemove(component.Id, out BaseActorComponent val)) { return; } if (!this.ActorComponents.TryRemove(component.ActorEntity.Id, out val)) { return; } this.Distributions.InnerNetworks.Send(component.Id, (ushort)MSGCommand.RemoveActorCmd); }
/// <summary> /// 发送当前Actor到指定的Network连接服务。 /// </summary> /// <param name="actor">Actor类</param> /// <param name="network">网络类</param> /// <param name="command">消息指令</param> internal static void SendMySelf(this BaseActorComponent actor, Network network, MSGCommand command) { if (!actor.IsLocalActor) { throw new ComponentException("非LocalActor不能发送ActorMySelf消息。"); } var syncMessage = new ActorSyncMessage { ActorId = actor.Id, ActorType = actor.ActorEntity.ActorType, }; network.Send(syncMessage, (ushort)command); }
private void RemoveRemote(BaseActorComponent component) { if (!this.NetIdActors.TryGetValue(component.ActorEntity.Network.Id, out Dictionary <int, BaseActorComponent> dicVal)) { return; } if (!dicVal.Remove(component.ActorEntity.ActorId)) { return; } if (!RemoteActors.TryRemove(component.Id, out BaseActorComponent val)) { return; } }
/// <summary> /// 从当前Actor发送消息给另一个Actor /// </summary> /// <typeparam name="TActorMessage"></typeparam> /// <param name="actor"></param> /// <param name="message"></param> /// <param name="command"></param> internal static void SendActor <TActorMessage>(this BaseActorComponent actor, TActorMessage message, MSGCommand command) where TActorMessage : IActorMessage { if (actor.IsLocalActor) { actor.ReceiveMessage(message, command); } else { var localActorId = message.ActorId; //需要使用远程进程的ActorId发送 message.ActorId = actor.ActorEntity.ActorId; actor.ActorEntity.Network.Send(message, (ushort)command); message.ActorId = localActorId; } }
private void AddRemote(BaseActorComponent component) { if (this.RemoteActors.ContainsKey(component.Id)) { return; } this.RemoteActors[component.Id] = component; var entity = component.ActorEntity; if (!this.NetIdActors.TryGetValue(entity.Network.Id, out Dictionary <int, BaseActorComponent> dicVal)) { dicVal = new Dictionary <int, BaseActorComponent>(); this.NetIdActors[entity.Network.Id] = dicVal; } dicVal[component.ActorEntity.ActorId] = component; }
private void AddLocal(BaseActorComponent component) { if (this.LocalActors.ContainsKey(component.Id)) { return; } this.LocalActors.AddOrUpdate(component.Id, component, (k, v) => component); this.ActorComponents[component.ActorEntity.Id] = component; var message = new ActorSyncMessage { ActorId = component.Id, ActorType = component.ActorEntity.ActorType, }; this.Distributions.InnerNetworks.Send(message, (ushort)MSGCommand.AddActorCmd); }
/// <summary> /// 请求一条Actor消息 /// </summary> /// <typeparam name="TActorRequest"></typeparam> /// <param name="actor"></param> /// <param name="message"></param> /// <param name="command"></param> /// <returns></returns> internal static async Task <TActorResponse> CallActor <TActorResponse>(this BaseActorComponent actor, IActorMessage message, MSGCommand command) where TActorResponse : IActorMessage { TActorResponse response = default; if (actor.IsLocalActor) { response = (TActorResponse)actor.ReceiveRpcMessage(message, command); } else { var localActorId = message.ActorId; //需要使用远程进程的ActorId发送 message.ActorId = actor.ActorEntity.ActorId; response = await actor.ActorEntity.Network.CallMessageAsync <TActorResponse>(message, (ushort)command); message.ActorId = localActorId; } return(response); }
internal void Remove(BaseActorComponent component) { if (component == null) { return; } if (component.ActorEntity == null) { return; } if (this.LocalActors.ContainsKey(component.Id)) { RemoveLocal(component); return; } if (this.RemoteActors.ContainsKey(component.Id)) { this.RemoveRemote(component); } }
/// <summary> /// 添加一个成员 /// </summary> /// <param name="current"></param> /// <param name="member"></param> public static void AddMember(this BaseActorComponent current, BaseActorComponent member) { member.Parent = current; current.Members.AddComponent(member); }
/// <summary> /// 删除一个组件 /// </summary> /// <param name="current"></param> /// <param name="id"></param> /// <returns></returns> public static bool Remove(this BaseActorComponent current, int id) { return(current.Members.Remove(id)); }
/// <summary> /// 获取一个成员 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="current"></param> /// <param name="id"></param> /// <returns></returns> public static T GetMember <T>(this BaseActorComponent current, int id) where T : BaseActorComponent { return(current.Members.GetComponent <T>(id)); }
/// <summary> /// 获取一个组件集合 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static HashSet <BaseActorComponent> GetMembers(this BaseActorComponent current, Type type) { return(current.Members.GetComponents(type)); }
/// <summary> /// 获取一个类型成员集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="current"></param> /// <returns></returns> public static HashSet <BaseActorComponent> GetMembers <T>(this BaseActorComponent current) where T : BaseActorComponent { return(current.Members.GetComponents <T>()); }