Эх сурвалжийг харах

修复ActorLocation消息不返回的bug,原因是NetClient收到消息处理顺序有问题

tanghai 2 жил өмнө
parent
commit
552117bd8b
18 өөрчлөгдсөн 69 нэмэгдсэн , 64 устгасан
  1. 1 1
      Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLockQueue.cs
  2. 0 2
      Unity/Assets/Scripts/Hotfix/Client/Demo/NetClient/A2NetClient_RequestHandler.cs
  3. 1 1
      Unity/Assets/Scripts/Hotfix/Client/Demo/NetClient/Ping/PingComponentSystem.cs
  4. 5 5
      Unity/Assets/Scripts/Hotfix/Client/Module/Message/NetClientComponentSystem.cs
  5. 2 2
      Unity/Assets/Scripts/Hotfix/Server/Module/ActorLocation/ActorLocationSenderComponentSystem.cs
  6. 14 11
      Unity/Assets/Scripts/Hotfix/Server/Module/Message/ActorOuterComponentSystem.cs
  7. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Module/Message/ActorSenderComponentSystem.cs
  8. 7 0
      Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetInnerComponentOnReadEvent.cs
  9. 10 17
      Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetProcessComponentSystem.cs
  10. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetProcessComponentSystem.cs.meta
  11. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Module/NetInner/FiberInit_NetInner.cs
  12. 13 9
      Unity/Assets/Scripts/Hotfix/Share/Module/Actor/ActorInnerComponentSystem.cs
  13. 1 1
      Unity/Assets/Scripts/Hotfix/Share/Module/Actor/ActorRecverComponentSystem.cs
  14. 4 4
      Unity/Assets/Scripts/Model/Server/Module/Message/ActorOuterComponent.cs
  15. 1 1
      Unity/Assets/Scripts/Model/Server/Module/Message/NetProcessComponent.cs
  16. 1 1
      Unity/Assets/Scripts/Model/Server/Module/Message/NetProcessComponent.cs.meta
  17. 0 3
      Unity/Assets/Scripts/Model/Server/Module/NetInner/A2NetInner_Message.cs
  18. 6 3
      Unity/Assets/Scripts/Model/Share/Module/Message/Session.cs

+ 1 - 1
Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLockQueue.cs

@@ -32,7 +32,7 @@ namespace ET
 
         public async ETTask<CoroutineLock> Wait(int time)
         {
-            if (this.currentCoroutineLock == null || this.currentCoroutineLock.IsDisposed)
+            if (this.currentCoroutineLock == null)
             {
                 this.currentCoroutineLock = CoroutineLock.Create(this.coroutineLockComponent, type, key, 1);
                 return this.currentCoroutineLock;

+ 0 - 2
Unity/Assets/Scripts/Hotfix/Client/Demo/NetClient/A2NetClient_RequestHandler.cs

@@ -6,8 +6,6 @@
         protected override async ETTask Run(Scene root, A2NetClient_Request request, A2NetClient_Response response)
         {
             response.MessageObject = await root.GetComponent<SessionComponent>().Session.Call(request.MessageObject);
-            
-            await ETTask.CompletedTask;
         }
     }
 }

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Client/Demo/NetClient/Ping/PingComponentSystem.cs

@@ -7,7 +7,7 @@ namespace ET.Client
         [EntitySystem]
         private static void Awake(this PingComponent self)
         {
-            //self.PingAsync().Coroutine();
+            self.PingAsync().Coroutine();
         }
         
         [EntitySystem]

+ 5 - 5
Unity/Assets/Scripts/Hotfix/Client/Module/Message/NetClientComponentSystem.cs

@@ -39,15 +39,15 @@ namespace ET.Client
             
             switch (message)
             {
-                case IActorMessage:
+                case IResponse response:
                 {
-                    // 扔到Main纤程队列中
-                    ActorMessageQueue.Instance.Send(new ActorId(self.Fiber().Process, ConstFiberId.Main), message as MessageObject);
+                    session.OnResponse(response);
                     break;
                 }
-                case IResponse response:
+                case IActorMessage:
                 {
-                    session.OnResponse(response);
+                    // 扔到Main纤程队列中
+                    ActorMessageQueue.Instance.Send(new ActorId(self.Fiber().Process, ConstFiberId.Main), message as MessageObject);
                     break;
                 }
                 default:

+ 2 - 2
Unity/Assets/Scripts/Hotfix/Server/Module/ActorLocation/ActorLocationSenderComponentSystem.cs

@@ -182,7 +182,6 @@ namespace ET.Server
 
             Scene root = self.Root();
             
-            // 先序列化好
             int rpcId = root.GetComponent<ActorSenderComponent>().GetRpcId();
             iActorRequest.RpcId = rpcId;
             
@@ -243,11 +242,12 @@ namespace ET.Server
                     return ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_NotFoundActor);
                 }
                 IActorResponse response = await root.GetComponent<ActorSenderComponent>().Call(actorLocationSender.ActorId, rpcId, iActorRequest, needException: false);
