using System.Collections;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using Milimoe.FunGame.Core.Library.Common.Architecture;
using Milimoe.FunGame.Core.Library.Constant;
// 通用工具类,客户端和服务器端都可以直接调用的工具方法都可以写在这里
namespace Milimoe.FunGame.Core.Api.Utility
{
#region 网络服务
///
/// 网络服务工具箱
///
public class NetworkUtility
{
///
/// 判断字符串是否是IP地址
///
///
///
public static bool IsIP(string str) => 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) => Regex.IsMatch(str, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$");
///
/// 判断字符串是否是正常的用户名(只有中英文和数字)
///
///
///
public static bool IsUserName(string str) => Regex.IsMatch(str, @"^[\u4e00-\u9fa5A-Za-z0-9]+$");
///
/// 获取用户名长度
///
///
///
public static int GetUserNameLength(string str)
{
int length = 0;
for (int i = 0; i < str.Length; i++)
{
char c = str[i];
if (c is >= 'A' and <= 'Z' or >= 'a' and <= 'z' or >= '0' and <= '9') length++;
else length += 2;
}
return length;
}
///
/// 判断字符串是否是一个FunGame可接受的服务器地址
///
///
///
public static ErrorIPAddressType 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 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;
}
///
/// 判断参数是否是一个FunGame可接受的服务器地址
///
///
///
///
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;
else return ErrorIPAddressType.WrongFormat;
}
///
/// 获取服务器的延迟
///
/// 服务器IP地址
///
public static int GetServerPing(string addr)
{
Ping pingSender = new();
PingOptions options = new()
{
DontFragment = true
};
string data = "getserverping";
byte[] buffer = General.DefaultEncoding.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(addr, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
return Convert.ToInt32(reply.RoundtripTime);
}
return -1;
}
///
/// 返回目标对象的Json字符串
///
///
///
///
public static string JsonSerialize(T obj) => Service.JsonManager.GetString(obj);
///
/// 返回目标对象的Json字符串 可指定反序列化选项
///
///
///
///
///
public static string JsonSerialize(T obj, JsonSerializerOptions options) => Service.JsonManager.GetString(obj, options);
///
/// 反序列化Json对象
///
///
///
///
public static T? JsonDeserialize(string json) => Service.JsonManager.GetObject(json);
///
/// 反序列化Json对象 可指定反序列化选项
///
///
///
///
///
public static T? JsonDeserialize(string json, JsonSerializerOptions options) => Service.JsonManager.GetObject(json, options);
///
/// 反序列化Hashtable中的Json对象
///
///
///
///
///
public static T? JsonDeserializeFromHashtable(Hashtable hashtable, string key) => Service.JsonManager.GetObject(hashtable, key);
///
/// 反序列化IEnumerable中的Json对象
///
///
///
///
///
public static T? JsonDeserializeFromIEnumerable(IEnumerable