Advertisement
Guest User

Untitled

a guest
Jun 6th, 2019
1,300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. using System;
  2. using Photon.Realtime;
  3. using UnityEngine;
  4.  
  5. namespace Photon.Pun.UtilityScripts
  6. {
  7. /// <summary>
  8. /// Unexpected disconnects recovery
  9. /// </summary>
  10. public class DisconnectsRecovery : MonoBehaviourPunCallbacks
  11. {
  12. [Tooltip("Whether or not attempt a rejoin without doing any checks.")]
  13. [SerializeField]
  14. private bool skipRejoinChecks;
  15. [Tooltip("Whether or not realtime webhooks are configured with persistence enabled")]
  16. [SerializeField]
  17. private bool persistenceEnabled;
  18.  
  19. private bool rejoinCalled;
  20.  
  21. private int minTimeRequiredToRejoin = 0; // TODO: set dynamically based on PhotonNetwork.NetworkingClient.LoadBalancingPeer.RoundTripTime
  22.  
  23. private DisconnectCause lastDisconnectCause;
  24. private bool wasInRoom;
  25.  
  26. private bool reconnectCalled;
  27.  
  28. private bool pendingAction;
  29.  
  30. public override void OnEnable()
  31. {
  32. base.OnEnable();
  33. PhotonNetwork.NetworkingClient.StateChanged += this.OnStateChanged;
  34. PhotonNetwork.KeepAliveInBackground = 0f;
  35. PhotonNetwork.PhotonServerSettings.RunInBackground = false;
  36. Application.runInBackground = false;
  37. }
  38.  
  39. public override void OnDisable()
  40. {
  41. base.OnDisable();
  42. PhotonNetwork.NetworkingClient.StateChanged -= this.OnStateChanged;
  43. }
  44.  
  45.  
  46. private void OnStateChanged(ClientState fromState, ClientState toState)
  47. {
  48. if (toState == ClientState.Disconnected)
  49. {
  50. Debug.LogFormat("OnStateChanged from {0} to {1}, PeerState={2}", fromState, toState,
  51. PhotonNetwork.NetworkingClient.LoadBalancingPeer.PeerState);
  52.  
  53. Debug.Log("Pending action raised");
  54. pendingAction = true;
  55.  
  56. }
  57. }
  58.  
  59. private void Update()
  60. {
  61. if (pendingAction)
  62. {
  63. pendingAction = false;
  64. Debug.Log("handle disconnect now");
  65. this.HandleDisconnect();
  66. }
  67. }
  68.  
  69.  
  70. public override void OnDisconnected(DisconnectCause cause)
  71. {
  72. Debug.LogFormat("OnDisconnected(cause={0}) ClientState={1} PeerState={2}",
  73. cause,
  74. PhotonNetwork.NetworkingClient.State,
  75. PhotonNetwork.NetworkingClient.LoadBalancingPeer.PeerState);
  76. if (rejoinCalled)
  77. {
  78. Debug.LogError("Rejoin failed, client disconnected");
  79. rejoinCalled = false;
  80. return;
  81. }
  82.  
  83. if (reconnectCalled)
  84. {
  85. Debug.LogError("Reconnect failed, client disconnected");
  86. reconnectCalled = false;
  87. return;
  88. }
  89. lastDisconnectCause = cause;
  90. wasInRoom = PhotonNetwork.CurrentRoom != null;
  91. if (PhotonNetwork.NetworkingClient.State == ClientState.Disconnected)
  92. {
  93. Debug.Log("Pending action raised");
  94. pendingAction = true;
  95. }
  96. }
  97.  
  98. private void HandleDisconnect()
  99. {
  100. switch (lastDisconnectCause)
  101. {
  102. case DisconnectCause.Exception:
  103. case DisconnectCause.ServerTimeout:
  104. case DisconnectCause.ClientTimeout:
  105. case DisconnectCause.DisconnectByServerLogic:
  106. case DisconnectCause.AuthenticationTicketExpired:
  107. case DisconnectCause.DisconnectByServerReasonUnknown:
  108. if (wasInRoom)
  109. {
  110. Debug.Log("CheckAndRejoin called");
  111. this.CheckAndRejoin();
  112. }
  113. else
  114. {
  115. Debug.Log("PhotonNetwork.Reconnect called");
  116. reconnectCalled = PhotonNetwork.Reconnect();
  117. }
  118. break;
  119. case DisconnectCause.OperationNotAllowedInCurrentState:
  120. case DisconnectCause.CustomAuthenticationFailed:
  121. case DisconnectCause.DisconnectByClientLogic:
  122. case DisconnectCause.InvalidAuthentication:
  123. case DisconnectCause.ExceptionOnConnect:
  124. case DisconnectCause.MaxCcuReached:
  125. case DisconnectCause.InvalidRegion:
  126. case DisconnectCause.None:
  127. break;
  128. default:
  129. throw new ArgumentOutOfRangeException("cause", lastDisconnectCause, null);
  130. }
  131. lastDisconnectCause = DisconnectCause.None;
  132. wasInRoom = false;
  133. }
  134.  
  135. public override void OnJoinRoomFailed(short returnCode, string message)
  136. {
  137. if (!rejoinCalled)
  138. {
  139. return;
  140. }
  141. rejoinCalled = false;
  142. Debug.LogErrorFormat("Quick rejoin failed with error code: {0} & error message: {1}", returnCode, message);
  143. }
  144.  
  145. public override void OnJoinedRoom()
  146. {
  147. if (rejoinCalled)
  148. {
  149. Debug.Log("Rejoin successful");
  150. rejoinCalled = false;
  151. }
  152. }
  153.  
  154. private void CheckAndRejoin()
  155. {
  156. if (skipRejoinChecks)
  157. {
  158. Debug.Log("PhotonNetwork.ReconnectAndRejoin called");
  159. rejoinCalled = PhotonNetwork.ReconnectAndRejoin();
  160. }
  161. else
  162. {
  163. bool wasLastActivePlayer = true;
  164. if (!persistenceEnabled)
  165. {
  166. for (int i = 0; i < PhotonNetwork.PlayerListOthers.Length; i++)
  167. {
  168. if (!PhotonNetwork.PlayerListOthers[i].IsInactive)
  169. {
  170. wasLastActivePlayer = false;
  171. break;
  172. }
  173. }
  174. }
  175. if ((PhotonNetwork.CurrentRoom.PlayerTtl < 0 || PhotonNetwork.CurrentRoom.PlayerTtl > minTimeRequiredToRejoin) // PlayerTTL checks
  176. && (!wasLastActivePlayer || PhotonNetwork.CurrentRoom.EmptyRoomTtl > minTimeRequiredToRejoin || persistenceEnabled)) // EmptyRoomTTL checks
  177. {
  178. Debug.Log("PhotonNetwork.ReconnectAndRejoin called");
  179. rejoinCalled = PhotonNetwork.ReconnectAndRejoin();
  180. }
  181. else
  182. {
  183. Debug.Log("PhotonNetwork.ReconnectAndRejoin not called, PhotonNetwork.Reconnect is called instead.");
  184. reconnectCalled = PhotonNetwork.Reconnect();
  185. }
  186. }
  187. }
  188.  
  189. public override void OnConnectedToMaster()
  190. {
  191. if (reconnectCalled)
  192. {
  193. Debug.Log("Reconnect successful");
  194. reconnectCalled = false;
  195. }
  196. }
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement