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.

357 lines
12 KiB

4 years ago
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="SupportLogger.cs" company="Exit Games GmbH">
  3. // Loadbalancing Framework for Photon - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Implements callbacks of the Realtime API to logs selected information
  7. // for support cases.
  8. // </summary>
  9. // <author>developer@photonengine.com</author>
  10. // ----------------------------------------------------------------------------
  11. #if UNITY_4_7 || UNITY_5 || UNITY_5_3_OR_NEWER
  12. #define SUPPORTED_UNITY
  13. #endif
  14. namespace Photon.Realtime
  15. {
  16. using System.Text;
  17. using System.Collections;
  18. using System.Collections.Generic;
  19. using Stopwatch = System.Diagnostics.Stopwatch;
  20. #if SUPPORTED_UNITY
  21. using UnityEngine;
  22. #endif
  23. #if SUPPORTED_UNITY || NETFX_CORE
  24. using Hashtable = ExitGames.Client.Photon.Hashtable;
  25. using SupportClass = ExitGames.Client.Photon.SupportClass;
  26. #endif
  27. /// <summary>
  28. /// Helper class to debug log basic information about Photon client and vital traffic statistics.
  29. /// </summary>
  30. /// <remarks>
  31. /// Set SupportLogger.Client for this to work.
  32. /// </remarks>
  33. #if SUPPORTED_UNITY
  34. [DisallowMultipleComponent]
  35. [AddComponentMenu("")] // hide from Unity Menus and searches
  36. public class SupportLogger : MonoBehaviour, IConnectionCallbacks , IMatchmakingCallbacks , IInRoomCallbacks, ILobbyCallbacks
  37. #else
  38. public class SupportLogger : IConnectionCallbacks, IInRoomCallbacks, IMatchmakingCallbacks , ILobbyCallbacks
  39. #endif
  40. {
  41. /// <summary>
  42. /// Toggle to enable or disable traffic statistics logging.
  43. /// </summary>
  44. public bool LogTrafficStats = true;
  45. private bool loggedStillOfflineMessage;
  46. private LoadBalancingClient client;
  47. private Stopwatch startStopwatch;
  48. private int pingMax;
  49. private int pingMin;
  50. /// <summary>
  51. /// Photon client to log information and statistics from.
  52. /// </summary>
  53. public LoadBalancingClient Client
  54. {
  55. get { return this.client; }
  56. set
  57. {
  58. if (this.client != value)
  59. {
  60. if (this.client != null)
  61. {
  62. this.client.RemoveCallbackTarget(this);
  63. }
  64. this.client = value;
  65. if (this.client != null)
  66. {
  67. this.client.AddCallbackTarget(this);
  68. }
  69. }
  70. }
  71. }
  72. #if SUPPORTED_UNITY
  73. protected void Start()
  74. {
  75. if (this.startStopwatch == null)
  76. {
  77. this.startStopwatch = new Stopwatch();
  78. this.startStopwatch.Start();
  79. }
  80. }
  81. protected void OnApplicationPause(bool pause)
  82. {
  83. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnApplicationPause: " + pause + " connected: " + (this.client == null ? "false" : this.client.IsConnected.ToString()));
  84. }
  85. protected void OnApplicationQuit()
  86. {
  87. this.CancelInvoke();
  88. }
  89. #endif
  90. public void StartLogStats()
  91. {
  92. #if SUPPORTED_UNITY
  93. this.InvokeRepeating("LogStats", 10, 10);
  94. #else
  95. Debug.Log("Not implemented for non-Unity projects.");
  96. #endif
  97. }
  98. public void StopLogStats()
  99. {
  100. #if SUPPORTED_UNITY
  101. this.CancelInvoke("LogStats");
  102. #else
  103. Debug.Log("Not implemented for non-Unity projects.");
  104. #endif
  105. }
  106. private void StartTrackValues()
  107. {
  108. #if SUPPORTED_UNITY
  109. this.InvokeRepeating("TrackValues", 0.5f, 0.5f);
  110. #else
  111. Debug.Log("Not implemented for non-Unity projects.");
  112. #endif
  113. }
  114. private void StopTrackValues()
  115. {
  116. #if SUPPORTED_UNITY
  117. this.CancelInvoke("TrackValues");
  118. #else
  119. Debug.Log("Not implemented for non-Unity projects.");
  120. #endif
  121. }
  122. private string GetFormattedTimestamp()
  123. {
  124. if (this.startStopwatch == null)
  125. {
  126. this.startStopwatch = new Stopwatch();
  127. this.startStopwatch.Start();
  128. }
  129. return string.Format("[{0}.{1}]", this.startStopwatch.Elapsed.Seconds, this.startStopwatch.Elapsed.Milliseconds);
  130. }
  131. // called via InvokeRepeatedly
  132. private void TrackValues()
  133. {
  134. if (this.client != null)
  135. {
  136. int currentRtt = this.client.LoadBalancingPeer.RoundTripTime;
  137. if (currentRtt > this.pingMax)
  138. {
  139. this.pingMax = currentRtt;
  140. }
  141. if (currentRtt < this.pingMin)
  142. {
  143. this.pingMin = currentRtt;
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// Debug logs vital traffic statistics about the attached Photon Client.
  149. /// </summary>
  150. public void LogStats()
  151. {
  152. if (this.client == null || this.client.State == ClientState.PeerCreated)
  153. {
  154. return;
  155. }
  156. if (this.LogTrafficStats)
  157. {
  158. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger " + this.client.LoadBalancingPeer.VitalStatsToString(false) + " Ping min/max: " + this.pingMin + "/" + this.pingMax);
  159. }
  160. }
  161. /// <summary>
  162. /// Debug logs basic information (AppId, AppVersion, PeerID, Server address, Region) about the attached Photon Client.
  163. /// </summary>
  164. private void LogBasics()
  165. {
  166. if (this.client != null)
  167. {
  168. StringBuilder sb = new StringBuilder();
  169. sb.AppendFormat("{0} SupportLogger Info: ", this.GetFormattedTimestamp());
  170. sb.AppendFormat("AppID: \"{0}\" AppVersion: \"{1}\" UserId: {3} PeerID: {2} ",
  171. string.IsNullOrEmpty(this.client.AppId) || this.client.AppId.Length < 8
  172. ? this.client.AppId
  173. : string.Concat(this.client.AppId.Substring(0, 8), "***"), this.client.AppVersion, this.client.LoadBalancingPeer.PeerID,
  174. this.client.UserId);
  175. //NOTE: this.client.LoadBalancingPeer.ServerIpAddress requires Photon3Unity3d.dll v4.1.2.5 and up
  176. sb.AppendFormat("NameServer: {0} Server: {1} IP: {2} Region: {3}", this.client.NameServerHost, this.client.CurrentServerAddress, this.client.LoadBalancingPeer.ServerIpAddress, this.client.CloudRegion);
  177. Debug.Log(sb.ToString());
  178. }
  179. }
  180. public void OnConnected()
  181. {
  182. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnConnected().");
  183. this.pingMax = 0;
  184. this.pingMin = this.client.LoadBalancingPeer.RoundTripTime;
  185. this.LogBasics();
  186. if (this.LogTrafficStats)
  187. {
  188. this.client.LoadBalancingPeer.TrafficStatsEnabled = false;
  189. this.client.LoadBalancingPeer.TrafficStatsEnabled = true;
  190. this.StartLogStats();
  191. }
  192. this.StartTrackValues();
  193. }
  194. public void OnConnectedToMaster()
  195. {
  196. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnConnectedToMaster().");
  197. }
  198. public void OnFriendListUpdate(List<FriendInfo> friendList)
  199. {
  200. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnFriendListUpdate(friendList).");
  201. }
  202. public void OnJoinedLobby()
  203. {
  204. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnJoinedLobby(" + this.client.CurrentLobby + ").");
  205. }
  206. public void OnLeftLobby()
  207. {
  208. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnLeftLobby().");
  209. }
  210. public void OnCreateRoomFailed(short returnCode, string message)
  211. {
  212. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnCreateRoomFailed(" + returnCode+","+message+").");
  213. }
  214. public void OnJoinedRoom()
  215. {
  216. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnJoinedRoom(" + this.client.CurrentRoom + "). " + this.client.CurrentLobby + " GameServer:" + this.client.GameServerAddress);
  217. }
  218. public void OnJoinRoomFailed(short returnCode, string message)
  219. {
  220. }
  221. public void OnJoinRandomFailed(short returnCode, string message)
  222. {
  223. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnJoinRandomFailed(" + returnCode+","+message+").");
  224. }
  225. public void OnCreatedRoom()
  226. {
  227. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnCreatedRoom(" + this.client.CurrentRoom + "). " + this.client.CurrentLobby + " GameServer:" + this.client.GameServerAddress);
  228. }
  229. public void OnLeftRoom()
  230. {
  231. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnLeftRoom().");
  232. }
  233. public void OnDisconnected(DisconnectCause cause)
  234. {
  235. this.StopLogStats();
  236. this.StopTrackValues();
  237. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnDisconnected(" + cause + ").");
  238. this.LogBasics();
  239. this.LogStats();
  240. }
  241. public void OnRegionListReceived(RegionHandler regionHandler)
  242. {
  243. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnRegionListReceived(regionHandler).");
  244. this.LogBasics();
  245. }
  246. public void OnRoomListUpdate(List<RoomInfo> roomList)
  247. {
  248. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnRoomListUpdate(roomList). roomList.Count: " + roomList.Count);
  249. }
  250. public void OnPlayerEnteredRoom(Player newPlayer)
  251. {
  252. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnPlayerEnteredRoom(" + newPlayer+").");
  253. }
  254. public void OnPlayerLeftRoom(Player otherPlayer)
  255. {
  256. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnPlayerLeftRoom(" + otherPlayer+").");
  257. }
  258. public void OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)
  259. {
  260. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnRoomPropertiesUpdate(propertiesThatChanged).");
  261. }
  262. public void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
  263. {
  264. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnPlayerPropertiesUpdate(targetPlayer,changedProps).");
  265. }
  266. public void OnMasterClientSwitched(Player newMasterClient)
  267. {
  268. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnMasterClientSwitched(" + newMasterClient+").");
  269. }
  270. public void OnCustomAuthenticationResponse(Dictionary<string, object> data)
  271. {
  272. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnCustomAuthenticationResponse(" + data.ToStringFull()+").");
  273. }
  274. public void OnCustomAuthenticationFailed (string debugMessage)
  275. {
  276. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnCustomAuthenticationFailed(" + debugMessage+").");
  277. }
  278. public void OnLobbyStatisticsUpdate(List<TypedLobbyInfo> lobbyStatistics)
  279. {
  280. Debug.Log(this.GetFormattedTimestamp() + " SupportLogger OnLobbyStatisticsUpdate(lobbyStatistics).");
  281. }
  282. #if !SUPPORTED_UNITY
  283. private static class Debug
  284. {
  285. public static void Log(string msg)
  286. {
  287. System.Diagnostics.Debug.WriteLine(msg);
  288. }
  289. public static void LogWarning(string msg)
  290. {
  291. System.Diagnostics.Debug.WriteLine(msg);
  292. }
  293. public static void LogError(string msg)
  294. {
  295. System.Diagnostics.Debug.WriteLine(msg);
  296. }
  297. }
  298. #endif
  299. }
  300. }