public static void Broadcast(Unit unit, IActorMessage message) { Dictionary <long, AOIEntity> dict = unit.GetBeSeePlayers(); (ushort _, MemoryStream memoryStream) = MessageSerializeHelper.MessageToStream(message); foreach (AOIEntity u in dict.Values) { ActorMessageSenderComponent.Instance.Send(u.Unit.GetComponent <UnitGateComponent>().GateSessionActorId, memoryStream); } }
public void Handle(Session session, MemoryStream memoryStream) { ushort opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.KcpOpcodeIndex); Type type = OpcodeTypeComponent.Instance.GetType(opcode); object message = MessageSerializeHelper.DeserializeFrom(opcode, type, memoryStream); if (message is IResponse response) { session.OnRead(opcode, response); return; } OpcodeHelper.LogMsg(session.DomainZone(), opcode, message); // 普通消息或者是Rpc请求消息 MessageDispatcherComponent.Instance.Handle(session, opcode, message); }
public static async ETTask <IActorResponse> Call( this ActorMessageSenderComponent self, long actorId, IActorRequest request, bool needException = true ) { request.RpcId = self.GetRpcId(); if (actorId == 0) { throw new Exception($"actor id is 0: {request}"); } (ushort _, MemoryStream stream) = MessageSerializeHelper.MessageToStream(request); return(await self.Call(actorId, request.RpcId, stream, needException)); }
public static async ETTask <IActorResponse> Call(this ActorLocationSenderComponent self, long entityId, IActorRequest iActorRequest) { ActorLocationSender actorLocationSender = self.GetOrCreate(entityId); // 先序列化好 int rpcId = ActorMessageSenderComponent.Instance.GetRpcId(); iActorRequest.RpcId = rpcId; (ushort _, MemoryStream stream) = MessageSerializeHelper.MessageToStream(iActorRequest); long actorLocationSenderInstanceId = actorLocationSender.InstanceId; using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.ActorLocationSender, entityId)) { if (actorLocationSender.InstanceId != actorLocationSenderInstanceId) { throw new RpcException(ErrorCore.ERR_ActorTimeout, $"{stream.ToActorMessage()}"); } // 队列中没处理的消息返回跟上个消息一样的报错 if (actorLocationSender.Error == ErrorCore.ERR_NotFoundActor) { return(ActorHelper.CreateResponse(iActorRequest, actorLocationSender.Error)); } try { return(await self.CallInner(actorLocationSender, rpcId, stream)); } catch (RpcException) { self.Remove(actorLocationSender.Id); throw; } catch (Exception e) { self.Remove(actorLocationSender.Id); throw new Exception($"{stream.ToActorMessage()}", e); } } }
public void Handle(Session session, MemoryStream memoryStream) { ushort opcode = 0; try { long actorId = BitConverter.ToInt64(memoryStream.GetBuffer(), Packet.ActorIdIndex); opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex); Type type = null; object message = null; // 内网收到外网消息,有可能是gateUnit消息,还有可能是gate广播消息 if (OpcodeTypeComponent.Instance.IsOutrActorMessage(opcode)) { InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId); instanceIdStruct.Process = Game.Options.Process; long realActorId = instanceIdStruct.ToLong(); Entity entity = Game.EventSystem.Get(realActorId); if (entity == null) { type = OpcodeTypeComponent.Instance.GetType(opcode); message = MessageSerializeHelper.DeserializeFrom(opcode, type, memoryStream); Log.Error($"not found actor: {session.DomainScene().Name} {opcode} {realActorId} {message}"); return; } if (entity is Session gateSession) { // 发送给客户端 memoryStream.Seek(Packet.OpcodeIndex, SeekOrigin.Begin); gateSession.Send(0, memoryStream); return; } } type = OpcodeTypeComponent.Instance.GetType(opcode); message = MessageSerializeHelper.DeserializeFrom(opcode, type, memoryStream); if (message is IResponse iResponse && !(message is IActorResponse)) { session.OnRead(opcode, iResponse); return; } OpcodeHelper.LogMsg(session.DomainZone(), opcode, message); // 收到actor消息,放入actor队列 switch (message) { case IActorRequest iActorRequest: { InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId); int fromProcess = instanceIdStruct.Process; instanceIdStruct.Process = Game.Options.Process; long realActorId = instanceIdStruct.ToLong(); void Reply(IActorResponse response) { Session replySession = NetInnerComponent.Instance.Get(fromProcess); // 发回真实的actorId 做查问题使用 replySession.Send(realActorId, response); } InnerMessageDispatcherHelper.HandleIActorRequest(opcode, realActorId, iActorRequest, Reply); return; } case IActorResponse iActorResponse: { InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId); instanceIdStruct.Process = Game.Options.Process; long realActorId = instanceIdStruct.ToLong(); InnerMessageDispatcherHelper.HandleIActorResponse(opcode, realActorId, iActorResponse); return; } case IActorMessage iactorMessage: { InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId); instanceIdStruct.Process = Game.Options.Process; long realActorId = instanceIdStruct.ToLong(); InnerMessageDispatcherHelper.HandleIActorMessage(opcode, realActorId, iactorMessage); return; } default: { MessageDispatcherComponent.Instance.Handle(session, opcode, message); break; } } } catch (Exception e) { Log.Error($"InnerMessageDispatcher error: {opcode}\n{e}"); } }