DownloadManager.DownloadCounter.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. namespace GameFramework.Download
  8. {
  9. internal sealed partial class DownloadManager : GameFrameworkModule, IDownloadManager
  10. {
  11. private sealed partial class DownloadCounter
  12. {
  13. private readonly GameFrameworkLinkedList<DownloadCounterNode> m_DownloadCounterNodes;
  14. private float m_UpdateInterval;
  15. private float m_RecordInterval;
  16. private float m_CurrentSpeed;
  17. private float m_Accumulator;
  18. private float m_TimeLeft;
  19. public DownloadCounter(float updateInterval, float recordInterval)
  20. {
  21. if (updateInterval <= 0f)
  22. {
  23. throw new GameFrameworkException("Update interval is invalid.");
  24. }
  25. if (recordInterval <= 0f)
  26. {
  27. throw new GameFrameworkException("Record interval is invalid.");
  28. }
  29. m_DownloadCounterNodes = new GameFrameworkLinkedList<DownloadCounterNode>();
  30. m_UpdateInterval = updateInterval;
  31. m_RecordInterval = recordInterval;
  32. Reset();
  33. }
  34. public float UpdateInterval
  35. {
  36. get
  37. {
  38. return m_UpdateInterval;
  39. }
  40. set
  41. {
  42. if (value <= 0f)
  43. {
  44. throw new GameFrameworkException("Update interval is invalid.");
  45. }
  46. m_UpdateInterval = value;
  47. Reset();
  48. }
  49. }
  50. public float RecordInterval
  51. {
  52. get
  53. {
  54. return m_RecordInterval;
  55. }
  56. set
  57. {
  58. if (value <= 0f)
  59. {
  60. throw new GameFrameworkException("Record interval is invalid.");
  61. }
  62. m_RecordInterval = value;
  63. Reset();
  64. }
  65. }
  66. public float CurrentSpeed
  67. {
  68. get
  69. {
  70. return m_CurrentSpeed;
  71. }
  72. }
  73. public void Shutdown()
  74. {
  75. Reset();
  76. }
  77. public void Update(float elapseSeconds, float realElapseSeconds)
  78. {
  79. if (m_DownloadCounterNodes.Count <= 0)
  80. {
  81. return;
  82. }
  83. m_Accumulator += realElapseSeconds;
  84. if (m_Accumulator > m_RecordInterval)
  85. {
  86. m_Accumulator = m_RecordInterval;
  87. }
  88. m_TimeLeft -= realElapseSeconds;
  89. foreach (DownloadCounterNode downloadCounterNode in m_DownloadCounterNodes)
  90. {
  91. downloadCounterNode.Update(elapseSeconds, realElapseSeconds);
  92. }
  93. while (m_DownloadCounterNodes.Count > 0)
  94. {
  95. DownloadCounterNode downloadCounterNode = m_DownloadCounterNodes.First.Value;
  96. if (downloadCounterNode.ElapseSeconds < m_RecordInterval)
  97. {
  98. break;
  99. }
  100. ReferencePool.Release(downloadCounterNode);
  101. m_DownloadCounterNodes.RemoveFirst();
  102. }
  103. if (m_DownloadCounterNodes.Count <= 0)
  104. {
  105. Reset();
  106. return;
  107. }
  108. if (m_TimeLeft <= 0f)
  109. {
  110. long totalDeltaLength = 0L;
  111. foreach (DownloadCounterNode downloadCounterNode in m_DownloadCounterNodes)
  112. {
  113. totalDeltaLength += downloadCounterNode.DeltaLength;
  114. }
  115. m_CurrentSpeed = m_Accumulator > 0f ? totalDeltaLength / m_Accumulator : 0f;
  116. m_TimeLeft += m_UpdateInterval;
  117. }
  118. }
  119. public void RecordDeltaLength(int deltaLength)
  120. {
  121. if (deltaLength <= 0)
  122. {
  123. return;
  124. }
  125. DownloadCounterNode downloadCounterNode = null;
  126. if (m_DownloadCounterNodes.Count > 0)
  127. {
  128. downloadCounterNode = m_DownloadCounterNodes.Last.Value;
  129. if (downloadCounterNode.ElapseSeconds < m_UpdateInterval)
  130. {
  131. downloadCounterNode.AddDeltaLength(deltaLength);
  132. return;
  133. }
  134. }
  135. downloadCounterNode = DownloadCounterNode.Create();
  136. downloadCounterNode.AddDeltaLength(deltaLength);
  137. m_DownloadCounterNodes.AddLast(downloadCounterNode);
  138. }
  139. private void Reset()
  140. {
  141. m_DownloadCounterNodes.Clear();
  142. m_CurrentSpeed = 0f;
  143. m_Accumulator = 0f;
  144. m_TimeLeft = 0f;
  145. }
  146. }
  147. }
  148. }