Client LuaCsForBarotrauma
NetStats.cs
1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
3 using System.Collections.Generic;
4 using System.Linq;
5 
6 namespace Barotrauma.Networking
7 {
8  class NetStats
9  {
10  public enum NetStatType
11  {
12  SentBytes = 0,
13  ReceivedBytes = 1,
14  ResentMessages = 2
15  }
16 
17  private readonly Graph[] graphs;
18 
19  private readonly float[] totalValue;
20  private readonly float[] lastValue;
21 
22  const float UpdateInterval = 0.1f;
23  float updateTimer;
24 
25  public NetStats()
26  {
27  graphs = new Graph[3];
28 
29  totalValue = new float[3];
30  lastValue = new float[3];
31  for (int i = 0; i < 3; i++ )
32  {
33  graphs[i] = new Graph();
34  }
35  }
36 
37  public void AddValue(NetStatType statType, float value)
38  {
39  float valueChange = value - lastValue[(int)statType];
40  totalValue[(int)statType] += valueChange;
41  lastValue[(int)statType] = value;
42  }
43 
44  public void Update(float deltaTime)
45  {
46  updateTimer -= deltaTime;
47 
48  if (updateTimer > 0.0f) return;
49 
50  for (int i = 0; i < 3; i++)
51  {
52  graphs[i].Update(totalValue[i] / UpdateInterval);
53  totalValue[i] = 0.0f;
54  }
55 
56  updateTimer = UpdateInterval;
57  }
58 
59  public void Draw(SpriteBatch spriteBatch, Rectangle rect)
60  {
61  GUI.DrawRectangle(spriteBatch, rect, Color.Black * 0.4f, true);
62 
63  graphs[(int)NetStatType.ReceivedBytes].Draw(spriteBatch, rect, color: Color.Cyan);
64  graphs[(int)NetStatType.SentBytes].Draw(spriteBatch, rect, null, color: GUIStyle.Orange);
65  if (graphs[(int)NetStatType.ResentMessages].Average() > 0)
66  {
67  graphs[(int)NetStatType.ResentMessages].Draw(spriteBatch, rect, color: GUIStyle.Red);
68  GUIStyle.SmallFont.DrawString(spriteBatch, "Peak resent: " + graphs[(int)NetStatType.ResentMessages].LargestValue() + " messages/s",
69  new Vector2(rect.Right + 10, rect.Y + 50), GUIStyle.Red);
70  }
71 
72  GUIStyle.SmallFont.DrawString(spriteBatch,
73  "Peak received: " + MathUtils.GetBytesReadable((int)graphs[(int)NetStatType.ReceivedBytes].LargestValue()) + "/s " +
74  "Avg received: " + MathUtils.GetBytesReadable((int)graphs[(int)NetStatType.ReceivedBytes].Average()) + "/s",
75  new Vector2(rect.Right + 10, rect.Y + 10), Color.Cyan);
76 
77  GUIStyle.SmallFont.DrawString(spriteBatch, "Peak sent: " + MathUtils.GetBytesReadable((int)graphs[(int)NetStatType.SentBytes].LargestValue()) + "/s " +
78  "Avg sent: " + MathUtils.GetBytesReadable((int)graphs[(int)NetStatType.SentBytes].Average()) + "/s",
79  new Vector2(rect.Right + 10, rect.Y + 30), GUIStyle.Orange);
80 #if DEBUG
81  /*int y = 10;
82 
83  foreach (KeyValuePair<string, long> msgBytesSent in server.messageCount.OrderBy(key => -key.Value))
84  {
85  GUIStyle.SmallFont.DrawString(spriteBatch, msgBytesSent.Key + ": " + MathUtils.GetBytesReadable(msgBytesSent.Value),
86  new Vector2(rect.Right - 200, rect.Y + y), GUIStyle.Red);
87 
88  y += 15;
89  }
90 
91  TODO: reimplement?*/
92 #endif
93  }
94  }
95 }
void AddValue(NetStatType statType, float value)
Definition: NetStats.cs:37
void Update(float deltaTime)
Definition: NetStats.cs:44
void Draw(SpriteBatch spriteBatch, Rectangle rect)
Definition: NetStats.cs:59