forked from LongXQ/RESP-client
/
main.c
141 lines (131 loc) · 3.09 KB
/
main.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
#include "hiredis.h"
#define SDSBUFSIZE 1024
sds getlinefromconsolo(void *b){
sds buf=(sds)b;
char c;
while((c=getchar())!='\n'){
buf=sdscatlen(buf,&c,1);
}
return buf;
}
void processStringReply(redisReply *reply,void **buf){
int len=reply->len;
*buf=sdscatlen(*buf,"$",1);
sds num=sdsfromlonglong(len);
*buf=sdscatlen(*buf,num,sdslen(num));
*buf=sdscatlen(*buf,"\\r\\n",4);
*buf=sdscatlen(*buf,reply->str,len);
*buf=sdscatlen(*buf,"\\r\\n",4);
}
void processErrorReply(redisReply *reply, void **buf){
int len=reply->len;
*buf=sdscatlen(*buf,"-",1);
*buf=sdscatlen(*buf,reply->str,len);
}
void processIntegerReply(redisReply *reply, void **buf){
long long integer=reply->integer;
sds num=sdsfromlonglong(integer);
*buf=sdscatlen(*buf,":",1);
*buf=sdscatlen(*buf,num,sdslen(num));
*buf=sdscatlen(*buf,"\\r\\n",4);
}
void processNilReply(redisReply *reply, void **buf){
int len=reply->len;
if(len==-1){
*buf=sdscatlen(*buf,"$-1\\r\\n",7);
}else{
*buf=sdscatlen(*buf,"*-1\\r\\n",7);
}
}
void processStatusReply(redisReply *reply, void **buf){
int len=reply->len;
*buf=sdscatlen(*buf,"+",1);
*buf=sdscatlen(*buf,reply->str,len);
*buf=sdscatlen(*buf,"\\r\\n",4);
}
int processLineReply(redisReply *reply,void **buf){
switch(reply->type){
case REDIS_REPLY_STRING:
processStringReply(reply,buf);
break;
case REDIS_REPLY_ERROR:
processErrorReply(reply,buf);
break;
case REDIS_REPLY_INTEGER:
processIntegerReply(reply,buf);
break;
case REDIS_REPLY_NIL:
processNilReply(reply,buf);
break;
case REDIS_REPLY_STATUS:
processStatusReply(reply,buf);
break;
default:
return REDIS_ERR;
}
return REDIS_OK;
}
int processMultiBulkReply(redisReply *reply, void **buf){
int elements=reply->elements;
*buf=sdscatlen(*buf,"*",1);
sds num=sdsfromlonglong(elements);
*buf=sdscatlen(*buf,num,sdslen(num));
*buf=sdscatlen(*buf,"\\r\\n",4);
int i=0;
for(;i<elements;i++){
redisReply *tmpReply=reply->element[i];
switch(tmpReply->type){
case REDIS_REPLY_STRING:
case REDIS_REPLY_ERROR:
case REDIS_REPLY_INTEGER:
case REDIS_REPLY_NIL:
case REDIS_REPLY_STATUS:
processLineReply(tmpReply,buf);
break;
case REDIS_REPLY_ARRAY:
processMultiBulkReply(tmpReply,buf);
break;
}
}
}
int main(int argc, char **argv){
if(argc!=3){
printf("Wrong arguments\n");
}
char *addr=argv[1];
int port=strtol(argv[2],NULL,10);
redisContext *c=redisConnect(addr,port);
if(c!=NULL && c->err){
printf("Error: %s\n", c->errstr);
return 1;
}
printf("RESP> ");
sds buf=sdsempty();
if(buf==NULL){
printf("%s\n", "out of memory");
}
while(1){
buf=getlinefromconsolo(buf);
redisReply *reply= redisCommand(c, buf);
sdsclear(buf);
int type=reply->type;
switch(type){
case REDIS_REPLY_STRING:
case REDIS_REPLY_ERROR:
case REDIS_REPLY_INTEGER:
case REDIS_REPLY_NIL:
case REDIS_REPLY_STATUS:
processLineReply(reply,&buf);
break;
case REDIS_REPLY_ARRAY:
processMultiBulkReply(reply,&buf);
break;
default:
printf("%s\n", "Unkown reply type");
}
printf("%s\n", buf);
sdsclear(buf);
printf("RESP> ");
}
return 0;
}