+                
                 if (actorLocationSender.InstanceId != instanceId)
                 {
                     throw new RpcException(ErrorCore.ERR_ActorLocationSenderTimeout3, $"{iActorRequest}");
                 }
-
+                
                 switch (response.Error)
                 {
                     case ErrorCore.ERR_NotFoundActor:

+ 14 - 11
Unity/Assets/Scripts/Hotfix/Server/Module/Message/ActorOuterComponentSystem.cs

@@ -8,19 +8,15 @@ namespace ET.Server
         [EntitySystem]
         private static void Awake(this ActorOuterComponent self)
         {
-            self.NetInnerComponent = self.Root().GetComponent<NetInnerComponent>();
+            self.NetProcessComponent = self.Root().GetComponent<NetProcessComponent>();
         }
 
         public static void HandleIActorResponse(this ActorOuterComponent self, IActorResponse response)
         {
-            ActorMessageSender actorMessageSender;
-            if (!self.requestCallback.TryGetValue(response.RpcId, out actorMessageSender))
+            if (!self.requestCallback.Remove(response.RpcId, out ActorMessageSender actorMessageSender))
             {
                 return;
             }
-
-            self.requestCallback.Remove(response.RpcId);
-
             Run(actorMessageSender, response);
         }
 
@@ -61,7 +57,7 @@ namespace ET.Server
             }
 
             StartSceneConfig startSceneConfig = StartSceneConfigCategory.Instance.NetInners[actorId.Process];
-            Session session = self.NetInnerComponent.Get(startSceneConfig.Id);
+            Session session = self.NetProcessComponent.Get(startSceneConfig.Id);
             actorId.Process = fiber.Process;
             session.Send(actorId, message);
         }
@@ -90,13 +86,20 @@ namespace ET.Server
             async ETTask Timeout()
             {
                 await fiber.TimerComponent.WaitAsync(ActorOuterComponent.TIMEOUT_TIME);
-                if (!self.requestCallback.TryGetValue(rpcId, out ActorMessageSender action))
+                if (!self.requestCallback.Remove(rpcId, out ActorMessageSender action))
                 {
                     return;
                 }
-
-                self.requestCallback.Remove(rpcId);
-                action.Tcs.SetException(new Exception($"actor sender timeout: {iActorRequest}"));
+                
+                if (needException)
+                {
+                    action.Tcs.SetException(new Exception($"actor sender timeout: {iActorRequest}"));
+                }
+                else
+                {
+                    IActorResponse response = ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_Timeout);
+                    action.Tcs.SetResult(response);
+                }
             }
 
             Timeout().Coroutine();

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Module/Message/ActorSenderComponentSystem.cs

@@ -71,7 +71,7 @@ namespace ET
             A2NetInner_Request a2NetInner_Request = A2NetInner_Request.Create();
             a2NetInner_Request.ActorId = actorId;
             a2NetInner_Request.MessageObject = iActorRequest;
-
+            a2NetInner_Request.NeedException = needException;
             StartSceneConfig startSceneConfig = StartSceneConfigCategory.Instance.NetInners[fiber.Process];
             A2NetInner_Response response = await fiber.Root.GetComponent<ActorSenderComponent>().Call(
                 startSceneConfig.ActorId, a2NetInner_Request, needException: a2NetInner_Request.NeedException) as A2NetInner_Response;

+ 7 - 0
Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetInnerComponentOnReadEvent.cs

