PollServiceWorkerThread.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 = new StreamReader(req.Request.Body);
  69. Hashtable responsedata = req.PollServiceArgs.GetEvents(req.RequestID, req.PollServiceArgs.Id, str.ReadToEnd());
  70. m_server.DoHTTPGruntWork(responsedata,
  71. new OSHttpResponse(new HttpResponse(req.HttpContext, req.Request),req.HttpContext));
  72. }
  73. else
  74. {
  75. if ((Environment.TickCount - req.RequestTime) > m_timeout)
  76. {
  77. m_server.DoHTTPGruntWork(req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id),
  78. new OSHttpResponse(new HttpResponse(req.HttpContext, req.Request),req.HttpContext));
  79. }
  80. else
  81. {
  82. ReQueuePollServiceItem reQueueItem = ReQueue;
  83. if (reQueueItem != null)
  84. reQueueItem(req);
  85. }
  86. }
  87. }
  88. catch (Exception e)
  89. {
  90. m_log.ErrorFormat("Exception in poll service thread: " + e.ToString());
  91. }
  92. }
  93. }
  94. internal void Enqueue(PollServiceHttpRequest pPollServiceHttpRequest)
  95. {
  96. m_request.Enqueue(pPollServiceHttpRequest);
  97. }
  98. }
  99. }