Exemplo n.º 1
1
        public NetInterface(NetworkInterface adapterIn)
        {
            // set up the adapter
            adapter = adapterIn;
            stats = adapter.GetIPv4Statistics();

            // set up the logging
            logPath = Path.Combine("logs", Path.Combine(adapter.Description, adapter.GetPhysicalAddress().ToString(), adapter.Id));
            logHandler = new LogHandler(logPath);
            loadDataInstant(DateTime.UtcNow.Ticks);

            // set up the data tracking
            dataTransferStart = currentTicks();
            bytesInSession = stats.BytesReceived;
            bytesOutSession = stats.BytesSent;
            properties = adapter.GetIPProperties();
            //Console.WriteLine(adapter.Name + " " + adapter.Description + " " + adapter.OperationalStatus);

            Tracker = new Tracker(logHandler);
        }
Exemplo n.º 2
0
        private long bytesOut()
        {
            stats = adapter.GetIPv4Statistics();
            long bytesOut = stats.BytesSent;
            long bytesRecent = bytesOut - bytesOutSession;
            bytesOutSession = bytesOut;

            if (bytesRecent < 0 || bytesRecent > Properties.Settings.Default.OutSpikeLimit)
            {
                bytesRecent = 0;
            }

            return bytesRecent;
        }
