Skip to main content

ServerPingProfiler

A class in Root.Township, Alta.Utilities.Profiling.NetworkTransportMetrics.ABTesting.

Networked behaviour that measures round-trip time to every player by sending a ping event and timing the reply. Server reads the result through ServerProfilingData.AveragePing; reach it through the static Instance.

Constructors​

MemberWhat it is
ServerPingProfiler()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

5 private and internal fields
MemberWhat it is
private const long maxPingConstant 5000. Not used in this class.
private readonly Dictionary<int, float> playerIdToPingMapLast measured round trip in seconds, keyed by player identifier.
private readonly Dictionary<int, float> playerIdToPingStartTimeMapTime each player's last ping was sent, keyed by player identifier.
private MethodSync syncPingClientToServerMethod sync (NetworkedEventB) the client uses to answer a ping.
private MethodSync syncPingServerToClientMethod sync (NetworkedEventA) the server uses to send a ping.

Properties​

MemberWhat it is
static ServerPingProfiler Instance { get; private set; }The single profiler in the scene, set in Initialize.

Methods​

MemberWhat it is
float GetAveragePing()Server: sends a new ping to every player, then returns the average of the last measured round trips in seconds, or -1 if none yet.
override void Initialize()Sets the static Instance (logs an error if one already exists) and creates the two ping MethodSync events.
4 private and internal methods
MemberWhat it is
private static float GetTimeStamp()Returns Time.realtimeSinceStartup, used to time pings.
private void SendPingMessagesToConnectedClients()Server: sends the ping event to each player in Player.AllPlayers.
private void SyncPlayerPingClientToServer(IPlayer player, Stream stream)Server side of the reply: works out the round trip from the stored start time and saves it for that player.
private void SyncPlayerPingServerToClient(IPlayer player, Stream stream)Ping event handler. When writing it stores the send time for the player; on a remote client it sends the reply event back.