Squishy.Network.Throttle.UpdateDownload C# (CSharp) Method

UpdateDownload() private method

private UpdateDownload ( int newBytes ) : void
newBytes int
return void
        internal void UpdateDownload(int newBytes)
        {
            if (TrafficWatch.Active && Active)
            {
                rcvdBytes += newBytes;
                downloadSuspended = rcvdBytes >= (maxDownloadSpeed*TrafficWatch.PollSpan.TotalSeconds);
            }
        }

Usage Example

Example #1
0
        private void OnRecieved(IAsyncResult ar)
        {
            lock (readLock)
            {
                if (isdisconnecting)
                {
                    return;
                }

                var buf = (ByteBuffer)ar.AsyncState;
                int n   = 0;
                try
                {
                    n = sock.EndReceive(ar);
                    totalRcvdBytes += n;
                }
                catch (ObjectDisposedException)
                {
                    // socket has been closed
                    return;
                }
                catch
                {
                    DisconnectNow(true);
                    return;
                }

                if (n > 0)
                {
                    if (DownloadThrottled)
                    {
                        currentRcvdBytes += n;
                        throttle.UpdateDownload(n);
                    }
                    else
                    {
                        // TODO: find a way for consistent speed calculation for non-polled connections
                        currentRcvdBytes = n;
                        lastReceiveTime  = DateTime.Now;
                    }

                    buf.offset = 0;
                    buf.length = n;
                    ReceivedNotify(buf);

                    if (throttle == null || !throttle.DownloadSuspended)
                    {
                        ContinueReadUnlocked();
                    }
                    else
                    {
                        reading = false;
                    }
                }
                else
                {
                    //throw new Exception("FATAL: Received non-positive amount of buf: " + n);
                    DisconnectNow(true);
                }
            }
        }