//using Amazon.OpenSearchService.Model.Internal.MarshallTransformations; //using Google.Protobuf; //using Google.Protobuf.Collections; //using Nettention.Proud; //using Newtonsoft.Json; //using ServerCommon.Redis; //using ServerCore; using ServerBase; //using System; //using System.Net; //using System.Net.Http; //using System.Net.NetworkInformation; //using System.Net.Sockets; //using System.Numerics; //using System.Security.Cryptography; //using System.Text; //using System.Threading.Channels; //namespace ServerCommon //{ // public static class ServerUtil // { // //public static Random rnd = new Random((int)DateTime.Now.Ticks); // //public static Mutex duplicateProcessCheckMutex = new Mutex(); // //static readonly object loginToGameIndexLock = new object(); // //static int loginToGameIndex = 0; // //public static void MakeP2PGroupToByteArray(ByteArray byArray, P2PGroupType p2PGroupType) // //{ // // using (MemoryStream stream = new()) // // { // // using (CodedOutputStream output = new(stream)) // // { // // p2PGroupType.WriteTo(output); // // } // // byArray.AddRange(stream.ToArray()); // // } // //} // //public static string CreateServerName(ServerType type, string ip, int port, int worldId = 0, int channel = 0) // //{ // // string serverType = type.ToString(); // // string serverName = string.Empty; // // switch (type) // // { // // case ServerType.Channel: // // { // // serverName = ($"{serverType}:{MakeWorldidToString(worldId)}:{MakeChannelToString(channel)}"); // // } // // break; // // case ServerType.Login: // // case ServerType.Indun: // // case ServerType.Chat: // // case ServerType.Auth: // // case ServerType.Manager: // // { // // serverName = ($"{serverType}:{ip}_{port.ToString()}"); // // } // // break; // // default: // // { // // serverName = ($"{serverType}:{ip}_{port.ToString()}"); // // Log.getLogger().error($"Check !!! EServerType:{type} And CreateServerName()"); // // } // // break; // // } // // return serverName; // //} // //public static string MakeWorldidToString(int worldId) // //{ // // return worldId.ToString("000"); // //} // //public static string MakeChannelToString(int channel) // //{ // // return channel.ToString("000"); // //} // //public static ServerType GetServerType(string serverName) // //{ // // var serverType = serverName.Split(":")[0]; // // if (!Enum.TryParse(serverType, out ServerType type)) // // return ServerType.None; // // return type; // //} // //public static bool MakeStringToInt(string data, out int result) // //{ // // try // // { // // result = int.Parse(data); // // } // // catch // // { // // result = 0; // // return false; // // } // // return true; // //} // //public static Pos MakePos(float x, float y, float z, int angle) // //{ // // Pos pos = new Pos(); // // pos.X = x; // // pos.Y = y; // // pos.Z = z; // // pos.Angle = angle; // // return pos; // //} // /// // /// *게임 서버 고르기*
// /// 서버 수용인원의 80% 되지 않은 서버에서 선택
// /// 모든 서버가 80% 이상인 경우 서버 수용인원을 넘지 않은 서버에서 선택 // ///
// /// 게임 서버 목록 // /// 입력값 List<ServerInfo> serverList 의 인덱스, 선택된 서버가 없는 경우 -1 // public static int GetBestGameServerIndex(in List serverList, LanguageType languageType) // { // int serverIndex = -1; // int limit_first = Constant.g_MaxUser * 5 / 10; // /* // foreach (var serverInfo in serverList.Select((value, index) => new { value, index })) // { // if (serverInfo.value.Language == languageType && // limit_first > serverInfo.value.Sessions) // { // serverIndex = serverInfo.index; // break; // } // } // if (serverIndex == -1) // { // foreach (var serverInfo in serverList.Select((value, index) => new { value, index })) // { // if (limit_first > serverInfo.value.Sessions) // { // serverIndex = serverInfo.index; // break; // } // } // } // */ // if (serverIndex == -1) // { // lock(loginToGameIndexLock) // { // if (loginToGameIndex >= serverList.Count) // loginToGameIndex = 0; // serverIndex = loginToGameIndex; // ++loginToGameIndex; // } // } // return serverIndex; // } // /// // /// *게임 서버 현재 총 접속자 구하기*
// ///
// /// 게임 서버 목록 // //public static bool GetGameServerCurrentUserCount(in List serverList, out int maxCapacity, out int userCount) // //{ // // maxCapacity = Constant.g_MaxUser * serverList.Count; // // userCount = 0; // // //capPercent = 0.0F; // // if (serverList.Count == 0) return false; // // foreach (var serverInfo in serverList.Select((value, index) => new { value, index })) // // { // // userCount += serverInfo.value.Sessions; // // } // // //capPercent = (userCount * 100.0F / maxCapacity) ; // // return true; // //} // /// // /// *인스턴스 룸을 생성할 인스턴스 서버 고르기*
// /// 인스턴스 룸 최대 인원이 들어갈 수 있는 서버 중 수용인원이 가장 많은 서버 선택 // ///
// /// 인스턴스 서버 목록 // /// 생성할 인스턴스 룸 최대 인원 // /// 입력값 List<ServerInfo> serverList 의 인덱스, 선택된 서버가 없는 경우 -1 // //public static int GetBestInstanceServerIndexForCreate(in List serverList, int roomCapacity) // //{ // // int retIndex = -1; // // int maxCount = 0; // // foreach (var (server_info, index) in serverList.Select((value, index) => (value, index))) // // { // // if (maxCount < server_info.RoomCapacity + roomCapacity && server_info.RoomCapacity + roomCapacity <= server_info.Capacity) // // { // // retIndex = index; // // maxCount = server_info.RoomCapacity + roomCapacity; // // } // // } // // return retIndex; // //} // //public static async Task RunPeriodicTask(Func action, TimeSpan interval, CancellationToken cancellationToken = default) // //{ // // using var timer = new PeriodicTimer(interval); // // while (cancellationToken.IsCancellationRequested == false) // // { // // try // // { // // await action(); // // await timer.WaitForNextTickAsync(cancellationToken); // // } // // catch (OperationCanceledException) // // { // // break; // // } // // catch (Exception ex) // // { // // Log.getLogger().error($"{ex}"); // // throw; // // } // // } // //} // //public static bool isExistProcess(string serverName) // //{ // // duplicateProcessCheckMutex = new Mutex(true, serverName, out bool createNew); // // return createNew == true ? false : true; // //} // //public static LanguageType getLanguageByChannel(int channel) // //{ // // if(channel < 1) // // { // // return LanguageType.Ko; // // } // // // 아래의 로직은 채널에 적용할 언어 설정을 메타로 별도 분리 하여 // // // 채널 언어 설정 정책에 따라 로직화 될 수 있도록 수정 한다. - kangms // // var language_count = (Enum.GetValues(typeof(LanguageType)).Length - 1); // // language_count -= 1; // 임시로 맨마지막 태국어는 제외 시킨다. - kangms // // int returnValue = channel % language_count; // // // 1번 채널을 영어로 설정하기 위해 변경 (현재 채널은 영어, 한국어만 사용중 2024.1.7) // // switch (returnValue) // // { // // case 0: // // returnValue = (int)LanguageType.Ko; // // break; // // case 1: // // returnValue = (int)LanguageType.En; // // break; // // } // // return ((LanguageType)returnValue); // //} // //public static readonly byte[] key = { 0xFF,0x10,0x24,0x31,0x44, // // 0x58,0x6F,0x79,0x8F,0x90, // // 0x0F,0x1E,0x2A,0x33,0x4B, // // 0x5A,0x67,0x70,0x83,0x90, // // 0x00,0x15,0x28,0x33,0x44, // // 0x51,0x65,0x7F,0x8E,0x91, // // 0x00,0x11 }; // //public static readonly byte[] iv = { 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5 }; // //AES 암호화 // /* // public static string AESEncrypt(string input) // { // try // { // RijndaelManaged aes = new RijndaelManaged(); // //aes.KeySize = 256; //AES256으로 사용시 // aes.KeySize = 128; //AES128로 사용시 // aes.BlockSize = 128; // aes.Mode = CipherMode.CBC; // aes.Padding = PaddingMode.PKCS7; // aes.Key = key; // aes.IV = iv; // var encrypt = aes.CreateEncryptor(aes.Key, aes.IV); // byte[] buf = null; // using (var ms = new MemoryStream()) // { // using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write)) // { // byte[] xXml = Encoding.UTF8.GetBytes(input); // cs.Write(xXml, 0, xXml.Length); // } // buf = ms.ToArray(); // } // string Output = Convert.ToBase64String(buf); // return Output; // } // catch (Exception ex) // { // Log.getLogger().error(ex.Message); // return string.Empty; // } // } // //AES 복호화 // public static string AESDecrypt(string input) // { // try // { // RijndaelManaged aes = new RijndaelManaged(); // //aes.KeySize = 256; //AES256으로 사용시 // aes.KeySize = 128; //AES128로 사용시 // aes.BlockSize = 128; // aes.Mode = CipherMode.CBC; // aes.Padding = PaddingMode.PKCS7; // aes.Key = key; // aes.IV = iv; // var decrypt = aes.CreateDecryptor(); // byte[] buf = null; // using (var ms = new MemoryStream()) // { // using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write)) // { // byte[] xXml = Convert.FromBase64String(input); // cs.Write(xXml, 0, xXml.Length); // } // buf = ms.ToArray(); // } // string Output = Encoding.UTF8.GetString(buf); // return Output; // } // catch (Exception ex) // { // Log.getLogger().error(ex.Message); // return string.Empty; // } // } // */ // //public static string EncryptAes(string plainText) // //{ // // // Check arguments. // // if (plainText == null || plainText.Length <= 0) // // NullReferenceCheckHelper.throwIfNull(plainText, () => $"plainText is null !!!"); // // if (key == null || key.Length <= 0) // // NullReferenceCheckHelper.throwIfNull(key, () => $"key is null !!!"); // // if (iv == null || iv.Length <= 0) // // NullReferenceCheckHelper.throwIfNull(iv, () => $"iv is null !!!"); // // byte[] encrypted; // // // Create an Aes object // // // with the specified key and IV. // // using (Aes aesAlg = Aes.Create()) // // { // // aesAlg.Key = key; // // aesAlg.IV = iv; // // // Create an encryptor to perform the stream transform. // // ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); // // // Create the streams used for encryption. // // using (MemoryStream msEncrypt = new MemoryStream()) // // { // // using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) // // { // // using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) // // { // // //Write all data to the stream. // // swEncrypt.Write(plainText); // // } // // encrypted = msEncrypt.ToArray(); // // } // // } // // } // // // Return the encrypted bytes from the memory stream. // // return Convert.ToBase64String(encrypted); // //} // //public static string DecryptAes(string cipherText) // //{ // // // Check arguments. // // if (cipherText == null || cipherText.Length <= 0) // // NullReferenceCheckHelper.throwIfNull(cipherText, () => $"cipherText is null !!!"); // // if (key == null || key.Length <= 0) // // NullReferenceCheckHelper.throwIfNull(key, () => $"key is null !!!"); // // if (iv == null || iv.Length <= 0) // // NullReferenceCheckHelper.throwIfNull(iv, () => $"iv is null !!!"); // // // Declare the string used to hold // // // the decrypted text. // // string plaintext = string.Empty; // // byte[] chipherBin = Convert.FromBase64String(cipherText); // // // Create an Aes object // // // with the specified key and IV. // // using (Aes aesAlg = Aes.Create()) // // { // // aesAlg.Key = key; // // aesAlg.IV = iv; // // // Create a decryptor to perform the stream transform. // // ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); // // // Create the streams used for decryption. // // using (MemoryStream msDecrypt = new MemoryStream(chipherBin)) // // { // // using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) // // { // // using (StreamReader srDecrypt = new StreamReader(csDecrypt)) // // { // // // Read the decrypted bytes from the decrypting stream // // // and place them in a string. // // plaintext = srDecrypt.ReadToEnd(); // // } // // } // // } // // } // // return plaintext; // //} // //public static string getByteStringFromString(string input) // //{ // // byte[] bytes = Encoding.Default.GetBytes(input); // // string byteString = BitConverter.ToString(bytes).Replace("-", ""); // // return byteString; // //} // //public static string getStringFromByteString(string input) { // // byte[] bytes = new byte[input.Length / 2]; // // for (int i = 0; i < bytes.Length; i++) // // { // // bytes[i] = Convert.ToByte(input.Substring(i * 2, 2), 16); // // } // // string result = Encoding.Default.GetString(bytes); // // return result; // //} // //public static async Task WriteServerInfoSaveData(AwsNewInstanceInfo myServerInfo) // //{ // // using (var stream = new StreamWriter("./serverInfo.json")) // // { // // string json = JsonConvert.SerializeObject(myServerInfo); // // await stream.WriteAsync(json); // // } // //} // //public static AwsNewInstanceInfo? ReadServerInfoSaveData() // //{ // // if (File.Exists("./serverInfo.json")) // // { // // string readData = File.ReadAllText("./serverInfo.json"); // // if (readData != null) // // { // // var dicValue = JsonConvert.DeserializeObject(readData); // // if (dicValue != null) // // { // // return dicValue; // // } // // } // // } // // return null; // //} // //public static bool isBanWorld(string word, bool includeNickname) // //{ // // var lowered_word = word.ToLower(); // // if (MetaData.Instance._BanWordMetaTable.Any(banWord => lowered_word.Contains(banWord) == true)) // // { // // return true; // // } // // return includeNickname && isBanNickname(word); // //} // //private static bool isBanNickname(string word) // //{ // // var lowered_word = word.ToLower(); // // return MetaData.Instance._BanWordNicknameMetaTable.Any(banNickNameWord => lowered_word.Contains(banNickNameWord) == true); // //} // //public static EPlaceType toPlaceType(this ContentsType contentsType) // //{ // // switch (contentsType) // // { // // case ContentsType.None: // // return EPlaceType.NONE; // // case ContentsType.MyHome: // // return EPlaceType.MyHome; // // case ContentsType.DressRoom: // // return EPlaceType.DressRoom; // // case ContentsType.Concert: // // return EPlaceType.Concert; // // case ContentsType.Movie: // // return EPlaceType.Movie; // // case ContentsType.Instance: // // return EPlaceType.Instance; // // case ContentsType.BeaconCreateRoom: // // return EPlaceType.BeaconCreateRoom; // // case ContentsType.BeaconEditRoom: // // return EPlaceType.BeaconEditRoom; // // case ContentsType.BeaconDraftRoom: // // return EPlaceType.BeaconDraftRoom; // // case ContentsType.EditRoom: // // return EPlaceType.EditRoom; // // case ContentsType.BeaconCustomizeRoom: // // return EPlaceType.BeaconCustomizeRoom; // // case ContentsType.BattleRoom: // // return EPlaceType.BattleRoom; // // } // // return EPlaceType.NONE; // //} // } //}