Assignment for RMIT Mixed Reality in 2020
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.

138 lines
3.2 KiB

  1. #if OVR_PLATFORM_ASYNC_MESSAGES
  2. using System.Threading.Tasks;
  3. #endif
  4. using UnityEngine;
  5. namespace Oculus.Platform
  6. {
  7. public sealed class Request<T> : Request
  8. {
  9. #if OVR_PLATFORM_ASYNC_MESSAGES
  10. private TaskCompletionSource<Message<T>> tcs_ = null;
  11. #endif
  12. private Message<T>.Callback callback_ = null;
  13. public Request(ulong requestID) : base (requestID) { }
  14. public Request<T> OnComplete(Message<T>.Callback callback)
  15. {
  16. if (callback_ != null)
  17. {
  18. throw new UnityException("Attempted to attach multiple handlers to a Request. This is not allowed.");
  19. }
  20. #if OVR_PLATFORM_ASYNC_MESSAGES
  21. if (tcs_ != null)
  22. {
  23. throw new UnityException("Attempted to attach multiple handlers to a Request. This is not allowed.");
  24. }
  25. #endif
  26. callback_ = callback;
  27. Callback.AddRequest(this);
  28. return this;
  29. }
  30. #if OVR_PLATFORM_ASYNC_MESSAGES
  31. new public async Task<Message<T>> Gen()
  32. {
  33. if (callback_ != null || tcs_ != null)
  34. {
  35. throw new UnityException("Attempted to attach multiple handlers to a Request. This is not allowed.");
  36. }
  37. tcs_ = new TaskCompletionSource<Message<T>>();
  38. Callback.AddRequest(this);
  39. return await tcs_.Task;
  40. }
  41. #endif
  42. override public void HandleMessage(Message msg)
  43. {
  44. if (! (msg is Message<T>))
  45. {
  46. Debug.LogError("Unable to handle message: " + msg.GetType());
  47. return;
  48. }
  49. #if OVR_PLATFORM_ASYNC_MESSAGES
  50. if (tcs_ != null)
  51. {
  52. tcs_.SetResult( (Message<T>)msg);
  53. return;
  54. }
  55. #endif
  56. if (callback_ != null)
  57. {
  58. callback_( (Message<T>)msg);
  59. return;
  60. }
  61. throw new UnityException("Request with no handler. This should never happen.");
  62. }
  63. }
  64. public class Request
  65. {
  66. #if OVR_PLATFORM_ASYNC_MESSAGES
  67. private TaskCompletionSource<Message> tcs_;
  68. #endif
  69. private Message.Callback callback_;
  70. public Request(ulong requestID) {this.RequestID = requestID;}
  71. public ulong RequestID {get; set;}
  72. public Request OnComplete(Message.Callback callback)
  73. {
  74. callback_ = callback;
  75. Callback.AddRequest(this);
  76. return this;
  77. }
  78. #if OVR_PLATFORM_ASYNC_MESSAGES
  79. public async Task<Message> Gen() {
  80. tcs_ = new TaskCompletionSource<Message>();
  81. Callback.AddRequest(this);
  82. return await tcs_.Task;
  83. }
  84. #endif
  85. virtual public void HandleMessage(Message msg)
  86. {
  87. #if OVR_PLATFORM_ASYNC_MESSAGES
  88. if (tcs_ != null)
  89. {
  90. tcs_.SetResult(msg);
  91. return;
  92. }
  93. #endif
  94. if (callback_ != null)
  95. {
  96. callback_(msg);
  97. return;
  98. }
  99. throw new UnityException("Request with no handler. This should never happen.");
  100. }
  101. /**
  102. * This will run callbacks on all messages that returned from the server.
  103. * If too many message are coming back at once, then a limit can be passed in
  104. * as an arg to limit the number of messages to run callbacks on at a time
  105. */
  106. public static void RunCallbacks(uint limit = 0)
  107. {
  108. // default of 0 will run callbacks on all messages on the queue
  109. if (limit == 0)
  110. {
  111. Callback.RunCallbacks();
  112. }
  113. else
  114. {
  115. Callback.RunLimitedCallbacks(limit);
  116. }
  117. }
  118. }
  119. }