Advertisement
Guest User

DisconnectAfterKeepAlive

a guest
Jul 13th, 2021
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.04 KB | None | 0 0
  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.  
  11.  
  12. #if UNITY_4_7 || UNITY_5 || UNITY_5_3_OR_NEWER
  13. #define SUPPORTED_UNITY
  14. #endif
  15.  
  16.  
  17. namespace Photon.Realtime
  18. {
  19.     using System;
  20.     using System.Diagnostics;
  21.     using SupportClass = ExitGames.Client.Photon.SupportClass;
  22.  
  23.     #if SUPPORTED_UNITY
  24.     using UnityEngine;
  25.     #endif
  26.  
  27.  
  28.     #if SUPPORTED_UNITY
  29.     public class ConnectionHandler : MonoBehaviour
  30.     #else
  31.     public class ConnectionHandler
  32.     #endif
  33.     {
  34.         /// <summary>
  35.         /// Photon client to log information and statistics from.
  36.         /// </summary>
  37.         public LoadBalancingClient Client { get; set; }
  38.  
  39.         /// <summary>Option to let the fallback thread call Disconnect after the KeepAliveInBackground time. Default: false.</summary>
  40.         /// <remarks>
  41.         /// If set to true, the thread will disconnect the client regularly, should the client not call SendOutgoingCommands / Service.
  42.         /// This may happen due to an app being in background (and not getting a lot of CPU time) or when loading assets.
  43.         ///
  44.         /// If false, a regular timeout time will have to pass (on top) to time out the client.
  45.         /// </remarks>
  46.         public bool DisconnectAfterKeepAlive = false;
  47.        
  48.         /// <summary>Defines for how long the Fallback Thread should keep the connection, before it may time out as usual.</summary>
  49.         /// <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>
  50.         public int KeepAliveInBackground = 60000;
  51.  
  52.         /// <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>
  53.         public int CountSendAcksOnly { get; private set; }
  54.  
  55.         /// <summary>True if a fallback thread is running. Will call the client's SendAcksOnly() method to keep the connection up.</summary>
  56.         public bool FallbackThreadRunning
  57.         {
  58.             get { return this.fallbackThreadId < 255; }
  59.         }
  60.        
  61.         /// <summary>Keeps the ConnectionHandler, even if a new scene gets loaded.</summary>
  62.         public bool ApplyDontDestroyOnLoad = true;
  63.  
  64.         /// <summary>Indicates that the app is closing. Set in OnApplicationQuit().</summary>
  65.         [NonSerialized]
  66.         public static bool AppQuits;
  67.  
  68.  
  69.         private byte fallbackThreadId = 255;
  70.         private bool didSendAcks;
  71.         private readonly Stopwatch backgroundStopwatch = new Stopwatch();
  72.  
  73.  
  74.         #if SUPPORTED_UNITY
  75.  
  76.         #if UNITY_2019_4_OR_NEWER
  77.  
  78.         /// <summary>
  79.         /// Resets statics for Domain Reload
  80.         /// </summary>
  81.         [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  82.         static void StaticReset()
  83.         {
  84.             AppQuits = false;
  85.         }
  86.  
  87.         #endif
  88.  
  89.  
  90.         /// <summary>Called by Unity when the application gets closed. The UnityEngine will also call OnDisable, which disconnects.</summary>
  91.         protected void OnApplicationQuit()
  92.         {
  93.             AppQuits = true;
  94.         }
  95.  
  96.  
  97.         /// <summary></summary>
  98.         protected virtual void Awake()
  99.         {
  100.             if (this.ApplyDontDestroyOnLoad)
  101.             {
  102.                 DontDestroyOnLoad(this.gameObject);
  103.             }
  104.         }
  105.  
  106.         /// <summary>Called by Unity when the application gets closed. Disconnects if OnApplicationQuit() was called before.</summary>
  107.         protected virtual void OnDisable()
  108.         {
  109.             this.StopFallbackSendAckThread();
  110.  
  111.             if (AppQuits)
  112.             {
  113.                 if (this.Client != null && this.Client.IsConnected)
  114.                 {
  115.                     this.Client.Disconnect();
  116.                     this.Client.LoadBalancingPeer.StopThread();
  117.                 }
  118.  
  119.                 SupportClass.StopAllBackgroundCalls();
  120.             }
  121.         }
  122.  
  123.         #endif
  124.  
  125.  
  126.         public void StartFallbackSendAckThread()
  127.         {
  128.             #if !UNITY_WEBGL
  129.             if (this.FallbackThreadRunning)
  130.             {
  131.                 return;
  132.             }
  133.  
  134.             #if UNITY_SWITCH
  135.             this.fallbackThreadId = SupportClass.StartBackgroundCalls(this.RealtimeFallbackThread, 50);  // as workaround, we don't name the Thread.
  136.             #else
  137.             this.fallbackThreadId = SupportClass.StartBackgroundCalls(this.RealtimeFallbackThread, 50, "RealtimeFallbackThread");
  138.             #endif
  139.             #endif
  140.         }
  141.  
  142.         public void StopFallbackSendAckThread()
  143.         {
  144.             #if !UNITY_WEBGL
  145.             if (!this.FallbackThreadRunning)
  146.             {
  147.                 return;
  148.             }
  149.  
  150.             SupportClass.StopBackgroundCalls(this.fallbackThreadId);
  151.             this.fallbackThreadId = 255;
  152.             #endif
  153.         }
  154.  
  155.  
  156.         /// <summary>A thread which runs independent from the Update() calls. Keeps connections online while loading or in background. See <see cref="KeepAliveInBackground"/>.</summary>
  157.         public bool RealtimeFallbackThread()
  158.         {
  159.             if (this.Client != null)
  160.             {
  161.                 if (!this.Client.IsConnected)
  162.                 {
  163.                     this.didSendAcks = false;
  164.                     return true;
  165.                 }
  166.  
  167.                 if (this.Client.LoadBalancingPeer.ConnectionTime - this.Client.LoadBalancingPeer.LastSendOutgoingTime > 100)
  168.                 {
  169.                     if (!this.didSendAcks)
  170.                     {
  171.                         backgroundStopwatch.Reset();
  172.                         backgroundStopwatch.Start();
  173.                     }
  174.                    
  175.                     // check if the client should disconnect after some seconds in background
  176.                     if (backgroundStopwatch.ElapsedMilliseconds > this.KeepAliveInBackground)
  177.                     {
  178.                         if (this.DisconnectAfterKeepAlive)
  179.                         {
  180.                             this.Client.Disconnect();
  181.                         }
  182.                         return true;
  183.                     }
  184.                    
  185.  
  186.                     this.didSendAcks = true;
  187.                     this.CountSendAcksOnly++;
  188.                     this.Client.LoadBalancingPeer.SendAcksOnly();
  189.                 }
  190.                 else
  191.                 {
  192.                     this.didSendAcks = false;
  193.                 }
  194.             }
  195.  
  196.             return true;
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement