public void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message) { // 收到actor rpc request if (message is ActorRpcRequest actorRpcRequest) { Entity entity = Game.Scene.GetComponent <ActorManagerComponent>().Get(actorRpcRequest.Id); if (entity == null) { Log.Warning($"not found actor: {actorRpcRequest.Id}"); ActorRpcResponse response = new ActorRpcResponse { RpcId = actorRpcRequest.RpcId, Error = ErrorCode.ERR_NotFoundActor }; session.Reply(response); return; } entity.GetComponent <ActorComponent>().Add(new ActorMessageInfo() { Session = session, Message = actorRpcRequest }); return; } // 收到actor消息分发给actor自己去处理 if (message is ActorRequest actorRequest) { Entity entity = Game.Scene.GetComponent <ActorManagerComponent>().Get(actorRequest.Id); if (entity == null) { Log.Warning($"not found actor: {actorRequest.Id}"); ActorResponse response = new ActorResponse { RpcId = actorRequest.RpcId, Error = ErrorCode.ERR_NotFoundActor }; session.Reply(response); return; } entity.GetComponent <ActorComponent>().Add(new ActorMessageInfo() { Session = session, Message = actorRequest }); return; } if (message is AMessage || message is ARequest) { Game.Scene.GetComponent <MessageDispatherComponent>().Handle(session, message); return; } throw new Exception($"message type error: {message.GetType().FullName}"); }
public override async Task <AResponse> Run() { ActorRpcRequest request = new ActorRpcRequest() { Id = this.proxy.Id, AMessage = this.message }; ActorRpcResponse response = await this.proxy.RealCall <ActorRpcResponse>(request, this.proxy.CancellationTokenSource.Token); if (response.Error != ErrorCode.ERR_NotFoundActor) { this.Tcs.SetResult((Response)response.AMessage); } return(response); }
public async Task Handle(Session session, Entity entity, ActorRequest message) { try { Request request = message.AMessage as Request; if (request == null) { Log.Error($"消息类型转换错误: {message.GetType().FullName} to {typeof (Request).Name}"); return; } E e = entity as E; if (e == null) { Log.Error($"Actor类型转换错误: {entity.GetType().Name} to {typeof(E).Name}"); return; } await this.Run(e, request, response => { // 等回调回来,session可以已经断开了,所以需要判断session id是否为0 if (session.Id == 0) { return; } ActorRpcResponse actorResponse = new ActorRpcResponse { RpcId = message.RpcId, AMessage = response }; session.Reply(actorResponse); }); } catch (Exception e) { throw new Exception($"解释消息失败: {message.GetType().FullName}", e); } }