Core/Realms: Minor refactor to realm address storage

Port From (https://github.com/TrinityCore/TrinityCore/commit/cc6dba1376905456062d1ade8adcbaed4f8f484e)
This commit is contained in:
hondacrx
2023-09-13 18:31:04 -04:00
parent f89df8dca8
commit 9f660fa4b0
5 changed files with 17 additions and 35 deletions
+7 -25
View File
@@ -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<Realm>
{
@@ -16,28 +16,13 @@ public class Realm : IEquatable<Realm>
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<Realm>
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<Realm>
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<IPAddress> Addresses = new();
public ushort Port;
public string Name;
public string NormalizedName;
+4 -4
View File
@@ -92,8 +92,8 @@ public class RealmManager : Singleton<RealmManager>
var realm = new Realm();
uint realmId = result.Read<uint>(0);
realm.Name = result.Read<string>(1);
realm.ExternalAddress = IPAddress.Parse(result.Read<string>(2));
realm.LocalAddress = IPAddress.Parse(result.Read<string>(3));
realm.Addresses.Add(IPAddress.Parse(result.Read<string>(2)));
realm.Addresses.Add(IPAddress.Parse(result.Read<string>(3)));
realm.Port = result.Read<ushort>(4);
RealmType realmType = (RealmType)result.Read<byte>(5);
if (realmType == RealmType.FFAPVP)
@@ -120,9 +120,9 @@ public class RealmManager : Singleton<RealmManager>
_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);
}