Ver código fonte

统一使用IPEndPoint表示地址,方便以后用IPV6

tanghai 8 anos atrás
pai
commit
450b916189

+ 11 - 10
Server/App/Program.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Net;
 using System.Threading;
 using Model;
 using NLog;
@@ -49,15 +50,15 @@ namespace App
 				switch (startConfig.AppType)
 				{
 					case AppType.Manager:
-						Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
-						Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
+						Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint);
+						Game.Scene.AddComponent<NetOuterComponent, IPEndPoint>(outerConfig.IPEndPoint);
 						Game.Scene.AddComponent<AppManagerComponent>();
 						break;
 					case AppType.Realm:
 						Game.Scene.AddComponent<ActorMessageDispatherComponent>();
 						Game.Scene.AddComponent<ActorManagerComponent>();
-						Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
-						Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
+						Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint);
+						Game.Scene.AddComponent<NetOuterComponent, IPEndPoint>(outerConfig.IPEndPoint);
 						Game.Scene.AddComponent<LocationProxyComponent>();
 						Game.Scene.AddComponent<RealmGateAddressComponent>();
 						break;
@@ -65,18 +66,18 @@ namespace App
 						Game.Scene.AddComponent<PlayerComponent>();
 						Game.Scene.AddComponent<ActorMessageDispatherComponent>();
 						Game.Scene.AddComponent<ActorManagerComponent>();
-						Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
-						Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
+						Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint);
+						Game.Scene.AddComponent<NetOuterComponent, IPEndPoint>(outerConfig.IPEndPoint);
 						Game.Scene.AddComponent<LocationProxyComponent>();
 						Game.Scene.AddComponent<ActorProxyComponent>();
 						Game.Scene.AddComponent<GateSessionKeyComponent>();
 						break;
 					case AppType.Location:
-						Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
+						Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint);
 						Game.Scene.AddComponent<LocationComponent>();
 						break;
 					case AppType.Map:
-						Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
+						Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint);
 						Game.Scene.AddComponent<ActorManagerComponent>();
 						Game.Scene.AddComponent<UnitComponent>();
 						Game.Scene.AddComponent<LocationProxyComponent>();
@@ -93,8 +94,8 @@ namespace App
 						Game.Scene.AddComponent<LocationComponent>();
 						Game.Scene.AddComponent<ActorMessageDispatherComponent>();
 						Game.Scene.AddComponent<ActorManagerComponent>();
-						Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
-						Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
+						Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint);
+						Game.Scene.AddComponent<NetOuterComponent, IPEndPoint>(outerConfig.IPEndPoint);
 						Game.Scene.AddComponent<LocationProxyComponent>();
 						Game.Scene.AddComponent<AppManagerComponent>();
 						Game.Scene.AddComponent<RealmGateAddressComponent>();

+ 1 - 0
Server/Base/Server.Base.csproj

@@ -38,6 +38,7 @@
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Network\KNet\KChannel.cs" Link="Network\KNet\KChannel.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Network\KNet\Kcp.cs" Link="Network\KNet\Kcp.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Network\KNet\KService.cs" Link="Network\KNet\KService.cs" />
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\Network\NetworkHelper.cs" Link="Network\NetworkHelper.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Network\TNet\PacketParser.cs" Link="Network\TNet\PacketParser.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Network\TNet\TBuffer.cs" Link="Network\TNet\TBuffer.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Network\TNet\TChannel.cs" Link="Network\TNet\TChannel.cs" />

+ 7 - 6
Server/Hotfix/System/NetInnerComponentSystem.cs

