Timer.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Threading;
  31. using libsecondlife;
  32. using Axiom.Math;
  33. using OpenSim.Region.Environment.Interfaces;
  34. using OpenSim.Region.Environment.Modules;
  35. using OpenSim.Region.Environment.Scenes;
  36. using OpenSim.Framework;
  37. namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase.AsyncCommandPlugins
  38. {
  39. public class Timer
  40. {
  41. public AsyncCommandManager m_CmdManager;
  42. public Timer(AsyncCommandManager CmdManager)
  43. {
  44. m_CmdManager = CmdManager;
  45. }
  46. //
  47. // TIMER
  48. //
  49. private class TimerClass
  50. {
  51. public uint localID;
  52. public LLUUID itemID;
  53. //public double interval;
  54. public long interval;
  55. //public DateTime next;
  56. public long next;
  57. }
  58. private List<TimerClass> Timers = new List<TimerClass>();
  59. private object TimerListLock = new object();
  60. public void SetTimerEvent(uint m_localID, LLUUID m_itemID, double sec)
  61. {
  62. Console.WriteLine("SetTimerEvent");
  63. // Always remove first, in case this is a re-set
  64. UnSetTimerEvents(m_localID, m_itemID);
  65. if (sec == 0) // Disabling timer
  66. return;
  67. // Add to timer
  68. TimerClass ts = new TimerClass();
  69. ts.localID = m_localID;
  70. ts.itemID = m_itemID;
  71. ts.interval = Convert.ToInt64(sec * 10000000); // How many 100 nanoseconds (ticks) should we wait
  72. // 2193386136332921 ticks
  73. // 219338613 seconds
  74. //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  75. ts.next = DateTime.Now.Ticks + ts.interval;
  76. lock (TimerListLock)
  77. {
  78. Timers.Add(ts);
  79. }
  80. }
  81. public void UnSetTimerEvents(uint m_localID, LLUUID m_itemID)
  82. {
  83. // Remove from timer
  84. lock (TimerListLock)
  85. {
  86. foreach (TimerClass ts in new ArrayList(Timers))
  87. {
  88. if (ts.localID == m_localID && ts.itemID == m_itemID)
  89. Timers.Remove(ts);
  90. }
  91. }
  92. // Old method: Create new list
  93. //List<TimerClass> NewTimers = new List<TimerClass>();
  94. //foreach (TimerClass ts in Timers)
  95. //{
  96. // if (ts.localID != m_localID && ts.itemID != m_itemID)
  97. // {
  98. // NewTimers.Add(ts);
  99. // }
  100. //}
  101. //Timers.Clear();
  102. //Timers = NewTimers;
  103. //}
  104. }
  105. public void CheckTimerEvents()
  106. {
  107. // Nothing to do here?
  108. if (Timers.Count == 0)
  109. return;
  110. lock (TimerListLock)
  111. {
  112. // Go through all timers
  113. foreach (TimerClass ts in Timers)
  114. {
  115. // Time has passed?
  116. if (ts.next < DateTime.Now.Ticks)
  117. {
  118. // Console.WriteLine("Time has passed: Now: " + DateTime.Now.Ticks + ", Passed: " + ts.next);
  119. // Add it to queue
  120. m_CmdManager.m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(ts.localID, ts.itemID, "timer", EventQueueManager.llDetectNull,
  121. null);
  122. // set next interval
  123. //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  124. ts.next = DateTime.Now.Ticks + ts.interval;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }