lichao
2021-04-23 02ba913dc7bb5d711471b27f2ea23a897d0f2e28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * =====================================================================================
 *
 *       Filename:  proto.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年04月07日 13时48分51秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Chao (), lichao@aiotlink.com
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef PROTO_UA9UWKL1
#define PROTO_UA9UWKL1
 
#include "bhome_msg.pb.h"
#include "bhome_msg_api.pb.h"
#include <chrono>
 
using namespace bhome_msg;
 
template <class Msg>
struct MsgToType {
};
 
#define BHOME_MAP_MSG_AND_TYPE(mSG, tYPE)  \
    template <>                            \
    struct MsgToType<mSG> {                \
        static const MsgType value = tYPE; \
    };
 
#define BHOME_SIMPLE_MAP_MSG(name) BHOME_MAP_MSG_AND_TYPE(Msg##name, kMsgType##name)
 
BHOME_SIMPLE_MAP_MSG(CommonReply);
BHOME_SIMPLE_MAP_MSG(Register);
BHOME_SIMPLE_MAP_MSG(RegisterRPC);
BHOME_SIMPLE_MAP_MSG(Heartbeat);
BHOME_SIMPLE_MAP_MSG(QueryTopic);
BHOME_SIMPLE_MAP_MSG(QueryTopicReply);
BHOME_SIMPLE_MAP_MSG(RequestTopic);
BHOME_SIMPLE_MAP_MSG(RequestTopicReply);
BHOME_SIMPLE_MAP_MSG(Publish);
BHOME_SIMPLE_MAP_MSG(Subscribe);
BHOME_SIMPLE_MAP_MSG(Unsubscribe);
 
#undef BHOME_SIMPLE_MAP_MSG
#undef BHOME_MAP_MSG_AND_TYPE
 
template <class Msg>
constexpr inline MsgType GetType(const Msg &)
{
    return MsgToType<Msg>::value;
}
 
inline void SetError(ErrorMsg &em, const ErrorCode err_code, const std::string &err_str = "")
{
    em.set_errcode(err_code);
    if (!err_str.empty()) {
        em.set_errstring(err_str);
    }
}
 
template <class Reply = MsgCommonReply>
inline Reply MakeReply(const ErrorCode err_code, const std::string &err_str = "")
{
    Reply msg;
    SetError(*msg.mutable_errmsg(), err_code, err_str);
    return msg;
}
std::string NewMsgId();
BHMsgHead InitMsgHead(const MsgType type, const std::string &proc_id, const std::string &msgid);
BHMsgHead InitMsgHead(const MsgType type, const std::string &proc_id);
// inline void AddRoute(BHMsgHead &head, const MQId &id) { head.add_route()->set_mq_id(&id, sizeof(id)); }
inline bool IsSuccess(const ErrorCode ec) { return ec == eSuccess; }
bool IsMsgExpired(const BHMsgHead &head);
 
inline int64_t CountSeconds(const std::chrono::steady_clock::time_point tp)
{
    return std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch()).count();
}
inline int64_t NowSec()
{
    return CountSeconds(std::chrono::steady_clock::now());
}
 
#endif // end of include guard: PROTO_UA9UWKL1