Example #1
0
        /// <summary>
        /// Create a new connection object
        /// </summary>
        /// <param name="connectionInfo">ConnecitonInfo 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)
        {
            SendTimesMSPerKBCache = new CommsMath();
            dataBuffer            = new byte[NetworkComms.ReceiveBufferSizeBytes];
            packetBuilder         = new PacketBuilder();

            //Intialise 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;
            }

            if (NetworkComms.commsShutdown)
            {
                throw new ConnectionSetupException("Attempting to create new connection after global comms 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.ConnectionType))
            {
                throw new ConnectionSetupException("A connection already exists with " + ConnectionInfo);
            }

            //We add a reference in the constructor to ensure any duplicate connection problems are picked up here
            NetworkComms.AddConnectionByReferenceEndPoint(this);
        }
Example #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));
            }
        }
Example #3
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));
 }