tanghai 3 лет назад
Родитель
Сommit
3975f82493

+ 4 - 1
Codes/Hotfix/Client/Demo/Login/LoginHelper.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Net.Sockets;
 
 namespace ET.Client
 {
@@ -18,8 +19,10 @@ namespace ET.Client
                     RouterAddressComponent routerAddressComponent = clientScene.GetComponent<RouterAddressComponent>();
                     if (routerAddressComponent == null)
                     {
-                        routerAddressComponent = clientScene.AddComponent<RouterAddressComponent, string>(ConstValue.RouterHttpAddress);
+                        routerAddressComponent = clientScene.AddComponent<RouterAddressComponent, string, int>(ConstValue.RouterHttpHost, ConstValue.RouterHttpPort);
                         await routerAddressComponent.Init();
+                        
+                        clientScene.AddComponent<NetKcpComponent, AddressFamily, int>(routerAddressComponent.AddressFamily, CallbackType.SessionStreamDispatcherClientOuter);
                     }
                     string realmAddress = routerAddressComponent.GetRealmAddress(account);
                     

+ 6 - 4
Codes/Hotfix/Client/Demo/Router/RouterAddressComponentSystem.cs

@@ -9,22 +9,24 @@ namespace ET.Client
     [FriendOf(typeof(RouterAddressComponent))]
     public static class RouterAddressComponentSystem
     {
-        public class RouterAddressComponentAwakeSystem: AwakeSystem<RouterAddressComponent, string>
+        public class RouterAddressComponentAwakeSystem: AwakeSystem<RouterAddressComponent, string, int>
         {
-            protected override void Awake(RouterAddressComponent self, string routerManagerAddress)
+            protected override void Awake(RouterAddressComponent self, string address, int port)
             {
-                self.RouterManagerAddress = routerManagerAddress;
+                self.RouterManagerHost = address;
+                self.RouterManagerPort = port;
             }
         }
         
         public static async ETTask Init(this RouterAddressComponent self)
         {
+            self.AddressFamily = NetworkHelper.GetAddressFamily(self.RouterManagerHost);
             await self.GetAllRouter();
         }
 
         private static async ETTask GetAllRouter(this RouterAddressComponent self)
         {
-            string url = $"http://{self.RouterManagerAddress}/get_router?v={RandomHelper.RandUInt32()}";
+            string url = $"http://{self.RouterManagerHost}:{self.RouterManagerPort}/get_router?v={RandomHelper.RandUInt32()}";
             Log.Debug($"start get router info: {url}");
             string routerInfo = await HttpClientHelper.Get(url);
             Log.Debug($"recv router info: {routerInfo}");

+ 1 - 1
Codes/Hotfix/Client/Demo/Router/RouterHelper.cs

@@ -43,7 +43,7 @@ namespace ET.Client
         {
             uint connectId = RandomHelper.RandUInt32();
             
-            using Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
+            using Socket socket = new Socket(routerAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
             
             int count = 30;
             byte[] sendCache = new byte[512];

+ 7 - 3
Codes/Hotfix/Client/Demo/Scene/SceneFactory.cs

@@ -1,13 +1,17 @@
+using System.Net.Sockets;
+
 namespace ET.Client
 {
     public static class SceneFactory
     {
-        public static Scene CreateClientScene(int zone, string name, Entity parent)
+        public static async ETTask<Scene> CreateClientScene(int zone, string name, Entity parent)
         {
+            await ETTask.CompletedTask;
+            
             Scene clientScene = EntitySceneFactory.CreateScene(zone, SceneType.Client, name, parent);
             clientScene.AddComponent<ClientSceneFlagComponent>();
-            clientScene.AddComponent<NetKcpComponent, int>(CallbackType.SessionStreamDispatcherClientOuter);
-			clientScene.AddComponent<CurrentScenesComponent>();
+            
+            clientScene.AddComponent<CurrentScenesComponent>();
             clientScene.AddComponent<ObjectWait>();
             clientScene.AddComponent<PlayerComponent>();
             

+ 2 - 2
Codes/Hotfix/Server/Demo/Robot/RobotCaseHelper.cs

@@ -64,7 +64,7 @@ namespace ET.Server
             Scene clientScene = null;
             try
             {
-                clientScene = Client.SceneFactory.CreateClientScene(zone, name, self);
+                clientScene = await Client.SceneFactory.CreateClientScene(zone, name, self);
                 await Client.LoginHelper.Login(clientScene, zone.ToString(), zone.ToString());
                 await Client.EnterMapHelper.EnterMapAsync(clientScene);
                 Log.Debug($"create robot ok: {zone}");
@@ -84,7 +84,7 @@ namespace ET.Server
 
             try
             {
-                clientScene = Client.SceneFactory.CreateClientScene(zone, $"Robot_{zone}", self);
+                clientScene = await Client.SceneFactory.CreateClientScene(zone, $"Robot_{zone}", self);
                 await Client.LoginHelper.Login(clientScene, zone.ToString(), zone.ToString());
                 await Client.EnterMapHelper.EnterMapAsync(clientScene);
                 Log.Debug($"create robot ok: {zone}");

+ 1 - 1
Codes/Hotfix/Server/Demo/Robot/RobotManagerComponentSystem.cs

@@ -10,7 +10,7 @@ namespace ET.Server
             Scene clientScene = null;
             try
             {
-                clientScene = Client.SceneFactory.CreateClientScene(zone, "Robot", self);
+                clientScene = await Client.SceneFactory.CreateClientScene(zone, "Robot", self);
                 await Client.LoginHelper.Login(clientScene, zone.ToString(), zone.ToString());
                 await Client.EnterMapHelper.EnterMapAsync(clientScene);
                 Log.Debug($"create robot ok: {zone}");

+ 3 - 2
Codes/Hotfix/Server/Module/MessageInner/NetInnerComponentSystem.cs

@@ -1,5 +1,6 @@
 using System.IO;
 using System.Net;
+using System.Net.Sockets;
 
 namespace ET.Server
 {
@@ -15,7 +16,7 @@ namespace ET.Server
                 NetInnerComponent.Instance = self;
                 self.SessionStreamDispatcherType = sessionStreamDispatcherType;
             
-                self.Service = new TService(NetThreadComponent.Instance.ThreadSynchronizationContext, ServiceType.Inner);
+                self.Service = new KService(NetThreadComponent.Instance.ThreadSynchronizationContext, AddressFamily.InterNetwork, ServiceType.Inner);
                 self.Service.ErrorCallback += self.OnError;
                 self.Service.ReadCallback += self.OnRead;
 
@@ -31,7 +32,7 @@ namespace ET.Server
                 NetInnerComponent.Instance = self;
                 self.SessionStreamDispatcherType = sessionStreamDispatcherType;
 
-                self.Service = new TService(NetThreadComponent.Instance.ThreadSynchronizationContext, address, ServiceType.Inner);
+                self.Service = new KService(NetThreadComponent.Instance.ThreadSynchronizationContext, address, ServiceType.Inner);
                 self.Service.ErrorCallback += self.OnError;
                 self.Service.ReadCallback += self.OnRead;
                 self.Service.AcceptCallback += self.OnAccept;

+ 2 - 2
Codes/Hotfix/Server/Module/Router/RouterComponentSystem.cs

@@ -14,7 +14,7 @@ namespace ET.Server
         {
             protected override void Awake(RouterComponent self, IPEndPoint outerAddress, string innerIP)
             {
-                self.OuterSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
+                self.OuterSocket = new Socket(outerAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
                 self.OuterSocket.Bind(outerAddress);
                 if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                 {
@@ -22,7 +22,7 @@ namespace ET.Server
                     self.OuterSocket.ReceiveBufferSize = 16 * Kcp.OneM;
                 }
 
-                self.InnerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
+                self.InnerSocket = new Socket(outerAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
                 self.InnerSocket.Bind(new IPEndPoint(IPAddress.Parse(innerIP), 0));
 
                 if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))

+ 2 - 1
Codes/Hotfix/Share/ConstValue.cs

@@ -2,7 +2,8 @@ namespace ET
 {
     public static class ConstValue
     {
-        public const string RouterHttpAddress = "127.0.0.1:10300";
+        public const string RouterHttpHost = "127.0.0.1";
+        public const int RouterHttpPort = 10300;
         public const int SessionTimeoutTime = 30 * 1000;
     }
 }

+ 4 - 4
Codes/Hotfix/Share/Module/Message/NetKcpComponentSystem.cs

@@ -1,6 +1,7 @@
 using System;
 using System.IO;
 using System.Net;
+using System.Net.Sockets;
 
 namespace ET
 {
@@ -8,13 +9,12 @@ namespace ET
     public static class NetKcpComponentSystem
     {
         [ObjectSystem]
-        public class NetKcpComponentAwakeSystem: AwakeSystem<NetKcpComponent, int>
+        public class NetKcpComponentAwakeSystem: AwakeSystem<NetKcpComponent, AddressFamily, int>
         {
-            protected override void Awake(NetKcpComponent self, int sessionStreamDispatcherType)
+            protected override void Awake(NetKcpComponent self, AddressFamily addressFamily, int sessionStreamDispatcherType)
             {
                 self.SessionStreamDispatcherType = sessionStreamDispatcherType;
-            
-                self.Service = new KService(NetThreadComponent.Instance.ThreadSynchronizationContext, ServiceType.Outer);
+                self.Service = new KService(NetThreadComponent.Instance.ThreadSynchronizationContext, addressFamily, ServiceType.Outer);
                 self.Service.ErrorCallback += (channelId, error) => self.OnError(channelId, error);
                 self.Service.ReadCallback += (channelId, Memory) => self.OnRead(channelId, Memory);
 

+ 1 - 1
Codes/HotfixView/Client/Demo/InitClient.cs

@@ -15,7 +15,7 @@ namespace ET.Client
 
             await ResourcesComponent.Instance.LoadBundleAsync("unit.unity3d");
             
-            Scene clientScene = SceneFactory.CreateClientScene(1, "Game", Game.Scene);
+            Scene clientScene = await SceneFactory.CreateClientScene(1, "Game", Game.Scene);
             
             await Game.EventSystem.PublishAsync(clientScene, new EventType.AppStartInitFinish());
         }

+ 5 - 2
Codes/Model/Client/Demo/Router/RouterAddressComponent.cs

@@ -1,12 +1,15 @@
 using System.Collections.Generic;
 using System.Net;
+using System.Net.Sockets;
 
 namespace ET.Client
 {
     [ComponentOf(typeof(Scene))]
-    public class RouterAddressComponent: Entity, IAwake<string>
+    public class RouterAddressComponent: Entity, IAwake<string, int>
     {
-        public string RouterManagerAddress;
+        public AddressFamily AddressFamily { get; set; }
+        public string RouterManagerHost;
+        public int RouterManagerPort;
         public HttpGetRouterResponse Info;
         public int RouterIndex;
     }

+ 2 - 2
Codes/Model/Share/Module/Message/NetKcpComponent.cs

@@ -1,12 +1,12 @@
 using System.Net;
+using System.Net.Sockets;
 
 namespace ET
 {
     [ComponentOf(typeof(Scene))]
-    public class NetKcpComponent: Entity, IAwake<int>, IAwake<IPEndPoint, int>, IDestroy, IAwake<IAction<int>>
+    public class NetKcpComponent: Entity, IAwake<AddressFamily, int>, IAwake<IPEndPoint, int>, IDestroy, IAwake<IAction<int>>
     {
         public AService Service;
-        
         public int SessionStreamDispatcherType { get; set; }
     }
 }

+ 6 - 0
Unity/Assets/Scripts/Core/Helper/NetworkHelper.cs

@@ -1,6 +1,7 @@
 using System.Collections.Generic;
 using System.Net;
 using System.Net.NetworkInformation;
+using System.Net.Sockets;
 
 namespace ET
 {
@@ -23,6 +24,11 @@ namespace ET
 			return list.ToArray();
 		}
 		
+		public static AddressFamily GetAddressFamily(string url)
+		{
+			return Dns.GetHostEntry(url).AddressList[0].AddressFamily;
+		}
+		
 		public static IPEndPoint ToIPEndPoint(string host, int port)
 		{
 			return new IPEndPoint(IPAddress.Parse(host), port);

+ 3 - 5
Unity/Assets/Scripts/Core/Network/KService.cs

@@ -105,7 +105,7 @@ namespace ET
             this.ServiceType = serviceType;
             this.ThreadSynchronizationContext = threadSynchronizationContext;
             this.startTime = TimeHelper.ClientNow();
-            this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
+            this.socket = new Socket(ipEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
             if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
             {
                 this.socket.SendBufferSize = Kcp.OneM * 64;
@@ -122,14 +122,12 @@ namespace ET
             }
         }
 
-        public KService(ThreadSynchronizationContext threadSynchronizationContext, ServiceType serviceType)
+        public KService(ThreadSynchronizationContext threadSynchronizationContext, AddressFamily addressFamily, ServiceType serviceType)
         {
             this.ServiceType = serviceType;
             this.ThreadSynchronizationContext = threadSynchronizationContext;
             this.startTime = TimeHelper.ClientNow();
-            this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-            // 作为客户端不需要修改发送跟接收缓冲区大小
-            this.socket.Bind(new IPEndPoint(IPAddress.Any, 0));
+            this.socket = new Socket(addressFamily, SocketType.Dgram, ProtocolType.Udp);
 
             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
             {

+ 1 - 1
Unity/Assets/Scripts/Core/Network/TChannel.cs

@@ -54,7 +54,7 @@ namespace ET
 			this.ChannelType = ChannelType.Connect;
 			this.Id = id;
 			this.Service = service;
-			this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+			this.socket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
 			this.socket.NoDelay = true;
 			this.parser = new PacketParser(this.recvBuffer, this.Service);
 			this.innArgs.Completed += this.OnComplete;

+ 1 - 1
Unity/Assets/Scripts/Core/Network/TService.cs

@@ -28,7 +28,7 @@ namespace ET
 			this.ServiceType = serviceType;
 			this.ThreadSynchronizationContext = threadSynchronizationContext;
 			
-			this.acceptor = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+			this.acceptor = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
 			// 容易出问题,先注释掉,按需开启
 			//this.acceptor.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
 			this.innArgs.Completed += this.OnComplete;