A class used for math operations in NetworkComms.Net. Primarily used for load analysis.
Exemplo n.º 1
0
            /// <summary>
            /// Returns the averaged value of CurrentNetworkLoadIncoming, as a value between 0 and 1, for a time window of up to 254 seconds.
            /// Triggers load analysis upon first call.
            /// </summary>
            /// <param name="secondsToAverage">Number of seconds over which historical data should be used to arrive at an average</param>
            /// <returns>Average network load as a double between 0 and 1</returns>
            public static double AverageNetworkLoadOutgoing(byte secondsToAverage)
            {
#if !WINDOWS_PHONE && !ANDROID && !NETFX_CORE
                if (!NetworkComms.commsShutdown && NetworkLoadThread == null)
                {
                    lock (NetworkComms.globalDictAndDelegateLocker)
                    {
                        if (!NetworkComms.commsShutdown && NetworkLoadThread == null)
                        {
                            currentNetworkLoadValuesIncoming = new CommsMath();
                            currentNetworkLoadValuesOutgoing = new CommsMath();

                            NetworkLoadThread              = new Thread(NetworkLoadWorker);
                            NetworkLoadThread.Name         = "NetworkLoadThread";
                            NetworkLoadThread.IsBackground = true;
                            NetworkLoadThread.Start();
                        }
                    }
                }

                return(currentNetworkLoadValuesOutgoing.CalculateMean((int)((secondsToAverage * 1000.0) / NetworkLoadUpdateWindowMS)));
#else
                throw new NotSupportedException("This feature is not supported on the current platform.");
#endif
            }