@@ -1,18 +1,19 @@
-using Model;
+using System.Net;
+using Model;
 
 namespace Hotfix
 {
 	[ObjectEvent]
-	public class NetInnerComponentEvent : ObjectEvent<NetInnerComponent>, IAwake, IAwake<string, int>, IUpdate
+	public class NetInnerComponentEvent : ObjectEvent<NetInnerComponent>, IAwake, IAwake<IPEndPoint>, IUpdate
 	{
 		public void Awake()
 		{
 			this.Get().Awake();
 		}
 
-		public void Awake(string a, int b)
+		public void Awake(IPEndPoint ipEndPoint)
 		{
-			this.Get().Awake(a, b);
+			this.Get().Awake(ipEndPoint);
 		}
 
 		public void Update()
@@ -30,9 +31,9 @@ namespace Hotfix
 			self.MessageDispatcher = new InnerMessageDispatcher();
 		}
 
-		public static void Awake(this NetInnerComponent self, string host, int port)
+		public static void Awake(this NetInnerComponent self, IPEndPoint ipEndPoint)
 		{
-			self.Awake(NetworkProtocol.TCP, host, port);
+			self.Awake(NetworkProtocol.TCP, ipEndPoint);
 			self.MessagePacker = new MongoPacker();
 			self.MessageDispatcher = new InnerMessageDispatcher();
 		}

+ 8 - 7
Server/Hotfix/System/NetOuterComponentSystem.cs

@@ -1,18 +1,19 @@
-using Model;
+using System.Net;
+using Model;
 
 namespace Hotfix
 {
 	[ObjectEvent]
-	public class NetOuterComponentEvent : ObjectEvent<NetOuterComponent>, IAwake, IAwake<string, int>, IUpdate
+	public class NetOuterComponentEvent : ObjectEvent<NetOuterComponent>, IAwake, IAwake<IPEndPoint>, IUpdate
 	{
 		public void Awake()
 		{
 			this.Get().Awake();
 		}
 
-		public void Awake(string a, int b)
+		public void Awake(IPEndPoint ipEndPoint)
 		{
-			this.Get().Awake(a, b);
+			this.Get().Awake(ipEndPoint);
 		}
 
 		public void Update()
@@ -25,14 +26,14 @@ namespace Hotfix
 	{
 		public static void Awake(this NetOuterComponent self)
 		{
-			self.Awake(NetworkProtocol.KCP);
+			self.Awake(NetworkProtocol.TCP);
 			self.MessagePacker = new MongoPacker();
 			self.MessageDispatcher = new OuterMessageDispatcher();
 		}
 
-		public static void Awake(this NetOuterComponent self, string host, int port)
+		public static void Awake(this NetOuterComponent self, IPEndPoint ipEndPoint)
 		{
-			self.Awake(NetworkProtocol.KCP, host, port);
+			self.Awake(NetworkProtocol.TCP, ipEndPoint);
 			self.MessagePacker = new MongoPacker();
 			self.MessageDispatcher = new OuterMessageDispatcher();
 		}

+ 8 - 8
Server/Model/Component/BenchmarkComponent.cs

@@ -1,14 +1,15 @@
 using System;
+using System.Net;
 using System.Threading.Tasks;
 
 namespace Model
 {
 	[ObjectEvent]
-	public class BenchmarkComponentEvent : ObjectEvent<BenchmarkComponent>, IAwake<string>
+	public class BenchmarkComponentEvent : ObjectEvent<BenchmarkComponent>, IAwake<IPEndPoint>
 	{
-		public void Awake(string address)
+		public void Awake(IPEndPoint ipEndPoint)
 		{
-			this.Get().Awake(address);
+			this.Get().Awake(ipEndPoint);
 		}
 	}
 
@@ -18,16 +19,15 @@ namespace Model
 
 		private long time1 = TimeHelper.ClientNow();
 
-		public async void Awake(string address)
+		public async void Awake(IPEndPoint ipEndPoint)
 		{
 			try
 			{
 				NetOuterComponent networkComponent = Game.Scene.GetComponent<NetOuterComponent>();
-
 				for (int i = 0; i < 100; i++)
 				{
 					await Game.Scene.GetComponent<TimerComponent>().WaitAsync(1000);
-					this.TestAsync(networkComponent, address, i);
+					this.TestAsync(networkComponent, ipEndPoint, i);
 				}
 			}
 			catch (Exception e)
@@ -36,11 +36,11 @@ namespace Model
 			}
 		}
 
-		public async void TestAsync(NetOuterComponent networkComponent, string address, int j)
+		public async void TestAsync(NetOuterComponent networkComponent, IPEndPoint ipEndPoint, int j)
 		{
 			try
 			{
-				using (Session session = networkComponent.Create(address))
+				using (Session session = networkComponent.Create(ipEndPoint))
 				{
 					int i = 0;
 					while (i < 100000000)

+ 11 - 1
Server/Model/Component/Config/InnerConfig.cs

@@ -1,4 +1,5 @@
-using MongoDB.Bson.Serialization.Attributes;
+using System.Net;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
@@ -16,5 +17,14 @@ namespace Model
 				return $"{this.Host}:{this.Port}";
 			}
 		}
+
+		[BsonIgnore]
+		public IPEndPoint IPEndPoint
+		{
+			get
+			{
+				return NetworkHelper.ToIPEndPoint(this.Host, this.Port);
+			}
+		}
 	}
 }

+ 11 - 1
Server/Model/Component/Config/OuterConfig.cs

@@ -1,4 +1,5 @@
-using MongoDB.Bson.Serialization.Attributes;
+using System.Net;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
@@ -16,5 +17,14 @@ namespace Model
 				return $"{this.Host}:{this.Port}";
 			}
 		}
+
+		[BsonIgnore]
+		public IPEndPoint IPEndPoint
+		{
+			get
+			{
+				return NetworkHelper.ToIPEndPoint(this.Host, this.Port);
+			}
+		}
 	}
 }

