PollServiceWorkerThread.cs 4.9 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 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. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Text;
  32. using HttpServer;
  33. using OpenMetaverse;
  34. using System.Reflection;
  35. using log4net;
  36. namespace OpenSim.Framework.Servers.HttpServer
  37. {
  38. public delegate void ReQueuePollServiceItem(PollServiceHttpRequest req);
  39. public class PollServiceWorkerThread
  40. {
  41. private static readonly ILog m_log =
  42. LogManager.GetLogger(
  43. MethodBase.GetCurrentMethod().DeclaringType);
  44. public event ReQueuePollServiceItem ReQueue;
  45. private readonly BaseHttpServer m_server;
  46. private BlockingQueue<PollServiceHttpRequest> m_request;
  47. private bool m_running = true;
  48. private int m_timeout = 250;
  49. public PollServiceWorkerThread(BaseHttpServer pSrv, int pTimeout)
  50. {
  51. m_request = new BlockingQueue<PollServiceHttpRequest>();
  52. m_server = pSrv;
  53. m_timeout = pTimeout;
  54. }
  55. public void ThreadStart()
  56. {
  57. Run();
  58. }
  59. public void Run()
  60. {
  61. while (m_running)
  62. {
  63. PollServiceHttpRequest req = m_request.Dequeue();
  64. Watchdog.UpdateThread();
  65. try
  66. {
  67. if (req.PollServiceArgs.HasEvents(req.RequestID, req.PollServiceArgs.Id))
  68. {
  69. StreamReader str;
  70. try
  71. {
  72. str = new StreamReader(req.Request.Body);
  73. }
  74. catch (System.ArgumentException)
  75. {
  76. // Stream was not readable means a child agent
  77. // was closed due to logout, leaving the
  78. // Event Queue request orphaned.
  79. continue;
  80. }
  81. Hashtable responsedata = req.PollServiceArgs.GetEvents(req.RequestID, req.PollServiceArgs.Id, str.ReadToEnd());
  82. m_server.DoHTTPGruntWork(responsedata,
  83. new OSHttpResponse(new HttpResponse(req.HttpContext, req.Request),req.HttpContext));
  84. }
  85. else
  86. {
  87. if ((Environment.TickCount - req.RequestTime) > m_timeout)
  88. {
  89. m_server.DoHTTPGruntWork(req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id),
  90. new OSHttpResponse(new HttpResponse(req.HttpContext, req.Request),req.HttpContext));
  91. }
  92. else
  93. {
  94. ReQueuePollServiceItem reQueueItem = ReQueue;
  95. if (reQueueItem != null)
  96. reQueueItem(req);
  97. }
  98. }
  99. }
  100. catch (Exception e)
  101. {
  102. m_log.ErrorFormat("Exception in poll service thread: " + e.ToString());
  103. }
  104. }
  105. }
  106. internal void Enqueue(PollServiceHttpRequest pPollServiceHttpRequest)
  107. {
  108. m_request.Enqueue(pPollServiceHttpRequest);
  109. }
  110. }
  111. }