public SubtMeasurement Measure(SubtLocalPeer subtLocalPeer) // manager thread // used by easyGui
        {
            // measure
            float    rxBandwidth          = 0;
            float    confirmedTxBandwidth = 0;
            TimeSpan?bestRttToPeers       = null;
            float?   averageTxLossG       = null;
            float?   averageRxLossG       = null;

            foreach (var cp in subtLocalPeer.ConnectedPeers)
            {
                AverageSingle averageTxLoss = new AverageSingle();
                AverageSingle averageRxLoss = new AverageSingle();
                foreach (var s in cp.Streams)
                {
                    rxBandwidth += s.RecentRxBandwidth;
                    var st = s.LatestRemoteStatus;
                    if (st != null)
                    {
                        confirmedTxBandwidth += st.RecentRxBandwidth;
                        if (bestRttToPeers == null || s.RecentRtt < bestRttToPeers.Value)
                        {
                            bestRttToPeers = s.RecentRtt;
                        }
                        averageTxLoss.Input(st.RecentRxPacketLoss);
                    }
                    averageRxLoss.Input(s.RecentPacketLoss);
                }
                var averageRxLossAverage = averageRxLoss.Average;
                if (averageRxLossAverage.HasValue)
                {
                    if (averageRxLossG == null || averageRxLossAverage.Value < averageRxLossG.Value)
                    {
                        averageRxLossG = averageRxLossAverage;
                    }
                }
                var averageTxLossAverage = averageTxLoss.Average;
                if (averageTxLossAverage.HasValue)
                {
                    if (averageTxLossG == null || averageTxLossAverage.Value < averageTxLossG.Value)
                    {
                        averageTxLossG = averageTxLossAverage;
                    }
                }
            }


            return(new SubtMeasurement
            {
                MeasurementPeriodEndUtc = subtLocalPeer.LocalPeer.DateTimeNowUtc,
                RxBandwidth = rxBandwidth,
                TxBandwidth = confirmedTxBandwidth,
                BestRttToPeers = bestRttToPeers,
                TxPacketLoss = averageTxLossG,
                RxPacketLoss = averageRxLossG
            });
        }
Example #2
0
        public SubtMeasurement Measure(SubtLocalPeer subtLocalPeer) // manager thread // used by easyGui
        {
            // measure
            float    rxBandwidth          = 0;
            float    confirmedTxBandwidth = 0;
            TimeSpan?bestRttToPeers       = null;
            float?   averageTxLossG       = null;
            float?   averageRxLossG       = null;

            foreach (var connectedPeer in subtLocalPeer.ConnectedPeers)
            {
                connectedPeer.GetStreamsAveragePacketLoss(out var averageRxLossAverage, out var averageTxLossAverage);

                foreach (var s in connectedPeer.Streams)
                {
                    rxBandwidth += s.RecentRxBandwidth;
                    var rtt = s.RecentRttConsideringP2ptp;
                    if (bestRttToPeers == null || rtt < bestRttToPeers.Value)
                    {
                        bestRttToPeers = rtt;
                    }
                    var st = s.LatestRemoteStatus;
                    if (st != null)
                    {
                        confirmedTxBandwidth += st.RecentRxBandwidth;
                    }
                }
                if (averageRxLossAverage.HasValue)
                {
                    if (averageRxLossG == null || averageRxLossAverage.Value < averageRxLossG.Value)
                    {
                        averageRxLossG = averageRxLossAverage;
                    }
                }
                if (averageTxLossAverage.HasValue)
                {
                    if (averageTxLossG == null || averageTxLossAverage.Value < averageTxLossG.Value)
                    {
                        averageTxLossG = averageTxLossAverage;
                    }
                }
            }

            var r = new SubtMeasurement
            {
                MeasurementTime = subtLocalPeer.LocalPeer.DateTimeNow,
                TargetBandwidth = subtLocalPeer.Configuration.BandwidthTarget,
                RxBandwidth     = rxBandwidth,
                TxBandwidth     = confirmedTxBandwidth,
                BestRttToPeers  = bestRttToPeers,
                TxPacketLoss    = averageTxLossG,
                RxPacketLoss    = averageRxLossG
            };

            return(r);
        }
