From 0121c42ac77ebf6dcf41f465cf8468fd5323f0f5 Mon Sep 17 00:00:00 2001 From: milimoe Date: Tue, 16 Jan 2024 23:40:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BD=BF=E7=94=A8=E5=9F=9F?= =?UTF-8?q?=E5=90=8D=E8=BF=9E=E6=8E=A5=E6=9C=8D=E5=8A=A1=E5=99=A8=EF=BC=9B?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E8=BF=9E=E6=8E=A510=E7=A7=92?= =?UTF-8?q?=E5=BC=BA=E5=88=B6=E8=B6=85=E6=97=B6=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/Utility/General.cs | 79 +++++++++++++++++++------- Controller/RunTimeController.cs | 10 ++-- Docs/FunGame.Core.xml | 34 ++++++++--- Interface/Base/ISocket.cs | 2 +- Library/Common/Network/ClientSocket.cs | 2 +- Library/Common/Network/ServerSocket.cs | 2 +- Library/Common/Network/Socket.cs | 12 ++-- Library/Constant/TypeEnum.cs | 2 +- Model/Session.cs | 4 +- Service/SocketManager.cs | 34 +++++++---- 10 files changed, 127 insertions(+), 54 deletions(-) diff --git a/Api/Utility/General.cs b/Api/Utility/General.cs index aac9a10..9ec6466 100644 --- a/Api/Utility/General.cs +++ b/Api/Utility/General.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.Net; using System.Net.NetworkInformation; using System.Security.Cryptography; using System.Text; @@ -56,30 +57,42 @@ namespace Milimoe.FunGame.Core.Api.Utility } /// - /// 判断字符串是否是一个FunGame可接受的服务器地址 + /// 判断字符串是否是一个FunGame可接受的服务器地址 + /// 此方法可以解析域名 /// /// - /// - public static ErrorIPAddressType IsServerAddress(string str) + /// + /// + /// 返回地址验证结果,同时输出服务器地址和端口号 + public static ErrorIPAddressType IsServerAddress(string str, out string addr, out int port) { - string[] strs = str.Split(':'); + addr = ""; + port = 22222; string ip; - int port; - if (strs.Length < 2) + // 包含端口号时,需要先截取 + string[] strs = str.Split(':'); + if (strs.Length == 1) + { + addr = str; + } + else if (strs.Length > 1) + { + addr = strs[0]; + port = int.Parse(strs[1]); + } + else if (strs.Length > 2) + { + return ErrorIPAddressType.WrongFormat; + } + try + { + ip = GetIPAddress(addr); + } + catch { ip = strs[0]; - port = 22222; } - else if (strs.Length < 3) - { - ip = strs[0]; - port = Convert.ToInt32(strs[1]); - } - else return ErrorIPAddressType.WrongFormat; - if (IsIP(ip) && port > 0 && port < 65536) return ErrorIPAddressType.None; - else if (!IsIP(ip) && port > 0 && port < 65536) return ErrorIPAddressType.IsNotIP; - else if (IsIP(ip) && (port <= 0 || port >= 65536)) return ErrorIPAddressType.IsNotPort; - else return ErrorIPAddressType.WrongFormat; + return IsServerAddress(ip, port); } /// @@ -90,9 +103,21 @@ namespace Milimoe.FunGame.Core.Api.Utility /// public static ErrorIPAddressType IsServerAddress(string ip, int port) { - if (IsIP(ip) && port > 0 && port < 65536) return ErrorIPAddressType.None; - else if (!IsIP(ip) && port > 0 && port < 65536) return ErrorIPAddressType.IsNotIP; - else if (IsIP(ip) && (port <= 0 || port >= 65536)) return ErrorIPAddressType.IsNotPort; + if (IsIP(ip)) + { + if (port > 0 && port < 65536) + { + return ErrorIPAddressType.None; + } + else + { + return ErrorIPAddressType.IsNotPort; + } + } + else if (port > 0 && port < 65536) + { + return ErrorIPAddressType.IsNotAddress; + } else return ErrorIPAddressType.WrongFormat; } @@ -119,6 +144,20 @@ namespace Milimoe.FunGame.Core.Api.Utility return -1; } + /// + /// 解析域名为IP地址 + /// + /// + /// + /// + internal static string GetIPAddress(string domain, System.Net.Sockets.AddressFamily family = System.Net.Sockets.AddressFamily.InterNetwork) + { + // 如果是域名,则解析为IP地址 + IPHostEntry entrys = Dns.GetHostEntry(domain); + // 这里使用断言,请自行添加try catch配合使用 + return entrys.AddressList.Where(addr => addr.AddressFamily == family).FirstOrDefault()!.ToString(); + } + /// /// 返回目标对象的Json字符串 /// diff --git a/Controller/RunTimeController.cs b/Controller/RunTimeController.cs index 4a65186..4ce979a 100644 --- a/Controller/RunTimeController.cs +++ b/Controller/RunTimeController.cs @@ -81,13 +81,13 @@ namespace Milimoe.FunGame.Core.Controller /// /// 连接服务器 /// - /// + /// /// /// - public ConnectResult Connect(string ip, int port) + public ConnectResult Connect(string addr, int port) { ArrayList ConnectArgs = []; - if (!BeforeConnect(ref ip, ref port, ConnectArgs)) + if (!BeforeConnect(ref addr, ref port, ConnectArgs)) { return ConnectResult.ConnectFailed; } @@ -98,7 +98,7 @@ namespace Milimoe.FunGame.Core.Controller string notice = ""; // 检查服务器IP地址和端口是否正确 - if (ip == "" || port <= 0) + if (addr == "" || port <= 0) { result = ConnectResult.FindServerFailed; } @@ -106,7 +106,7 @@ namespace Milimoe.FunGame.Core.Controller { // 与服务器建立连接 _Socket?.Close(); - _Socket = Socket.Connect(ip, port); + _Socket = Socket.Connect(addr, port); if (_Socket != null && _Socket.Connected) { if (_Socket.Send(SocketMessageType.Connect, ConnectArgs.Cast().ToArray()) == SocketResult.Success) diff --git a/Docs/FunGame.Core.xml b/Docs/FunGame.Core.xml index 57f602c..d6dd065 100644 --- a/Docs/FunGame.Core.xml +++ b/Docs/FunGame.Core.xml @@ -428,12 +428,15 @@ - + - 判断字符串是否是一个FunGame可接受的服务器地址 + 判断字符串是否是一个FunGame可接受的服务器地址 + 此方法可以解析域名 - + + + 返回地址验证结果,同时输出服务器地址和端口号 @@ -450,6 +453,14 @@ 服务器IP地址 + + + 解析域名为IP地址 + + + + + 返回目标对象的Json字符串 @@ -1086,7 +1097,7 @@ 连接服务器 - + @@ -2322,9 +2333,9 @@ - + - 服务器IP地址 + 服务器地址 @@ -2657,7 +2668,7 @@ 创建客户端Socket - 服务器IP地址 + 服务器IP地址 服务器监听端口 客户端专用Socket @@ -2736,5 +2747,14 @@ + + + 用于取消任务 + + + + + + diff --git a/Interface/Base/ISocket.cs b/Interface/Base/ISocket.cs index c64a26f..691f43c 100644 --- a/Interface/Base/ISocket.cs +++ b/Interface/Base/ISocket.cs @@ -7,7 +7,7 @@ namespace Milimoe.FunGame.Core.Interface.Base public System.Net.Sockets.Socket Instance { get; } public SocketRuntimeType Runtime { get; } public Guid Token { get; } - public string ServerIP { get; } + public string ServerAddress { get; } public int ServerPort { get; } public string ServerName { get; } public string ServerNotice { get; } diff --git a/Library/Common/Network/ClientSocket.cs b/Library/Common/Network/ClientSocket.cs index 40d7ccb..a4fdc55 100644 --- a/Library/Common/Network/ClientSocket.cs +++ b/Library/Common/Network/ClientSocket.cs @@ -9,7 +9,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public System.Net.Sockets.Socket Instance { get; } public SocketRuntimeType Runtime => SocketRuntimeType.Server; public Guid Token { get; } = Guid.Empty; - public string ServerIP { get; } = ""; + public string ServerAddress { get; } = ""; public int ServerPort { get; } = 0; public string ServerName { get; } = ""; public string ServerNotice { get; } = ""; diff --git a/Library/Common/Network/ServerSocket.cs b/Library/Common/Network/ServerSocket.cs index c26361f..66c1ac7 100644 --- a/Library/Common/Network/ServerSocket.cs +++ b/Library/Common/Network/ServerSocket.cs @@ -9,7 +9,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public System.Net.Sockets.Socket Instance { get; } public SocketRuntimeType Runtime => SocketRuntimeType.Server; public Guid Token { get; } = Guid.Empty; - public string ServerIP { get; } = ""; + public string ServerAddress { get; } = ""; public int ServerPort { get; } = 0; public string ServerName { get; } = ""; public string ServerNotice { get; } = ""; diff --git a/Library/Common/Network/Socket.cs b/Library/Common/Network/Socket.cs index 89cdbc6..b70ccf3 100644 --- a/Library/Common/Network/Socket.cs +++ b/Library/Common/Network/Socket.cs @@ -10,7 +10,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public System.Net.Sockets.Socket Instance { get; } public SocketRuntimeType Runtime => SocketRuntimeType.Client; public Guid Token { get; set; } = Guid.Empty; - public string ServerIP { get; } = ""; + public string ServerAddress { get; } = ""; public int ServerPort { get; } = 0; public string ServerName { get; } = ""; public string ServerNotice { get; } = ""; @@ -27,18 +27,18 @@ namespace Milimoe.FunGame.Core.Library.Common.Network private bool _SendingHeartBeat = false; private int _HeartBeatFaileds = 0; - private Socket(System.Net.Sockets.Socket Instance, string ServerIP, int ServerPort) + private Socket(System.Net.Sockets.Socket Instance, string ServerAddress, int ServerPort) { this.Instance = Instance; - this.ServerIP = ServerIP; + this.ServerAddress = ServerAddress; this.ServerPort = ServerPort; this.StartSendingHeartBeat(); } - public static Socket Connect(string IP, int Port = 22222) + public static Socket Connect(string Address, int Port = 22222) { - System.Net.Sockets.Socket? socket = SocketManager.Connect(IP, Port); - if (socket != null) return new Socket(socket, IP, Port); + System.Net.Sockets.Socket? socket = SocketManager.Connect(Address, Port); + if (socket != null) return new Socket(socket, Address, Port); else throw new ConnectFailedException(); } diff --git a/Library/Constant/TypeEnum.cs b/Library/Constant/TypeEnum.cs index 9269513..adb883a 100644 --- a/Library/Constant/TypeEnum.cs +++ b/Library/Constant/TypeEnum.cs @@ -145,7 +145,7 @@ namespace Milimoe.FunGame.Core.Library.Constant public enum ErrorIPAddressType { None, - IsNotIP, + IsNotAddress, IsNotPort, WrongFormat } diff --git a/Model/Session.cs b/Model/Session.cs index 4da6e91..cbc6ed8 100644 --- a/Model/Session.cs +++ b/Model/Session.cs @@ -6,9 +6,9 @@ namespace Milimoe.FunGame.Core.Model public class Session { /// - /// 服务器IP地址 + /// 服务器地址 /// - public string Server_IP { get; set; } = ""; + public string Server_Address { get; set; } = ""; /// /// 服务器端口号 diff --git a/Service/SocketManager.cs b/Service/SocketManager.cs index e4975cf..36aee0a 100644 --- a/Service/SocketManager.cs +++ b/Service/SocketManager.cs @@ -77,31 +77,45 @@ namespace Milimoe.FunGame.Core.Service /// /// 创建客户端Socket /// - /// 服务器IP地址 + /// 服务器IP地址 /// 服务器监听端口 /// 客户端专用Socket - internal static Socket? Connect(string IP, int Port = 22222) + internal static Socket? Connect(string Address, int Port = 22222) { Socket? ClientSocket; EndPoint ServerEndPoint; try { - ClientSocket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + string IP = Api.Utility.NetworkUtility.GetIPAddress(Address); ServerEndPoint = new IPEndPoint(IPAddress.Parse(IP), Port); if (ServerEndPoint != null) { - while (true) + ClientSocket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + bool Connecting = true; + Task t = Task.Run(() => { - if (!ClientSocket.Connected) + while (Connecting) { - ClientSocket.Connect(ServerEndPoint); - if (ClientSocket.Connected) + if (!ClientSocket.Connected && Connecting) { - ClientSocket.NoDelay = true; - _Socket = ClientSocket; - return _Socket; + ClientSocket.Connect(ServerEndPoint); + if (ClientSocket.Connected) + { + ClientSocket.NoDelay = true; + _Socket = ClientSocket; + break; + } } } + }); + if (t.Wait(10 * 1000) && (_Socket?.Connected ?? false)) + { + return _Socket; + } + else + { + Connecting = false; + throw new ConnectFailedException(); } } }