forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrpc.h
250 lines (214 loc) · 8.28 KB
/
librpc.h
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/******************************************************************************
* Copyright (C) 2014-2018 Zhifeng Gong <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libraries; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#ifndef LIBRPC_H
#define LIBRPC_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/uio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <libgevent.h>
#include <libthread.h>
#ifdef __cplusplus
extern "C" {
#endif
/******************************************************************************
* rpc_packet define (little endian)
* [rpc_header][rpc_payload]
*
* rpc_header define
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
* |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | destination_uuid=32 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | source_uuid=32 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | message_id=32 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+ time_stamp=64 +-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | payload_len=32 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | checksum=32 |
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* uuid is hash_of(connect_ip_info), generated by rpc server
*
* connect_ip_info define
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
* |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | socket_fd=32 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | ip_addr=32 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | ip_port=16 |x x x x x x x x x x x x x x x x|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* destination_uuid is message send to
* source_uuid is message send from
*
* message_id define
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
* |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | group_id=7 |unused=5 |R|D|P=2| cmd_id=16 |
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
* [31~25]: group id
* - max support 128 group, can be used to service group
* [24~20]: unused
* [ 19]: return indicator
* - 0: no need return
* - 1: need return
* [ 18]: direction
* - 0: UP client to server
* - 1: DOWN server to client
* [17~16]: parser of payload message
* - 0 json
* - 1 protobuf
* - 2 unused
* - 3 unused
* [15~ 0]: cmd id, defined in librpc_stub.h
* - 0 ~ 7 inner cmd
* - 8 ~ 255 user cmd
*
* Note: how to add a new bit define, e.g. foo:
* 1. add foo define in this commet;
* 2. define RPC_FOO_BIT and RPC_FOO_MASK, and add into BUILD_RPC_MSG_ID;
* 3. define GET_RPC_FOO;
* 4. define enum of foo value;
******************************************************************************/
typedef struct rpc_header {
uint32_t uuid_dst;
uint32_t uuid_src;
uint32_t msg_id;
uint64_t time_stamp;
uint32_t payload_len;
uint32_t checksum;
} rpc_header_t;
typedef struct rpc_packet {
struct rpc_header header;
void *payload;
} rpc_packet_t;
enum rpc_state {
rpc_inited,
rpc_connected,
rpc_disconnect,
};
typedef struct rpc {
int fd;
struct rpc_packet send_pkt;
struct rpc_packet recv_pkt;
struct gevent_base *evbase;
struct gevent *ev;
struct thread *dispatch_thread;
enum rpc_state state;
} rpc_t;
typedef int (*rpc_callback)(struct rpc *r, void *arg, int len);
typedef struct msg_handler {
uint32_t msg_id;
rpc_callback cb;
} msg_handler_t;
struct rpc *rpc_create(const char *host, uint16_t port);
int rpc_set_cb(struct rpc *r,
void (*on_read)(int fd, void *arg),
void (*on_write)(int fd, void *arg),
void (*on_error)(int fd, void *arg), void *arg);
int rpc_echo(struct rpc *r, const void *buf, size_t len);
int rpc_call(struct rpc *r, uint32_t cmd_id,
const void *in_arg, size_t in_len,
void *out_arg, size_t out_len);
int rpc_peer_call(struct rpc *r, uint32_t uuid, uint32_t cmd_id,
const void *in_arg, size_t in_len,
void *out_arg, size_t out_len);
int rpc_send(struct rpc *r, const void *buf, size_t len);
struct iovec *rpc_recv_buf(struct rpc *r);
int rpc_dispatch(struct rpc *r);
void rpc_destroy(struct rpc *r);
int rpc_packet_parse(struct rpc *r);
msg_handler_t *find_msg_handler(uint32_t msg_id);
int process_msg(struct rpc *r, struct iovec *buf);
void dump_buffer(void *buf, int len);
void dump_packet(struct rpc_packet *r);
void print_packet(struct rpc_packet *r);
int register_msg_map(msg_handler_t *map, int num_entry);
#define RPC_REGISTER_MSG_MAP(map_name) \
register_msg_map(__msg_action_map##map_name, \
(sizeof(__msg_action_map##map_name )/sizeof(msg_handler_t)));
#define BEGIN_RPC_MAP(map_name) \
static msg_handler_t __msg_action_map##map_name[] = {
#define RPC_MAP(x, y) {x, y},
#define END_RPC_MAP() };
#define RPC_MSG_ID_MASK 0xFFFFFFFF
#define RPC_GROUP_BIT (25)
#define RPC_GROUP_MASK 0x07
#define RPC_RET_BIT (19)
#define RPC_RET_MASK 0x01
#define RPC_DIR_BIT (18)
#define RPC_DIR_MASK 0x01
#define RPC_PARSE_BIT (16)
#define RPC_PARSE_MASK 0x03
#define RPC_CMD_BIT (0)
#define RPC_CMD_MASK 0xFF
#define BUILD_RPC_MSG_ID(group, ret, dir, parse, cmd) \
(((((uint32_t)group) & RPC_GROUP_MASK) << RPC_GROUP_BIT) | \
((((uint32_t)ret) & RPC_RET_MASK) << RPC_RET_BIT) | \
((((uint32_t)dir) & RPC_DIR_MASK) << RPC_DIR_BIT) | \
((((uint32_t)parse) & RPC_PARSE_MASK) << RPC_PARSE_BIT) | \
((((uint32_t)cmd) & RPC_CMD_MASK) << RPC_CMD_BIT))
#define IS_RPC_MSG_NEED_RETURN(cmd) \
(((cmd & RPC_MSG_ID_MASK)>>RPC_RET_BIT) & RPC_RET_MASK)
#define GET_RPC_MSG_GROUP(cmd) \
(((cmd & RPC_MSG_ID_MASK)>>RPC_GROUP_BIT) & RPC_GROUP_MASK)
#define GET_RPC_MSG_DIR(cmd) \
(((cmd & RPC_MSG_ID_MASK)>>RPC_DIR_BIT) & RPC_DIR_MASK)
#define GET_RPC_MSG_PARSE(cmd) \
(((cmd & RPC_MSG_ID_MASK)>>RPC_PARSE_BIT) & RPC_PARSE_MASK)
enum rpc_direction {
_RPC_DIR_UP = 0,
_RPC_DIR_DOWN = 1,
};
enum rpc_parser {
_RPC_PARSE_JSON = 0,
_RPC_PARSE_PROTOBUF = 1,
};
enum rpc_return {
_RPC_NO_RETURN = 0,
_RPC_NEED_RETURN = 1,
};
enum rpc_cmd_inner {
_RPC_INNER_0 = 0,
_RPC_INNER_1 = 1,
_RPC_INNER_2 = 2,
_RPC_INNER_3 = 3,
_RPC_INNER_4 = 4,
_RPC_INNER_5 = 5,
_RPC_INNER_6 = 6,
_RPC_INNER_7 = 7,
_RPC_USER_BASE = 8,
};
#ifdef __cplusplus
}
#endif
#endif