示例#1
0
        public static async ETVoid HandleAsync(this MailBoxComponent self)
        {
            MailboxDispatcherComponent mailboxDispatcherComponent = Game.Scene.GetComponent <MailboxDispatcherComponent>();

            long instanceId = self.InstanceId;

            while (true)
            {
                if (self.InstanceId != instanceId)
                {
                    return;
                }
                try
                {
                    ActorMessageInfo info = await self.GetAsync();

                    // 返回null表示actor已经删除,协程要终止
                    if (info.Message == null)
                    {
                        return;
                    }

                    // 根据这个mailbox类型分发给相应的处理
                    await mailboxDispatcherComponent.Handle(self, info);
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
            }
        }
示例#2
0
        public static async ETVoid HandleIActorMessage(Session session, IActorMessage iActorMessage)
        {
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(iActorMessage.ActorId);
            int replyId = instanceIdStruct.Process;

            instanceIdStruct.Process = IdGenerater.Process;
            iActorMessage.ActorId    = instanceIdStruct.ToLong();

            Entity entity = Game.EventSystem.Get(iActorMessage.ActorId);

            if (entity == null)
            {
                Log.Error($"not found actor: {MongoHelper.ToJson(iActorMessage)}");
                return;
            }

            MailBoxComponent mailBoxComponent = entity.GetComponent <MailBoxComponent>();

            if (mailBoxComponent == null)
            {
                Log.Error($"actor not add MailBoxComponent: {entity.GetType().Name} {iActorMessage}");
                return;
            }

            Session ss = NetInnerComponent.Instance.Get(replyId);
            await mailBoxComponent.Handle(ss, iActorMessage);
        }
        public static async void HandleAsync(this MailBoxComponent self)
        {
            ActorMessageDispatherComponent actorMessageDispatherComponent = Game.Scene.GetComponent <ActorMessageDispatherComponent>();

            long instanceId = self.InstanceId;

            while (true)
            {
                if (self.InstanceId != instanceId)
                {
                    return;
                }
                try
                {
                    ActorMessageInfo info = await self.GetAsync();

                    // 返回null表示actor已经删除,协程要终止
                    if (info.Message == null)
                    {
                        return;
                    }

                    // 根据这个actor的类型分发给相应的ActorHandler处理
                    await actorMessageDispatherComponent.ActorTypeHandle(self.ActorType, (Entity)self.Parent, info);
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
            }
        }
        public static async void HandleIActorMessage(Session session, IActorMessage iActorMessage)
        {
            long replyId = IdGenerater.GetProcessId(iActorMessage.ActorId);

            iActorMessage.ActorId = iActorMessage.ActorId & IdGenerater.HeadMask | IdGenerater.Head;

            Entity entity = Game.EventSystem.Get(iActorMessage.ActorId);

            if (entity == null)
            {
                Log.Error($"not found actor: {DCETRuntime.MongoHelper.ToJson(iActorMessage)}");
                return;
            }

            MailBoxComponent mailBoxComponent = entity.GetComponent <MailBoxComponent>();

            if (mailBoxComponent == null)
            {
                Log.Error($"actor not add MailBoxComponent: {entity.GetType().Name} {iActorMessage}");
                return;
            }

            Session ss = NetInnerComponent.Instance.Get(replyId);
            await mailBoxComponent.Handle(ss, iActorMessage);
        }
示例#5
0
        private async ETVoid HandleIActorRequest(Session session, IActorRequest message)
        {
            using (await CoroutineLockComponent.Instance.Wait(message.ActorId))
            {
                Entity entity = (Entity)Game.EventSystem.Get(message.ActorId);
                if (entity == null)
                {
                    Log.Warning($"not found actor: {message}");
                    ActorResponse response = new ActorResponse
                    {
                        Error = ErrorCode.ERR_NotFoundActor,
                        RpcId = message.RpcId
                    };
                    session.Reply(response);
                    return;
                }

                MailBoxComponent mailBoxComponent = entity.GetComponent <MailBoxComponent>();
                if (mailBoxComponent == null)
                {
                    ActorResponse response = new ActorResponse
                    {
                        Error = ErrorCode.ERR_ActorNoMailBoxComponent,
                        RpcId = message.RpcId
                    };
                    session.Reply(response);
                    Log.Error($"actor not add MailBoxComponent: {entity.GetType().Name} {message}");
                    return;
                }

                await mailBoxComponent.Add(session, message);
            }
        }
示例#6
0
        public static async ETTask Handle(this MailBoxComponent self, Session session, IActorMessage message)
        {
            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Mailbox, message.ActorId))
            {
                switch (self.MailboxType)
                {
                case MailboxType.GateSession:
                    IActorMessage iActorMessage = message as IActorMessage;
                    // 发送给客户端
                    Session clientSession = self.Parent as Session;
                    iActorMessage.ActorId = 0;
                    clientSession.Send(iActorMessage);
                    break;

                case MailboxType.MessageDispatcher:
                    await ActorMessageDispatcherComponent.Instance.Handle(self.Parent, session, message);

                    break;

                case MailboxType.UnOrderMessageDispatcher:
                    self.HandleInner(session, message).Coroutine();
                    break;
                }
            }
        }
 public static async Task RemoveLocation(this MailBoxComponent self)
 {
     if (!self.IsLocation)
     {
         return;
     }
     await Game.Scene.GetComponent <LocationProxyComponent>().Remove(self.Entity.Id);
 }
