using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using ServerCore; using ServerBase; namespace ServerBase; public static class ServerHelper { public static Mutex m_server_running_mutex = new Mutex(); public static bool isValidServerType(this ServerType type) { switch (type) { case ServerType.Channel: case ServerType.Login: case ServerType.Indun: case ServerType.Chat: case ServerType.Auth: case ServerType.Manager: break; default: { Log.getLogger().error($"Invalid ServerType !!! : ServerType:{type}"); return false; } } return true; } public static LocationTargetType toLocationTargetType(this string serverType) { var server_type = EnumHelper.convertEnumValueStringToEnum(serverType, ServerType.None); switch (server_type) { case ServerType.Channel: return LocationTargetType.World; case ServerType.Indun: return LocationTargetType.Instance; } return LocationTargetType.None; } public static ServerType toServerType(this string serverType) { return EnumHelper.convertEnumValueStringToEnum(serverType, ServerType.None); } public static ServerType fillupServerType(string serverName) { var server_type = serverName.Split(":")[0]; return EnumHelper.convertEnumValueStringToEnum(server_type, ServerType.None); } public static string toServerTypeString(this ServerType serverType) { return serverType.ToString(); } public static string toServerName(this ServerType type, string ip, UInt16 port, int worldId = 0, int channel = 0) { var server_type = type.ToString(); var server_name = string.Empty; if (false == type.isValidServerType()) { server_name = makeServerNameByNetworkAddress(type, ip, port); Log.getLogger().error($"Failed to convert ServerName !!! : {server_name}"); return server_name; } switch (type) { case ServerType.Channel: { server_name = makeGameServerName(type, worldId, channel); } break; default: { server_name = makeServerNameByNetworkAddress(type, ip, port); } break; } return server_name; } public static string makeGameServerName(ServerType type, int worldId, int channel) { return ($"{type.ToString()}:{ServerHelper.makeWorldIdToString(worldId)}:{ServerHelper.makeChannelToString(channel)}"); } public static string makeServerNameByNetworkAddress(ServerType type, string ip, UInt16 port) { return ($"{type.ToString()}:{ip}_{port.ToString()}"); } public static string makeWorldIdToString(int worldId) { return worldId.ToString("000"); } public static string makeChannelToString(int channel) { return channel.ToString("000"); } public static bool isRunningServerWithListenPort(this Process currProcess, UInt16 listenPort) { return isExistProcess(currProcess.ProcessName + Convert.ToString(listenPort)); } public static bool isExistProcess(string serverName) { m_server_running_mutex = new Mutex(true, serverName, out bool create_new); return create_new == true ? false : true; } public static NetworkAddress toNetworkAddress(this ServerInfo serverInfo) { var network_address = new NetworkAddress(); network_address.IP = serverInfo.Address; network_address.Port = (UInt16)serverInfo.Port; return network_address; } public static ClientToLoginMessage.Types.GameServerInfo toGameServerInfo(this NetworkAddress networkAddress) { var game_server_info = new ClientToLoginMessage.Types.GameServerInfo(); game_server_info.GameServerAddr = networkAddress.IP; game_server_info.GameServerPort = networkAddress.Port; return game_server_info; } }