Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HTTPUpdateDelegator.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using UnityEngine;
  2. #if NETFX_CORE || BUILD_FOR_WP8
  3. using System.Threading.Tasks;
  4. #endif
  5. namespace BestHTTP
  6. {
  7. /// <summary>
  8. /// Will route some U3D calls to the HTTPManager.
  9. /// </summary>
  10. [ExecuteInEditMode]
  11. public sealed class HTTPUpdateDelegator : MonoBehaviour
  12. {
  13. #region Public Properties
  14. /// <summary>
  15. /// The singleton instance of the HTTPUpdateDelegator
  16. /// </summary>
  17. public static HTTPUpdateDelegator Instance { get; private set; }
  18. /// <summary>
  19. /// True, if the Instance property should hold a valid value.
  20. /// </summary>
  21. public static bool IsCreated { get; private set; }
  22. /// <summary>
  23. /// Set it true before any CheckInstance() call, or before any request send to dispatch callbacks on another thread.
  24. /// </summary>
  25. public static bool IsThreaded { get; set; }
  26. /// <summary>
  27. /// It's true if the dispatch thread running.
  28. /// </summary>
  29. public static bool IsThreadRunning { get; private set; }
  30. /// <summary>
  31. /// How much time the plugin should wait between two update call. Its default value 100 ms.
  32. /// </summary>
  33. public static int ThreadFrequencyInMS { get; set; }
  34. /// <summary>
  35. /// Called in the OnApplicationQuit function. If this function returns False, the plugin will not start to
  36. /// shut down itself.
  37. /// </summary>
  38. public static System.Func<bool> OnBeforeApplicationQuit;
  39. #endregion
  40. private static bool IsSetupCalled;
  41. static HTTPUpdateDelegator()
  42. {
  43. ThreadFrequencyInMS = 100;
  44. }
  45. /// <summary>
  46. /// Will create the HTTPUpdateDelegator instance and set it up.
  47. /// </summary>
  48. public static void CheckInstance()
  49. {
  50. try
  51. {
  52. if (!IsCreated)
  53. {
  54. GameObject go = GameObject.Find("HTTP Update Delegator");
  55. if (go != null)
  56. Instance = go.GetComponent<HTTPUpdateDelegator>();
  57. if (Instance == null)
  58. {
  59. go = new GameObject("HTTP Update Delegator");
  60. go.hideFlags = HideFlags.HideAndDontSave;
  61. #if UNITY_EDITOR
  62. if (UnityEditor.EditorApplication.isPlaying)
  63. GameObject.DontDestroyOnLoad(go);
  64. #endif
  65. Instance = go.AddComponent<HTTPUpdateDelegator>();
  66. }
  67. IsCreated = true;
  68. #if UNITY_EDITOR
  69. if (!UnityEditor.EditorApplication.isPlaying)
  70. {
  71. UnityEditor.EditorApplication.update -= Instance.Update;
  72. UnityEditor.EditorApplication.update += Instance.Update;
  73. }
  74. UnityEditor.EditorApplication.playmodeStateChanged -= Instance.OnPlayModeStateChanged;
  75. UnityEditor.EditorApplication.playmodeStateChanged += Instance.OnPlayModeStateChanged;
  76. #endif
  77. }
  78. }
  79. catch
  80. {
  81. HTTPManager.Logger.Error("HTTPUpdateDelegator", "Please call the BestHTTP.HTTPManager.Setup() from one of Unity's event(eg. awake, start) before you send any request!");
  82. }
  83. }
  84. private void Setup()
  85. {
  86. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  87. Caching.HTTPCacheService.SetupCacheFolder();
  88. #endif
  89. #if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR)
  90. Cookies.CookieJar.SetupFolder();
  91. Cookies.CookieJar.Load();
  92. #endif
  93. #if UNITY_WEBGL
  94. // Threads are not implemented in WEBGL builds, disable it for now.
  95. IsThreaded = false;
  96. #endif
  97. if (IsThreaded)
  98. {
  99. #if NETFX_CORE
  100. Windows.System.Threading.ThreadPool.RunAsync(ThreadFunc);
  101. #else
  102. new System.Threading.Thread(ThreadFunc)
  103. .Start();
  104. #endif
  105. }
  106. IsSetupCalled = true;
  107. }
  108. #if NETFX_CORE
  109. async
  110. #endif
  111. void ThreadFunc(object obj)
  112. {
  113. HTTPManager.Logger.Information ("HTTPUpdateDelegator", "Update Thread Started");
  114. try
  115. {
  116. IsThreadRunning = true;
  117. while (IsThreadRunning)
  118. {
  119. HTTPManager.OnUpdate();
  120. #if NETFX_CORE
  121. await Task.Delay(ThreadFrequencyInMS);
  122. #else
  123. System.Threading.Thread.Sleep(ThreadFrequencyInMS);
  124. #endif
  125. }
  126. }
  127. finally
  128. {
  129. HTTPManager.Logger.Information("HTTPUpdateDelegator", "Update Thread Ended");
  130. }
  131. }
  132. void Update()
  133. {
  134. if (!IsSetupCalled)
  135. {
  136. IsSetupCalled = true;
  137. Setup();
  138. }
  139. if (!IsThreaded)
  140. HTTPManager.OnUpdate();
  141. }
  142. #if UNITY_EDITOR
  143. void OnPlayModeStateChanged()
  144. {
  145. if (UnityEditor.EditorApplication.isPlaying)
  146. UnityEditor.EditorApplication.update -= Update;
  147. else if (!UnityEditor.EditorApplication.isPlaying)
  148. UnityEditor.EditorApplication.update += Update;
  149. }
  150. #endif
  151. void OnDisable()
  152. {
  153. #if UNITY_EDITOR
  154. if (UnityEditor.EditorApplication.isPlaying)
  155. #endif
  156. OnApplicationQuit();
  157. }
  158. void OnApplicationQuit()
  159. {
  160. if (OnBeforeApplicationQuit != null)
  161. {
  162. try
  163. {
  164. if (!OnBeforeApplicationQuit())
  165. {
  166. HTTPManager.Logger.Information("HTTPUpdateDelegator", "OnBeforeApplicationQuit call returned false, postponing plugin shutdown.");
  167. return;
  168. }
  169. }
  170. catch(System.Exception ex)
  171. {
  172. HTTPManager.Logger.Exception("HTTPUpdateDelegator", string.Empty, ex);
  173. }
  174. }
  175. IsThreadRunning = false;
  176. if (!IsCreated)
  177. return;
  178. IsCreated = false;
  179. HTTPManager.OnQuit();
  180. #if UNITY_EDITOR
  181. UnityEditor.EditorApplication.update -= Update;
  182. UnityEditor.EditorApplication.playmodeStateChanged -= OnPlayModeStateChanged;
  183. #endif
  184. }
  185. }
  186. }