示例#8
0
        public void Awake(int account)
        {
            this.Account = account;
            //添加MailBoxComponent组件
            mailBoxComponent = this.AddComponent <MailBoxComponent>();

            //mailBoxComponent = this.AddComponent<MailBoxComponent, string>(MailboxType.GateSession);
        }
示例#9
0
        public void Dispatch(Session session, Packet packet)
        {
            IMessage message;

            try
            {
                Type messageType = Game.Scene.GetComponent <OpcodeTypeComponent>().GetType(packet.Opcode);
                message = (IMessage)session.Network.MessagePacker.DeserializeFrom(messageType, packet.Stream);
            }
            catch (Exception e)
            {
                // 出现任何解析消息异常都要断开Session,防止客户端伪造消息
                Log.Error(e);
                session.Error = ErrorCode.ERR_PacketParserError;
                session.Network.Remove(session.Id);
                return;
            }


            // 收到actor消息,放入actor队列
            if (message is IActorMessage iActorMessage)
            {
                Entity entity = (Entity)Game.EventSystem.Get(iActorMessage.ActorId);
                if (entity == null)
                {
                    Log.Warning($"not found actor: {iActorMessage.ActorId}");
                    ActorResponse response = new ActorResponse
                    {
                        Error = ErrorCode.ERR_NotFoundActor,
                        RpcId = iActorMessage.RpcId
                    };
                    session.Reply(response);
                    return;
                }

                MailBoxComponent mailBoxComponent = entity.GetComponent <MailBoxComponent>();
                if (mailBoxComponent == null)
                {
                    ActorResponse response = new ActorResponse
                    {
                        Error = ErrorCode.ERR_ActorNoMailBoxComponent,
                        RpcId = iActorMessage.RpcId
                    };
                    session.Reply(response);

                    Log.Error($"actor没有挂载MailBoxComponent组件: {entity.GetType().Name} {entity.Id}");
                    return;
                }

                mailBoxComponent.Add(new ActorMessageInfo()
                {
                    Session = session, Message = iActorMessage
                });
                return;
            }

            Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, new MessageInfo(packet.Opcode, message));
        }
示例#10
0
        /// <summary>
        /// 从堆栈获得一个消息进行处理
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        private static Task <ActorMessageInfo> GetAsync(this MailBoxComponent self)
        {
            if (self.Queue.Count > 0)
            {
                return(Task.FromResult(self.Queue.Dequeue()));
            }

            self.Tcs = new TaskCompletionSource <ActorMessageInfo>();
            return(self.Tcs.Task);
        }
示例#11
0
        /// <summary>
        /// 根据mailbox类型做不同的处理,有两种类型gate session类型的Mailbox,消息分发类型的Mailbox,对mailbox中的消息进行分发处理
        /// </summary>
        public static async ETTask Handle(
            this MailboxDispatcherComponent self, MailBoxComponent mailBoxComponent, Session session, object message)
        {
            IMailboxHandler iMailboxHandler;

            if (self.MailboxHandlers.TryGetValue(mailBoxComponent.MailboxType, out iMailboxHandler))
            {
                await iMailboxHandler.Handle(session, mailBoxComponent.Entity, message);
            }
        }
