/
client.c
executable file
·388 lines (329 loc) · 12.5 KB
/
client.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* =============================================================================
* Filename : client.c
* Summary : Handling of sockets which are connected with the db servers.
* Most of the time, sockets are waiting for data from db servers.
* These connections will be closed and restart after a time while
* exceptions occur. Authentication message is sent first always once
* connections is established.
* Compiler : gcc
*
* Version :
* Update :
* Date :
* Author :
* Org :
*
* History :
*
* ============================================================================*/
/*==============================================================================
* Header files
*=============================================================================*/
#include "client.h"
/*==============================================================================
* Interface declarations
*=============================================================================*/
extern struct ev_loop* host_loop; /* inner event loop */
extern ev_async async_wt; /* asynchronization watcher */
extern int8 Socket_init (int32* fd);
extern int8 Socket_close(int32* fd, cint8* nm);
extern int8 Socket_shutdown(int32* fd, cint8* nm);
static void Client_switch_to_start(CONN_INFO* conn);
static void Client_start_prepare (CONN_INFO* conn);
static void Client_switch_to_test(CONN_INFO* conn);
static void Client_switch_to_recv(CONN_INFO* conn);
static void Client_start_cb(struct ev_loop* lp, ev_timer* wt, int ev);
static void Client_stop_cb(struct ev_loop* lp, ev_timer* wt, int ev);
static void Client_test_cb(struct ev_loop* lp, ev_io* wt, int ev);
static void Client_recv_cb(struct ev_loop* lp, ev_io* wt, int ev);
#if 0
#endif
/*==============================================================================
* Name : int8 Client_init_acquire(DEVICE_INFO* device)
* Abstr : Acquire ip address and port of the target db servers according to
* the protocol version of each device
* Params : DEVICE_INFO* device : device information
* Return : int8 :
* EXEC_SUCS : execute success
* EXEC_FAIL : execute failed
* Modify :
*=============================================================================*/
int8 Client_init_acquire(DEVICE_INFO* device)
{
int8 err;
cint8 sql[SQL_TEXT_LEN]; /* sql text */
CONN_INFO* conn; /* temp connection */
ResultSet_T result; /* query result */
err = EXEC_SUCS;
conn = NULL;
result = NULL;
memset(&sql[0], 0x00, SQL_TEXT_LEN);
TRY {
snprintf(&sql[0], SQL_TEXT_LEN,
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
device->pt_ver);
result = Connection_executeQuery(db_conn, &sql[0]);
while ((ResultSet_next(result)) && (device->slv_num < SLV_NUM_MAX)) {
conn = NULL;
conn = &(device->s_conn[device->slv_num]);
/* initialize device pointer */
conn->dev = device;
/* ip address */
strncpy(&(conn->addr[0]), ResultSet_getString(result, 1), IP_ADDR_LEN);
/* listen port */
conn->port = (uint16)ResultSet_getInt(result, 2);
/* connection name */
snprintf(&(conn->name[0]), CONN_NM_LEN, "%s [%s:%d]",
&(device->key_id[0]), &(conn->addr[0]), conn->port);
/* prepare send data */
memcpy (&conn->s_buf[0], &(device->d_conn.r_buf[0]), RECV_LEN_MAX);
log_debug(log_cat, "%s: summon servant success", &conn->name[0]);
device->slv_num++;
}
log_debug(log_cat, "%s: [servant num=%d]", &(device->key_id[0]), device->slv_num);
(device->slv_num == 0) ? (err = EXEC_FAIL) : (err = EXEC_SUCS);
} CATCH(SQLException) {
log_fatal(log_cat, "%s: SQLException -> %s", &(device->key_id[0]),
Exception_frame.message);
err = EXEC_FAIL;
} END_TRY;
return err;
}
/*==============================================================================
* Name : void Client_start_prepare(CONN_INFO* conn)
* Abstr : Prepare authentication message for next connecting before close
* current connection
* Params : CONN_INFO* conn : connection with db server
* Return :
* Modify :
*=============================================================================*/
static void Client_start_prepare(CONN_INFO* conn)
{
uint8* ptr; /* buffer pointer */
uint16 code; /* check code */
uint16 len; /* msg length */
DEVICE_INFO* device; /* corresponding device */
code = 0;
len = 0;
ptr = &conn->s_buf[0];
device = conn->dev;
/* msg head */
len = MSG_TYPE_LEN + DEV_ID_LEN + sizeof(int16) + CRC_CODE_LEN;
*((uint16*)ptr) = len;
ptr += MSG_HEAD_LEN;
/* msg type */
*ptr = 0x0f;
ptr += 1;
/* device id */
memcpy(ptr, &(device->key_id[0]), DEV_ID_LEN);
ptr += DEV_ID_LEN;
/* can ver */
*((int16*)ptr) = device->pt_ver;
ptr += sizeof(int16);
/* crc check code */
code = CRCCode_get(&conn->s_buf[0], len);
*((uint16*)ptr) = code;
}
/*==============================================================================
* Name : void Client_start_handle(CONN_INFO* conn)
* Abstr : Start the connection with db server
* Params : CONN_INFO* conn : connection with db server
* Return :
* Modify :
*=============================================================================*/
void Client_start_handle(CONN_INFO* conn)
{
int8 err;
struct sockaddr_in addr; /* servant address info */
err = EXEC_SUCS;
memset(&addr, 0x00, sizeof(struct sockaddr_in));
/* initialize the socket fd */
err = Socket_init(&conn->fd);
if (!err) {
/* bind the address and port */
addr.sin_family = AF_INET;
addr.sin_port = htons(conn->port);
addr.sin_addr.s_addr = inet_addr(&conn->addr[0]);
/* start connection */
err = connect(conn->fd, (struct sockaddr *)(&addr), sizeof(struct sockaddr));
if ((err) && (errno != EINPROGRESS)) {
log_fatal(log_cat, "%s: connect failed [s_fd=%d] [%s]",
&conn->name[0], conn->fd, strerror(errno));
err = EXEC_FAIL;
} else {
err = EXEC_SUCS;
}
}
/* check the result of connect */
if (!err) {
Client_switch_to_test(conn);
} else {
Client_switch_to_stop(conn);
}
}
/*==============================================================================
* Name : void Client_switch_to_start(CONN_INFO* conn)
* Abstr : Start the connection restart timer
* Params : CONN_INFO* conn : connection with db server
* Return :
* Modify :
*=============================================================================*/
static void Client_switch_to_start(CONN_INFO* conn)
{
/* start the connection after 120s */
ev_timer_init(&conn->t_wt, Client_start_cb, timer_array[TID_CONN_START].t_val, 0.);
ev_timer_start(host_loop, &conn->t_wt);
/* notice the host loop */
ev_async_send(host_loop, &async_wt);
}
/*==============================================================================
* Name : void Client_switch_to_test(CONN_INFO* conn)
* Abstr : Start the socket test watcher
* Params : CONN_INFO* conn : connection with db server
* Return :
* Modify :
*=============================================================================*/
static void Client_switch_to_test(CONN_INFO* conn)
{
/* io wathcer switch to testing status */
ev_io_init(&conn->io_wt, Client_test_cb, conn->fd, EV_READ | EV_WRITE);
ev_io_start(host_loop, &conn->io_wt);
/* notice the host loop */
ev_async_send(host_loop, &async_wt);
}
/*==============================================================================
* Name : void Client_switch_to_recv(CONN_INFO* conn)
* Abstr : Start the io watcher to waiting for data
* Params : CONN_INFO* conn : connection with db server
* Return :
* Modify :
*=============================================================================*/
static void Client_switch_to_recv(CONN_INFO* conn)
{
/* io wathcer switch to waiting for data */
ev_io_init(&conn->io_wt, Client_recv_cb, conn->fd, EV_READ);
ev_io_start(host_loop, &conn->io_wt);
/* notice the host loop */
ev_async_send(host_loop, &async_wt);
}
/*==============================================================================
* Name : void Client_switch_to_stop(CONN_INFO* conn)
* Abstr : Stop the socket io watcher and close it after a moment
* Params : CONN_INFO* conn : connection with db server
* Return :
* Modify :
*=============================================================================*/
void Client_switch_to_stop(CONN_INFO* conn)
{
/* reset connection alive flag */
conn->is_alive = FALSE;
log_fatal(log_cat, "%s: catch exception", &conn->name[0]);
/* stop connection io watcher */
ev_io_stop(host_loop, &conn->io_wt);
/* do stopping handle after 10s */
ev_timer_init(&conn->t_wt, Client_stop_cb, timer_array[TID_STOP_DELAY].t_val, 0.);
ev_timer_start(host_loop, &conn->t_wt);
/* notice the host loop */
ev_async_send(host_loop, &async_wt);
}
/*==============================================================================
* Name : void Client_start_cb(struct ev_loop* lp, ev_timer* wt, int ev)
* Abstr : Time to start the connection
* Params : struct ev_loop* lp: inner event loop
* ev_timer* wt: connection start timer watcher
* int ev: specific event
* Return :
* Modify :
*=============================================================================*/
static void Client_start_cb(struct ev_loop* lp, ev_timer* wt, int ev)
{
CONN_INFO* conn; /* connection with servant server */
conn = container_of(wt, CONN_INFO, t_wt);
Client_start_handle(conn);
}
/*==============================================================================
* Name : void Client_test_cb(struct ev_loop* lp, ev_io* wt, int ev)
* Abstr : Check the connection is established successful or not
* Params : struct ev_loop* lp: inner event loop
* ev_io* wt: socket io watcher
* int ev: specific event
* Return :
* Modify :
*=============================================================================*/
static void Client_test_cb(struct ev_loop* lp, ev_io* wt, int ev)
{
int8 err;
uint16 len; /* msg length */
CONN_INFO* conn; /* connection with servant server */
err = EXEC_SUCS;
len = 0;
conn = container_of(wt, CONN_INFO, io_wt);
/* stop the socket io watcher */
ev_io_stop(host_loop, wt);
ev_async_send(host_loop, &async_wt);
/* check the result of connect */
switch (ev) {
case EV_WRITE: /* connect success */
log_debug(log_cat, "%s: connect success [s_fd=%d]", &conn->name[0], conn->fd);
conn->is_alive = TRUE;
/* send login msg */
err = Msg_send(&(conn->fd), &(conn->s_buf[0]), &(conn->name[0]));
if (!err) {
Client_switch_to_recv(conn);
} else {
Client_switch_to_stop(conn);
}
break;
case EV_READ:
case (EV_READ | EV_WRITE):
default: /* connect failed */
log_fatal(log_cat, "%s: connect failed [s_fd=%d] [%s]",
&conn->name[0], conn->fd, strerror(errno));
Client_switch_to_stop(conn);
break;
}
}
/*==============================================================================
* Name : void Client_recv_cb(struct ev_loop* lp, ev_io* wt, int ev)
* Abstr : Receive data handle
* Params : struct ev_loop* lp: inner event loop
* ev_io* wt: socket io watcher
* int ev: specific event
* Return :
* Modify :
*=============================================================================*/
static void Client_recv_cb(struct ev_loop* lp, ev_io* wt, int ev)
{
int8 err;
CONN_INFO* conn; /* connection with servant server */
err = EXEC_SUCS;
conn = container_of(wt, CONN_INFO, io_wt);
if (!err) {
err = Msg_recv(&conn->fd, &conn->r_buf[0], &conn->name[0]);
}
if (err) {
Client_switch_to_stop(conn);
}
}
/*==============================================================================
* Name : void Client_stop_cb(struct ev_loop* lp, ev_timer* wt, int ev)
* Abstr : Time to close the connection and start the re-connect timer
* Params : struct ev_loop* lp: inner event loop
* ev_timer* wt: timer watcher
* int ev: specific event
* Return :
* Modify :
*=============================================================================*/
static void Client_stop_cb(struct ev_loop* lp, ev_timer* wt, int ev)
{
CONN_INFO* conn; /* connection with servant server */
conn = container_of(wt, CONN_INFO, t_wt);
/* free socket resource */
Socket_shutdown(&conn->fd, &conn->name[0]);
Socket_close (&conn->fd, &conn->name[0]);
/* prepare reconnect msg */
memset(&conn->s_buf[0], 0x00, SEND_LEN_MAX);
Client_start_prepare(conn);
/* re-start the connection with servant server */
Client_switch_to_start(conn);
}