2012-04-19 20:58:29 +02:00
|
|
|
/*
|
|
|
|
* InspIRCd -- Internet Relay Chat Daemon
|
2007-07-16 17:30:04 +00:00
|
|
|
*
|
2024-06-07 10:37:56 +01:00
|
|
|
* Copyright (C) 2021 Dominic Hamon
|
2020-01-11 22:02:47 +00:00
|
|
|
* Copyright (C) 2013-2014 Attila Molnar <attilamolnar@hush.com>
|
2024-06-07 10:37:56 +01:00
|
|
|
* Copyright (C) 2013, 2021-2023 Sadie Powell <sadie@witchery.services>
|
2020-01-11 22:02:47 +00:00
|
|
|
* Copyright (C) 2012 Robby <robby@chatbelgie.be>
|
|
|
|
* Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
|
2012-04-19 20:58:29 +02:00
|
|
|
* Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
|
|
|
|
* Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
|
2022-12-30 11:31:28 +00:00
|
|
|
* Copyright (C) 2006-2007 Craig Edwards <brain@inspircd.org>
|
2007-07-16 17:30:04 +00:00
|
|
|
*
|
2012-04-19 20:58:29 +02:00
|
|
|
* 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.
|
2007-07-16 17:30:04 +00:00
|
|
|
*
|
2012-04-19 20:58:29 +02:00
|
|
|
* 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/>.
|
2007-07-16 17:30:04 +00:00
|
|
|
*/
|
|
|
|
|
2012-04-19 20:58:29 +02:00
|
|
|
|
2013-04-02 20:12:15 +01:00
|
|
|
#pragma once
|
2007-07-16 17:30:04 +00:00
|
|
|
|
2013-04-09 23:51:06 +02:00
|
|
|
class Module;
|
|
|
|
|
2007-07-16 17:30:04 +00:00
|
|
|
/** Timer class for one-second resolution timers
|
2007-10-15 21:03:30 +00:00
|
|
|
* Timer provides a facility which allows module
|
2007-07-16 17:30:04 +00:00
|
|
|
* developers to create one-shot timers. The timer
|
|
|
|
* can be made to trigger at any time up to a one-second
|
2007-10-15 21:03:30 +00:00
|
|
|
* resolution. To use Timer, inherit a class from
|
|
|
|
* Timer, then insert your inherited class into the
|
2007-07-16 17:30:04 +00:00
|
|
|
* queue using Server::AddTimer(). The Tick() method of
|
2013-04-09 23:51:06 +02:00
|
|
|
* your object (which you have to override) will be called
|
2007-07-16 17:30:04 +00:00
|
|
|
* at the given time.
|
|
|
|
*/
|
2009-09-02 00:46:11 +00:00
|
|
|
class CoreExport Timer
|
2007-07-16 17:30:04 +00:00
|
|
|
{
|
|
|
|
/** The triggering time
|
|
|
|
*/
|
2022-10-21 17:15:10 +01:00
|
|
|
time_t trigger = 0;
|
2013-04-09 23:51:06 +02:00
|
|
|
|
2007-07-16 17:30:04 +00:00
|
|
|
/** Number of seconds between triggers
|
|
|
|
*/
|
2021-05-30 20:37:54 +01:00
|
|
|
unsigned long secs;
|
2013-04-09 23:51:06 +02:00
|
|
|
|
2007-07-16 17:30:04 +00:00
|
|
|
/** True if this is a repeating timer
|
|
|
|
*/
|
|
|
|
bool repeat;
|
2013-04-09 23:51:06 +02:00
|
|
|
|
2022-01-25 13:59:42 +00:00
|
|
|
public:
|
2007-07-16 17:30:04 +00:00
|
|
|
/** Default constructor, initializes the triggering time
|
|
|
|
* @param secs_from_now The number of seconds from now to trigger the timer
|
|
|
|
* @param repeating Repeat this timer every secs_from_now seconds if set to true
|
|
|
|
*/
|
2022-05-17 12:06:26 +01:00
|
|
|
Timer(unsigned long secs_from_now, bool repeating);
|
2007-07-16 17:30:04 +00:00
|
|
|
|
2013-04-09 23:51:06 +02:00
|
|
|
/** Default destructor, removes the timer from the timer manager
|
2007-07-16 17:30:04 +00:00
|
|
|
*/
|
2013-04-09 23:51:06 +02:00
|
|
|
virtual ~Timer();
|
2007-07-16 17:30:04 +00:00
|
|
|
|
2022-05-17 11:35:14 +01:00
|
|
|
/** Retrieves the time at which this timer will tick next. If the timer is not active then 0 will be returned. */
|
2013-04-09 23:51:06 +02:00
|
|
|
time_t GetTrigger() const
|
2007-07-16 17:30:04 +00:00
|
|
|
{
|
|
|
|
return trigger;
|
|
|
|
}
|
|
|
|
|
2008-01-13 21:29:53 +00:00
|
|
|
/** Sets the trigger timeout to a new value
|
2013-04-09 23:51:06 +02:00
|
|
|
* This does not update the bookkeeping in TimerManager, use SetInterval()
|
|
|
|
* to change the interval between ticks while keeping TimerManager updated
|
2008-01-13 21:29:53 +00:00
|
|
|
*/
|
2013-04-09 23:51:06 +02:00
|
|
|
void SetTrigger(time_t nexttrigger)
|
2008-01-13 21:29:53 +00:00
|
|
|
{
|
2013-04-09 23:51:06 +02:00
|
|
|
trigger = nexttrigger;
|
2008-01-13 21:29:53 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 23:51:06 +02:00
|
|
|
/** Sets the interval between two ticks.
|
|
|
|
*/
|
2023-07-21 11:22:42 +01:00
|
|
|
void SetInterval(unsigned long interval, bool restart = true);
|
2013-04-09 23:51:06 +02:00
|
|
|
|
2007-07-16 17:30:04 +00:00
|
|
|
/** Called when the timer ticks.
|
|
|
|
* You should override this method with some useful code to
|
|
|
|
* handle the tick event.
|
2014-01-30 21:44:51 +01:00
|
|
|
* @return True if the Timer object is still valid, false if it was destructed.
|
2007-07-16 17:30:04 +00:00
|
|
|
*/
|
2021-11-05 04:24:14 +00:00
|
|
|
virtual bool Tick() = 0;
|
2007-07-16 17:30:04 +00:00
|
|
|
|
|
|
|
/** Returns true if this timer is set to repeat
|
|
|
|
*/
|
2013-04-09 23:51:06 +02:00
|
|
|
bool GetRepeat() const
|
2007-07-16 17:30:04 +00:00
|
|
|
{
|
|
|
|
return repeat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the interval (number of seconds between ticks)
|
|
|
|
* of this timer object.
|
|
|
|
*/
|
2021-05-30 20:37:54 +01:00
|
|
|
unsigned long GetInterval() const
|
2007-07-16 17:30:04 +00:00
|
|
|
{
|
|
|
|
return secs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Cancels the repeat state of a repeating timer.
|
|
|
|
* If you call this method, then the next time your
|
|
|
|
* timer ticks, it will be removed immediately after.
|
|
|
|
*/
|
|
|
|
void CancelRepeat()
|
|
|
|
{
|
|
|
|
repeat = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-10-15 21:03:30 +00:00
|
|
|
/** This class manages sets of Timers, and triggers them at their defined times.
|
2007-07-16 17:30:04 +00:00
|
|
|
* This will ensure timers are not missed, as well as removing timers that have
|
|
|
|
* expired and allowing the addition of new ones.
|
|
|
|
*/
|
2021-12-20 20:00:03 +00:00
|
|
|
class CoreExport TimerManager final
|
2007-07-16 17:30:04 +00:00
|
|
|
{
|
2014-07-10 12:19:26 +02:00
|
|
|
typedef std::multimap<time_t, Timer*> TimerMap;
|
|
|
|
|
2008-01-13 21:29:53 +00:00
|
|
|
/** A list of all pending timers
|
2007-07-16 17:30:04 +00:00
|
|
|
*/
|
2013-04-09 23:51:06 +02:00
|
|
|
TimerMap Timers;
|
2008-01-13 21:29:53 +00:00
|
|
|
|
2022-01-25 13:59:42 +00:00
|
|
|
public:
|
2007-10-15 21:03:30 +00:00
|
|
|
/** Tick all pending Timers
|
2007-07-16 17:30:04 +00:00
|
|
|
*/
|
2021-11-05 04:24:14 +00:00
|
|
|
void TickTimers();
|
2008-01-13 21:29:53 +00:00
|
|
|
|
2007-10-15 21:03:30 +00:00
|
|
|
/** Add an Timer
|
|
|
|
* @param T an Timer derived class to add
|
2007-07-16 17:30:04 +00:00
|
|
|
*/
|
2022-09-29 12:01:29 +01:00
|
|
|
void AddTimer(Timer* T);
|
2008-01-13 21:29:53 +00:00
|
|
|
|
2013-04-09 23:51:06 +02:00
|
|
|
/** Remove a Timer
|
|
|
|
* @param T an Timer derived class to remove
|
2007-07-16 17:30:04 +00:00
|
|
|
*/
|
2007-10-15 21:03:30 +00:00
|
|
|
void DelTimer(Timer* T);
|
2007-07-16 17:30:04 +00:00
|
|
|
};
|