@@ -12,6 +12,13 @@
 
             switch (message)
             {
+                case IActorLocationRequest iActorRequest:
+                {
+                    IActorResponse response = await root.GetComponent<ActorInnerComponent>().Call(actorId, iActorRequest, false);
+                    actorId.Process = fromProcess;
+                    root.GetComponent<ActorOuterComponent>().Send(actorId, response);
+                    break;
+                }
                 case IActorResponse iActorResponse:
                     root.GetComponent<ActorOuterComponent>().HandleIActorResponse(iActorResponse);
                     return;

+ 10 - 17
Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetInnerComponentSystem.cs → Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetProcessComponentSystem.cs

@@ -1,14 +1,13 @@
 using System;
 using System.Net;
-using System.Net.Sockets;
 
 namespace ET.Server
 {
-    [FriendOf(typeof(NetInnerComponent))]
-    public static partial class NetInnerComponentSystem
+    [FriendOf(typeof(NetProcessComponent))]
+    public static partial class NetProcessComponentSystem
     {
         [EntitySystem]
-        private static void Awake(this NetInnerComponent self, IPEndPoint address)
+        private static void Awake(this NetProcessComponent self, IPEndPoint address)
         {
             switch (self.InnerProtocol)
             {
@@ -30,18 +29,18 @@ namespace ET.Server
         }
         
         [EntitySystem]
-        private static void Update(this NetInnerComponent self)
+        private static void Update(this NetProcessComponent self)
         {
             self.AService.Update();
         }
 
         [EntitySystem]
-        private static void Destroy(this NetInnerComponent self)
+        private static void Destroy(this NetProcessComponent self)
         {
             self.AService.Dispose();
         }
 
-        private static void OnRead(this NetInnerComponent self, long channelId, ActorId actorId, object message)
+        private static void OnRead(this NetProcessComponent self, long channelId, ActorId actorId, object message)
         {
             Session session = self.GetChild<Session>(channelId);
             if (session == null)
@@ -54,13 +53,7 @@ namespace ET.Server
             EventSystem.Instance.Publish(self.Scene(), new NetInnerComponentOnRead() {ActorId = actorId, Message = message});
         }
 
-        private static void HandleMessage(this NetInnerComponent self, ActorId actorId, object message)
-        {
-            // 扔到队列中
-            ActorMessageQueue.Instance.Send(actorId, message as MessageObject);
-        }
-
-        private static void OnError(this NetInnerComponent self, long channelId, int error)
+        private static void OnError(this NetProcessComponent self, long channelId, int error)
         {
             Session session = self.GetChild<Session>(channelId);
             if (session == null)
@@ -73,14 +66,14 @@ namespace ET.Server
         }
 
         // 这个channelId是由CreateAcceptChannelId生成的
-        private static void OnAccept(this NetInnerComponent self, long channelId, IPEndPoint ipEndPoint)
+        private static void OnAccept(this NetProcessComponent self, long channelId, IPEndPoint ipEndPoint)
         {
             Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
             session.RemoteAddress = ipEndPoint;
             //session.AddComponent<SessionIdleCheckerComponent, int, int, int>(NetThreadComponent.checkInteral, NetThreadComponent.recvMaxIdleTime, NetThreadComponent.sendMaxIdleTime);
         }
 
-        private static Session CreateInner(this NetInnerComponent self, long channelId, IPEndPoint ipEndPoint)
+        private static Session CreateInner(this NetProcessComponent self, long channelId, IPEndPoint ipEndPoint)
         {
             Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
             session.RemoteAddress = ipEndPoint;
@@ -93,7 +86,7 @@ namespace ET.Server
         }
 
         // 内网actor session,channelId是进程号
-        public static Session Get(this NetInnerComponent self, long channelId)
+        public static Session Get(this NetProcessComponent self, long channelId)
         {
             Session session = self.GetChild<Session>(channelId);
             if (session != null)

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetInnerComponentSystem.cs.meta → Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetProcessComponentSystem.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: ea9a08306f1c190419eaeea3fbf42e24
+guid: e206328a79213b14bbcd96191528fcea
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Module/NetInner/FiberInit_NetInner.cs

@@ -11,7 +11,7 @@ namespace ET.Server
             root.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.UnOrderedMessage);
             
             StartSceneConfig startSceneConfig = StartSceneConfigCategory.Instance.Get(fiberInit.Fiber.Id);
-            root.AddComponent<NetInnerComponent, IPEndPoint>(startSceneConfig.InnerIPPort);
+            root.AddComponent<NetProcessComponent, IPEndPoint>(startSceneConfig.InnerIPPort);
             root.AddComponent<ActorOuterComponent>();
             root.AddComponent<ActorInnerComponent>();
             //root.AddComponent<ActorSenderComponent>();

+ 13 - 9
Unity/Assets/Scripts/Hotfix/Share/Module/Actor/ActorInnerComponentSystem.cs

@@ -9,14 +9,10 @@ namespace ET
     {
         public static void HandleIActorResponse(this ActorInnerComponent self, IActorResponse response)
         {
-            ActorMessageSender actorMessageSender;
-            if (!self.requestCallback.TryGetValue(response.RpcId, out actorMessageSender))
+            if (!self.requestCallback.Remove(response.RpcId, out ActorMessageSender actorMessageSender))
             {
                 return;
             }
-
-            self.requestCallback.Remove(response.RpcId);
-            
             Run(actorMessageSender, response);
         }
         
@@ -109,13 +105,21 @@ namespace ET
             async ETTask Timeout()
             {
                 await fiber.TimerComponent.WaitAsync(ActorInnerComponent.TIMEOUT_TIME);
-                if (!self.requestCallback.TryGetValue(rpcId, out ActorMessageSender action))
+
+                if (!self.requestCallback.Remove(rpcId, out ActorMessageSender action))
                 {
                     return;
                 }
-
-                self.requestCallback.Remove(rpcId);
-                action.Tcs.SetException(new Exception($"actor sender timeout: {iActorRequest}"));
+                
+                if (needException)
+                {
+                    action.Tcs.SetException(new Exception($"actor sender timeout: {iActorRequest}"));
+                }
+                else
+                {
+                    IActorResponse response = ActorHelper.CreateResponse(iActorRequest, ErrorCore.ERR_Timeout);
+                    action.Tcs.SetResult(response);
+                }
             }
             
             Timeout().Coroutine();

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Share/Module/Actor/ActorRecverComponentSystem.cs

@@ -37,7 +37,7 @@
                 MailBoxComponent mailBoxComponent = self.Fiber().Mailboxes.Get(actorId.InstanceId);
                 if (mailBoxComponent == null)
                 {
-                    Log.Warning($"actor not found mailbox: {actorId} {message}");
+                    Log.Warning($"actor not found mailbox, from: {actorId} current: {fiber.Address} {message}");
                     if (message is IActorRequest request)
                     {
                         IActorResponse resp = ActorHelper.CreateResponse(request, ErrorCore.ERR_NotFoundActor);

+ 4 - 4
Unity/Assets/Scripts/Model/Server/Module/Message/ActorOuterComponent.cs

@@ -11,17 +11,17 @@ namespace ET.Server
 
         public readonly Dictionary<int, ActorMessageSender> requestCallback = new();
 
-        private EntityRef<NetInnerComponent> netInnerComponent;
+        private EntityRef<NetProcessComponent> netProcessComponent;
 
-        public NetInnerComponent NetInnerComponent
+        public NetProcessComponent NetProcessComponent
         {
             get
             {
-                return this.netInnerComponent;
+                return this.netProcessComponent;
             }
             set
             {
-                this.netInnerComponent = value;
+                this.netProcessComponent = value;
             }
         }
     }

+ 1 - 1
Unity/Assets/Scripts/Model/Server/Module/Message/NetInnerComponent.cs → Unity/Assets/Scripts/Model/Server/Module/Message/NetProcessComponent.cs

@@ -10,7 +10,7 @@ namespace ET.Server
     }
     
     [ComponentOf(typeof(Scene))]
-    public class NetInnerComponent: Entity, IAwake<IPEndPoint>, IDestroy, IUpdate
+    public class NetProcessComponent: Entity, IAwake<IPEndPoint>, IDestroy, IUpdate
     {
         public AService AService;
         

+ 1 - 1
Unity/Assets/Scripts/Model/Server/Module/Message/NetInnerComponent.cs.meta → Unity/Assets/Scripts/Model/Server/Module/Message/NetProcessComponent.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: d24d71588367f824a8c1f00f1606ef7b
+guid: 2ea858a854e459547a0c06656bbe291e
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 0 - 3
Unity/Assets/Scripts/Model/Server/Module/NetInner/A2NetInner_Message.cs

@@ -58,8 +58,6 @@ namespace ET
 
         public override void Dispose()
         {
-            this.ActorId = default;
-
             this.RpcId = default;
             this.Error = default;
             this.Message = default;
@@ -71,7 +69,6 @@ namespace ET
         public string Message { get; set; }
         public int RpcId { get; set; }
         
-        public ActorId ActorId;
         public IActorResponse MessageObject;
 
     }

+ 6 - 3
Unity/Assets/Scripts/Model/Share/Module/Message/Session.cs

@@ -50,12 +50,11 @@ namespace ET
         
         public static void OnResponse(this Session self, IResponse response)
         {
-            if (!self.requestCallbacks.TryGetValue(response.RpcId, out var action))
+            if (!self.requestCallbacks.Remove(response.RpcId, out var action))
             {
                 return;
             }
 
-            self.requestCallbacks.Remove(response.RpcId);
             if (ErrorCore.IsRpcNeedThrowException(response.Error))
             {
                 action.Tcs.SetException(new Exception($"Rpc error, request: {action.Request} response: {response}"));
@@ -118,7 +117,11 @@ namespace ET
                         return;
                     }
 
-                    self.requestCallbacks.Remove(rpcId);
+                    if (!self.requestCallbacks.Remove(rpcId))
+                    {
+                        return;
+                    }
+                    
                     action.Tcs.SetException(new Exception($"session call timeout: {request} {time}"));
                 }