lichao
2021-04-13 3e191ac65bd65f678e9a344163f74d181726f6bd
refactor, add TODO.
2个文件已修改
31 ■■■■ 已修改文件
src/failed_msg.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/timed_queue.h 30 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/failed_msg.h
@@ -23,6 +23,7 @@
#include "timed_queue.h"
#include <string>
//TODO change storage to map<Remote,list<TimedData<Msg>>>, to avoid retry failed remotes messages.
class FailedMsgQ
{
    typedef std::function<bool(void *)> Func;
src/timed_queue.h
@@ -24,23 +24,37 @@
#include <string>
template <class Data, class ClockType = std::chrono::steady_clock>
class TimedQueue
class TimedData
{
public:
    typedef ClockType Clock;
    typedef typename Clock::time_point TimePoint;
    typedef typename Clock::duration Duration;
private:
    struct Record {
        TimePoint expire_;
        Data data_;
        Record(const TimePoint &expire, const Data &data) :
    TimedData(const TimePoint &expire, const Data &data) :
            expire_(expire), data_(data) {}
        Record(const TimePoint &expire, Data &&data) :
    TimedData(const TimePoint &expire, Data &&data) :
            expire_(expire), data_(std::move(data)) {}
        bool Expired() { return Clock::now() > expire_; }
    Data &data() { return data_; }
    Data const &data() const { return data_; }
private:
    TimePoint expire_;
    Data data_;
    };
template <class Data, class ClockType = std::chrono::steady_clock>
class TimedQueue
{
    typedef TimedData<Data, ClockType> Record;
public:
    typedef typename Record::Clock Clock;
    typedef typename Record::TimePoint TimePoint;
    typedef typename Record::Duration Duration;
private:
    typedef std::list<Record> Queue;
    Synced<Queue> queue_;
@@ -62,7 +76,7 @@
            do {
                if (it->Expired()) {
                    it = q.erase(it);
                } else if (func(it->data_)) {
                } else if (func(it->data())) {
                    it = q.erase(it);
                } else {
                    ++it;