Exemplo n.º 2
0
        /// <summary>
        /// Return the standard deviation of the current list.
        /// </summary>
        /// <param name="lastNValues">If less than the number of items in the value list returns the mean of the lastNValues</param>
        /// <returns>The mean of relevant values</returns>
        public double CalculateStdDeviation(int lastNValues)
        {
            lock (locker)
            {
                int itemsToSkip = 0;

                if (lastNValues < values.Count)
                {
                    itemsToSkip = values.Count - lastNValues;
                }

                List <double> itemsForCalc = new List <double>(lastNValues);
                List <double> itemWeights  = new List <double>(lastNValues);

                for (int i = itemsToSkip; i < values.Count; ++i)
                {
                    itemsForCalc.Add(values[i]);
                    itemWeights.Add(weights[i]);
                }

                return(CommsMath.CalculateStdDeviation(itemsForCalc, itemWeights));
            }
        }
        /// <summary>
        /// Create a new connection object
        /// </summary>
        /// <param name="connectionInfo">ConnectionInfo corresponding to the new connection</param>
        /// <param name="defaultSendReceiveOptions">The SendReceiveOptions which should be used as connection defaults</param>
        protected Connection(ConnectionInfo connectionInfo, SendReceiveOptions defaultSendReceiveOptions)
        {
            //If the application layer protocol is disabled the serialiser must be NullSerializer
            //and no data processors are allowed.
            if (connectionInfo.ApplicationLayerProtocol == ApplicationLayerProtocolStatus.Disabled)
            {
                if (defaultSendReceiveOptions.Options.ContainsKey("ReceiveConfirmationRequired"))
                    throw new ArgumentException("Attempted to create an unmanaged connection when the provided send receive" +
                        " options specified the ReceiveConfirmationRequired option. Please provide compatible send receive options in order to successfully" +
                        " instantiate this unmanaged connection.", "defaultSendReceiveOptions");

                if (defaultSendReceiveOptions.DataSerializer != DPSManager.GetDataSerializer<NullSerializer>())
                    throw new ArgumentException("Attempted to create an unmanaged connection when the provided send receive" +
                        " options serialiser was not NullSerializer. Please provide compatible send receive options in order to successfully" +
                        " instantiate this unmanaged connection.", "defaultSendReceiveOptions");

                if (defaultSendReceiveOptions.DataProcessors.Count > 0)
                    throw new ArgumentException("Attempted to create an unmanaged connection when the provided send receive" +
                        " options contains data processors. Data processors may not be used with unmanaged connections." +
                        " Please provide compatible send receive options in order to successfully instantiate this unmanaged connection.", "defaultSendReceiveOptions");
            }

            SendTimesMSPerKBCache = new CommsMath();            
            packetBuilder = new PacketBuilder();

            //Initialise the sequence counter using the global value
            //Subsequent values on this connection are guaranteed to be sequential
            packetSequenceCounter = Interlocked.Increment(ref NetworkComms.totalPacketSendCount);

            ConnectionInfo = connectionInfo;

            if (defaultSendReceiveOptions != null)
                ConnectionDefaultSendReceiveOptions = defaultSendReceiveOptions;
            else
                ConnectionDefaultSendReceiveOptions = NetworkComms.DefaultSendReceiveOptions;

            //Add any listener specific packet handlers if required
            if (connectionInfo.ConnectionListener != null)
                connectionInfo.ConnectionListener.AddListenerPacketHandlersToConnection(this);

            if (NetworkComms.commsShutdown) throw new ConnectionSetupException("Attempting to create new connection after global NetworkComms.Net shutdown has been initiated.");

            if (ConnectionInfo.ConnectionType == ConnectionType.Undefined || ConnectionInfo.RemoteEndPoint == null)
                throw new ConnectionSetupException("ConnectionType and RemoteEndPoint must be defined within provided ConnectionInfo.");

            //If a connection already exists with this info then we can throw an exception here to prevent duplicates
            if (NetworkComms.ConnectionExists(connectionInfo.RemoteEndPoint, connectionInfo.LocalEndPoint, connectionInfo.ConnectionType, connectionInfo.ApplicationLayerProtocol))
                throw new ConnectionSetupException("A " + connectionInfo.ConnectionType.ToString() + " connection already exists with info " + connectionInfo);

            //We add a reference in the constructor to ensure any duplicate connection problems are picked up here
            NetworkComms.AddConnectionReferenceByRemoteEndPoint(this);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Return the standard deviation of the current list.
 /// </summary>
 /// <returns>The standard deviation of all values currently in the list.</returns>
 public double CalculateStdDeviation()
 {
     lock (locker)
         return(CommsMath.CalculateStdDeviation(this.values, this.weights));
 }
Exemplo n.º 5
0
            /// <summary>
            /// Returns the averaged value of CurrentNetworkLoadIncoming, as a value between 0 and 1, for a time window of up to 254 seconds.
            /// Triggers load analysis upon first call.
            /// </summary>
            /// <param name="secondsToAverage">Number of seconds over which historical data should be used to arrive at an average</param>
            /// <returns>Average network load as a double between 0 and 1</returns>
            public static double AverageNetworkLoadOutgoing(byte secondsToAverage)
            {
#if !WINDOWS_PHONE && !ANDROID && !NETFX_CORE
                if (!NetworkComms.commsShutdown && NetworkLoadThread == null)
                {
                    lock (NetworkComms.globalDictAndDelegateLocker)
                    {
                        if (!NetworkComms.commsShutdown && NetworkLoadThread == null)
                        {
                            currentNetworkLoadValuesIncoming = new CommsMath();
                            currentNetworkLoadValuesOutgoing = new CommsMath();

                            NetworkLoadThread = new Thread(NetworkLoadWorker);
                            NetworkLoadThread.Name = "NetworkLoadThread";
                            NetworkLoadThread.IsBackground = true;
                            NetworkLoadThread.Start();
                        }
                    }
                }

                return currentNetworkLoadValuesOutgoing.CalculateMean((int)((secondsToAverage * 1000.0) / NetworkLoadUpdateWindowMS));
#else
            throw new NotSupportedException("This feature is not supported on the current platform.");
#endif
            }