NetworkDiagnostics.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. namespace Mirror
  3. {
  4. /// <summary>Profiling statistics for tool to subscribe to (profiler etc.)</summary>
  5. public static class NetworkDiagnostics
  6. {
  7. /// <summary>Describes an outgoing message</summary>
  8. public readonly struct MessageInfo
  9. {
  10. /// <summary>The message being sent</summary>
  11. public readonly NetworkMessage message;
  12. /// <summary>channel through which the message was sent</summary>
  13. public readonly int channel;
  14. /// <summary>how big was the message (does not include transport headers)</summary>
  15. public readonly int bytes;
  16. /// <summary>How many connections was the message sent to.</summary>
  17. public readonly int count;
  18. internal MessageInfo(NetworkMessage message, int channel, int bytes, int count)
  19. {
  20. this.message = message;
  21. this.channel = channel;
  22. this.bytes = bytes;
  23. this.count = count;
  24. }
  25. }
  26. /// <summary>Event for when Mirror sends a message. Can be subscribed to.</summary>
  27. public static event Action<MessageInfo> OutMessageEvent;
  28. /// <summary>Event for when Mirror receives a message. Can be subscribed to.</summary>
  29. public static event Action<MessageInfo> InMessageEvent;
  30. // RuntimeInitializeOnLoadMethod -> fast playmode without domain reload
  31. [UnityEngine.RuntimeInitializeOnLoadMethod]
  32. static void ResetStatics()
  33. {
  34. InMessageEvent = null;
  35. OutMessageEvent = null;
  36. }
  37. internal static void OnSend<T>(T message, int channel, int bytes, int count)
  38. where T : struct, NetworkMessage
  39. {
  40. if (count > 0 && OutMessageEvent != null)
  41. {
  42. MessageInfo outMessage = new MessageInfo(message, channel, bytes, count);
  43. OutMessageEvent?.Invoke(outMessage);
  44. }
  45. }
  46. internal static void OnReceive<T>(T message, int channel, int bytes)
  47. where T : struct, NetworkMessage
  48. {
  49. if (InMessageEvent != null)
  50. {
  51. MessageInfo inMessage = new MessageInfo(message, channel, bytes, 1);
  52. InMessageEvent?.Invoke(inMessage);
  53. }
  54. }
  55. }
  56. }