示例#12
0
        /// <summary>
        /// 根据mailbox类型做不同的处理
        /// </summary>
        public static async ETTask Handle(
            this MailboxDispatcherComponent self, MailBoxComponent mailBoxComponent, ActorMessageInfo actorMessageInfo)
        {
            IMailboxHandler iMailboxHandler;

            if (self.MailboxHandlers.TryGetValue(mailBoxComponent.MailboxType, out iMailboxHandler))
            {
                await iMailboxHandler.Handle(actorMessageInfo.Session, mailBoxComponent.Entity, actorMessageInfo.Message);
            }
        }
示例#13
0
        /// <summary>
        /// 根据mailbox类型做不同的处理
        /// </summary>
        public static async ETTask Handle(this MailboxDispatcherComponent self, MailBoxComponent mailBoxComponent, Session session, object message)
        {
            IMailboxHandler iMailboxHandler;

            //根据邮箱类型 获取邮箱...
            if (self.MailboxHandlers.TryGetValue(mailBoxComponent.MailboxType, out iMailboxHandler))
            {
                //交由邮箱处理请求 邮箱是挂载在会话实体上的...外网的会话实体
                await iMailboxHandler.Handle(session, mailBoxComponent.Entity, message);
            }
        }
示例#14
0
        /// <summary>
        /// 添加一个消息到发送缓存中,发送
        /// </summary>
        /// <param name="self"></param>
        /// <param name="info"></param>
        public static void Add(this MailBoxComponent self, ActorMessageInfo info)
        {
            self.Queue.Enqueue(info);

            if (self.Tcs == null)
            {
                return;
            }

            var t = self.Tcs;

            self.Tcs = null;
            t.SetResult(self.Queue.Dequeue());
        }
        public void Dispatch(Session session, Packet packet)
        {
            ushort   opcode      = packet.Opcode;
            Type     messageType = Game.Scene.GetComponent <OpcodeTypeComponent>().GetType(opcode);
            IMessage message     = (IMessage)session.Network.MessagePacker.DeserializeFrom(messageType, packet.Bytes, Packet.Index, packet.Length - Packet.Index);

            // 收到actor消息,放入actor队列
            if (message is IActorLanuch iActorLaunch)
            {
                Entity entity = (Entity)Game.EventSystem.Get(iActorLaunch.ActorId);
                if (entity == null)
                {
                    Log.Warning($"not found actor: {iActorLaunch.ActorId}");
                    ActorResponse response = new ActorResponse
                    {
                        Error = ErrorCode.ERR_NotFoundActor,
                        RpcId = iActorLaunch.RpcId
                    };
                    session.Reply(response);
                    return;
                }

                MailBoxComponent mailBoxComponent = entity.GetComponent <MailBoxComponent>();
                if (mailBoxComponent == null)
                {
                    ActorResponse response = new ActorResponse
                    {
                        Error = ErrorCode.ERR_ActorNoActorComponent,
                        RpcId = iActorLaunch.RpcId
                    };
                    session.Reply(response);
                    Log.Error($"actor没有挂载ActorComponent组件: {entity.GetType().Name} {entity.Id}");
                    return;
                }

                mailBoxComponent.Add(new ActorMessageInfo()
                {
                    Session = session, Launch = iActorLaunch
                });
                return;
            }

            Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, new MessageInfo(opcode, message));
        }
示例#16
0
        private async ETVoid HandleIActorMessage(Session session, IActorMessage message)
        {
            using (await CoroutineLockComponent.Instance.Wait(message.ActorId))
            {
                Entity entity = (Entity)Game.EventSystem.Get(message.ActorId);
                if (entity == null)
                {
                    Log.Error($"not found actor: {message}");
                    return;
                }

                MailBoxComponent mailBoxComponent = entity.GetComponent <MailBoxComponent>();
                if (mailBoxComponent == null)
                {
                    Log.Error($"actor not add MailBoxComponent: {entity.GetType().Name} {message}");
                    return;
                }
                await mailBoxComponent.Add(session, message);
            }
        }
