PollServiceWorkerThread.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(object o)
  56. {
  57. Run();
  58. }
  59. public void Run()
  60. {
  61. while (m_running)
  62. {
  63. PollServiceHttpRequest req = m_request.Dequeue();
  64. try
  65. {
  66. if (req.PollServiceArgs.HasEvents(req.RequestID, req.PollServiceArgs.Id))
  67. {
  68. StreamReader str;
  69. try
  70. {
  71. str = new StreamReader(req.Request.Body);
  72. }
  73. catch (System.ArgumentException)
  74. {
  75. // Stream was not readable means a child agent
  76. // was closed due to logout, leaving the
  77. // Event Queue request orphaned.
  78. continue;
  79. }
  80. Hashtable responsedata = req.PollServiceArgs.GetEvents(req.RequestID, req.PollServiceArgs.Id, str.ReadToEnd());
  81. m_server.DoHTTPGruntWork(responsedata,
  82. new OSHttpResponse(new HttpResponse(req.HttpContext, req.Request),req.HttpContext));
  83. }
  84. else
  85. {
  86. if ((Environment.TickCount - req.RequestTime) > m_timeout)
  87. {
  88. m_server.DoHTTPGruntWork(req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id),
  89. new OSHttpResponse(new HttpResponse(req.HttpContext, req.Request),req.HttpContext));
  90. }
  91. else
  92. {
  93. ReQueuePollServiceItem reQueueItem = ReQueue;
  94. if (reQueueItem != null)
  95. reQueueItem(req);
  96. }
  97. }
  98. }
  99. catch (Exception e)
  100. {
  101. m_log.ErrorFormat("Exception in poll service thread: " + e.ToString());
  102. }
  103. }
  104. }
  105. internal void Enqueue(PollServiceHttpRequest pPollServiceHttpRequest)
  106. {
  107. m_request.Enqueue(pPollServiceHttpRequest);
  108. }
  109. }
  110. }