using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Milimoe.FunGame.Core.Entity.Enum; namespace Milimoe.FunGame.Core.Api.Utility { /// /// 通用工具类,客户端和服务器端都可以直接调用的工具方法都可以写在这里 /// public class Utility { /// /// 判断字符串是否是IP地址 /// /// /// public static bool IsIP(string str) { //判断是否为IP return Regex.IsMatch(str, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); } /// /// 判断字符串是否为邮箱地址 /// /// /// public static bool IsEmail(string str) { //判断是否为Email return Regex.IsMatch(str, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$"); } /// /// 判断字符串是否是一个FunGame可接受的服务器地址 /// /// /// public static ErrorType IsServerAddress(string str) { string[] strs = str.Split(':'); string ip; int port; if (strs.Length < 2) { ip = strs[0]; port = 22222; } else if (strs.Length < 3) { ip = strs[0]; port = Convert.ToInt32(strs[1]); } else return ErrorType.WrongFormat; if (IsIP(ip) && port > 0 && port < 65536) return ErrorType.None; else if (!IsIP(ip) && port > 0 && port < 65536) return ErrorType.IsNotIP; else if (IsIP(ip) && (port <= 0 || port >= 65536)) return ErrorType.IsNotPort; else return ErrorType.WrongFormat; } /// /// 判断参数是否是一个FunGame可接受的服务器地址 /// /// /// /// public static ErrorType IsServerAddress(string ip, int port) { if (IsIP(ip) && port > 0 && port < 65536) return ErrorType.None; else if (!IsIP(ip) && port > 0 && port < 65536) return ErrorType.IsNotIP; else if (IsIP(ip) && (port <= 0 || port >= 65536)) return ErrorType.IsNotPort; else return ErrorType.WrongFormat; } } }