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

93 lines
3.2 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2019-01-01 13:37:28 +00:00
* Copyright (c) 2013-2019 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>
2019-04-30 00:24:52 +00:00
// The handler for the tick.
class ISrsHourGlass
{
public:
ISrsHourGlass();
virtual ~ISrsHourGlass();
public:
2019-04-30 00:24:52 +00:00
// When time is ticked, this function is called.
2019-04-18 00:11:16 +00:00
virtual srs_error_t notify(int type, srs_utime_t interval, srs_utime_t tick) = 0;
};
2019-04-30 00:24:52 +00:00
// he 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.
//
// Usage:
// SrsHourGlass* hg = new SrsHourGlass(handler, 1 * SRS_UTIME_MILLISECONDS);
// hg->tick(1, 3 * SRS_UTIME_MILLISECONDS);
// hg->tick(2, 5 * SRS_UTIME_MILLISECONDS);
// hg->tick(3, 7 * SRS_UTIME_MILLISECONDS);
// // create a thread to cycle, which will call handerl when ticked.
// while (true) {
// hg->cycle();
// }
class SrsHourGlass
{
private:
ISrsHourGlass* handler;
2019-04-18 00:11:16 +00:00
srs_utime_t _resolution;
2019-04-30 00:24:52 +00:00
// The ticks:
// key: the type of tick.
// value: the interval of tick.
2019-04-18 00:11:16 +00:00
std::map<int, srs_utime_t> ticks;
2019-04-30 00:24:52 +00:00
// The total elapsed time,
// for each cycle, we increase it with a resolution.
2019-04-18 00:11:16 +00:00
srs_utime_t total_elapse;
public:
2019-04-18 00:11:16 +00:00
SrsHourGlass(ISrsHourGlass* h, srs_utime_t resolution);
virtual ~SrsHourGlass();
public:
2019-04-30 00:24:52 +00:00
// Add a pair of tick(type, interval).
// @param type the type of tick.
2019-04-18 00:11:16 +00:00
// @param interval the interval in srs_utime_t of tick.
virtual srs_error_t tick(int type, srs_utime_t interval);
public:
2019-04-30 00:24:52 +00:00
// 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