forked from QTCode/QtRedis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qReader.cpp
202 lines (168 loc) · 3.8 KB
/
qReader.cpp
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
#include "qReader.h"
Reader::Reader(QString host, int port)
{
this->host = host;
this->port = port;
socket = new QTcpSocket;
reading = false;
connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(socketConnectionClosed()));
connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
}
Reader::~Reader()
{
delete socket;
}
void Reader::connectHost(const QString &host, quint16 port)
{
if (reading)
return;
socket->connectToHost(host, port);
}
bool Reader::isConnected() const
{
return reading;
}
void Reader::disconnectHost()
{
socket->disconnectFromHost();
}
void Reader::socketConnected()
{
reading = true;
emit connected();
}
void Reader::socketConnectionClosed()
{
reading = false;
emit disconnected();
}
void Reader::socketReadyRead()
{
QString reply("");
reply.append(socket->readAll());
readLine(reply);
}
void Reader::socketError(QAbstractSocket::SocketError e)
{
emit error(e);
}
void Reader::sendData(const QString &data)
{
if (!reading)
return;
QTextStream stream(socket);
QString test(data);
int k;
QChar c,next;
QStringList parts;
QString buffer="";
bool open=false;
for(k=0;k<test.length();k++)
{
c=test.at(k);
if(open)
{
next=k<test.length()-1?test
.at(k+1):' ';
if(c=='\\'&&next=='"')
{
buffer+='"';
k++;
}
else if(c=='"')
open=false;
else
buffer+=c;
}
else
{
if(!c.isSpace())
{
if(c=='"')
open=true;
else
buffer+=c;
}
else if(!buffer.isEmpty())
{
parts<<buffer;
buffer="";
}
}
}
if(!buffer.isEmpty())
{
parts<<buffer;
}
QString bin="";
bin.append(QString("*%1\r\n")
.arg(parts.length()));
int i;
for(i=0;i<parts.length();i++)
{
bin.append(QString("$%1\r\n")
.arg(parts.at(i)
.length()));
bin.append(QString("%1\r\n")
.arg(parts.at(i)));
}
stream<<bin;
stream.flush();
}
void Reader::readLine(const QString &reply)
{
QChar first=reply.at(0);
QString value;
QStringList result;
if(first=='+')
{
value=reply.mid(1);
value.chop(2);
result << "string" << value;
}
else if(first=='-')
{
value=reply.mid(1);
value.chop(2);
result << "error" << value;
}
else if(first==':')
{
value=reply.mid(1);
value.chop(2);
result << "integer" << value;
} else if(first=='$')
{
int index=reply.indexOf("\r\n");
int len=reply.mid(1,index-1)
.toInt();
if(len==-1)
value="NULL";
else
value=reply.mid(index+2,len);
result<< "bulk" << value;
} else if(first=='*')
{
int index=reply.indexOf("\r\n");
int count=reply.mid(1,index-1)
.toInt();
int i;
int len;
int pos=index+2;
result << "list";
for(i=0;i<count;i++)
{
index=reply.indexOf("\r\n",pos);
len=reply.mid(pos+1,index-pos-1)
.toInt();
if(len==-1)
result<<"NULL";
else
result<<reply.mid(index+2,len);
pos=index+2+len+2;
}
}
emit response(result);
}