coding style cleanup

This commit is contained in:
Dima Krasner 2023-11-07 08:10:29 +02:00 committed by Felix Queißner
parent 64fd9d7484
commit e18f9e4ecb
1 changed files with 80 additions and 54 deletions

View File

@ -28,7 +28,7 @@ bool GuppyClient::startRequest(const QUrl &url, RequestOptions options)
{
Q_UNUSED(options)
if(isInProgress())
if(this->isInProgress())
return false;
if(url.scheme() != "guppy")
@ -39,43 +39,43 @@ bool GuppyClient::startRequest(const QUrl &url, RequestOptions options)
this->requested_url = url;
this->was_cancelled = false;
this->prev_seq = this->first_seq = this->last_seq = 0;
socket.connectToHost(url.host(), url.port(6775));
this->socket.connectToHost(url.host(), url.port(6775));
return true;
}
bool GuppyClient::isInProgress() const
{
return socket.isOpen();
return this->socket.isOpen();
}
bool GuppyClient::cancelRequest()
{
was_cancelled = true;
socket.close();
timer.stop();
body.clear();
chunks.clear();
this->socket.close();
this->timer.stop();
this->body.clear();
this->chunks.clear();
return true;
}
void GuppyClient::on_connected()
{
request = (requested_url.toString(QUrl::FormattingOptions(QUrl::FullyEncoded)) + "\r\n").toUtf8();
if(socket.write(request.constData(), request.size()) <= 0)
request = (this->requested_url.toString(QUrl::FormattingOptions(QUrl::FullyEncoded)) + "\r\n").toUtf8();
if(this->socket.write(request.constData(), request.size()) <= 0)
{
socket.close();
this->socket.close();
return;
}
timer.start(2000);
this->timer.start(2000);
emit this->requestStateChange(RequestState::Connected);
}
void GuppyClient::on_readRead()
{
QByteArray chunk = socket.read(65535);
QByteArray chunk = this->socket.read(65535);
if(int crlf = chunk.indexOf("\r\n"); crlf > 0) {
QByteArray header = chunk.left(crlf);
@ -87,82 +87,102 @@ void GuppyClient::on_readRead()
seq = chunk.left(space).toInt();
if(seq < 6 || seq > 2147483647) {
timer.stop();
body.clear();
chunks.clear();
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
if(seq == 4) {
emit networkError(UnknownError, meta); // error
}
else if(seq == 3) { // redirect
QUrl new_url(meta);
if(new_url.isRelative()) new_url = requested_url.resolved(new_url);
if(new_url.isRelative()) new_url = this->requested_url.resolved(new_url);
assert(not new_url.isRelative());
socket.close();
timer.stop();
body.clear();
chunks.clear();
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
socket.close();
timer.stop();
body.clear();
chunks.clear();
}
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"));
}
else {
emit networkError(ProtocolViolation, QObject::tr("invalid seq"));
}
return;
}
first_seq = seq; // success
mime = meta;
} else seq = header.toInt();
this->first_seq = seq; // success
this->mime = meta;
}
else {
seq = header.toInt();
}
if(seq < first_seq) return;
if(chunk.size() == crlf + 2) last_seq = seq; // eof
if(seq < this->first_seq) {
return;
}
if(chunk.size() == crlf + 2) { // eof
last_seq = seq;
}
else if(seq >= first_seq) { // success or continuation
if(!prev_seq || seq >= prev_seq) chunks[seq] = chunk.mid(crlf + 2, chunk.size() - crlf - 2);
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 == prev_seq + 1) timer.start();
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(socket.error(), socket.errorString());
}
else {
emitNetworkError(this->socket.error(), this->socket.errorString());
return;
}
// append all consequent chunks we have
int next_seq = prev_seq ? prev_seq + 1 : first_seq;
int next_seq = this->prev_seq ? this->prev_seq + 1 : this->first_seq;
while(next_seq != last_seq) {
QByteArray next = chunks.take(next_seq);
if(next.isNull()) break;
QByteArray next = this->chunks.take(next_seq);
if(next.isNull()) {
break;
}
body.append(next.constData(), next.size());
prev_seq = next_seq;
this->prev_seq = next_seq;
}
if(not was_cancelled) {
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(next_seq == last_seq) {
if(not was_cancelled) {
emit this->requestComplete(this->body, mime);
was_cancelled = true;
if(next_seq == this->last_seq) {
if(not this->was_cancelled) {
emit this->requestComplete(this->body, this->mime);
this->was_cancelled = true;
}
socket.close();
timer.stop();
body.clear();
chunks.clear();
this->socket.close();
this->timer.stop();
this->body.clear();
this->chunks.clear();
emit this->requestStateChange(RequestState::None);
}
@ -171,9 +191,15 @@ void GuppyClient::on_readRead()
void GuppyClient::on_timerTick()
{
QByteArray pkt;
if(prev_seq) pkt = QString("%1\r\n").arg(prev_seq).toUtf8(); // resend the last ack
else if(chunks.empty()) pkt = request; // resend the request
else return;
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;
}
socket.write(pkt.constData(), pkt.size());
this->socket.write(pkt.constData(), pkt.size());
}