Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

     public IEnumerator PingSocket(Region region)
     {
         region.Ping = Attempts*MaxMilliseconsPerPing;

         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
         PhotonPing ping;
         //Debug.Log("PhotonHandler.PingImplementation " + PhotonHandler.PingImplementation);
         if (PhotonHandler.PingImplementation == typeof(PingNativeDynamic))
         {
             Debug.Log("Using constructor for new PingNativeDynamic()"); // it seems on android, the Activator can't find the default Constructor
             ping = new PingNativeDynamic();
         }
         else
         {
             ping = (PhotonPing)Activator.CreateInstance(PhotonHandler.PingImplementation);
         }

         //Debug.Log("Ping is: " + ping + " type " + ping.GetType());

         float rttSum = 0.0f;
         int replyCount = 0;


         // PhotonPing.StartPing() requires a plain IP address without port (on all but Windows 8 platforms).
         // So: remove port and do the DNS-resolving if needed
         string cleanIpOfRegion = region.HostAndPort;
         int indexOfColon = cleanIpOfRegion.LastIndexOf(':');
         if (indexOfColon > 1)
         {
             cleanIpOfRegion = cleanIpOfRegion.Substring(0, indexOfColon);
         }
         cleanIpOfRegion = ResolveHost(cleanIpOfRegion);
         //Debug.Log("Resolved and port-less IP is: " + cleanIpOfRegion);


         for (int i = 0; i < Attempts; i++)
         {
             bool overtime = false;
             Stopwatch sw = new Stopwatch();
             sw.Start();

             try
             {
                 ping.StartPing(cleanIpOfRegion);
             }
             catch (Exception e)
             {
                 Debug.Log("catched: " + e);
                 this.PingsRunning--;
                 break;
             }


             while (!ping.Done())
             {
                 if (sw.ElapsedMilliseconds >= MaxMilliseconsPerPing)
                 {
                     overtime = true;
                     break;
                 }
                 yield return 0; // keep this loop tight, to avoid adding local lag to rtt.
             }
             int rtt = (int)sw.ElapsedMilliseconds;


             if (IgnoreInitialAttempt && i == 0)
             {
                 // do nothing.
             }
             else if (ping.Successful && !overtime)
             {
                 rttSum += rtt;
                 replyCount++;
                 region.Ping = (int)((rttSum) / replyCount);
                 //Debug.Log("region " + region.Code + " RTT " + region.Ping + " success: " + ping.Successful + " over: " + overtime);
             }

             yield return new WaitForSeconds(0.1f);
         }

         this.PingsRunning--;

         //Debug.Log("this.PingsRunning: " + this.PingsRunning + " this debug: " + ping.DebugString);
         yield return null;
     }