X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Ftimer.cpp;h=0b0d8bac3664d93add0cffa572fc5666ba161403;hb=0ec19b7ac91eedc83b31c3da733e237bfe28fc48;hp=b9c87c873f0ab763396e7fac47074a073569a33d;hpb=6a869d0701bbfe3c7a5e370793adfda4b5b45c65;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/timer.cpp b/src/timer.cpp index b9c87c873..0b0d8bac3 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -1,66 +1,83 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ +/* + * InspIRCd -- Internet Relay Chat Daemon * - * InspIRCd: (C) 2002-2008 InspIRCd Development Team - * See: http://www.inspircd.org/wiki/index.php/Credits + * Copyright (C) 2008 Robin Burchell + * Copyright (C) 2007 Dennis Friis + * Copyright (C) 2006-2007 Craig Edwards + * Copyright (C) 2006 Oliver Lupton * - * 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 . */ -/* $Core */ #include "inspircd.h" -#include "timer.h" -TimerManager::TimerManager(InspIRCd* Instance) : ServerInstance(Instance) +void Timer::SetInterval(time_t 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::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::iterator i = std::find(Timers.begin(), Timers.end(), T); + std::pair 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)); } - -