From 9f660fa4b07f84df347f15758e097039be170437 Mon Sep 17 00:00:00 2001 From: hondacrx Date: Wed, 13 Sep 2023 18:31:04 -0400 Subject: [PATCH] Core/Realms: Minor refactor to realm address storage Port From (https://github.com/TrinityCore/TrinityCore/commit/cc6dba1376905456062d1ade8adcbaed4f8f484e) --- .../Managers/LoginServiceManager.cs | 4 +-- Source/Framework/Realm/Realm.cs | 32 ++++--------------- Source/Framework/Realm/RealmManager.cs | 8 ++--- Source/Game/Server/WorldSession.cs | 4 +-- Source/Game/World/WorldManager.cs | 4 +-- 5 files changed, 17 insertions(+), 35 deletions(-) diff --git a/Source/BNetServer/Managers/LoginServiceManager.cs b/Source/BNetServer/Managers/LoginServiceManager.cs index 22684c270..4a4d1e114 100644 --- a/Source/BNetServer/Managers/LoginServiceManager.cs +++ b/Source/BNetServer/Managers/LoginServiceManager.cs @@ -120,9 +120,9 @@ namespace BNetServer public string GetHostnameForClient(IPEndPoint address) { if (IPAddress.IsLoopback(address.Address)) - return _hostnames[0]; + return _hostnames[1]; - return _hostnames[1]; + return _hostnames[0]; } public int GetPort() { return _port; } diff --git a/Source/Framework/Realm/Realm.cs b/Source/Framework/Realm/Realm.cs index ff99d2374..dfa0c6943 100644 --- a/Source/Framework/Realm/Realm.cs +++ b/Source/Framework/Realm/Realm.cs @@ -4,8 +4,8 @@ using Framework.Constants; using Framework.Realm; using System; +using System.Collections.Generic; using System.Net; -using System.Net.Sockets; public class Realm : IEquatable { @@ -16,28 +16,13 @@ public class Realm : IEquatable NormalizedName = NormalizedName.Replace(" ", ""); } - public IPEndPoint GetAddressForClient(IPAddress clientAddr) + public IPAddress GetAddressForClient(IPAddress clientAddr) { - IPAddress realmIp = ExternalAddress; - // Attempt to send best address for client if (IPAddress.IsLoopback(clientAddr)) - { - // Try guessing if realm is also connected locally - if (IPAddress.IsLoopback(LocalAddress) || IPAddress.IsLoopback(ExternalAddress)) - realmIp = clientAddr; - else - { - // Assume that user connecting from the machine that authserver is located on - // has all realms available in his local network - realmIp = LocalAddress; - } - } + return Addresses[1]; - IPEndPoint endpoint = new(realmIp, Port); - - // Return external IP - return endpoint; + return Addresses[0]; } public uint GetConfigId() @@ -57,9 +42,7 @@ public class Realm : IEquatable public bool Equals(Realm other) { - return other.ExternalAddress.Equals(ExternalAddress) - && other.LocalAddress.Equals(LocalAddress) - && other.Port == Port + return other.Port == Port && other.Name == Name && other.Type == Type && other.Flags == Flags @@ -70,13 +53,12 @@ public class Realm : IEquatable public override int GetHashCode() { - return new { ExternalAddress, LocalAddress, Port, Name, Type, Flags, Timezone, AllowedSecurityLevel, PopulationLevel }.GetHashCode(); + return new { Port, Name, Type, Flags, Timezone, AllowedSecurityLevel, PopulationLevel }.GetHashCode(); } public RealmId Id; public uint Build; - public IPAddress ExternalAddress; - public IPAddress LocalAddress; + public List Addresses = new(); public ushort Port; public string Name; public string NormalizedName; diff --git a/Source/Framework/Realm/RealmManager.cs b/Source/Framework/Realm/RealmManager.cs index d3b17d606..1d132258d 100644 --- a/Source/Framework/Realm/RealmManager.cs +++ b/Source/Framework/Realm/RealmManager.cs @@ -92,8 +92,8 @@ public class RealmManager : Singleton var realm = new Realm(); uint realmId = result.Read(0); realm.Name = result.Read(1); - realm.ExternalAddress = IPAddress.Parse(result.Read(2)); - realm.LocalAddress = IPAddress.Parse(result.Read(3)); + realm.Addresses.Add(IPAddress.Parse(result.Read(2))); + realm.Addresses.Add(IPAddress.Parse(result.Read(3))); realm.Port = result.Read(4); RealmType realmType = (RealmType)result.Read(5); if (realmType == RealmType.FFAPVP) @@ -120,9 +120,9 @@ public class RealmManager : Singleton _subRegions.Add(subRegion); if (!existingRealms.ContainsKey(realm.Id)) - Log.outInfo(LogFilter.Realmlist, "Added realm \"{0}\" at {1}:{2}", realm.Name, realm.ExternalAddress.ToString(), realm.Port); + Log.outInfo(LogFilter.Realmlist, "Added realm \"{0}\" at {1}:{2}", realm.Name, realm.Addresses[0].ToString(), realm.Port); else - Log.outDebug(LogFilter.Realmlist, "Updating realm \"{0}\" at {1}:{2}", realm.Name, realm.ExternalAddress.ToString(), realm.Port); + Log.outDebug(LogFilter.Realmlist, "Updating realm \"{0}\" at {1}:{2}", realm.Name, realm.Addresses[0].ToString(), realm.Port); existingRealms.Remove(realm.Id); } diff --git a/Source/Game/Server/WorldSession.cs b/Source/Game/Server/WorldSession.cs index 6162f8a9a..6c94aac48 100644 --- a/Source/Game/Server/WorldSession.cs +++ b/Source/Game/Server/WorldSession.cs @@ -490,12 +490,12 @@ namespace Game if (instanceAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { - connectTo.Payload.Where.IPv4 = instanceAddress.Address.GetAddressBytes(); + connectTo.Payload.Where.IPv4 = instanceAddress.GetAddressBytes(); connectTo.Payload.Where.Type = ConnectTo.AddressType.IPv4; } else { - connectTo.Payload.Where.IPv6 = instanceAddress.Address.GetAddressBytes(); + connectTo.Payload.Where.IPv6 = instanceAddress.GetAddressBytes(); connectTo.Payload.Where.Type = ConnectTo.AddressType.IPv6; } diff --git a/Source/Game/World/WorldManager.cs b/Source/Game/World/WorldManager.cs index 78548db67..b55136a94 100644 --- a/Source/Game/World/WorldManager.cs +++ b/Source/Game/World/WorldManager.cs @@ -2443,8 +2443,8 @@ namespace Game return false; _realm.SetName(result.Read(1)); - _realm.ExternalAddress = System.Net.IPAddress.Parse(result.Read(2)); - _realm.LocalAddress = System.Net.IPAddress.Parse(result.Read(3)); + _realm.Addresses.Add(System.Net.IPAddress.Parse(result.Read(2))); + _realm.Addresses.Add(System.Net.IPAddress.Parse(result.Read(3))); _realm.Port = result.Read(4); _realm.Type = result.Read(5); _realm.Flags = (RealmFlags)result.Read(6);