ConnectionQuality.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // standalone, Unity-independent connection-quality algorithm & enum.
  2. // don't need to use this directly, it's built into Mirror's NetworkClient.
  3. using UnityEngine;
  4. namespace Mirror
  5. {
  6. public enum ConnectionQuality : byte
  7. {
  8. EXCELLENT, // ideal experience for high level competitors
  9. GOOD, // very playable for everyone but high level competitors
  10. FAIR, // very noticeable latency, not very enjoyable anymore
  11. POOR, // unplayable
  12. ESTIMATING, // still estimating
  13. }
  14. // provide different heuristics for users to choose from.
  15. // simple heuristics to get started.
  16. // this will be iterated on over time based on user feedback.
  17. public static class ConnectionQualityHeuristics
  18. {
  19. // convenience extension to color code Connection Quality
  20. public static Color ColorCode(this ConnectionQuality quality)
  21. {
  22. switch (quality)
  23. {
  24. case ConnectionQuality.EXCELLENT: return Color.green;
  25. case ConnectionQuality.GOOD: return Color.yellow;
  26. case ConnectionQuality.FAIR: return new Color(1.0f, 0.647f, 0.0f);
  27. case ConnectionQuality.POOR: return Color.red;
  28. default: return Color.gray;
  29. }
  30. }
  31. // straight forward estimation
  32. // rtt: average round trip time in seconds.
  33. // jitter: average latency variance.
  34. public static ConnectionQuality Simple(double rtt, double jitter)
  35. {
  36. if (rtt <= 0.100 && jitter <= 0.10) return ConnectionQuality.EXCELLENT;
  37. if (rtt <= 0.200 && jitter <= 0.20) return ConnectionQuality.GOOD;
  38. if (rtt <= 0.400 && jitter <= 0.50) return ConnectionQuality.FAIR;
  39. return ConnectionQuality.POOR;
  40. }
  41. // snapshot interpolation based estimation.
  42. // snap. interp. adjusts buffer time based on connection quality.
  43. // based on this, we can measure how far away we are from the ideal.
  44. // the returned quality will always directly correlate with gameplay.
  45. // => requires SnapshotInterpolation dynamicAdjustment to be enabled!
  46. public static ConnectionQuality Pragmatic(double targetBufferTime, double currentBufferTime)
  47. {
  48. // buffer time is set by the game developer.
  49. // estimating in multiples is a great way to be game independent.
  50. // for example, a fast paced shooter and a slow paced RTS will both
  51. // have poor connection if the multiplier is >10.
  52. double multiplier = currentBufferTime / targetBufferTime;
  53. // empirically measured with Tanks demo + LatencySimulation.
  54. // it's not obvious to estimate on paper.
  55. if (multiplier <= 1.15) return ConnectionQuality.EXCELLENT;
  56. if (multiplier <= 1.25) return ConnectionQuality.GOOD;
  57. if (multiplier <= 1.50) return ConnectionQuality.FAIR;
  58. // anything else is poor
  59. return ConnectionQuality.POOR;
  60. }
  61. }
  62. }