LLPacketThrottle.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. namespace OpenSim.Region.ClientStack.LindenUDP
  28. {
  29. public class LLPacketThrottle
  30. {
  31. private readonly int m_maxAllowableThrottle;
  32. private readonly int m_minAllowableThrottle;
  33. private int m_currentThrottle;
  34. private const int m_throttleTimeDivisor = 7;
  35. private int m_currentBitsSent;
  36. private int m_throttleBits;
  37. /// <value>
  38. /// Value with which to multiply all the throttle fields
  39. /// </value>
  40. private float m_throttleMultiplier;
  41. public int Max
  42. {
  43. get { return m_maxAllowableThrottle; }
  44. }
  45. public int Min
  46. {
  47. get { return m_minAllowableThrottle; }
  48. }
  49. public int Current
  50. {
  51. get { return m_currentThrottle; }
  52. }
  53. /// <summary>
  54. /// Constructor.
  55. /// </summary>
  56. /// <param name="min"></param>
  57. /// <param name="max"></param>
  58. /// <param name="throttle"></param>
  59. /// <param name="throttleMultiplier">
  60. /// A parameter that's ends up multiplying all throttle settings. An alternative solution would have been
  61. /// to multiply all the parameters by this before giving them to the constructor. But doing it this way
  62. /// represents the fact that the multiplier is a hack that pumps data to clients much faster than the actual
  63. /// settings that we are given.
  64. /// </param>
  65. public LLPacketThrottle(int min, int max, int throttle, float throttleMultiplier)
  66. {
  67. m_throttleMultiplier = throttleMultiplier;
  68. m_maxAllowableThrottle = max;
  69. m_minAllowableThrottle = min;
  70. m_currentThrottle = throttle;
  71. m_currentBitsSent = 0;
  72. CalcBits();
  73. }
  74. /// <summary>
  75. /// Calculate the actual throttle required.
  76. /// </summary>
  77. private void CalcBits()
  78. {
  79. m_throttleBits = (int)((float)m_currentThrottle * m_throttleMultiplier / (float)m_throttleTimeDivisor);
  80. }
  81. public void Reset()
  82. {
  83. m_currentBitsSent = 0;
  84. }
  85. public bool UnderLimit()
  86. {
  87. return m_currentBitsSent < m_throttleBits;
  88. }
  89. public int AddBytes(int bytes)
  90. {
  91. m_currentBitsSent += bytes * 8;
  92. return m_currentBitsSent;
  93. }
  94. public int Throttle
  95. {
  96. get { return m_currentThrottle; }
  97. set
  98. {
  99. if (value < m_minAllowableThrottle)
  100. {
  101. m_currentThrottle = m_minAllowableThrottle;
  102. }
  103. else if (value > m_maxAllowableThrottle)
  104. {
  105. m_currentThrottle = m_maxAllowableThrottle;
  106. }
  107. else
  108. {
  109. m_currentThrottle = value;
  110. }
  111. CalcBits();
  112. }
  113. }
  114. }
  115. }