forked from viabtc/viabtc_exchange_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nw_clt.c
290 lines (261 loc) · 7.22 KB
/
nw_clt.c
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Description:
* History: [email protected], 2016/03/22, create
*/
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <sys/time.h>
# include "nw_clt.h"
static int create_socket(int family, int sock_type)
{
int sockfd = socket(family, sock_type, 0);
if (sockfd < 0) {
return -1;
}
if (nw_sock_set_nonblock(sockfd) < 0) {
close(sockfd);
return -1;
}
if (sock_type == SOCK_STREAM && (family == AF_INET || family == AF_INET6)) {
if (nw_sock_set_no_delay(sockfd) < 0) {
close(sockfd);
return -1;
}
}
return sockfd;
}
static int set_socket_option(nw_clt *clt, int sockfd)
{
if (clt->read_mem > 0) {
if (nw_sock_set_recv_buf(sockfd, clt->read_mem) < 0) {
close(sockfd);
return -1;
}
}
if (clt->write_mem > 0) {
if (nw_sock_set_send_buf(sockfd, clt->write_mem) < 0) {
close(sockfd);
return -1;
}
}
return 0;
}
static void generate_random_path(char *path, size_t size, char *prefix, char *suffix)
{
struct timeval tv;
gettimeofday(&tv, NULL);
srand(tv.tv_sec * tv.tv_usec);
char randname[11];
for (int i = 0; i < 10; ++i) {
randname[i] = 'a' + rand() % 26;
}
randname[10] = '\0';
snprintf(path, size, "%s/%s%s%s", P_tmpdir, prefix, randname, suffix);
}
static void on_reconnect_timeout(nw_timer *timer, void *privdata)
{
nw_clt *clt = (nw_clt *)privdata;
nw_clt_start(clt);
}
static void reconnect_later(nw_clt *clt)
{
nw_timer_set(&clt->timer, clt->reconnect_timeout, false, on_reconnect_timeout, clt);
nw_timer_start(&clt->timer);
}
static void on_connect_timeout(nw_timer *timer, void *privdata)
{
nw_clt *clt = (nw_clt *)privdata;
if (!clt->on_connect_called) {
nw_clt_close(clt);
nw_clt_start(clt);
}
}
static void watch_connect(nw_clt *clt)
{
nw_timer_set(&clt->timer, clt->reconnect_timeout, false, on_connect_timeout, clt);
nw_timer_start(&clt->timer);
}
static void on_recv_fd(nw_ses *ses, int fd)
{
close(fd);
}
static int clt_close(nw_clt *clt)
{
if (nw_timer_active(&clt->timer)) {
nw_timer_stop(&clt->timer);
}
clt->connected = false;
return nw_ses_close(&clt->ses);
}
static void on_connect(nw_ses *ses, bool result)
{
nw_clt *clt = (nw_clt *)ses;
clt->on_connect_called = true;
if (result) {
clt->connected = true;
set_socket_option(clt, clt->ses.sockfd);
nw_sock_host_addr(ses->sockfd, ses->host_addr);
if (clt->type.on_connect) {
clt->type.on_connect(ses, result);
}
} else {
if (clt->type.on_connect) {
clt->type.on_connect(ses, result);
}
int ret = 0;
if (clt->type.on_close) {
ret = clt->type.on_close(&clt->ses);
}
clt_close(clt);
if (ret > 0) {
nw_clt_start(clt);
} else {
reconnect_later(clt);
}
}
}
static void on_error(nw_ses *ses, const char *msg)
{
nw_clt *clt = (nw_clt *)ses;
if (clt->type.on_error_msg) {
clt->type.on_error_msg(ses, msg);
}
if (ses->sock_type == SOCK_DGRAM)
return;
int ret = 0;
if (clt->type.on_close) {
ret = clt->type.on_close(&clt->ses);
}
clt_close(clt);
if (ret > 0) {
nw_clt_start(clt);
} else {
reconnect_later(clt);
}
}
static void on_close(nw_ses *ses)
{
nw_clt *clt = (nw_clt *)ses;
int ret = 0;
if (clt->type.on_close) {
ret = clt->type.on_close(&clt->ses);
}
clt_close(clt);
if (ret > 0) {
nw_clt_start(clt);
} else {
reconnect_later(clt);
}
}
nw_clt *nw_clt_create(nw_clt_cfg *cfg, nw_clt_type *type, void *privdata)
{
nw_loop_init();
if (cfg->max_pkg_size == 0)
return NULL;
if (type->decode_pkg == NULL)
return NULL;
if (type->on_recv_pkg == NULL)
return NULL;
nw_clt *clt = malloc(sizeof(nw_clt));
memset(clt, 0, sizeof(nw_clt));
clt->type = *type;
clt->reconnect_timeout = cfg->reconnect_timeout == 0 ? 1.0 : cfg->reconnect_timeout;
if (cfg->buf_pool) {
clt->custom_buf_pool = true;
clt->buf_pool = cfg->buf_pool;
} else {
clt->custom_buf_pool = false;
clt->buf_pool = nw_buf_pool_create(cfg->max_pkg_size);
if (clt->buf_pool == NULL) {
nw_clt_release(clt);
return NULL;
}
}
clt->read_mem = cfg->read_mem;
clt->write_mem = cfg->write_mem;
nw_addr_t *host_addr = malloc(sizeof(nw_addr_t));
if (host_addr == NULL) {
nw_clt_release(clt);
return NULL;
}
memset(host_addr, 0, sizeof(nw_addr_t));
host_addr->family = cfg->addr.family;
host_addr->addrlen = cfg->addr.addrlen;
if (nw_ses_init(&clt->ses, nw_default_loop, clt->buf_pool, cfg->buf_limit, NW_SES_TYPE_CLIENT) < 0) {
nw_clt_release(clt);
return NULL;
}
memcpy(&clt->ses.peer_addr, &cfg->addr, sizeof(nw_addr_t));
clt->ses.host_addr = host_addr;
clt->ses.sockfd = -1;
clt->ses.sock_type = cfg->sock_type;
clt->ses.privdata = privdata;
clt->ses.decode_pkg = type->decode_pkg;
clt->ses.on_recv_pkg = type->on_recv_pkg;
clt->ses.on_recv_fd = type->on_recv_fd == NULL ? on_recv_fd : type->on_recv_fd;
clt->ses.on_connect = on_connect;
clt->ses.on_error = on_error;
clt->ses.on_close = on_close;
return clt;
}
int nw_clt_start(nw_clt *clt)
{
int sockfd = create_socket(clt->ses.peer_addr.family, clt->ses.sock_type);
if (sockfd < 0) {
return -1;
}
clt->ses.sockfd = sockfd;
if (clt->ses.peer_addr.family == AF_UNIX && clt->ses.sock_type == SOCK_DGRAM) {
clt->ses.host_addr->un.sun_family = AF_UNIX;
generate_random_path(clt->ses.host_addr->un.sun_path, sizeof(clt->ses.host_addr->un.sun_path), "dgram", ".sock");
if (nw_ses_bind(&clt->ses, clt->ses.host_addr) < 0) {
return -1;
}
}
if (clt->ses.sock_type == SOCK_STREAM || clt->ses.sock_type == SOCK_SEQPACKET) {
clt->connected = false;
clt->on_connect_called = false;
int ret = nw_ses_connect(&clt->ses, &clt->ses.peer_addr);
if (ret < 0) {
if (clt->type.on_close) {
ret = clt->type.on_close(&clt->ses);
}
clt_close(clt);
if (ret > 0) {
nw_clt_start(clt);
} else {
reconnect_later(clt);
}
}
if (!clt->on_connect_called) {
watch_connect(clt);
}
return 0;
} else {
clt->connected = true;
set_socket_option(clt, clt->ses.sockfd);
nw_sock_host_addr(clt->ses.sockfd, clt->ses.host_addr);
return nw_ses_start(&clt->ses);
}
}
int nw_clt_close(nw_clt *clt)
{
if (clt->type.on_close) {
clt->type.on_close(&clt->ses);
}
return clt_close(clt);
}
void nw_clt_release(nw_clt *clt)
{
nw_ses_release(&clt->ses);
if (!clt->custom_buf_pool && clt->buf_pool) {
nw_buf_pool_release(clt->buf_pool);
}
free(clt->ses.host_addr);
free(clt);
}
bool nw_clt_connected(nw_clt *clt)
{
return clt->connected;
}