Search Unity

Question about Ping (or RTT) and Packet Loss

Discussion in 'Multiplayer' started by IcaroDLima, Oct 30, 2019.

  1. IcaroDLima

    IcaroDLima

    Joined:
    Aug 6, 2019
    Posts:
    25
    I have a game where I want to make the current Ping available to the user, so I make some requests and store them in a list, old requests are removed.

    My code is similar to this one, I made some simplifications so you don't get bored, the problem here is not about the code, but how it should work:
    Code (CSharp):
    1. public class Metrics : MonoBehaviour
    2. {
    3.     public int pingAverage = -1;
    4.  
    5.     void Start()
    6.     {
    7.         StartCoroutine(Refreshing());
    8.     }
    9.  
    10.     IEnumerator Refreshing()
    11.     {
    12.         // Remember 10 pings.
    13.         const int WINDOW_SIZE = 10;
    14.  
    15.         int sum = 0;
    16.         LinkedList<int> pings = new LinkedList<int>();
    17.  
    18.         while (true)
    19.         {
    20.             // Simplified. Low precision.
    21.             float start = Time.timeSinceLevelLoad;
    22.             yield return conn.Request();
    23.             float end = Time.timeSinceLevelLoad;
    24.  
    25.             if (!conn.err)
    26.             {
    27.                 int ping = (end - start) * 1000;
    28.  
    29.                 sum += ping;
    30.                 pings.AddLast(ping);
    31.  
    32.                 if (pings.Count > WINDOW_SIZE)
    33.                 {
    34.                     int first = pings.First.Value;
    35.  
    36.                     pings.RemoveFirst();
    37.                     sum -= first;
    38.                 }
    39.  
    40.                 pingAverage = sum / pings.Count;
    41.             }
    42.             else
    43.             {
    44.                 // What i do here?
    45.             }
    46.  
    47.             yield return new WaitForSeconds(0.5f);
    48.         }
    49.     }
    50. }
    With this code, if there are errors, it may be that the average ping is 10 ms, but 10 minutes ago that no measurements were made. I remember that for example League of Legends set Ping at 500 ms when the game was disconnected.

    *Note that error checking sounds like a packet loss to me.
     
    Last edited: Oct 31, 2019
  2. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    154
    In your code there's a reference to tcp. Are you trying to monitor TCP packet loss? If so it sounds a bit confusing as packet loss in TCP is handled by the protocol itself.

    Also what is the error condition you're checking in the code?
     
    Joe-Censored likes this.
  3. IcaroDLima

    IcaroDLima

    Joined:
    Aug 6, 2019
    Posts:
    25
    I edited, tcp was just any name.