You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.1 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. namespace Networking
  6. {
  7. public static class ExtensionMethods
  8. {
  9. public static bool TryRead<T>(this NetworkMessage msg, out T outMsg, bool logError = true) where T : MessageBase, new()
  10. {
  11. try
  12. {
  13. outMsg = msg.ReadMessage<T>();
  14. return true;
  15. }
  16. catch
  17. {
  18. outMsg = default(T);
  19. if (logError)
  20. Debug.LogError("Recieved unknown NetworkMessage. Expected typeof " + typeof(T).Name);
  21. return false;
  22. }
  23. }
  24. public static T TryRead<T>(this NetworkMessage msg, bool logError = true) where T : MessageBase, new()
  25. {
  26. T retval;
  27. if (msg.TryRead(out retval,logError))
  28. return retval;
  29. return null;
  30. }
  31. public static int Hash(this NetworkConnection conn)
  32. {
  33. return (conn.address.GetHashCode() + conn.connectionId).GetHashCode();
  34. }
  35. }
  36. }