-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedisClt.c
executable file
·509 lines (441 loc) · 10.8 KB
/
RedisClt.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#include <sys/wait.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <poll.h>
#include <arpa/inet.h> //inet_addr,inet_aton
#include <netdb.h> //gethostbyname
#include <sys/time.h>
#include <time.h>
#include <getopt.h>
#include <error.h> //perror
#include <errno.h> //errno
#include "vos.h"
#include "RedisClt.h"
extern int Log_MsgLine(char *LogFileName,char *sLine);
#if 1
#define CachePrint(fmt,args...) do{ \
char _PrtBuf[1000]; \
sprintf(_PrtBuf,":" fmt , ## args); \
Log_MsgLine("redis.log",_PrtBuf); \
}while(0)
#else
#define CachePrint(fmt,args...)
#endif
/*
127.0.0.1:3389[1]> HGETALL 38001170301SN0000041
1) "InsId"
2) "0"
3) "TcpFd"
4) "7"
5) "InsCnt"
6) "5"
127.0.0.1:3389[1]>
除非内存掉电,否则数据将一直保存,哪怕redis-server restart也不影响。
*/
/*
TS:10:31:57 :DevStatusNotifyUrl(devId=W20000011705SN000054&devToken=9&online=1),ret=1
TS:10:32:17 :DevStatusNotifyUrl(devId=W20000011705SN000054&devToken=12&online=0),ret=1
还是一样的同一个设备多个连接问题,后删除旧连接,导致设备状态错误!
解决方案:
针对每个devid,统计连接数量InsCnt。在改变devid的状态为offline之前,看InsCnt是否为0。
如果<=0,则说明Ins都被delete了。应该设置状态为offline。
建立一个new Ins就要将InsCnt加1。为了快速检索,还是用redis作为cache。
进程重启(未掉电),初始化redis时,要将cache里的InsCnt全部置为0。
方案: 多进程共用同一个connection, 同一个db。
*/
static DB_CONN_T gRedisConn;
static pthread_mutex_t gThrdLock;
void *REDIS_Init(char *hostname,int port,char *authpass,int db)
{
int ret=0;
redisReply *reply=NULL;
if (gRedisConn.hdl!=NULL){
CachePrint("redis server already has connection!\n");
printf("redis server already has connection!\n");
gRedisConn.InsCnt++;
return (void *)gRedisConn.hdl;
}
struct timeval timeout = { 2, 500000 }; // 1.5 seconds
gRedisConn.hdl= redisConnectWithTimeout(hostname, port, timeout);
if (gRedisConn.hdl) {
if (gRedisConn.hdl->err){
CachePrint("connect error: %s\n", gRedisConn.hdl->errstr);
ret=-3;goto End;
}
reply = (redisReply*)redisCommand(gRedisConn.hdl,"AUTH %s",authpass);
if (!reply){
ret=-4;goto End;
}
if (reply->type == REDIS_REPLY_ERROR){
CachePrint("AUTH error response: %s\n", reply->str);
}
freeReplyObject(reply);
reply = (redisReply*)redisCommand(gRedisConn.hdl,"SELECT %d",db);
if (!reply){
ret=-5;goto End;
}
if (reply->type == REDIS_REPLY_ERROR){
CachePrint("SELECT error response: %s\n", reply->str);
}
freeReplyObject(reply);
reply = (redisReply*)redisCommand(gRedisConn.hdl,"FLUSHDB");
if (reply){
if (reply->type!=REDIS_REPLY_ERROR){
CachePrint("FLUSHDB success!\n");
}
freeReplyObject(reply);
}
gRedisConn.InsCnt=1;
}
else{
ret=-2;goto End;
}
End:
CachePrint("%s(db=%d),ret=%d\n",__FUNCTION__,db,ret);
if (ret<0){
REDIS_UnInit(gRedisConn.hdl);
gRedisConn.hdl = NULL;
gRedisConn.InsCnt=0;
}
pthread_mutex_init(&gThrdLock, NULL);
return (void *)gRedisConn.hdl;
}
int REDIS_UnInit(void *hdl)
{
gRedisConn.InsCnt--;
if (gRedisConn.InsCnt == 0)
{
if (gRedisConn.hdl){
redisFree(gRedisConn.hdl);gRedisConn.hdl=NULL;
pthread_mutex_destroy(&gThrdLock);
}
}
return 0;
}
int REDIS_SelectDb(void *hdl,int db)
{
redisReply *reply=NULL;
pthread_mutex_lock(&gThrdLock);
reply = (redisReply*)redisCommand(gRedisConn.hdl,"SELECT %d",db);
if ((reply) && (reply->type == REDIS_REPLY_ERROR)){
CachePrint("SELECT error response: %s\n", reply->str);
}
if (reply)
freeReplyObject(reply);
pthread_mutex_unlock(&gThrdLock);
return 0;
}
/*
针对每个devid,我要缓存[InsId,TcpFd]这两个值!
127.0.0.1:6379> HMSET Cam001 InsId 3 TcpFd 5
OK
127.0.0.1:6379> HGET Cam001 InsId
"3"
127.0.0.1:6379> HGET Cam001 TcpFd
"5"
*/
int REDIS_HMSET(char* devid,int InsId, int TcpFd)
{
int ret=-1;
redisReply *reply=NULL;
if ((!devid)||(!gRedisConn.hdl))
return -1;
if (strlen(devid)<=3)
return -2;
pthread_mutex_lock(&gThrdLock);
reply = (redisReply*)redisCommand(gRedisConn.hdl,"HMSET %s %s %d %s %d"
,devid,KEY_FIELD_INSID,InsId,KEY_FIELD_TCPFD,TcpFd);
if (reply){
CachePrint("HMSET: %s\n", reply->str);
if (reply->type == REDIS_REPLY_ERROR){
ret=-3;
}
else{
if (reply->str){
if (!strcmp(reply->str,"OK"))
ret=0;
else
ret=-4;
}
else{
ret=-5;
// 退出当前进程。因为系统异常发生了。
exit(ret);
}
}
freeReplyObject(reply);
}
pthread_mutex_unlock(&gThrdLock);
CachePrint("%s(%s = %d),ret=%d\n",__FUNCTION__,devid,TcpFd,ret);
return ret;
}
/*
TS:10:31:57 :DevStatusNotifyUrl(devId=W20000011705SN000054&devToken=9&online=1),ret=1
TS:10:32:17 :DevStatusNotifyUrl(devId=W20000011705SN000054&devToken=12&online=0),ret=1
还是一样的同一个设备多个连接问题,后删除旧连接,导致设备状态错误!
解决方案:
针对每个devid,统计连接数量InsCnt。在改变devid的状态为offline之前,看InsCnt是否为0。
如果<=0,则说明Ins都被delete了。应该设置状态为offline。
建立一个new Ins就要将InsCnt加1。为了快速检索,还是用redis作为cache。
进程重启(未掉电),初始化redis时,要将cache里的InsCnt全部置为0。
*/
int REDIS_HGET_InsCnt(char* devid)
{
int ret=-1;
redisReply *reply=NULL;
if ((!devid)||(!gRedisConn.hdl))
return -1;
pthread_mutex_lock(&gThrdLock);
reply = (redisReply*)redisCommand(gRedisConn.hdl,"HGET %s %s",devid,KEY_FIELD_INSCNT);
if (reply){
if (reply->type==REDIS_REPLY_ERROR){
ret=-2;
}
else{
if (!reply->str)
ret=-3;
else if (!strcmp(reply->str,"OK"))
ret=-4;
else{
ret=atoi(reply->str);
}
}
freeReplyObject(reply);
}
else
CachePrint("%s(%s) failed!!\n",__FUNCTION__,devid);
pthread_mutex_unlock(&gThrdLock);
CachePrint("HGET InsCnt=ret=%d\n", ret);
return ret;
}
/*
redis> HSET myhash field 5
(integer) 1
redis> HINCRBY myhash field 1
(integer) 6
redis> HINCRBY myhash field -1
(integer) 5
redis> HINCRBY myhash field -10
(integer) -5
redis>
127.0.0.1:6379> AUTH foo12345
OK
127.0.0.1:6379>
127.0.0.1:6379> select 5
OK
127.0.0.1:6379[5]> HLEN Cam001
(integer) 3
127.0.0.1:6379[5]> HINCRBY Cam001 InsCnt 1
(integer) 2
127.0.0.1:6379[5]> HINCRBY Cam001 InsCnt -1
(integer) 1
127.0.0.1:6379[5]>
*/
int REDIS_HSET_InsCnt(char* devid,int AddInsCnt)
{
int ret=-1;
redisReply *reply=NULL;
if ((!devid)||(!gRedisConn.hdl))
return -1;
if (strlen(devid)<=3)
return -2;
pthread_mutex_lock(&gThrdLock);
reply = (redisReply*)redisCommand(gRedisConn.hdl,"HINCRBY %s %s %d"
,devid,KEY_FIELD_INSCNT,AddInsCnt);
if (reply){
if (reply->type == REDIS_REPLY_ERROR)
ret=-3;
else
ret=reply->integer;
freeReplyObject(reply);
}
else
ret=-2;
pthread_mutex_unlock(&gThrdLock);
CachePrint("%s(%s,%d),ret=%d\n",__FUNCTION__,devid,AddInsCnt,ret);
return ret;
}
int REDIS_HGET_InsId(char* devid)
{
int ret=-1;
redisReply *reply=NULL;
if ((!devid)||(!gRedisConn.hdl))
return -1;
pthread_mutex_lock(&gThrdLock);
reply = (redisReply*)redisCommand(gRedisConn.hdl,"HGET %s %s",devid,KEY_FIELD_INSID);
if (reply){
CachePrint("HGET InsId=ret=%s\n", reply->str);
ret=atoi(reply->str);
freeReplyObject(reply);
}
else
CachePrint("%s(%s) failed!!\n",__FUNCTION__,devid);
pthread_mutex_unlock(&gThrdLock);
return ret;
}
int REDIS_HDEL_InsId(char* devid)
{
int ret=0;
redisReply *reply=NULL;
if ((!devid)||(!gRedisConn.hdl))
return -1;
pthread_mutex_lock(&gThrdLock);
reply = (redisReply*)redisCommand(gRedisConn.hdl,"HDEL %s %s",devid,KEY_FIELD_INSID);
if (reply){
CachePrint("HDEL InsId: %lld\n", reply->integer);
freeReplyObject(reply);
}
else
CachePrint("%s(%s) failed!!\n",__FUNCTION__,devid);
pthread_mutex_unlock(&gThrdLock);
return ret;
}
/*
[root@izwz9jay6aqdkrnnlgscchz bin]# redis-cli
127.0.0.1:6379> AUTH foo12345
OK
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> HSET app_123456 111
(error) ERR wrong number of arguments for 'hset' command
127.0.0.1:6379[1]> HSET app-dev app-123456 123456
(integer) 1
127.0.0.1:6379[1]> HSET app-dev app-123455 123455
(integer) 1
127.0.0.1:6379[1]> HVALS app-dev
1) "123456"
2) "123455"
127.0.0.1:6379[1]> HGET app-dev app-123455
"123455"
127.0.0.1:6379[1]> HGET app-dev app-12345
(nil)
127.0.0.1:6379[1]> exit
*/
int REDIS_HSET_AppFd(char* field, int sockfd)
{
int ret=-1;
redisReply *reply=NULL;
if ((!field)||(!gRedisConn.hdl))
return -1;
if (strlen(field)<=3)
return -2;
pthread_mutex_lock(&gThrdLock);
reply = (redisReply*)redisCommand(gRedisConn.hdl,"HSET %s %s %d",KEY_APP_DEV,field,sockfd);
if (reply){
CachePrint("HSET: %s\n", reply->str);
if (reply->type == REDIS_REPLY_ERROR){
ret=-3;
}
else{
ret=0;
}
freeReplyObject(reply);
}
else
CachePrint("%s(%s) failed!!\n",__FUNCTION__,field);
pthread_mutex_unlock(&gThrdLock);
CachePrint("HSET AppFd,ret=%d\n", ret);
return ret;
}
int REDIS_HGET_AppFd(char* field)
{
int ret=-1;
redisReply *reply=NULL;
if ((!field)||(!gRedisConn.hdl))
return -1;
if (strlen(field)<=3)
return -2;
pthread_mutex_lock(&gThrdLock);
reply = (redisReply*)redisCommand(gRedisConn.hdl,"HGET %s %s",KEY_APP_DEV,field);
if (reply){
if (reply->type==REDIS_REPLY_ERROR){
ret=-2;
}
else{
if (!reply->str)
ret=-3;
else if (!strcmp(reply->str,"OK"))
ret=-4;
else{
ret=atoi(reply->str);
}
}
freeReplyObject(reply);
}
else
CachePrint("%s(%s) failed!!\n",__FUNCTION__,field);
pthread_mutex_unlock(&gThrdLock);
CachePrint("HGET(AppFd=%s). ret=%d\n", field, ret);
return ret;
}
/*
127.0.0.1:6379[2]> HGET A68000011705SN000015 TcpFd
"6"
why it is so unstabilable?
TS:09:38:23 :HGET TcpFd=ret=-4
TS:09:38:23 :HGET TcpFd=ret=-4
*/
int REDIS_HGET_TcpFd(char* devid)
{
int ret=-1;
redisReply *reply=NULL;
if ((!devid)||(!gRedisConn.hdl))
return -1;
if (strlen(devid)<=3)
return -2;
pthread_mutex_lock(&gThrdLock);
reply = (redisReply*)redisCommand(gRedisConn.hdl,"HGET %s %s",devid,KEY_FIELD_TCPFD);
if (reply){
if (reply->type==REDIS_REPLY_ERROR){
ret=-2;
}
else{
if (!reply->str)
ret=-3;
else if (!strcmp(reply->str,"OK"))
ret=-4;
else{
ret=atoi(reply->str);
}
}
freeReplyObject(reply);
}
else
CachePrint("%s(%s) failed!!\n",__FUNCTION__,devid);
pthread_mutex_unlock(&gThrdLock);
CachePrint("HGET(devid=%s). TcpFd=ret=%d\n", devid, ret);
return ret;
}
int REDIS_HDEL_TcpFd(char* devid)
{
int ret=0;
redisReply *reply=NULL;
if ((!devid)||(!gRedisConn.hdl))
return -1;
pthread_mutex_lock(&gThrdLock);
reply = (redisReply*)redisCommand(gRedisConn.hdl,"HDEL %s %s",devid,KEY_FIELD_TCPFD);
if (reply){
CachePrint("HDEL TcpFd: %lld\n", reply->integer);
freeReplyObject(reply);
}
else
CachePrint("%s(%s) failed!!\n",__FUNCTION__,devid);
pthread_mutex_unlock(&gThrdLock);
return ret;
}