Пример #1
0
 public static IEnumerable <System.Net.NetworkInformation.NetworkInterface> GetNetworkInterfaces(this ISocketReference reference)
 {
     foreach (System.Net.Sockets.Socket s in reference.GetReferencedSockets())
     {
         yield return(NetworkInterfaceExtensions.GetNetworkInterface(s));
     }
 }
        public void TestInterframeGap()
        {
            long speedInBitsPerSecond;

            double gapNanos;

            double gapMicros;

            System.TimeSpan microTime;

            //Todo, method with various types of interfaces
            foreach (var nif in NetworkInterfaceExtensions.GetNetworkInterface(System.Net.NetworkInformation.NetworkInterfaceType.Ethernet, System.Net.NetworkInformation.NetworkInterfaceType.Wireless80211))
            {
                //Speed changes for Wifi between calls but speed always shows 100000 would need interop to ask stack what the current speed or max speed is...
                speedInBitsPerSecond = nif.Speed;

                //Calulcate the gap in Nanoseconds
                gapNanos = NetworkInterfaceExtensions.GetInterframeGapNanoseconds(nif);

                //Caulcate the same gap but in Microseconds
                gapMicros = NetworkInterfaceExtensions.GetInterframeGapMicroseconds(nif);

                //Calulcate a TimeSpan which represents the total Microseconds.
                microTime = TimeSpanExtensions.FromMicroseconds((double)gapMicros);

                //Rounding ...
                //if (TimeSpanExtensions.TotalMicroseconds(microTime) != gapMicros) throw new System.Exception("TotalMicroseconds");

                //Verify that the conversion was correct
                if ((int)TimeSpanExtensions.TotalMicroseconds(microTime) != (int)gapMicros)
                {
                    throw new System.Exception("TotalMicroseconds");
                }

                //Calculate how much difference there is when converting from the microsecond term to the nano second term.
                double diff = gapMicros * TimeSpanExtensions.NanosecondsPerMicrosecond - TimeSpanExtensions.TotalNanoseconds(microTime);

                //If there was any difference
                if (diff > 0)
                {
                    //if that difference is greater than 1 nano second throw an exception
                    if (diff > NetworkInterfaceExtensions.MinimumInterframeGapBits)
                    {
                        throw new System.Exception("TotalNanoseconds");
                    }
                    else
                    {
                        System.Console.WriteLine("μs to ns conversion Different By: " + diff);
                    }
                }

                //Write the information
                System.Console.WriteLine(string.Format("Name: {0}, Type: {1}, \r\nSpeed Bits Per Second: {2}, \r\nGapMicros:{3} GapNanos{4}, MicroTime:{5}", nif.Name, nif.NetworkInterfaceType, speedInBitsPerSecond, gapMicros, gapNanos, microTime));

                System.Console.WriteLine("Speed In MBytes Per Second: " + NetworkInterfaceExtensions.GetSpeedInMBytesPerSecond(nif));

                //Verify results
                switch (speedInBitsPerSecond)
                {
                //1Gpbs
                case 10000000000:
                {
                    if (gapNanos != 9.6)
                    {
                        throw new System.Exception("Invalid InterframeGap");
                    }
                    break;
                }

                //1Gpbs
                case 1000000000:
                {
                    if (gapNanos != 96)
                    {
                        throw new System.Exception("Invalid InterframeGap");
                    }
                    break;
                }

                //100 Mbps
                case 100000000:
                {
                    if (gapNanos != 960)
                    {
                        throw new System.Exception("Invalid InterframeGap");
                    }
                    break;
                }

                //10 Mbps
                case 10000000:
                {
                    if (gapNanos != 9600)
                    {
                        throw new System.Exception("Invalid InterframeGap");
                    }
                    break;
                }

                //1 Mbps
                case 1000000:
                {
                    if (gapNanos != 96000)
                    {
                        throw new System.Exception("Invalid InterframeGap");
                    }
                    break;
                }
                }
            }

            ////Todo, fix overflow for all speeds.
            //for (int i = 1; i <= 1000000000; ++i)
            //{
            //    speedInBitsPerSecond = i * 100;

            //    gapNanos = NetworkInterfaceExtensions.CaulculateInterframeGapNanoseconds(speedInBitsPerSecond);

            //    gapMicros = (long)(gapNanos * TimeSpanExtensions.NanosecondsPerMicrosecond);

            //    microTime = TimeSpanExtensions.FromMicroseconds((double)gapMicros);

            //    if ((int)TimeSpanExtensions.TotalMicroseconds(microTime) != (int)gapMicros) throw new System.Exception("TotalMicroseconds");

            //    //Calculate how much difference there is when converting from the microsecond term to the nano second term.
            //    double diff = gapMicros * TimeSpanExtensions.NanosecondsPerMicrosecond - TimeSpanExtensions.TotalNanoseconds(microTime);

            //    if (diff > 0)
            //    {
            //        //if that difference is greater than 1 nano second throw an exception
            //        if (diff > TimeSpanExtensions.NanosecondsPerTick) throw new System.Exception("TotalNanoseconds");
            //        else System.Console.WriteLine("μs to ns conversion Different By: " + diff);
            //    }

            //    System.Console.WriteLine(string.Format("Name{0}, Type: {1}, Speed: {2}, GapMicros:{3} GapNanos{4}, MicroTime:{5}", "N/A", "N/A", speedInBitsPerSecond, gapMicros, gapNanos, microTime));

            //    System.Console.WriteLine("Speed In MBytes Per Second: " + speedInBitsPerSecond / TimeSpanExtensions.NanosecondsPerMillisecond);
            //}
        }