示例#17
0
        public static async Task Handle(
            this ActorMessageDispatherComponent self, MailBoxComponent mailBoxComponent, ActorMessageInfo actorMessageInfo)
        {
            // 有拦截器使用拦截器处理
            IActorInterceptTypeHandler iActorInterceptTypeHandler;

            if (self.ActorTypeHandlers.TryGetValue(mailBoxComponent.ActorInterceptType, out iActorInterceptTypeHandler))
            {
                await iActorInterceptTypeHandler.Handle(actorMessageInfo.Session, mailBoxComponent.Entity, actorMessageInfo.Message);

                return;
            }

            // 没有拦截器就用IMActorHandler处理
            if (!self.ActorMessageHandlers.TryGetValue(actorMessageInfo.Message.GetType(), out IMActorHandler handler))
            {
                throw new Exception($"not found message handler: {MongoHelper.ToJson(actorMessageInfo.Message)}");
            }

            await handler.Handle(actorMessageInfo.Session, mailBoxComponent.Entity, actorMessageInfo.Message);
        }
示例#18
0
        public static async ETVoid HandleIActorRequest(Session session, IActorRequest iActorRequest)
        {
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(iActorRequest.ActorId);
            int replyId = instanceIdStruct.Process;

            instanceIdStruct.Process = IdGenerater.Process;
            iActorRequest.ActorId    = instanceIdStruct.ToLong();

            string  address = StartProcessConfigCategory.Instance.Get(replyId).InnerAddress;
            Session ss      = NetInnerComponent.Instance.Get(address);
            Entity  entity  = Game.EventSystem.Get(iActorRequest.ActorId);

            if (entity == null)
            {
                Log.Warning($"not found actor: {MongoHelper.ToJson(iActorRequest)}");
                ActorResponse response = new ActorResponse
                {
                    Error = ErrorCode.ERR_NotFoundActor,
                    RpcId = iActorRequest.RpcId,
                };
                ss.Reply(response);
                return;
            }

            MailBoxComponent mailBoxComponent = entity.GetComponent <MailBoxComponent>();

            if (mailBoxComponent == null)
            {
                ActorResponse response = new ActorResponse
                {
                    Error = ErrorCode.ERR_ActorNoMailBoxComponent,
                    RpcId = iActorRequest.RpcId,
                };
                ss.Reply(response);
                Log.Error($"actor not add MailBoxComponent: {entity.GetType().Name} {iActorRequest}");
                return;
            }

            await mailBoxComponent.Handle(ss, iActorRequest);
        }
示例#19
0
        public void Dispatch(Session session, ushort opcode, object message)
        {
            // 收到actor消息,放入actor队列
            if (message is IActorMessage iActorMessage)
            {
                Entity entity = (Entity)Game.EventSystem.Get(iActorMessage.ActorId);
                if (entity == null)
                {
                    Log.Warning($"not found actor: {iActorMessage.ActorId}");
                    ActorResponse response = new ActorResponse
                    {
                        Error = ErrorCode.ERR_NotFoundActor,
                        RpcId = iActorMessage.RpcId
                    };
                    session.Reply(response);
                    return;
                }

                MailBoxComponent mailBoxComponent = entity.GetComponent <MailBoxComponent>();
                if (mailBoxComponent == null)
                {
                    ActorResponse response = new ActorResponse
                    {
                        Error = ErrorCode.ERR_ActorNoMailBoxComponent,
                        RpcId = iActorMessage.RpcId
                    };
                    session.Reply(response);
                    Log.Error($"actor没有挂载MailBoxComponent组件: {entity.GetType().Name} {entity.Id}");
                    return;
                }

                mailBoxComponent.Add(new ActorMessageInfo()
                {
                    Session = session, Message = iActorMessage
                });
                return;
            }

            Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, new MessageInfo(opcode, message));
        }
        public static async void HandleIActorRequest(Session session, IActorRequest iActorRequest)
        {
            long replyId = IdGenerater.GetProcessId(iActorRequest.ActorId);

            iActorRequest.ActorId = iActorRequest.ActorId & IdGenerater.HeadMask | IdGenerater.Head;

            string  address = StartConfigComponent.Instance.GetProcessInnerAddress(replyId);
            Session ss      = NetInnerComponent.Instance.Get(address);
            Entity  entity  = Game.EventSystem.Get(iActorRequest.ActorId);

            if (entity == null)
            {
                Log.Warning($"not found actor: {DCETRuntime.MongoHelper.ToJson(iActorRequest)}");
                ActorResponse response = new ActorResponse
                {
                    Error = ErrorCode.ERR_NotFoundActor,
                    RpcId = iActorRequest.RpcId,
                };
                ss.Reply(response);
                return;
            }

            MailBoxComponent mailBoxComponent = entity.GetComponent <MailBoxComponent>();

            if (mailBoxComponent == null)
            {
                ActorResponse response = new ActorResponse
                {
                    Error = ErrorCode.ERR_ActorNoMailBoxComponent,
                    RpcId = iActorRequest.RpcId,
                };
                ss.Reply(response);
                Log.Error($"actor not add MailBoxComponent: {entity.GetType().Name} {iActorRequest}");
                return;
            }

            await mailBoxComponent.Handle(ss, iActorRequest);
        }
