NetworkDiagnostics.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. internal static void OnSend<T>(T message, int channel, int bytes, int count)
  29. where T : struct, NetworkMessage
  30. {
  31. if (count > 0 && OutMessageEvent != null)
  32. {
  33. MessageInfo outMessage = new MessageInfo(message, channel, bytes, count);
  34. OutMessageEvent?.Invoke(outMessage);
  35. }
  36. }
  37. /// <summary>Event for when Mirror receives a message. Can be subscribed to.</summary>
  38. public static event Action<MessageInfo> InMessageEvent;
  39. internal static void OnReceive<T>(T message, int channel, int bytes)
  40. where T : struct, NetworkMessage
  41. {
  42. if (InMessageEvent != null)
  43. {
  44. MessageInfo inMessage = new MessageInfo(message, channel, bytes, 1);
  45. InMessageEvent?.Invoke(inMessage);
  46. }
  47. }
  48. }
  49. }