+ 6 - 2
Server/Model/Component/NetInnerComponent.cs

@@ -1,4 +1,5 @@
 using System.Collections.Generic;
+using System.Net;
 
 namespace Model
 {
@@ -13,7 +14,7 @@ namespace Model
 			{
 				return;
 			}
-			this.adressSessions.Remove(session.RemoteAddress);
+			this.adressSessions.Remove(session.RemoteAddress.ToString());
 
 			base.Remove(id);
 		}
@@ -28,7 +29,10 @@ namespace Model
 				return session;
 			}
 
-			session = this.Create(address);
+			string[] ss = address.Split(':');
+			IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(ss[0]), int.Parse(ss[1]));
+			session = this.Create(ipEndPoint);
+
 			this.adressSessions.Add(address, session);
 			return session;
 		}

+ 2 - 1
Unity/Assets/Scripts/Base/Network/AChannel.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Net;
 using System.Net.Sockets;
 using System.Threading.Tasks;
 
@@ -28,7 +29,7 @@ namespace Model
 
 		protected AService service;
 
-		public string RemoteAddress { get; protected set; }
+		public IPEndPoint RemoteAddress { get; protected set; }
 
 		private event Action<AChannel, SocketError> errorCallback;
 

+ 2 - 1
Unity/Assets/Scripts/Base/Network/AService.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Net;
 using System.Threading.Tasks;
 
 namespace Model
@@ -18,7 +19,7 @@ namespace Model
 
 		public abstract Task<AChannel> AcceptChannel();
 
-		public abstract AChannel ConnectChannel(string host, int port);
+		public abstract AChannel ConnectChannel(IPEndPoint ipEndPoint);
 
 		public abstract void Remove(long channelId);
 

+ 3 - 4
Unity/Assets/Scripts/Base/Network/KNet/KService.cs

@@ -35,10 +35,10 @@ namespace Model
 		private readonly MultiMap<long, long> timerMap = new MultiMap<long, long>();
 		private readonly Queue<long> timeoutTimer = new Queue<long>();
 
-		public KService(string host, int port)
+		public KService(IPEndPoint ipEndPoint)
 		{
 			this.TimeNow = (uint)TimeHelper.Now();
-			this.socket = new UdpClient(new IPEndPoint(IPAddress.Parse(host), port));
+			this.socket = new UdpClient(ipEndPoint);
 			this.StartRecv();
 		}
 
@@ -198,9 +198,8 @@ namespace Model
 			return this.acceptTcs.Task;
 		}
 
-		public override AChannel ConnectChannel(string host, int port)
+		public override AChannel ConnectChannel(IPEndPoint ipEndPoint)
 		{
-			IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(host), port);
 			KChannel channel = this.CreateConnectChannel(ipEndPoint);
 			return channel;
 		}

+ 21 - 0
Unity/Assets/Scripts/Base/Network/NetworkHelper.cs

