]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/timer.cpp
Add support for blocking tag messages with the deaf mode.
[user/henk/code/inspircd.git] / src / timer.cpp
index 27f2aecfdca8dd5cf8d5eb3f72b7287da90ba885..c499b7991e724904e1f63c033b2367ac9c35cdfb 100644 (file)
@@ -1,66 +1,87 @@
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
  *
- *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
- * See: http://www.inspircd.org/wiki/index.php/Credits
+ *   Copyright (C) 2017 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2013-2014 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
+ *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
  *
- * This program is free but copyrighted software; see
- *            the file COPYING for details.
+ * This file is part of InspIRCd.  InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
  *
- * ---------------------------------------------------
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* $Core */
 
 #include "inspircd.h"
-#include "timer.h"
 
-TimerManager::TimerManager(InspIRCd* Instance) : ServerInstance(Instance)
+void Timer::SetInterval(unsigned int newinterval)
+{
+       ServerInstance->Timers.DelTimer(this);
+       secs = newinterval;
+       SetTrigger(ServerInstance->Time() + newinterval);
+       ServerInstance->Timers.AddTimer(this);
+}
+
+Timer::Timer(unsigned int secs_from_now, bool repeating)
+       : trigger(ServerInstance->Time() + secs_from_now)
+       , secs(secs_from_now)
+       , repeat(repeating)
 {
 }
 
+Timer::~Timer()
+{
+       ServerInstance->Timers.DelTimer(this);
+}
+
 void TimerManager::TickTimers(time_t TIME)
 {
-       while ((Timers.size()) && (TIME > (*Timers.begin())->GetTimer()))
+       for (TimerMap::iterator i = Timers.begin(); i != Timers.end(); )
        {
-               std::vector<Timer *>::iterator i = Timers.begin();
-               Timer *t = (*i);
-               
-               // Probable fix: move vector manipulation to *before* we modify the vector.
-               Timers.erase(i);
+               Timer* t = i->second;
+               if (t->GetTrigger() > TIME)
+                       break;
+
+               Timers.erase(i++);
+
+               if (!t->Tick(TIME))
+                       continue;
 
-               t->Tick(TIME);
                if (t->GetRepeat())
                {
-                       t->SetTimer(TIME + t->GetSecs());
+                       t->SetTrigger(TIME + t->GetInterval());
                        AddTimer(t);
                }
-               else
-                       delete t;
        }
 }
 
-void TimerManager::DelTimer(Timer* T)
+void TimerManager::DelTimer(Timer* t)
 {
-       std::vector<Timer *>::iterator i = std::find(Timers.begin(), Timers.end(), T);
+       std::pair<TimerMap::iterator, TimerMap::iterator> itpair = Timers.equal_range(t->GetTrigger());
 
-       if (i != Timers.end())
+       for (TimerMap::iterator i = itpair.first; i != itpair.second; ++i)
        {
-               delete (*i);
-               Timers.erase(i);
+               if (i->second == t)
+               {
+                       Timers.erase(i);
+                       break;
+               }
        }
 }
 
-void TimerManager::AddTimer(Timer* T)
-{
-       Timers.push_back(T);
-       sort(Timers.begin(), Timers.end(), TimerManager::TimerComparison);
-}
-
-bool TimerManager::TimerComparison( Timer *one, Timer *two)
+void TimerManager::AddTimer(Timer* t)
 {
-       return (one->GetTimer()) < (two->GetTimer());
+       Timers.insert(std::make_pair(t->GetTrigger(), t));
 }
-
-