Seven is the number.
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.

185 lines
6.0 KiB

4 years ago
3 years ago
4 years ago
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="ConnectionHandler.cs" company="Exit Games GmbH">
  3. // Loadbalancing Framework for Photon - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // If the game logic does not call Service() for whatever reason, this keeps the connection.
  7. // </summary>
  8. // <author>developer@photonengine.com</author>
  9. // ----------------------------------------------------------------------------
  10. #if UNITY_4_7 || UNITY_5 || UNITY_5_3_OR_NEWER
  11. #define SUPPORTED_UNITY
  12. #endif
  13. namespace Photon.Realtime
  14. {
  15. using System;
  16. using SupportClass = ExitGames.Client.Photon.SupportClass;
  17. #if SUPPORTED_UNITY
  18. using UnityEngine;
  19. #endif
  20. #if SUPPORTED_UNITY
  21. public class ConnectionHandler : MonoBehaviour
  22. #else
  23. public class ConnectionHandler
  24. #endif
  25. {
  26. /// <summary>
  27. /// Photon client to log information and statistics from.
  28. /// </summary>
  29. public LoadBalancingClient Client { get; set; }
  30. private byte fallbackThreadId = 255;
  31. private bool didSendAcks;
  32. private int startedAckingTimestamp;
  33. private int deltaSinceStartedToAck;
  34. /// <summary>Defines for how long the Fallback Thread should keep the connection, before it may time out as usual.</summary>
  35. /// <remarks>We want to the Client to keep it's connection when an app is in the background (and doesn't call Update / Service Clients should not keep their connection indefinitely in the background, so after some milliseconds, the Fallback Thread should stop keeping it up.</remarks>
  36. public int KeepAliveInBackground = 60000;
  37. /// <summary>Counts how often the Fallback Thread called SendAcksOnly, which is purely of interest to monitor if the game logic called SendOutgoingCommands as intended.</summary>
  38. public int CountSendAcksOnly { get; private set; }
  39. public bool FallbackThreadRunning
  40. {
  41. get { return this.fallbackThreadId < 255; }
  42. }
  43. #if SUPPORTED_UNITY
  44. #if UNITY_2019_4_OR_NEWER
  45. /// <summary>
  46. /// Resets statics for Domain Reload
  47. /// </summary>
  48. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  49. static void StaticReset()
  50. {
  51. AppQuits = false;
  52. }
  53. #endif
  54. /// <summary>Keeps the ConnectionHandler, even if a new scene gets loaded.</summary>
  55. public bool ApplyDontDestroyOnLoad = true;
  56. /// <summary>Indicates that the app is closing. Set in OnApplicationQuit().</summary>
  57. [NonSerialized]
  58. public static bool AppQuits;
  59. /// <summary>Called by Unity when the application gets closed. The UnityEngine will also call OnDisable, which disconnects.</summary>
  60. protected void OnApplicationQuit()
  61. {
  62. AppQuits = true;
  63. }
  64. /// <summary></summary>
  65. protected virtual void Awake()
  66. {
  67. if (this.ApplyDontDestroyOnLoad)
  68. {
  69. DontDestroyOnLoad(this.gameObject);
  70. }
  71. }
  72. /// <summary>Called by Unity when the application gets closed. Disconnects if OnApplicationQuit() was called before.</summary>
  73. protected virtual void OnDisable()
  74. {
  75. this.StopFallbackSendAckThread();
  76. if (AppQuits)
  77. {
  78. if (this.Client != null && this.Client.IsConnected)
  79. {
  80. this.Client.Disconnect();
  81. this.Client.LoadBalancingPeer.StopThread();
  82. }
  83. SupportClass.StopAllBackgroundCalls();
  84. }
  85. }
  86. #endif
  87. public void StartFallbackSendAckThread()
  88. {
  89. #if !UNITY_WEBGL
  90. if (this.FallbackThreadRunning)
  91. {
  92. return;
  93. }
  94. #if UNITY_SWITCH
  95. this.fallbackThreadId = SupportClass.StartBackgroundCalls(this.RealtimeFallbackThread, 50); // as workaround, we don't name the Thread.
  96. #else
  97. this.fallbackThreadId = SupportClass.StartBackgroundCalls(this.RealtimeFallbackThread, 50, "RealtimeFallbackThread");
  98. #endif
  99. #endif
  100. }
  101. public void StopFallbackSendAckThread()
  102. {
  103. #if !UNITY_WEBGL
  104. if (!this.FallbackThreadRunning)
  105. {
  106. return;
  107. }
  108. SupportClass.StopBackgroundCalls(this.fallbackThreadId);
  109. this.fallbackThreadId = 255;
  110. #endif
  111. }
  112. /// <summary>A thread which runs independent from the Update() calls. Keeps connections online while loading or in background. See <see cref="KeepAliveInBackground"/>.</summary>
  113. public bool RealtimeFallbackThread()
  114. {
  115. if (this.Client != null)
  116. {
  117. if (!this.Client.IsConnected)
  118. {
  119. this.didSendAcks = false;
  120. return true;
  121. }
  122. if (this.Client.LoadBalancingPeer.ConnectionTime - this.Client.LoadBalancingPeer.LastSendOutgoingTime > 100)
  123. {
  124. if (this.didSendAcks)
  125. {
  126. // check if the client should disconnect after some seconds in background
  127. this.deltaSinceStartedToAck = Environment.TickCount - this.startedAckingTimestamp;
  128. if (this.deltaSinceStartedToAck > this.KeepAliveInBackground)
  129. {
  130. return true;
  131. }
  132. }
  133. else
  134. {
  135. this.startedAckingTimestamp = Environment.TickCount;
  136. }
  137. this.didSendAcks = true;
  138. this.CountSendAcksOnly++;
  139. this.Client.LoadBalancingPeer.SendAcksOnly();
  140. }
  141. else
  142. {
  143. this.didSendAcks = false;
  144. }
  145. }
  146. return true;
  147. }
  148. }
  149. }