⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 congestionwindow.cs

📁 rudp可靠保障得udp传输
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Helper.Net.RUDP.Simple01
{

	sealed internal class CongestionWindow : AbstractWindow
	{

		#region Variables

		public static int MIN_CWIND = 3000; // Minimum congestion window
		public static int MAX_CWIND = 1000000; // Max congestion window
		public static double ALPHA = 0.3125; // AIMD increase parameter
		public static double BETA = 0.875; // AIMD decrease parameter
		public static double GAMMA = 3.0; // Slow start divisor

		private int inflight = 0; // Bytes sent but not acked
		private bool slowStart = true; // Are we in the slow start phase?

		#endregion

		#region Constructor

		internal CongestionWindow(RUDPSocket rudp)
			: base(rudp)
		{
			_rudp = rudp;
		}

		#endregion

		#region Reset

		internal override void Reset()
		{
			base.Reset();

			_cwnd = MIN_CWIND; // Size of window in bytes
			slowStart = true;
		}

		#endregion

		#region OnACK_UpdateWindow

		internal override void OnACK_UpdateWindow(RUDPOutgoingPacket packet)
		{
			inflight -= packet.Payload.Length;

			// Increase the window
			if (slowStart)
				_cwnd += packet.Payload.Length / GAMMA;
			else
				_cwnd += packet.Payload.Length * packet.Payload.Length * ALPHA / _cwnd;

			if (_cwnd > MAX_CWIND)
				_cwnd = MAX_CWIND;
		}

		#endregion

		#region OnSend_UpdateWindow

		override internal void OnSend_UpdateWindow(int payloadLength)
		{
			inflight += payloadLength;
		}

		#endregion

		#region OnResend_UpdateWindow

		internal override void OnResend_UpdateWindow()
		{
			if (slowStart)
				fastRetransmission(); // Leave slow start
			else
			{
				// Reset the window and return to slow start
				_cwnd = MIN_CWIND; // Size of window in bytes
				slowStart = true;
			}
		}

		// Decrease the window when a packet is fast retransmitted
		public void fastRetransmission()
		{
			_cwnd *= BETA;
			if (_cwnd < MIN_CWIND)
				_cwnd = MIN_CWIND;

			// The slow start phase ends when the first packet is lost
			if (slowStart)
				slowStart = false;
		}

		#endregion

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -