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.

172 lines
5.7 KiB

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. /// <summary>Keeps the ConnectionHandler, even if a new scene gets loaded.</summary>
  45. public bool ApplyDontDestroyOnLoad = true;
  46. /// <summary>Indicates that the app is closing. Set in OnApplicationQuit().</summary>
  47. [NonSerialized]
  48. public static bool AppQuits;
  49. /// <summary>Called by Unity when the application gets closed. The UnityEngine will also call OnDisable, which disconnects.</summary>
  50. protected void OnApplicationQuit()
  51. {
  52. AppQuits = true;
  53. }
  54. /// <summary></summary>
  55. protected virtual void Awake()
  56. {
  57. if (this.ApplyDontDestroyOnLoad)
  58. {
  59. DontDestroyOnLoad(this.gameObject);
  60. }
  61. }
  62. /// <summary>Called by Unity when the application gets closed. Disconnects if OnApplicationQuit() was called before.</summary>
  63. protected virtual void OnDisable()
  64. {
  65. this.StopFallbackSendAckThread();
  66. if (AppQuits)
  67. {
  68. if (this.Client != null && this.Client.IsConnected)
  69. {
  70. this.Client.Disconnect();
  71. this.Client.LoadBalancingPeer.StopThread();
  72. }
  73. SupportClass.StopAllBackgroundCalls();
  74. }
  75. }
  76. #endif
  77. public void StartFallbackSendAckThread()
  78. {
  79. #if !UNITY_WEBGL
  80. if (this.FallbackThreadRunning)
  81. {
  82. return;
  83. }
  84. #if UNITY_SWITCH
  85. this.fallbackThreadId = SupportClass.StartBackgroundCalls(this.RealtimeFallbackThread, 50); // as workaround, we don't name the Thread.
  86. #else
  87. this.fallbackThreadId = SupportClass.StartBackgroundCalls(this.RealtimeFallbackThread, 50, "RealtimeFallbackThread");
  88. #endif
  89. #endif
  90. }
  91. public void StopFallbackSendAckThread()
  92. {
  93. #if !UNITY_WEBGL
  94. if (!this.FallbackThreadRunning)
  95. {
  96. return;
  97. }
  98. SupportClass.StopBackgroundCalls(this.fallbackThreadId);
  99. this.fallbackThreadId = 255;
  100. #endif
  101. }
  102. /// <summary>A thread which runs independent from the Update() calls. Keeps connections online while loading or in background. See <see cref="KeepAliveInBackground"/>.</summary>
  103. public bool RealtimeFallbackThread()
  104. {
  105. if (this.Client != null)
  106. {
  107. if (!this.Client.IsConnected)
  108. {
  109. this.didSendAcks = false;
  110. return true;
  111. }
  112. if (this.Client.LoadBalancingPeer.ConnectionTime - this.Client.LoadBalancingPeer.LastSendOutgoingTime > 100)
  113. {
  114. if (this.didSendAcks)
  115. {
  116. // check if the client should disconnect after some seconds in background
  117. this.deltaSinceStartedToAck = Environment.TickCount - this.startedAckingTimestamp;
  118. if (this.deltaSinceStartedToAck > this.KeepAliveInBackground)
  119. {
  120. return true;
  121. }
  122. }
  123. else
  124. {
  125. this.startedAckingTimestamp = Environment.TickCount;
  126. }
  127. this.didSendAcks = true;
  128. this.CountSendAcksOnly++;
  129. this.Client.LoadBalancingPeer.SendAcksOnly();
  130. }
  131. else
  132. {
  133. this.didSendAcks = false;
  134. }
  135. }
  136. return true;
  137. }
  138. }
  139. }