This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
/
Connection.cpp
130 lines (115 loc) · 3.1 KB
/
Connection.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
#include "Connection.h"
#include "WebPage.h"
#include "Visit.h"
#include "Find.h"
#include "Command.h"
#include "Reset.h"
#include "Node.h"
#include "Url.h"
#include "Source.h"
#include "Evaluate.h"
#include "Execute.h"
#include <QTcpSocket>
#include <iostream>
Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
QObject(parent) {
m_socket = socket;
m_page = page;
m_command = NULL;
m_expectingDataSize = -1;
connect(m_socket, SIGNAL(readyRead()), this, SLOT(checkNext()));
}
void Connection::checkNext() {
if (m_expectingDataSize == -1) {
if (m_socket->canReadLine()) {
readLine();
checkNext();
}
} else {
if (m_socket->bytesAvailable() >= m_expectingDataSize) {
readDataBlock();
checkNext();
}
}
}
void Connection::readLine() {
char buffer[128];
qint64 lineLength = m_socket->readLine(buffer, 128);
if (lineLength != -1) {
buffer[lineLength - 1] = 0;
processNext(buffer);
}
}
void Connection::readDataBlock() {
char *buffer = new char[m_expectingDataSize + 1];
m_socket->read(buffer, m_expectingDataSize);
buffer[m_expectingDataSize] = 0;
processNext(buffer);
m_expectingDataSize = -1;
delete buffer;
}
void Connection::processNext(const char *data) {
if (m_commandName.isNull()) {
m_commandName = data;
m_argumentsExpected = -1;
} else {
processArgument(data);
}
}
void Connection::processArgument(const char *data) {
if (m_argumentsExpected == -1) {
m_argumentsExpected = QString(data).toInt();
} else if (m_expectingDataSize == -1) {
m_expectingDataSize = QString(data).toInt();
} else {
m_arguments.append(data);
}
if (m_arguments.length() == m_argumentsExpected) {
if (m_page->isLoading())
connect(m_page, SIGNAL(loadFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
else
startCommand();
}
}
void Connection::startCommand() {
m_command = createCommand(m_commandName.toAscii().constData());
if (m_command) {
connect(m_command,
SIGNAL(finished(bool, QString &)),
this,
SLOT(finishCommand(bool, QString &)));
m_command->start(m_arguments);
} else {
QString failure = QString("Unknown command: ") + m_commandName + "\n";
writeResponse(false, failure);
}
m_commandName = QString();
}
Command *Connection::createCommand(const char *name) {
#include "find_command.h"
return NULL;
}
void Connection::pendingLoadFinished(bool success) {
m_page->disconnect(this, SLOT(pendingLoadFinished(bool)));
if (success) {
startCommand();
} else {
QString response = m_page->failureString();
finishCommand(false, response);
}
}
void Connection::finishCommand(bool success, QString &response) {
m_command->deleteLater();
m_command = NULL;
m_arguments.clear();
writeResponse(success, response);
}
void Connection::writeResponse(bool success, QString &response) {
if (success)
m_socket->write("ok\n");
else
m_socket->write("failure\n");
QString responseLength = QString::number(response.size()) + "\n";
m_socket->write(responseLength.toAscii());
m_socket->write(response.toAscii());
}