コード例 #1
0
        public static void HandleIActorMessage(ushort opcode, long actorId, IActorMessage iActorMessage)
        {
            OpcodeHelper.LogMsg(opcode, actorId, iActorMessage);

            Entity entity = Game.EventSystem.Get(actorId);

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

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

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

            switch (mailBoxComponent.MailboxType)
            {
            case MailboxType.MessageDispatcher:
            {
                async ETTask MessageDispatcherHandler()
                {
                    long instanceId = entity.InstanceId;

                    using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Mailbox, actorId))
                    {
                        if (entity.InstanceId != instanceId)
                        {
                            return;
                        }

                        await ActorMessageDispatcherComponent.Instance.Handle(entity, iActorMessage, null);
                    }
                }

                MessageDispatcherHandler().Coroutine();
                break;
            }

            case MailboxType.UnOrderMessageDispatcher:
            {
                ActorMessageDispatcherComponent.Instance.Handle(entity, iActorMessage, null).Coroutine();
                break;
            }
            }
        }
コード例 #2
0
        public void Dispatch(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);

            DispatchAsync(session, opcode, message).Coroutine();
        }
コード例 #3
0
ファイル: Session.cs プロジェクト: suziye123/ET
        public void OnRead(ushort opcode, IResponse response)
        {
            OpcodeHelper.LogMsg(this.DomainZone(), opcode, response);

            if (!this.requestCallbacks.TryGetValue(response.RpcId, out var action))
            {
                return;
            }

            this.requestCallbacks.Remove(response.RpcId);
            if (ErrorCore.IsRpcNeedThrowException(response.Error))
            {
                action.Tcs.SetException(new Exception($"Rpc error, request: {action.Request} response: {response}"));
                return;
            }
            action.Tcs.SetResult(response);
        }
コード例 #4
0
ファイル: Session.cs プロジェクト: suziye123/ET
        public void Send(IMessage message)
        {
            switch (this.AService.ServiceType)
            {
            case ServiceType.Inner:
            {
                (ushort opcode, MemoryStream stream) = MessageSerializeHelper.MessageToStream(0, message);
                OpcodeHelper.LogMsg(this.DomainZone(), opcode, message);
                this.Send(0, stream);
                break;
            }

            case ServiceType.Outer:
            {
                (ushort opcode, MemoryStream stream) = MessageSerializeHelper.MessageToStream(message);
                OpcodeHelper.LogMsg(this.DomainZone(), opcode, message);
                this.Send(0, stream);
                break;
            }
            }
        }
コード例 #5
0
ファイル: OuterMessageDispatcher.cs プロジェクト: liws/ETX
        public void Dispatch(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 (TimeHelper.ClientFrameTime() - this.lastMessageTime > 3000)
            {
                Log.Info($"可能导致卡死的消息: {this.LastMessage}");
            }

            this.lastMessageTime = TimeHelper.ClientFrameTime();
            this.LastMessage     = message;

            if (message is IResponse response)
            {
                session.OnRead(opcode, response);
                return;
            }

            OpcodeHelper.LogMsg(session.DomainZone(), opcode, message);
            // 普通消息或者是Rpc请求消息
            MessageDispatcherComponent.Instance.Handle(session, opcode, message);
        }
コード例 #6
0
ファイル: Session.cs プロジェクト: suziye123/ET
 public void Send(long actorId, IMessage message)
 {
     (ushort opcode, MemoryStream stream) = MessageSerializeHelper.MessageToStream(actorId, message);
     OpcodeHelper.LogMsg(this.DomainZone(), opcode, message);
     this.Send(actorId, stream);
 }
コード例 #7
0
        public void Dispatch(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;
#if SERVER
                // 内网收到外网消息,有可能是gateUnit消息,还有可能是gate广播消息
                if (OpcodeTypeComponent.Instance.IsOutrActorMessage(opcode))
                {
                    InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
                    instanceIdStruct.Process = GlobalDefine.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;
                    }
                }
#endif


                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 = GlobalDefine.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 = GlobalDefine.Options.Process;
                    long realActorId = instanceIdStruct.ToLong();
                    InnerMessageDispatcherHelper.HandleIActorResponse(opcode, realActorId, iActorResponse);
                    return;
                }

                case IActorMessage iactorMessage:
                {
                    InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
                    instanceIdStruct.Process = GlobalDefine.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}");
            }
        }
コード例 #8
0
 public static void Send(this Session self, long actorId, IMessage message)
 {
     (ushort opcode, MemoryStream stream) = MessageSerializeHelper.MessageToStream(message);
     OpcodeHelper.LogMsg(self.DomainZone(), opcode, message);
     self.Send(actorId, stream);
 }