From 03ee73afee03be9eb7b8198f31e0923656a602d7 Mon Sep 17 00:00:00 2001 From: in0finite Date: Sun, 3 Apr 2022 19:01:00 +0200 Subject: [PATCH] cache IP address of clients --- Assets/Scripts/Commands/CoreCommands.cs | 2 +- Assets/Scripts/Networking/Player.cs | 26 +++++++++++++++---------- Assets/Scripts/Stats/PlayerStats.cs | 2 +- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Assets/Scripts/Commands/CoreCommands.cs b/Assets/Scripts/Commands/CoreCommands.cs index 67d2e1c5..dd771b23 100644 --- a/Assets/Scripts/Commands/CoreCommands.cs +++ b/Assets/Scripts/Commands/CoreCommands.cs @@ -78,7 +78,7 @@ namespace SanAndreasUnity.Commands { response += player.netId; if (NetUtils.IsServer && context.hasServerPermissions) - response += " | " + player.connectionToClient.address; + response += " | " + player.CachedIpAddress; response += "\n"; } } diff --git a/Assets/Scripts/Networking/Player.cs b/Assets/Scripts/Networking/Player.cs index 5e678413..43885f62 100644 --- a/Assets/Scripts/Networking/Player.cs +++ b/Assets/Scripts/Networking/Player.cs @@ -41,11 +41,16 @@ namespace SanAndreasUnity.Net public static readonly int MinPlayerNameLength = 2; public static readonly string DefaultPlayerName = "Player"; - public string DescriptionForLogging => "(netId=" + this.netId + ", addr=" + (this.connectionToClient != null ? this.connectionToClient.address : "") + ")"; + public string DescriptionForLogging => $"(netId={this.netId}, addr={this.CachedIpAddress})"; private readonly SyncDictionary m_syncDictionary = new SyncDictionary(); public SyncedBag ExtraData { get; } + /// + /// We cache IP address of the client, because it's sometimes not available (eg. in destroy callbacks). + /// + public string CachedIpAddress { get; private set; } = string.Empty; + Player() { @@ -86,6 +91,16 @@ namespace SanAndreasUnity.Net F.InvokeEventExceptionSafe(onDisable, this); + // log some info about this + if (!this.isLocalPlayer) + Debug.LogFormat("Player {0} disconnected, time: {1}", this.DescriptionForLogging, F.CurrentDateForLogging); + } + + public override void OnStartServer() + { + base.OnStartServer(); + + this.CachedIpAddress = this.connectionToClient.address; } public override void OnStartClient() @@ -113,15 +128,6 @@ namespace SanAndreasUnity.Net F.InvokeEventExceptionSafe(onStart, this); } - public override void OnStopClient() - { - base.OnStopClient(); - - // log some info about this - if (!this.isLocalPlayer) - Debug.LogFormat("Player {0} disconnected, time: {1}", this.DescriptionForLogging, F.CurrentDateForLogging); - } - void OnOwnedGameObjectChanged(GameObject oldGo, GameObject newGo) { Debug.LogFormat("Owned game object changed for player (net id {0})", this.netId); diff --git a/Assets/Scripts/Stats/PlayerStats.cs b/Assets/Scripts/Stats/PlayerStats.cs index 63caef16..59e0a917 100644 --- a/Assets/Scripts/Stats/PlayerStats.cs +++ b/Assets/Scripts/Stats/PlayerStats.cs @@ -52,7 +52,7 @@ namespace SanAndreasUnity.Stats m_currentIndex = 0; GUILayout.Label(p.PlayerName, GUILayout.Width(GetWidth())); - GUILayout.Label(isServer ? p.connectionToClient.address : "", GUILayout.Width(GetWidth())); + GUILayout.Label(p.CachedIpAddress, GUILayout.Width(GetWidth())); GUILayout.Label(p.netId.ToString(), GUILayout.Width(GetWidth())); GUILayout.Label(p.OwnedPed != null ? p.OwnedPed.netId.ToString() : "", GUILayout.Width(GetWidth())); GUILayout.Label(p.OwnedPed != null && p.OwnedPed.PedDef != null ? p.OwnedPed.PedDef.ModelName : "", GUILayout.Width(GetWidth()));