瀏覽代碼

修复router重连的bug

tanghai 2 年之前
父節點
當前提交
7e164c1242

+ 7 - 6
Unity/Assets/Scripts/Core/Network/KService.cs

@@ -111,14 +111,14 @@ namespace ET
         public readonly ArrayPool<byte> byteArrayPool = ArrayPool<byte>.Create(2048,200);
 #endif
 
-        private readonly Dictionary<uint, Action<byte, uint, uint>> routerAckCallback = new();
+        private readonly Dictionary<long, Action<byte>> routerAckCallback = new();
 
-        public void AddRouterAckCallback(uint id, Action<byte, uint, uint> action)
+        public void AddRouterAckCallback(long id, Action<byte> action)
         {
             this.routerAckCallback.Add(id, action);
         }
         
-        public void RemoveRouterAckCallback(uint id)
+        public void RemoveRouterAckCallback(long id)
         {
             this.routerAckCallback.Remove(id);
         }
@@ -198,16 +198,17 @@ namespace ET
                             remoteConn = BitConverter.ToUInt32(this.cache, 1);
                             localConn = BitConverter.ToUInt32(this.cache, 5);
 
-                            if (this.routerAckCallback.TryGetValue(localConn, out var action))
+                            long id = (long)(((ulong)localConn << 32) | remoteConn);
+                            if (this.routerAckCallback.TryGetValue(id, out var action))
                             {
-                                action.Invoke(flag, localConn, remoteConn);
+                                action.Invoke(flag);
                             }
                             break;
                         }
                         case KcpProtocalType.RouterReconnectSYN:
                         {
                             // 长度!=5,不是RouterReconnectSYN消息
-                            if (messageLength != 13)
+                            if (messageLength != 9)
                             {
                                 break;
                             }

+ 2 - 4
Unity/Assets/Scripts/Hotfix/Client/Demo/NetClient/Router/RouterConnectorSystem.cs

@@ -12,11 +12,9 @@ namespace ET.Client
         {
             NetComponent netComponent = self.GetParent<NetComponent>();
             KService kService = (KService)netComponent.AService;
-            kService.AddRouterAckCallback((uint)self.Id, (flag, local, remote) =>
+            kService.AddRouterAckCallback(self.Id, (flag) =>
             {
                 self.Flag = flag;
-                self.LocalConn = local;
-                self.RemoteConn = remote;
             });
         }
         [EntitySystem]
@@ -24,7 +22,7 @@ namespace ET.Client
         {
             NetComponent netComponent = self.GetParent<NetComponent>();
             KService kService = (KService)netComponent.AService;
-            kService.RemoveRouterAckCallback((uint)self.Id);
+            kService.RemoveRouterAckCallback(self.Id);
         }
 
         public static void Connect(this RouterConnector self, byte[] bytes, int index, int length, IPEndPoint ipEndPoint)

+ 8 - 6
Unity/Assets/Scripts/Hotfix/Client/Demo/NetClient/Router/RouterHelper.cs

@@ -7,7 +7,7 @@ namespace ET.Client
     public static partial class RouterHelper
     {
         // 注册router
-        public static async ETTask<Session> CreateRouterSession(NetComponent netComponent, IPEndPoint address)
+        public static async ETTask<Session> CreateRouterSession(this NetComponent netComponent, IPEndPoint address)
         {
             (uint recvLocalConn, IPEndPoint routerAddress) = await GetRouterAddress(netComponent, address, 0, 0);
 
@@ -25,7 +25,7 @@ namespace ET.Client
             return routerSession;
         }
         
-        public static async ETTask<(uint, IPEndPoint)> GetRouterAddress(NetComponent netComponent, IPEndPoint address, uint localConn, uint remoteConn)
+        public static async ETTask<(uint, IPEndPoint)> GetRouterAddress(this NetComponent netComponent, IPEndPoint address, uint localConn, uint remoteConn)
         {
             Log.Info($"start get router address: {netComponent.Root().Id} {address} {localConn} {remoteConn}");
             //return (RandomHelper.RandUInt32(), address);
@@ -39,7 +39,7 @@ namespace ET.Client
         }
 
         // 向router申请
-        private static async ETTask<uint> Connect(NetComponent netComponent, IPEndPoint routerAddress, IPEndPoint realAddress, uint localConn, uint remoteConn)
+        private static async ETTask<uint> Connect(this NetComponent netComponent, IPEndPoint routerAddress, IPEndPoint realAddress, uint localConn, uint remoteConn)
         {
             uint synFlag;
             if (localConn == 0)
@@ -52,7 +52,9 @@ namespace ET.Client
                 synFlag = KcpProtocalType.RouterReconnectSYN;
             }
 
-            using RouterConnector routerConnector = netComponent.AddChildWithId<RouterConnector>(localConn);
+            // 注意,session也以localConn作为id,所以这里不能用localConn作为id
+            long id = (long)(((ulong)localConn << 32) | remoteConn);
+            using RouterConnector routerConnector = netComponent.AddChildWithId<RouterConnector>(id);
             
             int count = 20;
             byte[] sendCache = new byte[512];
@@ -85,12 +87,12 @@ namespace ET.Client
 
                 await timerComponent.WaitFrameAsync();
                 
-                if (routerConnector.LocalConn != localConn || routerConnector.RemoteConn != remoteConn)
+                if (routerConnector.Flag == 0)
                 {
                     continue;
                 }
 
-                return routerConnector.LocalConn;
+                return localConn;
             }
         }
     }

+ 0 - 2
Unity/Assets/Scripts/Model/Client/Demo/NetClient/Router/RouterConnector.cs

@@ -4,7 +4,5 @@
     public class RouterConnector: Entity, IAwake, IDestroy
     {
         public byte Flag { get; set; }
-        public uint LocalConn { get; set; }
-        public uint RemoteConn { get; set; }
     }
 }