PacketQueue.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Threading;
  31. using System.Timers;
  32. using libsecondlife.Packets;
  33. using OpenSim.Framework;
  34. using Timer=System.Timers.Timer;
  35. namespace OpenSim.Region.ClientStack
  36. {
  37. public class PacketQueue
  38. {
  39. private bool m_enabled = true;
  40. private BlockingQueue<QueItem> SendQueue;
  41. private Queue<QueItem> IncomingPacketQueue;
  42. private Queue<QueItem> OutgoingPacketQueue;
  43. private Queue<QueItem> ResendOutgoingPacketQueue;
  44. private Queue<QueItem> LandOutgoingPacketQueue;
  45. private Queue<QueItem> WindOutgoingPacketQueue;
  46. private Queue<QueItem> CloudOutgoingPacketQueue;
  47. private Queue<QueItem> TaskOutgoingPacketQueue;
  48. private Queue<QueItem> TextureOutgoingPacketQueue;
  49. private Queue<QueItem> AssetOutgoingPacketQueue;
  50. private Dictionary<uint, uint> PendingAcks = new Dictionary<uint, uint>();
  51. private Dictionary<uint, Packet> NeedAck = new Dictionary<uint, Packet>();
  52. // All throttle times and number of bytes are calculated by dividing by this value
  53. // This value also determines how many times per throttletimems the timer will run
  54. // If throttleimems is 1000 ms, then the timer will fire every 1000/7 milliseconds
  55. private int throttleTimeDivisor = 7;
  56. private int throttletimems = 1000;
  57. private PacketThrottle ResendThrottle;
  58. private PacketThrottle LandThrottle;
  59. private PacketThrottle WindThrottle;
  60. private PacketThrottle CloudThrottle;
  61. private PacketThrottle TaskThrottle;
  62. private PacketThrottle AssetThrottle;
  63. private PacketThrottle TextureThrottle;
  64. private PacketThrottle TotalThrottle;
  65. // private long LastThrottle;
  66. // private long ThrottleInterval;
  67. private Timer throttleTimer;
  68. public PacketQueue()
  69. {
  70. // While working on this, the BlockingQueue had me fooled for a bit.
  71. // The Blocking queue causes the thread to stop until there's something
  72. // in it to process. it's an on-purpose threadlock though because
  73. // without it, the clientloop will suck up all sim resources.
  74. SendQueue = new BlockingQueue<QueItem>();
  75. IncomingPacketQueue = new Queue<QueItem>();
  76. OutgoingPacketQueue = new Queue<QueItem>();
  77. ResendOutgoingPacketQueue = new Queue<QueItem>();
  78. LandOutgoingPacketQueue = new Queue<QueItem>();
  79. WindOutgoingPacketQueue = new Queue<QueItem>();
  80. CloudOutgoingPacketQueue = new Queue<QueItem>();
  81. TaskOutgoingPacketQueue = new Queue<QueItem>();
  82. TextureOutgoingPacketQueue = new Queue<QueItem>();
  83. AssetOutgoingPacketQueue = new Queue<QueItem>();
  84. // Set up the throttle classes (min, max, current) in bytes
  85. ResendThrottle = new PacketThrottle(5000, 100000, 50000);
  86. LandThrottle = new PacketThrottle(1000, 100000, 100000);
  87. WindThrottle = new PacketThrottle(1000, 100000, 10000);
  88. CloudThrottle = new PacketThrottle(1000, 100000, 50000);
  89. TaskThrottle = new PacketThrottle(1000, 800000, 100000);
  90. AssetThrottle = new PacketThrottle(1000, 800000, 80000);
  91. TextureThrottle = new PacketThrottle(1000, 800000, 100000);
  92. // Total Throttle trumps all
  93. // Number of bytes allowed to go out per second. (256kbps per client)
  94. TotalThrottle = new PacketThrottle(0, 162144, 1536000);
  95. throttleTimer = new Timer((int) (throttletimems/throttleTimeDivisor));
  96. throttleTimer.Elapsed += new ElapsedEventHandler(ThrottleTimerElapsed);
  97. throttleTimer.Start();
  98. // TIMERS needed for this
  99. // LastThrottle = DateTime.Now.Ticks;
  100. // ThrottleInterval = (long)(throttletimems/throttleTimeDivisor);
  101. }
  102. /* STANDARD QUEUE MANIPULATION INTERFACES */
  103. public void Enqueue(QueItem item)
  104. {
  105. if (!m_enabled)
  106. {
  107. return;
  108. }
  109. // We could micro lock, but that will tend to actually
  110. // probably be worse than just synchronizing on SendQueue
  111. lock (this)
  112. {
  113. switch (item.throttleType)
  114. {
  115. case ThrottleOutPacketType.Resend:
  116. ThrottleCheck(ref ResendThrottle, ref ResendOutgoingPacketQueue, item);
  117. break;
  118. case ThrottleOutPacketType.Texture:
  119. ThrottleCheck(ref TextureThrottle, ref TextureOutgoingPacketQueue, item);
  120. break;
  121. case ThrottleOutPacketType.Task:
  122. ThrottleCheck(ref TaskThrottle, ref TaskOutgoingPacketQueue, item);
  123. break;
  124. case ThrottleOutPacketType.Land:
  125. ThrottleCheck(ref LandThrottle, ref LandOutgoingPacketQueue, item);
  126. break;
  127. case ThrottleOutPacketType.Asset:
  128. ThrottleCheck(ref AssetThrottle, ref AssetOutgoingPacketQueue, item);
  129. break;
  130. case ThrottleOutPacketType.Cloud:
  131. ThrottleCheck(ref CloudThrottle, ref CloudOutgoingPacketQueue, item);
  132. break;
  133. case ThrottleOutPacketType.Wind:
  134. ThrottleCheck(ref WindThrottle, ref WindOutgoingPacketQueue, item);
  135. break;
  136. default:
  137. // Acknowledgements and other such stuff should go directly to the blocking Queue
  138. // Throttling them may and likely 'will' be problematic
  139. SendQueue.Enqueue(item);
  140. break;
  141. }
  142. }
  143. }
  144. public QueItem Dequeue()
  145. {
  146. return SendQueue.Dequeue();
  147. }
  148. public void Flush()
  149. {
  150. lock (this)
  151. {
  152. while (PacketsWaiting())
  153. {
  154. //Now comes the fun part.. we dump all our elements into m_packetQueue that we've saved up.
  155. if (ResendOutgoingPacketQueue.Count > 0)
  156. {
  157. SendQueue.Enqueue(ResendOutgoingPacketQueue.Dequeue());
  158. }
  159. if (LandOutgoingPacketQueue.Count > 0)
  160. {
  161. SendQueue.Enqueue(LandOutgoingPacketQueue.Dequeue());
  162. }
  163. if (WindOutgoingPacketQueue.Count > 0)
  164. {
  165. SendQueue.Enqueue(WindOutgoingPacketQueue.Dequeue());
  166. }
  167. if (CloudOutgoingPacketQueue.Count > 0)
  168. {
  169. SendQueue.Enqueue(CloudOutgoingPacketQueue.Dequeue());
  170. }
  171. if (TaskOutgoingPacketQueue.Count > 0)
  172. {
  173. SendQueue.Enqueue(TaskOutgoingPacketQueue.Dequeue());
  174. }
  175. if (TextureOutgoingPacketQueue.Count > 0)
  176. {
  177. SendQueue.Enqueue(TextureOutgoingPacketQueue.Dequeue());
  178. }
  179. if (AssetOutgoingPacketQueue.Count > 0)
  180. {
  181. SendQueue.Enqueue(AssetOutgoingPacketQueue.Dequeue());
  182. }
  183. }
  184. // MainLog.Instance.Verbose("THROTTLE", "Processed " + throttleLoops + " packets");
  185. }
  186. }
  187. public void Close()
  188. {
  189. m_enabled = false;
  190. throttleTimer.Stop();
  191. }
  192. private void ResetCounters()
  193. {
  194. ResendThrottle.Reset();
  195. LandThrottle.Reset();
  196. WindThrottle.Reset();
  197. CloudThrottle.Reset();
  198. TaskThrottle.Reset();
  199. AssetThrottle.Reset();
  200. TextureThrottle.Reset();
  201. TotalThrottle.Reset();
  202. }
  203. private bool PacketsWaiting()
  204. {
  205. return (ResendOutgoingPacketQueue.Count > 0 ||
  206. LandOutgoingPacketQueue.Count > 0 ||
  207. WindOutgoingPacketQueue.Count > 0 ||
  208. CloudOutgoingPacketQueue.Count > 0 ||
  209. TaskOutgoingPacketQueue.Count > 0 ||
  210. AssetOutgoingPacketQueue.Count > 0 ||
  211. TextureOutgoingPacketQueue.Count > 0);
  212. }
  213. public void ProcessThrottle()
  214. {
  215. // I was considering this.. Will an event fire if the thread it's on is blocked?
  216. // Then I figured out.. it doesn't really matter.. because this thread won't be blocked for long
  217. // The General overhead of the UDP protocol gets sent to the queue un-throttled by this
  218. // so This'll pick up about around the right time.
  219. int MaxThrottleLoops = 4550; // 50*7 packets can be dequeued at once.
  220. int throttleLoops = 0;
  221. // We're going to dequeue all of the saved up packets until
  222. // we've hit the throttle limit or there's no more packets to send
  223. lock (this)
  224. {
  225. ResetCounters();
  226. // MainLog.Instance.Verbose("THROTTLE", "Entering Throttle");
  227. while (TotalThrottle.UnderLimit() && PacketsWaiting() &&
  228. (throttleLoops <= MaxThrottleLoops))
  229. {
  230. throttleLoops++;
  231. //Now comes the fun part.. we dump all our elements into m_packetQueue that we've saved up.
  232. if (ResendThrottle.UnderLimit() && ResendOutgoingPacketQueue.Count > 0)
  233. {
  234. QueItem qpack = ResendOutgoingPacketQueue.Dequeue();
  235. SendQueue.Enqueue(qpack);
  236. TotalThrottle.Add(qpack.Packet.ToBytes().Length);
  237. ResendThrottle.Add(qpack.Packet.ToBytes().Length);
  238. }
  239. if (LandThrottle.UnderLimit() && LandOutgoingPacketQueue.Count > 0)
  240. {
  241. QueItem qpack = LandOutgoingPacketQueue.Dequeue();
  242. SendQueue.Enqueue(qpack);
  243. TotalThrottle.Add(qpack.Packet.ToBytes().Length);
  244. LandThrottle.Add(qpack.Packet.ToBytes().Length);
  245. }
  246. if (WindThrottle.UnderLimit() && WindOutgoingPacketQueue.Count > 0)
  247. {
  248. QueItem qpack = WindOutgoingPacketQueue.Dequeue();
  249. SendQueue.Enqueue(qpack);
  250. TotalThrottle.Add(qpack.Packet.ToBytes().Length);
  251. WindThrottle.Add(qpack.Packet.ToBytes().Length);
  252. }
  253. if (CloudThrottle.UnderLimit() && CloudOutgoingPacketQueue.Count > 0)
  254. {
  255. QueItem qpack = CloudOutgoingPacketQueue.Dequeue();
  256. SendQueue.Enqueue(qpack);
  257. TotalThrottle.Add(qpack.Packet.ToBytes().Length);
  258. CloudThrottle.Add(qpack.Packet.ToBytes().Length);
  259. }
  260. if (TaskThrottle.UnderLimit() && TaskOutgoingPacketQueue.Count > 0)
  261. {
  262. QueItem qpack = TaskOutgoingPacketQueue.Dequeue();
  263. SendQueue.Enqueue(qpack);
  264. TotalThrottle.Add(qpack.Packet.ToBytes().Length);
  265. TaskThrottle.Add(qpack.Packet.ToBytes().Length);
  266. }
  267. if (TextureThrottle.UnderLimit() && TextureOutgoingPacketQueue.Count > 0)
  268. {
  269. QueItem qpack = TextureOutgoingPacketQueue.Dequeue();
  270. SendQueue.Enqueue(qpack);
  271. TotalThrottle.Add(qpack.Packet.ToBytes().Length);
  272. TextureThrottle.Add(qpack.Packet.ToBytes().Length);
  273. }
  274. if (AssetThrottle.UnderLimit() && AssetOutgoingPacketQueue.Count > 0)
  275. {
  276. QueItem qpack = AssetOutgoingPacketQueue.Dequeue();
  277. SendQueue.Enqueue(qpack);
  278. TotalThrottle.Add(qpack.Packet.ToBytes().Length);
  279. AssetThrottle.Add(qpack.Packet.ToBytes().Length);
  280. }
  281. }
  282. // MainLog.Instance.Verbose("THROTTLE", "Processed " + throttleLoops + " packets");
  283. }
  284. }
  285. private void ThrottleTimerElapsed(object sender, ElapsedEventArgs e)
  286. {
  287. // just to change the signature, and that ProcessThrottle
  288. // will be used elsewhere possibly
  289. ProcessThrottle();
  290. }
  291. private void ThrottleCheck(ref PacketThrottle throttle, ref Queue<QueItem> q, QueItem item)
  292. {
  293. // The idea.. is if the packet throttle queues are empty
  294. // and the client is under throttle for the type. Queue
  295. // it up directly. This basically short cuts having to
  296. // wait for the timer to fire to put things into the
  297. // output queue
  298. if ((q.Count == 0) && (throttle.UnderLimit()))
  299. {
  300. Monitor.Enter(this);
  301. throttle.Add(item.Packet.ToBytes().Length);
  302. TotalThrottle.Add(item.Packet.ToBytes().Length);
  303. SendQueue.Enqueue(item);
  304. Monitor.Pulse(this);
  305. Monitor.Exit(this);
  306. }
  307. else
  308. {
  309. q.Enqueue(item);
  310. }
  311. }
  312. private int ScaleThrottle(int value, int curmax, int newmax)
  313. {
  314. return (int) (((float) value/(float) curmax)*newmax);
  315. }
  316. private byte[] GetThrottlesPacked(float multiplier)
  317. {
  318. int singlefloat = 4;
  319. float tResend = ResendThrottle.Throttle*multiplier;
  320. float tLand = LandThrottle.Throttle*multiplier;
  321. float tWind = WindThrottle.Throttle*multiplier;
  322. float tCloud = CloudThrottle.Throttle*multiplier;
  323. float tTask = TaskThrottle.Throttle*multiplier;
  324. float tTexture = TextureThrottle.Throttle*multiplier;
  325. float tAsset = AssetThrottle.Throttle*multiplier;
  326. byte[] throttles = new byte[singlefloat*7];
  327. int i = 0;
  328. Buffer.BlockCopy(BitConverter.GetBytes(tResend), 0, throttles, singlefloat*i, singlefloat);
  329. i++;
  330. Buffer.BlockCopy(BitConverter.GetBytes(tLand), 0, throttles, singlefloat*i, singlefloat);
  331. i++;
  332. Buffer.BlockCopy(BitConverter.GetBytes(tWind), 0, throttles, singlefloat*i, singlefloat);
  333. i++;
  334. Buffer.BlockCopy(BitConverter.GetBytes(tCloud), 0, throttles, singlefloat*i, singlefloat);
  335. i++;
  336. Buffer.BlockCopy(BitConverter.GetBytes(tTask), 0, throttles, singlefloat*i, singlefloat);
  337. i++;
  338. Buffer.BlockCopy(BitConverter.GetBytes(tTexture), 0, throttles, singlefloat*i, singlefloat);
  339. i++;
  340. Buffer.BlockCopy(BitConverter.GetBytes(tAsset), 0, throttles, singlefloat*i, singlefloat);
  341. return throttles;
  342. }
  343. public void SetThrottleFromClient(byte[] throttle)
  344. {
  345. int tResend = -1;
  346. int tLand = -1;
  347. int tWind = -1;
  348. int tCloud = -1;
  349. int tTask = -1;
  350. int tTexture = -1;
  351. int tAsset = -1;
  352. int tall = -1;
  353. int singlefloat = 4;
  354. //Agent Throttle Block contains 7 single floatingpoint values.
  355. int j = 0;
  356. // Some Systems may be big endian...
  357. // it might be smart to do this check more often...
  358. if (!BitConverter.IsLittleEndian)
  359. for (int i = 0; i < 7; i++)
  360. Array.Reverse(throttle, j + i*singlefloat, singlefloat);
  361. // values gotten from libsecondlife.org/wiki/Throttle. Thanks MW_
  362. // bytes
  363. // Convert to integer, since.. the full fp space isn't used.
  364. tResend = (int) BitConverter.ToSingle(throttle, j);
  365. j += singlefloat;
  366. tLand = (int) BitConverter.ToSingle(throttle, j);
  367. j += singlefloat;
  368. tWind = (int) BitConverter.ToSingle(throttle, j);
  369. j += singlefloat;
  370. tCloud = (int) BitConverter.ToSingle(throttle, j);
  371. j += singlefloat;
  372. tTask = (int) BitConverter.ToSingle(throttle, j);
  373. j += singlefloat;
  374. tTexture = (int) BitConverter.ToSingle(throttle, j);
  375. j += singlefloat;
  376. tAsset = (int) BitConverter.ToSingle(throttle, j);
  377. tall = tResend + tLand + tWind + tCloud + tTask + tTexture + tAsset;
  378. /*
  379. MainLog.Instance.Verbose("CLIENT", "Client AgentThrottle - Got throttle:resendbytes=" + tResend +
  380. " landbytes=" + tLand +
  381. " windbytes=" + tWind +
  382. " cloudbytes=" + tCloud +
  383. " taskbytes=" + tTask +
  384. " texturebytes=" + tTexture +
  385. " Assetbytes=" + tAsset +
  386. " Allbytes=" + tall);
  387. */
  388. // Total Sanity
  389. // Make sure that the client sent sane total values.
  390. // If the client didn't send acceptable values....
  391. // Scale the clients values down until they are acceptable.
  392. if (tall <= TotalThrottle.Max)
  393. {
  394. ResendThrottle.Throttle = tResend;
  395. LandThrottle.Throttle = tLand;
  396. WindThrottle.Throttle = tWind;
  397. CloudThrottle.Throttle = tCloud;
  398. TaskThrottle.Throttle = tTask;
  399. TextureThrottle.Throttle = tTexture;
  400. AssetThrottle.Throttle = tAsset;
  401. TotalThrottle.Throttle = tall;
  402. }
  403. else if (tall < 1)
  404. {
  405. // client is stupid, penalize him by minning everything
  406. ResendThrottle.Throttle = ResendThrottle.Min;
  407. LandThrottle.Throttle = LandThrottle.Min;
  408. WindThrottle.Throttle = WindThrottle.Min;
  409. CloudThrottle.Throttle = CloudThrottle.Min;
  410. TaskThrottle.Throttle = TaskThrottle.Min;
  411. TextureThrottle.Throttle = TextureThrottle.Min;
  412. AssetThrottle.Throttle = AssetThrottle.Min;
  413. TotalThrottle.Throttle = TotalThrottle.Min;
  414. }
  415. else
  416. {
  417. // we're over so figure out percentages and use those
  418. ResendThrottle.Throttle = tResend;
  419. LandThrottle.Throttle = ScaleThrottle(tLand, tall, TotalThrottle.Max);
  420. WindThrottle.Throttle = ScaleThrottle(tWind, tall, TotalThrottle.Max);
  421. CloudThrottle.Throttle = ScaleThrottle(tCloud, tall, TotalThrottle.Max);
  422. TaskThrottle.Throttle = ScaleThrottle(tTask, tall, TotalThrottle.Max);
  423. TextureThrottle.Throttle = ScaleThrottle(tTexture, tall, TotalThrottle.Max);
  424. AssetThrottle.Throttle = ScaleThrottle(tAsset, tall, TotalThrottle.Max);
  425. TotalThrottle.Throttle = TotalThrottle.Max;
  426. }
  427. // effectively wiggling the slider causes things reset
  428. ResetCounters();
  429. }
  430. }
  431. }