AGVC SDK API  0.7.0
rpc.h
浏览该文件的文档.
1/** @file rpc.h
2 * @brief 用于RPC模块的交互,如登录、连接等功能
3 */
4#ifndef AGVC_INTERFACE_RPC_H
5#define AGVC_INTERFACE_RPC_H
6
7#define AGVC_ABI
8
9#include <memory>
10#include <functional>
11
13
14namespace agvc_interface {
15
16/// RPC客户端
18{
19public:
25
26 /**
27 * @brief RpcClient
28 * @param mode 0-TCP 1-UDS
29 */
30 RpcClient(int mode = 0);
32
33 /**
34 * 设置日志处理器
35 *
36 * 此函数可设置自定义的日志处理函数来处理日志消息。 \n
37 * Agvc SDK 有一套默认的日志系统,按照默认的格式输出到默认的文件。
38 * 如果用户不希望采用默认的格式或者不希望输出到默认的文件,那就可以通过这个接口重新自定义格式,或者输出路径。
39 * 这个函数可以将用户自定义的日志系统与 Agvc SDK 默认的日志系统合并。
40 *
41 * @note setLogHandler函数要放在即将触发的日志之前,
42 * 否则会按照默认的形式输出日志。
43 *
44 * @param handler 日志处理函数 \n
45 * 此日志处理函数的下定义如下: \n
46 * void handler(int level, const char* filename, int line, const
47 * std::string& message) \n
48 * level 表示日志等级 \n
49 * &nbsp; 0: LOGLEVEL_FATAL 严重的错误 \n
50 * &nbsp; 1: LOGLEVEL_ERROR 错误 \n
51 * &nbsp; 2: LOGLEVEL_WARNING 警告 \n
52 * &nbsp; 3: LOGLEVEL_INFO 通知 \n
53 * &nbsp; 4: LOGLEVEL_DEBUG 调试 \n
54 * &nbsp; 5: LOGLEVEL_BACKTRACE 跟踪 \n
55 * filename 表示文件名 \n
56 * line 表示代码行号 \n
57 * message 表示日志信息 \n
58 * @return 无
59 */
61 std::function<void(int /*level*/, const char * /*filename*/, int /*line*/, const std::string & /*message*/)>
62 handler);
63
64 /**
65 * 连接到RPC服务
66 *
67 * @param ip IP地址
68 * @param port 端口号,RPC的端口号是30104
69 * @param ip和port为空时,采用unix domain sockets通讯方式
70 * @retval 0 RPC连接成功
71 * @retval -8 RPC连接失败,RPC连接被拒绝
72 * @retval -15 RPC连接失败,SDK版本与Server版本不兼容
73 */
74 int connect(const std::string &ip = "", int port = 0);
75
76 /**
77 * 断开RPC连接
78 *
79 * @retval 0 成功
80 * @retval -1 失败
81 */
83
84 /**
85 * 判断是否连接RPC
86 *
87 * @retval true 已连接RPC
88 * @retval false 未连接RPC
89 */
90 bool hasConnected() const;
91
92 /**
93 * 登录
94 *
95 * @param usrname 用户名
96 * @param passwd 密码
97 * @return 0
98 */
99 int login(const std::string &usrname, const std::string &passwd);
100
101 /**
102 * 登出
103 *
104 * @return 0
105 */
106 int logout();
107
108 /**
109 * 判断是否登录
110 *
111 * @retval true 已登录
112 * @retval false 未登录
113 */
115
116 /**
117 * 设置RPC请求超时时间
118 *
119 * @param timeout 请求超时时间,单位 ms
120 * @return 0
121 */
122 int setRequestTimeout(int timeout = 10);
123
124 /**
125 * 设置事件处理
126 *
127 * @param cb
128 * @return
129 */
130 int setEventHandler(std::function<void(int /*event*/)> cb);
131
132 /**
133 * 是否关闭异常抛出
134 *
135 * @param enable
136 * @return
137 */
138 int setExceptionFree(bool enable);
139
140 /**
141 * 返回错误代码
142 *
143 * @return
144 */
145 int errorCode() const;
146};
147using RpcClientPtr = std::shared_ptr<RpcClient>;
148
149} // namespace agvc_interface
150
151#define RpcClient_DECLARES \
152 _FUNC(RpcClient, 1, setLogHandler, handler) \
153 _FUNC(RpcClient, 2, connect, ip, port) \
154 _FUNC(RpcClient, 0, disconnect) \
155 _FUNC(RpcClient, 0, hasConnected) \
156 _FUNC(RpcClient, 2, login, usrname, passwd) \
157 _FUNC(RpcClient, 0, logout) \
158 _FUNC(RpcClient, 0, hasLogined) \
159 _FUNC(RpcClient, 1, setRequestTimeout, timeout) \
160 _FUNC(RpcClient, 1, setExceptionFree, timenableeout) \
161 _FUNC(RpcClient, 0, errorCode)
162
163#endif
int setRequestTimeout(int timeout=10)
设置RPC请求超时时间
int disconnect()
断开RPC连接
int setEventHandler(std::function< void(int)> cb)
设置事件处理
int errorCode() const
返回错误代码
void setLogHandler(std::function< void(int, const char *, int, const std::string &)> handler)
设置日志处理器
RpcClient(int mode=0)
RpcClient
bool hasConnected() const
判断是否连接RPC
int login(const std::string &usrname, const std::string &passwd)
登录
bool hasLogined()
判断是否登录
int setExceptionFree(bool enable)
是否关闭异常抛出
int connect(const std::string &ip="", int port=0)
连接到RPC服务
std::shared_ptr< RpcClient > RpcClientPtr
定义 rpc.h:147
#define AGVC_ABI
定义 rpc.h:7