@@ -0,0 +1,21 @@
+using System.Net;
+
+namespace Model
+{
+	public static class NetworkHelper
+	{
+		public static IPEndPoint ToIPEndPoint(string host, int port)
+		{
+			return new IPEndPoint(IPAddress.Parse(host), port);
+		}
+
+		public static IPEndPoint ToIPEndPoint(string address)
+		{
+			int index = address.LastIndexOf(':');
+			string host = address.Substring(0, index);
+			string p = address.Substring(index + 1);
+			int port = int.Parse(p);
+			return ToIPEndPoint(host, port);
+		}
+	}
+}

+ 12 - 0
Unity/Assets/Scripts/Base/Network/NetworkHelper.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 5f24e42ac059909498f35805b76f9102
+timeCreated: 1511941046
+licenseType: Free
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 7 - 7
Unity/Assets/Scripts/Base/Network/TNet/TChannel.cs

@@ -23,13 +23,13 @@ namespace Model
 		/// <summary>
 		/// connect
 		/// </summary>
-		public TChannel(TcpClient tcpClient, string host, int port, TService service) : base(service, ChannelType.Connect)
+		public TChannel(TcpClient tcpClient, IPEndPoint ipEndPoint, TService service) : base(service, ChannelType.Connect)
 		{
 			this.tcpClient = tcpClient;
 			this.parser = new PacketParser(this.recvBuffer);
-			this.RemoteAddress = host + ":" + port;
+			this.RemoteAddress = ipEndPoint;
 
-			this.ConnectAsync(host, port);
+			this.ConnectAsync(ipEndPoint);
 		}
 
 		/// <summary>
@@ -41,15 +41,15 @@ namespace Model
 			this.parser = new PacketParser(this.recvBuffer);
 
 			IPEndPoint ipEndPoint = (IPEndPoint)this.tcpClient.Client.RemoteEndPoint;
-			this.RemoteAddress = ipEndPoint.Address + ":" + ipEndPoint.Port;
+			this.RemoteAddress = ipEndPoint;
 			this.OnAccepted();
 		}
 
-		private async void ConnectAsync(string host, int port)
+		private async void ConnectAsync(IPEndPoint ipEndPoint)
 		{
 			try
 			{
-				await this.tcpClient.ConnectAsync(host, port);
+				await this.tcpClient.ConnectAsync(ipEndPoint.Address, ipEndPoint.Port);
 				this.isConnected = true;
 				this.StartSend();
 				this.StartRecv();
@@ -60,7 +60,7 @@ namespace Model
 			}
 			catch (Exception e)
 			{
-				Log.Error($"connect error: {host} {port} {e}");
+				Log.Error($"connect error: {ipEndPoint} {e}");
 			}
 		}
 

+ 4 - 6
Unity/Assets/Scripts/Base/Network/TNet/TService.cs

@@ -18,11 +18,9 @@ namespace Model
 		/// <summary>
 		/// 即可做client也可做server
 		/// </summary>
-		/// <param name="host"></param>
-		/// <param name="port"></param>
-		public TService(string host, int port)
+		public TService(IPEndPoint ipEndPoint)
 		{
-			this.acceptor = new TcpListener(new IPEndPoint(IPAddress.Parse(host), port));
+			this.acceptor = new TcpListener(ipEndPoint);
 			this.acceptor.Start();
 		}
 
@@ -70,10 +68,10 @@ namespace Model
 			return channel;
 		}
 
