Timer.cs 5.3 KB

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