LLPacketThrottle.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 OpenSim 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. /// <summary>
  50. /// Constructor.
  51. /// </summary>
  52. /// <param name="min"></param>
  53. /// <param name="max"></param>
  54. /// <param name="throttle"></param>
  55. /// <param name="throttleMultiplier">
  56. /// A parameter that's ends up multiplying all throttle settings. An alternative solution would have been
  57. /// to multiply all the parameters by this before giving them to the constructor. But doing it this way
  58. /// represents the fact that the multiplier is a hack that pumps data to clients much faster than the actual
  59. /// settings that we are given.
  60. /// </param>
  61. public LLPacketThrottle(int min, int max, int throttle, float throttleMultiplier)
  62. {
  63. m_throttleMultiplier = throttleMultiplier;
  64. m_maxAllowableThrottle = max;
  65. m_minAllowableThrottle = min;
  66. m_currentThrottle = throttle;
  67. m_currentBitsSent = 0;
  68. CalcBits();
  69. }
  70. /// <summary>
  71. /// Calculate the actual throttle required.
  72. /// </summary>
  73. private void CalcBits()
  74. {
  75. m_throttleBits = (int)((float)m_currentThrottle * m_throttleMultiplier / (float)m_throttleTimeDivisor);
  76. }
  77. public void Reset()
  78. {
  79. m_currentBitsSent = 0;
  80. }
  81. public bool UnderLimit()
  82. {
  83. return m_currentBitsSent < m_throttleBits;
  84. }
  85. public int AddBytes(int bytes)
  86. {
  87. m_currentBitsSent += bytes * 8;
  88. return m_currentBitsSent;
  89. }
  90. public int Throttle
  91. {
  92. get { return m_currentThrottle; }
  93. set
  94. {
  95. if (value < m_minAllowableThrottle)
  96. {
  97. m_currentThrottle = m_minAllowableThrottle;
  98. }
  99. else if (value > m_maxAllowableThrottle)
  100. {
  101. m_currentThrottle = m_maxAllowableThrottle;
  102. }
  103. else
  104. {
  105. m_currentThrottle = value;
  106. }
  107. CalcBits();
  108. }
  109. }
  110. }
  111. }