-		public override AChannel ConnectChannel(string host, int port)
+		public override AChannel ConnectChannel(IPEndPoint ipEndPoint)
 		{
 			TcpClient tcpClient = new TcpClient();
-			TChannel channel = new TChannel(tcpClient, host, port, this);
+			TChannel channel = new TChannel(tcpClient, ipEndPoint, this);
 			this.idChannels[channel.Id] = channel;
 
 			return channel;

+ 4 - 3
Unity/Assets/Scripts/Base/Network/UNet/UChannel.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Net;
 using System.Net.Sockets;
 using System.Threading.Tasks;
 
@@ -15,12 +16,12 @@ namespace Model
 		/// <summary>
 		/// connect
 		/// </summary>
-		public UChannel(USocket socket, string host, int port, UService service): base(service, ChannelType.Connect)
+		public UChannel(USocket socket, IPEndPoint ipEndPoint, UService service): base(service, ChannelType.Connect)
 		{
 			this.socket = socket;
 			this.service = service;
-			this.RemoteAddress = host + ":" + port;
-			this.socket.ConnectAsync(host, (ushort)port);
+			this.RemoteAddress = ipEndPoint;
+			this.socket.ConnectAsync(ipEndPoint);
 			this.socket.Received += this.OnRecv;
 			this.socket.Disconnect += () => { this.OnError(this, SocketError.SocketError); };
 		}

+ 5 - 4
Unity/Assets/Scripts/Base/Network/UNet/UService.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Net;
 using System.Threading.Tasks;
 
 namespace Model
@@ -14,9 +15,9 @@ namespace Model
 		/// <summary>
 		/// 即可做client也可做server
 		/// </summary>
-		public UService(string host, int port)
+		public UService(IPEndPoint ipEndPoint)
 		{
-			this.poller = new UPoller(host, (ushort)port);
+			this.poller = new UPoller(ipEndPoint.Address.ToString(), (ushort)ipEndPoint.Port);
 		}
 
 		/// <summary>
@@ -51,10 +52,10 @@ namespace Model
 			return channel;
 		}
 
-		public override AChannel ConnectChannel(string host, int port)
+		public override AChannel ConnectChannel(IPEndPoint ipEndPoint)
 		{
 			USocket newSocket = new USocket(this.poller);
-			UChannel channel = new UChannel(newSocket, host, port, this);
+			UChannel channel = new UChannel(newSocket, ipEndPoint, this);
 			this.idChannels[channel.Id] = channel;
 			return channel;
 		}

+ 12 - 6
Unity/Assets/Scripts/Base/Network/UNet/USocket.cs

@@ -21,6 +21,7 @@ namespace Model
 		private bool isConnected;
 		private Action disconnect;
 		private Action received;
+		private IPEndPoint ipEndPoint;
 
 		public event Action Received
 		{
@@ -69,13 +70,18 @@ namespace Model
 			this.PeerPtr = IntPtr.Zero;
 		}
 
-		public string RemoteAddress
+		public IPEndPoint RemoteAddress
 		{
 			get
 			{
+				if (ipEndPoint != null)
+				{
+					return this.ipEndPoint;
+				}
+
 				ENetPeer peer = this.Struct;
-				IPAddress ipaddr = new IPAddress(peer.Address.Host);
-				return $"{ipaddr}:{peer.Address.Port}";
+				this.ipEndPoint = new IPEndPoint(peer.Address.Host, peer.Address.Port);
+				return this.ipEndPoint;
 			}
 		}
 
@@ -104,15 +110,15 @@ namespace Model
 			}
 		}
 
-		public void ConnectAsync(string host, ushort port)
+		public void ConnectAsync(IPEndPoint ipEndPoint)
 		{
-			UAddress address = new UAddress(host, port);
+			UAddress address = new UAddress(ipEndPoint.Address.ToString(), ipEndPoint.Port);
 			ENetAddress nativeAddress = address.Struct;
 
 			this.PeerPtr = NativeMethods.enet_host_connect(this.poller.Host, ref nativeAddress, 2, 0);
 			if (this.PeerPtr == IntPtr.Zero)
 			{
-				throw new Exception($"host connect call failed, {host}:{port}");
+				throw new Exception($"host connect call failed, {ipEndPoint}");
 			}
 			this.poller.USocketManager.Add(this.PeerPtr, this);
 		}

+ 2 - 14
Unity/Assets/Scripts/Component/NetOuterComponent.cs

