/*
|
* =====================================================================================
|
*
|
* Filename: sendq.h
|
*
|
* Description:
|
*
|
* Version: 1.0
|
* Created: 2021年04月14日 09时22分59秒
|
* Revision: none
|
* Compiler: gcc
|
*
|
* Author: Li Chao (), lichao@aiotlink.com
|
* Organization:
|
*
|
* =====================================================================================
|
*/
|
#ifndef SENDQ_IWKMSK7M
|
#define SENDQ_IWKMSK7M
|
|
#include "defs.h"
|
#include "msg.h"
|
#include "timed_queue.h"
|
#include <deque>
|
#include <functional>
|
#include <string>
|
#include <unordered_map>
|
|
namespace bhome_shm
|
{
|
class ShmMsgQueue;
|
} // namespace bhome_shm
|
|
class SendQ
|
{
|
public:
|
typedef std::string Remote;
|
typedef bhome_msg::MsgI MsgI;
|
typedef std::function<void(const MsgI &msg)> OnMsgEvent;
|
struct MsgInfo {
|
MsgI msg_;
|
OnMsgEvent on_expire_;
|
// OnMsgEvent on_send_;
|
};
|
typedef TimedData<MsgInfo> TimedMsg;
|
typedef TimedMsg::TimePoint TimePoint;
|
typedef TimedMsg::Duration Duration;
|
|
void Append(const MQId &id, const MsgI &msg, OnMsgEvent onExpire = OnMsgEvent())
|
{
|
Append(std::string((const char *) &id, sizeof(id)), msg, onExpire);
|
}
|
void Append(const Remote &addr, const MsgI &msg, OnMsgEvent onExpire = OnMsgEvent())
|
{
|
using namespace std::chrono_literals;
|
Append(addr, msg, Now() + 60s, onExpire);
|
}
|
bool TrySend(bhome_shm::ShmMsgQueue &mq);
|
// bool empty() const { return store_.empty(); }
|
|
private:
|
static TimePoint Now() { return TimedMsg::Clock::now(); }
|
void Append(const Remote &addr, const MsgI &msg, const TimePoint &expire, OnMsgEvent onExpire)
|
{
|
msg.AddRef();
|
store_[addr].emplace_back(TimedMsg(expire, MsgInfo{msg, onExpire}));
|
}
|
typedef std::deque<TimedMsg> MsgList;
|
typedef std::unordered_map<Remote, MsgList> Store;
|
|
Store store_;
|
};
|
|
#endif // end of include guard: SENDQ_IWKMSK7M
|