AgoraCallbackQueue.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Agora.Rtc
  6. {
  7. internal sealed class AgoraCallbackQueue : MonoBehaviour
  8. {
  9. private readonly Queue<Action> _queue = new Queue<Action>();
  10. internal void ClearQueue()
  11. {
  12. lock (_queue)
  13. {
  14. _queue.Clear();
  15. }
  16. }
  17. internal void EnQueue(Action action)
  18. {
  19. lock (_queue)
  20. {
  21. if (action != null)
  22. {
  23. _queue.Enqueue(action);
  24. }
  25. }
  26. }
  27. internal Action DeQueue()
  28. {
  29. Action action = null;
  30. lock (_queue)
  31. {
  32. if (_queue.Count > 0)
  33. {
  34. action = _queue.Dequeue();
  35. }
  36. return action;
  37. }
  38. }
  39. private void Update()
  40. {
  41. lock (_queue)
  42. {
  43. while (_queue.Count > 0)
  44. {
  45. try
  46. {
  47. _queue.Dequeue().Invoke();
  48. }
  49. catch(Exception e)
  50. {
  51. AgoraLog.LogError("[Exception] AgoraCallbackQueue: " + e);
  52. }
  53. }
  54. }
  55. }
  56. private void OnDestroy()
  57. {
  58. ClearQueue();
  59. }
  60. }
  61. }
  62. #endif