@@ -1,18 +1,13 @@
 namespace Model
 {
 	[ObjectEvent]
-	public class NetOuterComponentEvent : ObjectEvent<NetOuterComponent>, IAwake, IAwake<string, int>, IUpdate
+	public class NetOuterComponentEvent : ObjectEvent<NetOuterComponent>, IAwake, IUpdate
 	{
 		public void Awake()
 		{
 			this.Get().Awake();
 		}
-
-		public void Awake(string host, int port)
-		{
-			this.Get().Awake();
-		}
-
+		
 		public void Update()
 		{
 			this.Get().Update();
@@ -28,13 +23,6 @@
 			this.MessageDispatcher = new ClientDispatcher();
 		}
 
-		public void Awake(string host, int port)
-		{
-			this.Awake(NetworkProtocol.TCP, host, port);
-			this.MessagePacker = new MongoPacker();
-			this.MessageDispatcher = new ClientDispatcher();
-		}
-
 		public new void Update()
 		{
 			base.Update();

+ 8 - 10
Unity/Assets/Scripts/Component/NetworkComponent.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Net;
 using System.Threading.Tasks;
 
 namespace Model
@@ -33,20 +34,20 @@ namespace Model
 			}
 		}
 
-		public void Awake(NetworkProtocol protocol, string host, int port)
+		public void Awake(NetworkProtocol protocol, IPEndPoint ipEndPoint)
 		{
 			try
 			{
 				switch (protocol)
 				{
 					case NetworkProtocol.TCP:
-						this.Service = new TService(host, port);
+						this.Service = new TService(ipEndPoint);
 						break;
 					case NetworkProtocol.UDP:
-						this.Service = new UService(host, port);
+						this.Service = new UService(ipEndPoint);
 						break;
 					case NetworkProtocol.KCP:
-						this.Service = new KService(host, port);
+						this.Service = new KService(ipEndPoint);
 						break;
 					default:
 						throw new ArgumentOutOfRangeException();
@@ -56,7 +57,7 @@ namespace Model
 			}
 			catch (Exception e)
 			{
-				throw new Exception($"{host} {port}", e);
+				throw new Exception($"{ipEndPoint}", e);
 			}
 		}
 
@@ -103,14 +104,11 @@ namespace Model
 		/// <summary>
 		/// 创建一个新Session
 		/// </summary>
-		public virtual Session Create(string address)
+		public virtual Session Create(IPEndPoint ipEndPoint)
 		{
 			try
 			{
-				string[] ss = address.Split(':');
-				int port = int.Parse(ss[1]);
-				string host = ss[0];
-				AChannel channel = this.Service.ConnectChannel(host, port);
+				AChannel channel = this.Service.ConnectChannel(ipEndPoint);
 				Session session = new Session(this, channel);
 				channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
 				this.sessions.Add(session.Id, session);

+ 2 - 1
Unity/Assets/Scripts/Entity/Session.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Net;
 using System.Threading;
 using System.Threading.Tasks;
 
@@ -20,7 +21,7 @@ namespace Model
 			this.StartRecv();
 		}
 
-		public string RemoteAddress
+		public IPEndPoint RemoteAddress
 		{
 			get
 			{

+ 5 - 2
Unity/Hotfix/UI/UILogin/Component/UILoginComponent.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Net;
 using Model;
 using UnityEngine;
 using UnityEngine.UI;
@@ -31,7 +32,8 @@ namespace Hotfix
 		private void OnLogin()
 		{
 			Session session = null;
-			session = Game.Scene.GetComponent<NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address);
+			IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address);
+			session = Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
 			string text = this.account.GetComponent<InputField>().text;
 			session.CallWithAction(new C2R_Login() { Account = text, Password = "111111" }, (response) => LoginOK(response));
 		}
@@ -45,7 +47,8 @@ namespace Hotfix
 				return;
 			}
 
-			Session gateSession = Game.Scene.GetComponent<NetOuterComponent>().Create(r2CLogin.Address);
+			IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(r2CLogin.Address);
+			Session gateSession = Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
 			Game.Scene.AddComponent<SessionComponent>().Session = gateSession;
 
 			SessionComponent.Instance.Session.CallWithAction(new C2G_LoginGate() { Key = r2CLogin.Key },

+ 1 - 0
Unity/Unity.csproj

@@ -231,6 +231,7 @@
     <Compile Include="Assets\Scripts\Base\Network\KNet\Kcp.cs" />
     <Compile Include="Assets\Scripts\Base\Network\KNet\KChannel.cs" />
     <Compile Include="Assets\Scripts\Base\Network\KNet\KService.cs" />
+    <Compile Include="Assets\Scripts\Base\Network\NetworkHelper.cs" />
     <Compile Include="Assets\Scripts\Base\Network\TNet\PacketParser.cs" />
     <Compile Include="Assets\Scripts\Base\Network\TNet\TBuffer.cs" />
     <Compile Include="Assets\Scripts\Base\Network\TNet\TChannel.cs" />