示例#21
0
 public static async ETTask Add(this MailBoxComponent self, Session session, object message)
 {
     //获取到邮箱分发组件 进行处理
     MailboxDispatcherComponent mailboxDispatcherComponent = Game.Scene.GetComponent <MailboxDispatcherComponent>();
     await mailboxDispatcherComponent.Handle(self, session, message);
 }
 public static async Task AddLocation(this MailBoxComponent self)
 {
     self.IsLocation = true;
     await Game.Scene.GetComponent <LocationProxyComponent>().Add(self.Entity.Id, self.Entity.InstanceId);
 }
示例#23
0
 /// <summary>
 /// 注册位置到位置服务器
 /// </summary>
 /// <param name="self"></param>
 /// <returns></returns>
 public static async Task AddLocation(this MailBoxComponent self)                                          //扩展方法必须是静态 且第一个参数是自己 this
 {
     await Game.Scene.GetComponent <LocationProxyComponent>().Add(self.Entity.Id, self.Entity.InstanceId); //角色ID  服务器ID
 }
示例#24
0
 private static async ETVoid HandleInner(this MailBoxComponent self, Session session, IActorMessage message)
 {
     await ActorMessageDispatcherComponent.Instance.Handle(self.Parent, session, message);
 }
示例#25
0
        public void Dispatch(Session session, ushort opcode, object message)
        {
            // 收到actor消息,放入actor队列
            switch (message)
            {
            case IActorRequest iActorRequest:
            {
                Entity entity = (Entity)Game.EventSystem.Get(iActorRequest.ActorId);
                if (entity == null)
                {
                    Log.Warning($"not found actor: {message}");
                    ActorResponse response = new ActorResponse
                    {
                        Error = ErrorCode.ERR_NotFoundActor,
                        RpcId = iActorRequest.RpcId
                    };
                    session.Reply(response);
                    return;
                }

                MailBoxComponent mailBoxComponent = entity.GetComponent <MailBoxComponent>();
                if (mailBoxComponent == null)
                {
                    ActorResponse response = new ActorResponse
                    {
                        Error = ErrorCode.ERR_ActorNoMailBoxComponent,
                        RpcId = iActorRequest.RpcId
                    };
                    session.Reply(response);
                    Log.Error($"actor not add MailBoxComponent: {entity.GetType().Name} {message}");
                    return;
                }

                mailBoxComponent.Add(new ActorMessageInfo()
                    {
                        Session = session, Message = iActorRequest
                    });
                return;
            }

            case IActorMessage iactorMessage:
            {
                Entity entity = (Entity)Game.EventSystem.Get(iactorMessage.ActorId);

                if (entity == null)
                {
                    return;
                }

                //if (entity == null)
                //{
                //	Log.Error($"not found actor: {message}");
                //	return;
                //}

                MailBoxComponent mailBoxComponent = entity.GetComponent <MailBoxComponent>();
                if (mailBoxComponent == null)
                {
                    Log.Error($"actor not add MailBoxComponent: {entity.GetType().Name} {message}");
                    return;
                }

                mailBoxComponent.Add(new ActorMessageInfo()
                    {
                        Session = session, Message = iactorMessage
                    });

                return;
            }

            default:
            {
                Game.Scene.GetComponent <MessageDispatcherComponent>().Handle(session, new MessageInfo(opcode, message));
                break;
            }
            }
        }
示例#26
0
 public static async ETTask AddLocation(this MailBoxComponent self)
 {        // 向位置服务器注册 Unit单位的Id以及实例Id
     await Game.Scene.GetComponent <LocationProxyComponent>().Add(self.Entity.Id, self.Entity.InstanceId);
 }
示例#27
0
 public static async ETTask RemoveLocation(this MailBoxComponent self)
 {
     await Game.Scene.GetComponent <LocationProxyComponent>().Remove(self.Entity.Id);
 }