Rtt









How do I use Rtt
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: PhotonConverter.cs Copy
160     static void ConvertScripts(List scriptPathList)
161     {
162         bool ignoreWarningIsLogged = false;
163
164         foreach (string script in scriptPathList)
165         {
166             if (script.Contains("PhotonNetwork")) //Don't convert this file (and others)
167             {
168                 if (!ignoreWarningIsLogged)
169                 {
170                     ignoreWarningIsLogged = true;
171                     Debug.LogWarning("Conversion to PUN ignores all files with \"PhotonNetwork\" in their file-path.\nCheck: " + script);
172                 }
173                 continue;
174             }
175             if (script.Contains("Image Effects"))
176             {
177                 continue;
178             }
179
180             ConvertToPhotonAPI(script);
181         }
182
183         foreach (string script in scriptPathList)
184         {
185             AssetDatabase.ImportAsset(script, ImportAssetOptions.ForceUpdate);
186         }
187     }
File name: PhotonConverter.cs Copy
189     static void ConvertToPhotonAPI(string file)
190     {
191         string text = File.ReadAllText(file);
192
193         bool isJS = file.Contains(".js");
194
195         file = file.Replace("\\", "/"); // Get Class name for JS
196         string className = file.Substring(file.LastIndexOf("/")+1);
197         className = className.Substring(0, className.IndexOf("."));
198
199
200         //REGEXP STUFF
201         //Valid are: Space { } , /n /r
202         //string NOT_VAR = @"([^A-Za-z0-9_\[\]\.]+)";
203         string NOT_VAR_WITH_DOT = @"([^A-Za-z0-9_]+)";
204
205         //string VAR_NONARRAY = @"[^A-Za-z0-9_]";
206
207
208         //NetworkView
209         {
210             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkView" + NOT_VAR_WITH_DOT, "$1PhotonView$2");
211             text = PregReplace(text, NOT_VAR_WITH_DOT + "networkView" + NOT_VAR_WITH_DOT, "$1photonView$2");
212             text = PregReplace(text, NOT_VAR_WITH_DOT + "stateSynchronization" + NOT_VAR_WITH_DOT, "$1synchronization$2");
213             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkStateSynchronization" + NOT_VAR_WITH_DOT, "$1ViewSynchronization$2"); // map Unity enum to ours
214             //.RPC
215             text = PregReplace(text, NOT_VAR_WITH_DOT + "RPCMode.Server" + NOT_VAR_WITH_DOT, "$1PhotonTargets.MasterClient$2");
216             text = PregReplace(text, NOT_VAR_WITH_DOT + "RPCMode" + NOT_VAR_WITH_DOT, "$1PhotonTargets$2");
217         }
218
219         //NetworkMessageInfo: 100%
220         {
221             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkMessageInfo" + NOT_VAR_WITH_DOT, "$1PhotonMessageInfo$2");
222             text = PregReplace(text, NOT_VAR_WITH_DOT + "networkView" + NOT_VAR_WITH_DOT, "$1photonView$2");
223         }
224
225         //NetworkViewID:
226         {
227             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkViewID" + NOT_VAR_WITH_DOT, "$1int$2"); //We simply use an int
228         }
229
230         //NetworkPlayer
231         {
232             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkPlayer" + NOT_VAR_WITH_DOT, "$1PhotonPlayer$2");
233         }
234
235         //Network
236         {
237             //Monobehaviour callbacks
238             {
239                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnPlayerConnected" + NOT_VAR_WITH_DOT, "$1OnPhotonPlayerConnected$2");
240                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnPlayerDisconnected" + NOT_VAR_WITH_DOT, "$1OnPhotonPlayerDisconnected$2");
241                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnNetworkInstantiate" + NOT_VAR_WITH_DOT, "$1OnPhotonInstantiate$2");
242                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnSerializeNetworkView" + NOT_VAR_WITH_DOT, "$1OnPhotonSerializeView$2");
243                 text = PregReplace(text, NOT_VAR_WITH_DOT + "BitStream" + NOT_VAR_WITH_DOT, "$1PhotonStream$2");
244
245                 //Not completely the same meaning
246                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnServerInitialized" + NOT_VAR_WITH_DOT, "$1OnCreatedRoom$2");
247                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnConnectedToServer" + NOT_VAR_WITH_DOT, "$1OnJoinedRoom$2");
248
249                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnFailedToConnectToMasterServer" + NOT_VAR_WITH_DOT, "$1OnFailedToConnectToPhoton$2");
250                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnFailedToConnect" + NOT_VAR_WITH_DOT, "$1OnFailedToConnect_OBSELETE$2");
251             }
252
253             //Variables
254             {
255
256                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.connections" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.playerList$2");
257                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.isServer" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isMasterClient$2");
258                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.isClient" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isNonMasterClientInRoom$2");
259
260                 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkPeerType" + NOT_VAR_WITH_DOT, "$1ConnectionState$2");
261                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.peerType" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.connectionState$2");
262                 text = PregReplace(text, NOT_VAR_WITH_DOT + "ConnectionState.Server" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isMasterClient$2");
263                 text = PregReplace(text, NOT_VAR_WITH_DOT + "ConnectionState.Client" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isNonMasterClientInRoom$2");
264                 text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.playerList.Length" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.playerList.Count$2");
265
266                 /*DROPPED:
267                     minimumAllocatableViewIDs
268                     natFacilitatorIP is dropped
269                     natFacilitatorPort is dropped
270                     connectionTesterIP
271                     connectionTesterPort
272                     proxyIP
273                     proxyPort
274                     useProxy
275                     proxyPassword
276                  */
277             }
278
279             //Methods
280             {
281                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.InitializeServer" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.CreateRoom$2");
282                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.Connect" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.JoinRoom$2");
283                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.GetAveragePing" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.GetPing$2");
284                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.GetLastPing" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.GetPing$2");
285                 /*DROPPED:
286                     TestConnection
287                     TestConnectionNAT
288                     HavePublicAddress
289                 */
290             }
291
292             //Overall
293             text = PregReplace(text, NOT_VAR_WITH_DOT + "Network" + NOT_VAR_WITH_DOT, "$1PhotonNetwork$2");
294
295
296         //Changed methods
297              string ignoreMe = @"([A-Za-z0-9_\[\]\(\) ]+)";
298
299          text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.GetPing\\(" + ignoreMe+"\\);", "$1PhotonNetwork.GetPing();");
300         text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.CloseConnection\\(" + ignoreMe+","+ignoreMe+"\\);", "$1PhotonNetwork.CloseConnection($2);");
301
302         }
303
304         //General
305         {
306             if (text.Contains("Photon")) //Only use the PhotonMonoBehaviour if we use photonView and friends.
307             {
308                 if (isJS)//JS
309                 {
310                     if (text.Contains("extends MonoBehaviour"))
311                         text = PregReplace(text, "extends MonoBehaviour", "extends Photon.MonoBehaviour");
312                     else
313                         text = "class " + className + " extends Photon.MonoBehaviour {\n" + text + "\n}";
314                 }
315                 else //C#
316                     text = PregReplace(text, ": MonoBehaviour", ": Photon.MonoBehaviour");
317             }
318         }
319
320         File.WriteAllText(file, text);
321     }
File name: PhotonLagSimulationGui.cs Copy
57     private void NetSimWindow(int windowId)
58     {
59         GUILayout.Label(string.Format("Rtt:{0,4} +/-{1,3}", this.Peer.RoundTripTime, this.Peer.RoundTripTimeVariance));
60
61         bool simEnabled = this.Peer.IsSimulationEnabled;
62         bool newSimEnabled = GUILayout.Toggle(simEnabled, "Simulate");
63         if (newSimEnabled != simEnabled)
64         {
65             this.Peer.IsSimulationEnabled = newSimEnabled;
66         }
67
68         float inOutLag = this.Peer.NetworkSimulationSettings.IncomingLag;
69         GUILayout.Label("Lag " + inOutLag);
70         inOutLag = GUILayout.HorizontalSlider(inOutLag, 0, 500);
71
72         this.Peer.NetworkSimulationSettings.IncomingLag = (int)inOutLag;
73         this.Peer.NetworkSimulationSettings.OutgoingLag = (int)inOutLag;
74
75         float inOutJitter = this.Peer.NetworkSimulationSettings.IncomingJitter;
76         GUILayout.Label("Jit " + inOutJitter);
77         inOutJitter = GUILayout.HorizontalSlider(inOutJitter, 0, 100);
78
79         this.Peer.NetworkSimulationSettings.IncomingJitter = (int)inOutJitter;
80         this.Peer.NetworkSimulationSettings.OutgoingJitter = (int)inOutJitter;
81
82         float loss = this.Peer.NetworkSimulationSettings.IncomingLossPercentage;
83         GUILayout.Label("Loss " + loss);
84         loss = GUILayout.HorizontalSlider(loss, 0, 10);
85
86         this.Peer.NetworkSimulationSettings.IncomingLossPercentage = (int)loss;
87         this.Peer.NetworkSimulationSettings.OutgoingLossPercentage = (int)loss;
88
89         // if anything was clicked, the height of this window is likely changed. reduce it to be layouted again next frame
90         if (GUI.changed)
91         {
92             this.WindowRect.height = 100;
93         }
94
95         GUI.DragWindow();
96     }
File name: PingCloudRegions.cs Copy
88     public static int MaxMilliseconsPerPing = 800; // enter a value you're sure some server can beat (have a lower rtt)
92     {
93         get
94         {
95             Region result = null;
96             int bestRtt = Int32.MaxValue;
97             foreach (Region region in PhotonNetwork.networkingPeer.AvailableRegions)
98             {
99                 Debug.Log("BestRegion checks region: " + region);
100                 if (region.Ping != 0 && region.Ping < bestRtt)
101                 {
102                     bestRtt = region.Ping;
103                     result = region;
104                 }
105             }
106
107             return (Region)result;
108         }
109     }
File name: PingCloudRegions.cs Copy
118     public IEnumerator PingSocket(Region region)
119     {
120         region.Ping = Attempts*MaxMilliseconsPerPing;
121
122         this.PingsRunning++; // TODO: Add try-catch to make sure the PingsRunning are reduced at the end and that the lib does not crash the app
123         PhotonPing ping;
124         //Debug.Log("PhotonHandler.PingImplementation " + PhotonHandler.PingImplementation);
125         if (PhotonHandler.PingImplementation == typeof(PingNativeDynamic))
126         {
127             Debug.Log("Using constructor for new PingNativeDynamic()"); // it seems on android, the Activator can't find the default Constructor
128             ping = new PingNativeDynamic();
129         }
130         else
131         {
132             ping = (PhotonPing)Activator.CreateInstance(PhotonHandler.PingImplementation);
133         }
134
135         //Debug.Log("Ping is: " + ping + " type " + ping.GetType());
136
137         float rttSum = 0.0f;
138         int replyCount = 0;
139
140
141         // PhotonPing.StartPing() requires a plain IP address without port (on all but Windows 8 platforms).
142         // So: remove port and do the DNS-resolving if needed
143         string cleanIpOfRegion = region.HostAndPort;
144         int indexOfColon = cleanIpOfRegion.LastIndexOf(':');
145         if (indexOfColon > 1)
146         {
147             cleanIpOfRegion = cleanIpOfRegion.Substring(0, indexOfColon);
148         }
149         cleanIpOfRegion = ResolveHost(cleanIpOfRegion);
150         //Debug.Log("Resolved and port-less IP is: " + cleanIpOfRegion);
151
152
153         for (int i = 0; i < Attempts; i++)
154         {
155             bool overtime = false;
156             Stopwatch sw = new Stopwatch();
157             sw.Start();
158
159             try
160             {
161                 ping.StartPing(cleanIpOfRegion);
162             }
163             catch (Exception e)
164             {
165                 Debug.Log("catched: " + e);
166                 this.PingsRunning--;
167                 break;
168             }
169
170
171             while (!ping.Done())
172             {
173                 if (sw.ElapsedMilliseconds >= MaxMilliseconsPerPing)
174                 {
175                     overtime = true;
176                     break;
177                 }
178                 yield return 0; // keep this loop tight, to avoid adding local lag to rtt.
179             }
180             int rtt = (int)sw.ElapsedMilliseconds;
181
182
183             if (IgnoreInitialAttempt && i == 0)
184             {
185                 // do nothing.
186             }
187             else if (ping.Successful && !overtime)
188             {
189                 rttSum += rtt;
190                 replyCount++;
191                 region.Ping = (int)((rttSum) / replyCount);
192                 //Debug.Log("region " + region.Code + " RTT " + region.Ping + " success: " + ping.Successful + " over: " + overtime);
193             }
194
195             yield return new WaitForSeconds(0.1f);
196         }
197
198         this.PingsRunning--;
199
200         //Debug.Log("this.PingsRunning: " + this.PingsRunning + " this debug: " + ping.DebugString);
201         yield return null;
202     }
File name: InRoomRoundTimer.cs Copy
28     private void StartRoundNow()
29     {
30         // in some cases, when you enter a room, the server time is not available immediately.
31         // time should be 0.0f but to make sure we detect it correctly, check for a very low value.
32         if (PhotonNetwork.time < 0.0001f)
33         {
34             // we can only start the round when the time is available. let's check that in Update()
35             startRoundWhenTimeIsSynced = true;
36             return;
37         }
38         startRoundWhenTimeIsSynced = false;
39
40
41
42         ExitGames.Client.Photon.Hashtable startTimeProp = new Hashtable(); // only use ExitGames.Client.Photon.Hashtable for Photon
43         startTimeProp[StartTimeKey] = PhotonNetwork.time;
44         PhotonNetwork.room.SetCustomProperties(startTimeProp); // implement OnPhotonCustomRoomPropertiesChanged(Hashtable propertiesThatChanged) to get this change everywhere
45     }
File name: InRoomRoundTimer.cs Copy
49     public void OnJoinedRoom()
50     {
51         if (PhotonNetwork.isMasterClient)
52         {
53             this.StartRoundNow();
54         }
55         else
56         {
57             // as the creator of the room sets the start time after entering the room, we may enter a room that has no timer started yet
58             Debug.Log("StartTime already set: " + PhotonNetwork.room.customProperties.ContainsKey(StartTimeKey));
59         }
60     }
File name: InRoomRoundTimer.cs Copy
63     public void OnPhotonCustomRoomPropertiesChanged(Hashtable propertiesThatChanged)
64     {
65         if (propertiesThatChanged.ContainsKey(StartTimeKey))
66         {
67             StartTime = (double)propertiesThatChanged[StartTimeKey];
68         }
69     }
File name: InRoomRoundTimer.cs Copy
76     public void OnMasterClientSwitched(PhotonPlayer newMasterClient)
77     {
78         if (!PhotonNetwork.room.customProperties.ContainsKey(StartTimeKey))
79         {
80             Debug.Log("The new master starts a new round, cause we didn't start yet.");
81             this.StartRoundNow();
82         }
83     }
File name: InRoomRoundTimer.cs Copy
94     public void OnGUI()
95     {
96         // alternatively to doing this calculation here:
97         // calculate these values in Update() and make them publicly available to all other scripts
98         double elapsedTime = (PhotonNetwork.time - StartTime);
99         double remainingTime = SecondsPerTurn - (elapsedTime % SecondsPerTurn);
100         int turn = (int)(elapsedTime / SecondsPerTurn);
101
102
103         // simple gui for output
104         GUILayout.BeginArea(TextPos);
105         GUILayout.Label(string.Format("elapsed: {0:0.000}", elapsedTime));
106         GUILayout.Label(string.Format("remaining: {0:0.000}", remainingTime));
107         GUILayout.Label(string.Format("turn: {0:0}", turn));
108         if (GUILayout.Button("new round"))
109         {
110             this.StartRoundNow();
111         }
112         GUILayout.EndArea();
113     }

Download file with original file name:Rtt

Rtt 107 lượt xem

Gõ tìm kiếm nhanh...