Exemplo n.º 3
0
        private long bytesIn()
        {
            stats = adapter.GetIPv4Statistics();
            long bytesIn = stats.BytesReceived;
            long bytesRecent = bytesIn - bytesInSession;
            bytesInSession = bytesIn;

            if (bytesRecent < 0 || bytesRecent > Properties.Settings.Default.InSpikeLimit)
            {
                bytesRecent = 0;
            }

            return bytesRecent;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Obtain new sample from interface stats, and refresh the values saved in dlSpeed, ulSpeed, etc.
        /// This method is supposed to be called only in NetworkMonitor, one time every second.
        /// </summary>
        internal void refresh(double iIntervall)
        {
            try
            {
                interfaceStats = nic.GetIPv4Statistics();
                this.dlValue = (long)(interfaceStats.BytesReceived / iIntervall);
                this.ulValue = (long)(interfaceStats.BytesSent / iIntervall);

                // Calculates download and upload speed.
                this.dlSpeed	=	this.dlValue - this.dlValueOld;
                this.ulSpeed	=	this.ulValue - this.ulValueOld;

                this.dlValueOld	=	this.dlValue;
                this.ulValueOld	=	this.ulValue;

            }
            catch (Exception)
            {
                //throw;
                this.dlSpeed	=	-1;
                this.ulSpeed	=	-1;

                this.dlValueOld	=	-1;
                this.ulValueOld	=	-1;
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Preparations for monitoring.
 /// </summary>
 internal void init()
 {
     interfaceStats = nic.GetIPv4Statistics();
     props = nic.GetIPProperties();
     gateway = props.GatewayAddresses.Count > 0 ? props.GatewayAddresses[props.GatewayAddresses.Count - 1].Address.ToString() : " N/A ";
     uniCast = props.UnicastAddresses;
     uniIP = uniCast.Count > 0 ? uniCast[uniCast.Count - 1].Address.ToString() : " N/A "; //the last address in array should be IPv4...but not always...
     uniIPMask = uniCast.Count > 0 ? uniCast[uniCast.Count - 1].IPv4Mask.ToString() : " N/A ";
     // Since dlValueOld and ulValueOld are used in method refresh() to calculate network speed, they must have be initialized.
     this.dlValueOld = interfaceStats.BytesReceived;
     this.ulValueOld = interfaceStats.BytesSent;
 }
Exemplo n.º 6
0
            /// <summary>
            /// Takes a network load snapshot (CurrentNetworkLoad) every NetworkLoadUpdateWindowMS
            /// </summary>
            private static void NetworkLoadWorker()
            {
                NetworkLoadThreadWait = new ManualResetEvent(false);

                //Get all interfaces
                NetworkInterface[] interfacesToUse = NetworkInterface.GetAllNetworkInterfaces();

                long[] startSent, startReceived, endSent, endReceived;

                while (!NetworkComms.commsShutdown)
                {
                    try
                    {
                        //we need to look at the load across all adaptors, by default we will probably choose the adaptor with the highest usage
                        DateTime startTime = DateTime.Now;

                        IPv4InterfaceStatistics[] stats = new IPv4InterfaceStatistics[interfacesToUse.Length];
                        startSent = new long[interfacesToUse.Length];
                        startReceived = new long[interfacesToUse.Length];

                        for (int i = 0; i < interfacesToUse.Length; ++i)
                        {
                            stats[i] = interfacesToUse[i].GetIPv4Statistics();
                            startSent[i] = stats[i].BytesSent;
                            startReceived[i] = stats[i].BytesReceived;
                        }

                        if (NetworkComms.commsShutdown) return;

                        //Thread.Sleep(NetworkLoadUpdateWindowMS);
#if NET2
                        NetworkLoadThreadWait.WaitOne(NetworkLoadUpdateWindowMS, false);
#else
                        NetworkLoadThreadWait.WaitOne(NetworkLoadUpdateWindowMS);
#endif

                        if (NetworkComms.commsShutdown) return;

                        stats = new IPv4InterfaceStatistics[interfacesToUse.Length];
                        endSent = new long[interfacesToUse.Length];
                        endReceived = new long[interfacesToUse.Length];

                        for (int i = 0; i < interfacesToUse.Length; ++i)
                        {
                            stats[i] = interfacesToUse[i].GetIPv4Statistics();
                            endSent[i] = stats[i].BytesSent;
                            endReceived[i] = stats[i].BytesReceived;
                        }

                        DateTime endTime = DateTime.Now;

                        List<double> outUsage = new List<double>();
                        List<double> inUsage = new List<double>();
                        for (int i = 0; i < startSent.Length; i++)
                        {
                            outUsage.Add((double)(endSent[i] - startSent[i]) / ((double)(InterfaceLinkSpeed * (endTime - startTime).TotalMilliseconds) / 8000));
                            inUsage.Add((double)(endReceived[i] - startReceived[i]) / ((double)(InterfaceLinkSpeed * (endTime - startTime).TotalMilliseconds) / 8000));
                        }

                        //double loadValue = Math.Max(outUsage.Max(), inUsage.Max());
                        double inMax = double.MinValue, outMax = double.MinValue;
                        for (int i = 0; i < startSent.Length; ++i)
                        {
                            if (inUsage[i] > inMax) inMax = inUsage[i];
                            if (outUsage[i] > outMax) outMax = outUsage[i];
                        }

                        //If either of the usage levels have gone above 2 it suggests we are most likely on a faster connection that we think
                        //As such we will bump the interface link speed up to 1Gbps so that future load calculations more accurately reflect the 
                        //actual load.
                        if (inMax > 2 || outMax > 2) InterfaceLinkSpeed = 950000000;

                        //Limit to one
                        CurrentNetworkLoadIncoming = (inMax > 1 ? 1 : inMax);
                        CurrentNetworkLoadOutgoing = (outMax > 1 ? 1 : outMax);

                        currentNetworkLoadValuesIncoming.AddValue(CurrentNetworkLoadIncoming);
                        currentNetworkLoadValuesOutgoing.AddValue(CurrentNetworkLoadOutgoing);

                        //We can only have up to 255 seconds worth of data in the average list
                        int maxListSize = (int)(255000.0 / NetworkLoadUpdateWindowMS);
                        currentNetworkLoadValuesIncoming.TrimList(maxListSize);
                        currentNetworkLoadValuesOutgoing.TrimList(maxListSize);
                    }
                    catch (Exception ex)
                    {
                        //There is no need to log if the exception is due to a change in interfaces
                        if (ex.GetType() != typeof(NetworkInformationException))
                            LogTools.LogException(ex, "NetworkLoadWorker");

                        //It may be the interfaces available to the OS have changed so we will reset them here
                        interfacesToUse = NetworkInterface.GetAllNetworkInterfaces();
                        //If an error has happened we don't want to thrash the problem, we wait for 5 seconds and hope whatever was wrong goes away
                        Thread.Sleep(5000);
                    }
                }
            }