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
|
#include "guppyclient.hpp"
#include "ioutil.hpp"
#include "kristall.hpp"
GuppyClient::GuppyClient(QObject *parent) : ProtocolHandler(parent)
{
connect(&socket, &QUdpSocket::connected, this, &GuppyClient::on_connected);
connect(&socket, &QUdpSocket::readyRead, this, &GuppyClient::on_readRead);
connect(&timer, &QTimer::timeout, this, &GuppyClient::on_timerTick);
connect(&socket, &QAbstractSocket::hostFound, this, [this]() {
emit this->requestStateChange(RequestState::HostFound);
});
emit this->requestStateChange(RequestState::None);
}
GuppyClient::~GuppyClient()
{
}
bool GuppyClient::supportsScheme(const QString &scheme) const
{
return (scheme == "guppy");
}
bool GuppyClient::startRequest(const QUrl &url, RequestOptions options)
{
Q_UNUSED(options)
if(this->isInProgress())
return false;
if(url.scheme() != "guppy")
return false;
emit this->requestStateChange(RequestState::Started);
this->requested_url = url;
this->was_cancelled = false;
this->prev_seq = 0;
this->first_seq = 0;
this->last_seq = 0;
this->socket.connectToHost(url.host(), url.port(6775));
return true;
}
bool GuppyClient::isInProgress() const
{
return this->socket.isOpen();
}
bool GuppyClient::cancelRequest()
{
was_cancelled = true;
this->socket.close();
this->timer.stop();
this->body.clear();
this->chunks.clear();
return true;
}
void GuppyClient::on_connected()
{
request = (this->requested_url.toString(QUrl::FormattingOptions(QUrl::FullyEncoded)) + "\r\n").toUtf8();
if(this->socket.write(request.constData(), request.size()) <= 0)
{
this->socket.close();
return;
}
this->timer.start(2000);
emit this->requestStateChange(RequestState::Connected);
}
void GuppyClient::on_readRead()
{
QByteArray chunk = this->socket.read(65535);
if(int crlf = chunk.indexOf("\r\n"); crlf > 0) {
QByteArray header = chunk.left(crlf);
// first response packet (success, error or redirect) header should contain a space
int seq = -1;
if(int space = header.indexOf(' '); space > 0) {
QByteArray meta = header.mid(space + 1);
seq = chunk.left(space).toInt();
if(seq < 6 || seq > 2147483647) {
this->timer.stop();
this->body.clear();
this->chunks.clear();
emit this->requestStateChange(RequestState::None);
if(seq == 4) {
emit networkError(UnknownError, meta); // error
}
else if(seq == 3) { // redirect
QUrl new_url(meta);
if(new_url.isRelative()) new_url = this->requested_url.resolved(new_url);
assert(not new_url.isRelative());
this->socket.close();
this->timer.stop();
this->body.clear();
this->chunks.clear();
emit this->requestStateChange(RequestState::None);
emit redirected(new_url, false);
}
else if(seq == 1) { // input prompt
this->socket.close();
this->timer.stop();
this->body.clear();
this->chunks.clear();
emit this->requestStateChange(RequestState::None);
emit inputRequired(meta, false);
}
else {
emit networkError(ProtocolViolation, QObject::tr("invalid seq"));
}
return;
}
this->first_seq = seq; // success
this->mime = meta;
}
else {
seq = header.toInt();
}
if(seq < this->first_seq) {
return;
}
if(chunk.size() == crlf + 2) { // eof
last_seq = seq;
}
else if(seq >= first_seq) { // success or continuation
if(!this->prev_seq || seq >= this->prev_seq) {
this->chunks[seq] = chunk.mid(crlf + 2, chunk.size() - crlf - 2);
}
// postpone the timer when we receive the next packet
if(seq == this->prev_seq + 1) {
this->timer.start();
}
}
// acknowledge every valid packet we receive
QByteArray ack = QString("%1\r\n").arg(seq).toUtf8();
socket.write(ack.constData(), ack.size());
}
else {
emitNetworkError(this->socket.error(), this->socket.errorString());
return;
}
// append all consequent chunks we have
int next_seq = this->prev_seq ? this->prev_seq + 1 : this->first_seq;
while(next_seq != last_seq) {
QByteArray next = this->chunks.take(next_seq);
if(next.isNull()) {
break;
}
body.append(next.constData(), next.size());
this->prev_seq = next_seq++;
}
if(not this->was_cancelled) {
emit this->requestProgress(body.size());
}
// we're done when the last appended chunk is the one before the eof chunk
if(this->last_seq && next_seq == this->last_seq) {
if(not this->was_cancelled) {
emit this->requestComplete(this->body, this->mime);
this->was_cancelled = true;
}
this->socket.close();
this->timer.stop();
this->body.clear();
this->chunks.clear();
emit this->requestStateChange(RequestState::None);
}
}
void GuppyClient::on_timerTick()
{
QByteArray pkt;
if(this->prev_seq) { // resend the last ack
pkt = QString("%1\r\n").arg(this->prev_seq).toUtf8();
}
else if(this->chunks.empty()) { // resend the request
pkt = request;
}
else {
return;
}
this->socket.write(pkt.constData(), pkt.size());
}
|