Timer.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.XEngine.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. }
  87. public void CheckTimerEvents()
  88. {
  89. // Nothing to do here?
  90. if (Timers.Count == 0)
  91. return;
  92. lock (TimerListLock)
  93. {
  94. // Go through all timers
  95. foreach (TimerClass ts in Timers)
  96. {
  97. // Time has passed?
  98. if (ts.next < DateTime.Now.Ticks)
  99. {
  100. // Console.WriteLine("Time has passed: Now: " + DateTime.Now.Ticks + ", Passed: " + ts.next);
  101. // Add it to queue
  102. m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID,
  103. new XEventParams("timer", new Object[0],
  104. new XDetectParams[0]));
  105. // set next interval
  106. //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  107. ts.next = DateTime.Now.Ticks + ts.interval;
  108. }
  109. }
  110. }
  111. }
  112. public Object[] GetSerializationData(LLUUID itemID)
  113. {
  114. List<Object> data = new List<Object>();
  115. lock (TimerListLock)
  116. {
  117. foreach (TimerClass ts in Timers)
  118. {
  119. if(ts.itemID == itemID)
  120. {
  121. data.Add(ts.interval);
  122. data.Add(ts.next-DateTime.Now.Ticks);
  123. }
  124. }
  125. }
  126. return data.ToArray();
  127. }
  128. public void CreateFromData(uint localID, LLUUID itemID, LLUUID objectID,
  129. Object[] data)
  130. {
  131. int idx=0;
  132. while(idx < data.Length)
  133. {
  134. TimerClass ts = new TimerClass();
  135. ts.localID = localID;
  136. ts.itemID = itemID;
  137. ts.interval = (long)data[idx];
  138. ts.next = DateTime.Now.Ticks + (long)data[idx+1];
  139. idx += 2;
  140. Timers.Add(ts);
  141. }
  142. }
  143. }
  144. }