Example #3
0
 public SubtSenderThread(SubtLocalPeer localPeer, string threadName)
 {
     _threadName   = threadName;
     _localPeer    = localPeer;
     _actionsQueue = new ActionsQueue(exc => _localPeer.HandleException(exc), null);
     _thread       = new Thread(ThreadEntry);
     _thread.Name  = threadName;
     _thread.Start();
     _thread.Priority = ThreadPriority.Highest;
 }
Example #4
0
 public SubtLocalPeer(SubtLocalPeerConfiguration configuration, SubtLocalPeer instanceFromPreviousTestAfterPause = null)
 {
     if (configuration.SenderThreadsCount <= 0 || configuration.SenderThreadsCount > 32)
     {
         throw new ArgumentException(nameof(configuration.SenderThreadsCount));
     }
     Configuration = configuration;
     if (instanceFromPreviousTestAfterPause != null)
     {
         MeasurementsHistory.CopyFrom(instanceFromPreviousTestAfterPause.MeasurementsHistory);
     }
 }
Example #5
0
        public SubtConnectedPeerStream(IConnectedPeerStream stream, SubtLocalPeer subtLocalPeer, SubtConnectedPeer subtConnectedPeer)
        {
            SubtConnectedPeer = subtConnectedPeer;
            SubtLocalPeer     = subtLocalPeer;
            _stream           = stream;
            InitializePayloadPacket();

            _txSequence    = (ushort)subtLocalPeer.LocalPeer.Random.Next(ushort.MaxValue);
            _rxMeasurement = new RxMeasurement(subtLocalPeer, this);

            _senderThread = subtLocalPeer.SenderThreadForNewStream;
            _senderThread.OnCreatedDestroyedStream(this, true);
        }
Example #6
0
        public SubtConnectedPeerStream(IConnectedPeerStream stream, SubtLocalPeer subtLocalPeer, SubtConnectedPeer subtConnectedPeer)
        {
            SubtConnectedPeer = subtConnectedPeer;
            SubtLocalPeer     = subtLocalPeer;
            _stream           = stream;
            InitializePayloadPacket();

            _txSequence    = (ushort)subtLocalPeer.LocalPeer.Random.Next(ushort.MaxValue);
            _rxMeasurement = new RxMeasurement(subtLocalPeer, this);

            if (subtLocalPeer.LocalPeer.Configuration.RoleAsUser)
            {
                TargetTxBandwidth = SubtLogicConfiguration.BandwidthForStreams_UserInitial;
            }

            _senderThread = subtLocalPeer.SenderThreadForNewStream;
            _senderThread.OnCreatedDestroyedStream(this, true);
        }
Example #7
0
        internal SubtMeasurement MeasureIfNeeded(SubtLocalPeer subtLocalPeer) // manager thread
        {
            try
            {
                var now = subtLocalPeer.LocalPeer.DateTimeNowUtc;
                if (_initializedTime == null)
                {
                    return(null);
                }
                if (now < _initializedTime.Value.AddTicks(SubtLogicConfiguration.MeasurementInitializationTimeTicks))
                {
                    return(null);
                }

                if (_lastTimeMeasured == null || _lastTimeMeasured.Value.AddTicks(SubtLogicConfiguration.MeasurementsIntervalTicks) < now)
                {
                    _lastTimeMeasured = now;

                    // measure
                    var m = Measure(subtLocalPeer);

                    lock (_measurementsInRam)
                    {
                        _measurementsInRam.AddFirst(m);
                        if (_measurementsInRam.Count > SubtLogicConfiguration.MaxMeasurementsCountInRAM)
                        {
                            _measurementsInRam.RemoveLast();
                        }
                    }

                    OnMeasured?.Invoke(m);
                    return(m);
                }
            }
            catch (Exception exc)
            {
                subtLocalPeer.HandleException(exc);
            }
            return(null);
        }
Example #8
0
 internal SubtConnectedPeer(SubtLocalPeer subtLocalPeer, IConnectedPeer connectedPeer)
 {
     _connectedPeer = connectedPeer;
     _subtLocalPeer = subtLocalPeer;
 }
Example #9
0
 internal void OnReinitialized(SubtLocalPeer subtLocalPeer)
 {
     _initializedTime = subtLocalPeer.LocalPeer.DateTimeNowUtc;
 }
Example #10
0
 internal RxMeasurement(SubtLocalPeer subtLocalPeer, SubtConnectedPeerStream stream)
 {
     _subtLocalPeer = subtLocalPeer;
     _stream = stream;
 }