Advertisement
Guest User

Loloo

a guest
Apr 20th, 2014
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1.  
  2.  
  3. /// <summary>
  4. /// Internal Monobehaviour that allows Photon to run an Update loop.
  5. /// </summary>
  6. internal class PhotonHandler : Photon.MonoBehaviour, IPhotonPeerListener
  7. {
  8. public static PhotonHandler SP;
  9.  
  10. public int updateInterval; // time [ms] between consecutive SendOutgoingCommands calls
  11.  
  12. public int updateIntervalOnSerialize; // time [ms] between consecutive RunViewUpdate calls (sending syncs, etc)
  13.  
  14. private int nextSendTickCount = 0;
  15.  
  16. private int nextSendTickCountOnSerialize = 0;
  17.  
  18. private static bool sendThreadShouldRun;
  19.  
  20. protected void Awake()
  21. {
  22. if (SP != null && SP != this && SP.gameObject != null)
  23. {
  24. GameObject.DestroyImmediate(SP.gameObject);
  25. }
  26.  
  27. SP = this;
  28. DontDestroyOnLoad(this.gameObject);
  29.  
  30. this.updateInterval = 1000 / PhotonNetwork.sendRate;
  31. this.updateIntervalOnSerialize = 1000 / PhotonNetwork.sendRateOnSerialize;
  32.  
  33. PhotonHandler.StartFallbackSendAckThread();
  34. }
  35.  
  36. /// <summary>Called by Unity when the application is closed. Tries to disconnect.</summary>
  37. protected void OnApplicationQuit()
  38. {
  39. PhotonHandler.StopFallbackSendAckThread();
  40. PhotonNetwork.Disconnect();
  41. }
  42.  
  43. protected void Update()
  44. {
  45. if (PhotonNetwork.networkingPeer == null)
  46. {
  47. Debug.LogError("NetworkPeer broke!");
  48. return;
  49. }
  50.  
  51. if (PhotonNetwork.connectionStateDetailed == PeerState.PeerCreated || PhotonNetwork.connectionStateDetailed == PeerState.Disconnected || PhotonNetwork.offlineMode)
  52. {
  53. return;
  54. }
  55.  
  56. // the messageQueue might be paused. in that case a thread will send acknowledgements only. nothing else to do here.
  57. if (!PhotonNetwork.isMessageQueueRunning)
  58. {
  59. return;
  60. }
  61.  
  62. bool doDispatch = true;
  63. while (PhotonNetwork.isMessageQueueRunning && doDispatch)
  64. {
  65. // DispatchIncomingCommands() returns true of it found any command to dispatch (event, result or state change)
  66. Profiler.BeginSample("DispatchIncomingCommands");
  67. doDispatch = PhotonNetwork.networkingPeer.DispatchIncomingCommands();
  68. Profiler.EndSample();
  69. }
  70.  
  71. int currentMsSinceStart = (int)(Time.realtimeSinceStartup * 1000); // avoiding Environment.TickCount, which could be negative on long-running platforms
  72. if (PhotonNetwork.isMessageQueueRunning && currentMsSinceStart > this.nextSendTickCountOnSerialize)
  73. {
  74. PhotonNetwork.networkingPeer.RunViewUpdate();
  75. this.nextSendTickCountOnSerialize = currentMsSinceStart + this.updateIntervalOnSerialize;
  76. this.nextSendTickCount = 0; // immediately send when synchronization code was running
  77. }
  78.  
  79. currentMsSinceStart = (int)(Time.realtimeSinceStartup * 1000);
  80. if (currentMsSinceStart > this.nextSendTickCount)
  81. {
  82. bool doSend = true;
  83. while (PhotonNetwork.isMessageQueueRunning && doSend)
  84. {
  85. // Send all outgoing commands
  86. Profiler.BeginSample("SendOutgoingCommands");
  87. doSend = PhotonNetwork.networkingPeer.SendOutgoingCommands();
  88. Profiler.EndSample();
  89. }
  90.  
  91. this.nextSendTickCount = currentMsSinceStart + this.updateInterval;
  92. }
  93. }
  94.  
  95. /// <summary>Called by Unity after a new level was loaded.</summary>
  96. protected void OnLevelWasLoaded(int level)
  97. {
  98. PhotonNetwork.networkingPeer.NewSceneLoaded();
  99. PhotonNetwork.networkingPeer.SetLevelInPropsIfSynced(Application.loadedLevelName);
  100. }
  101.  
  102. protected void OnJoinedRoom()
  103. {
  104. PhotonNetwork.networkingPeer.LoadLevelIfSynced();
  105. }
  106.  
  107. protected void OnCreatedRoom()
  108. {
  109. PhotonNetwork.networkingPeer.SetLevelInPropsIfSynced(Application.loadedLevelName);
  110. }
  111.  
  112. public static void StartFallbackSendAckThread()
  113. {
  114. if (sendThreadShouldRun)
  115. {
  116. return;
  117. }
  118.  
  119. sendThreadShouldRun = true;
  120. SupportClass.CallInBackground(FallbackSendAckThread); // thread will call this every 100ms until method returns false
  121. }
  122.  
  123. public static void StopFallbackSendAckThread()
  124. {
  125. sendThreadShouldRun = false;
  126. }
  127.  
  128. public static bool FallbackSendAckThread()
  129. {
  130. if (sendThreadShouldRun && PhotonNetwork.networkingPeer != null)
  131. {
  132. PhotonNetwork.networkingPeer.SendAcksOnly();
  133. }
  134.  
  135. return sendThreadShouldRun;
  136. }
  137.  
  138. #region Implementation of IPhotonPeerListener
  139.  
  140. public void DebugReturn(DebugLevel level, string message)
  141. {
  142. if (level == DebugLevel.ERROR)
  143. {
  144. Debug.LogError(message);
  145. }
  146. else if (level == DebugLevel.WARNING)
  147. {
  148. Debug.LogWarning(message);
  149. }
  150. else if (level == DebugLevel.INFO && PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
  151. {
  152. Debug.Log(message);
  153. }
  154. else if (level == DebugLevel.ALL && PhotonNetwork.logLevel == PhotonLogLevel.Full)
  155. {
  156. Debug.Log(message);
  157. }
  158. }
  159.  
  160. public void OnOperationResponse(OperationResponse operationResponse)
  161. {
  162. }
  163.  
  164. public void OnStatusChanged(StatusCode statusCode)
  165. {
  166. }
  167.  
  168. public void OnEvent(EventData photonEvent)
  169. {
  170. }
  171.  
  172. #endregion
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement