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

客户端localConn生成带上account password信息,这样更加随机

tanghai 2 жил өмнө
parent
commit
a867dac4e4

+ 1 - 1
Unity/Assets/Scripts/Core/Helper/RandomGenerator.cs

@@ -13,7 +13,7 @@ namespace ET
 
         private static Random GetRandom()
         {
-            return random ??= new Random(Guid.NewGuid().GetHashCode());
+            return random ??= new Random(Guid.NewGuid().GetHashCode() ^ Environment.TickCount);
         }
 
         public static ulong RandUInt64()

+ 4 - 3
Unity/Assets/Scripts/Hotfix/Client/Demo/NetClient/Main2NetClient_LoginHandler.cs

@@ -1,4 +1,5 @@
-using System.Net;
+using System;
+using System.Net;
 using System.Net.Sockets;
 
 namespace ET.Client
@@ -28,13 +29,13 @@ namespace ET.Client
             IPEndPoint realmAddress = routerAddressComponent.GetRealmAddress(account);
 
             R2C_Login r2CLogin;
-            using (Session session = await RouterHelper.CreateRouterSession(netComponent, realmAddress))
+            using (Session session = await netComponent.CreateRouterSession(realmAddress, account, password))
             {
                 r2CLogin = (R2C_Login)await session.Call(new C2R_Login() { Account = account, Password = password });
             }
 
             // 创建一个gate Session,并且保存到SessionComponent中
-            Session gateSession = await RouterHelper.CreateRouterSession(netComponent, NetworkHelper.ToIPEndPoint(r2CLogin.Address));
+            Session gateSession = await netComponent.CreateRouterSession(NetworkHelper.ToIPEndPoint(r2CLogin.Address), account, password);
             gateSession.AddComponent<ClientSessionErrorComponent>();
             root.AddComponent<SessionComponent>().Session = gateSession;
             G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call(new C2G_LoginGate() { Key = r2CLogin.Key, GateId = r2CLogin.GateId });

+ 5 - 14
Unity/Assets/Scripts/Hotfix/Client/Demo/NetClient/Router/RouterHelper.cs

@@ -1,15 +1,15 @@
 using System;
 using System.Net;
-using System.Net.Sockets;
 
 namespace ET.Client
 {
     public static partial class RouterHelper
     {
         // 注册router
-        public static async ETTask<Session> CreateRouterSession(this NetComponent netComponent, IPEndPoint address)
+        public static async ETTask<Session> CreateRouterSession(this NetComponent netComponent, IPEndPoint address, string account, string password)
         {
-            (uint recvLocalConn, IPEndPoint routerAddress) = await GetRouterAddress(netComponent, address, 0, 0);
+            uint localConn = (uint)(account.GetLongHashCode() ^ password.GetLongHashCode() ^ RandomGenerator.RandUInt32());
+            (uint recvLocalConn, IPEndPoint routerAddress) = await GetRouterAddress(netComponent, address, localConn, 0);
 
             if (recvLocalConn == 0)
             {
@@ -32,7 +32,7 @@ namespace ET.Client
             RouterAddressComponent routerAddressComponent = netComponent.Root().GetComponent<RouterAddressComponent>();
             IPEndPoint routerInfo = routerAddressComponent.GetAddress();
             
-            uint recvLocalConn = await Connect(netComponent, routerInfo, address, localConn, remoteConn);
+            uint recvLocalConn = await netComponent.Connect(routerInfo, address, localConn, remoteConn);
             
             Log.Info($"finish get router address: {netComponent.Root().Id} {address} {localConn} {remoteConn} {recvLocalConn} {routerInfo}");
             return (recvLocalConn, routerInfo);
@@ -41,16 +41,7 @@ namespace ET.Client
         // 向router申请
         private static async ETTask<uint> Connect(this NetComponent netComponent, IPEndPoint routerAddress, IPEndPoint realAddress, uint localConn, uint remoteConn)
         {
-            uint synFlag;
-            if (localConn == 0)
-            {
-                synFlag = KcpProtocalType.RouterSYN;
-                localConn = RandomGenerator.RandUInt32();
-            }
-            else
-            {
-                synFlag = KcpProtocalType.RouterReconnectSYN;
-            }
+            uint synFlag = remoteConn == 0? KcpProtocalType.RouterSYN : KcpProtocalType.RouterReconnectSYN;
 
             // 注意,session也以localConn作为id,所以这里不能用localConn作为id
             long id = (long)(((ulong)localConn << 32) | remoteConn);