1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_hourglass.hpp

98 lines
3.1 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2018-01-07 02:58:53 +00:00
* Copyright (c) 2013-2018 Winlin
2017-03-25 09:21:39 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SRS_APP_HOURGLASS_HPP
#define SRS_APP_HOURGLASS_HPP
#include <srs_core.hpp>
#include <map>
/**
* the handler for the tick.
*/
class ISrsHourGlass
{
public:
ISrsHourGlass();
virtual ~ISrsHourGlass();
public:
/**
* notify the handler, the type and tick.
*/
2018-01-01 11:39:57 +00:00
virtual srs_error_t notify(int type, int interval, int64_t tick) = 0;
};
/**
* the hourglass used to do some specieal task,
* while these task is cycle when some interval, for example,
* there are N=3 tasks to do:
* 1. heartbeat every 3s.
* 2. print message every 5s.
* 3. notify backend every 7s.
* the hourglass will call back when ticks:
* 1. notify(type=1, time=3)
* 2. notify(type=2, time=5)
* 3. notify(type=1, time=6)
* 4. notify(type=3, time=7)
* 5. notify(type=1, time=9)
* 6. notify(type=2, time=10)
* this is used for server and bocar server and other manager.
2017-03-25 09:21:39 +00:00
*
* Usage:
* SrsHourGlass* hg = new SrsHourGlass(handler, 1000);
* hg->tick(1, 3000);
* hg->tick(2, 5000);
* hg->tick(3, 7000);
* // create a thread to cycle, which will call handerl when ticked.
* while (true) {
* hg->cycle();
* }
*/
class SrsHourGlass
{
private:
ISrsHourGlass* handler;
int resolution;
// key: the type of tick.
// value: the interval of tick.
std::map<int, int> ticks;
// the total elapsed time,
// for each cycle, we increase it with a resolution.
int64_t total_elapse;
public:
SrsHourGlass(ISrsHourGlass* h, int resolution_ms);
virtual ~SrsHourGlass();
public:
// add a pair of tick(type, interval).
// @param type the type of tick.
// @param interval the interval in ms of tick.
2018-01-01 11:39:57 +00:00
virtual srs_error_t tick(int type, int interval);
public:
// cycle the hourglass, which will sleep resolution every time.
// and call handler when ticked.
2018-01-01 11:39:57 +00:00
virtual srs_error_t cycle();
};
#endif