-
Notifications
You must be signed in to change notification settings - Fork 6
/
SocketWorker.cpp
210 lines (178 loc) · 5.41 KB
/
SocketWorker.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
203
204
205
206
207
208
209
210
#include "SocketWorker.h"
#include <QCoreApplication>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonObject>
#include <QJsonArray>
#include "NetworkEngine.h"
SocketWorker::SocketWorker(NetworkTaskQueue *taskQueue, NetworkEngine *engine)
:QThread(),
m_taskQueue(taskQueue),
m_networkEngine(engine),
m_hasTaskFinished(false),
m_stop(false),
m_socket(NULL),
m_incommingMessageSize(-1),
m_tempDataBuffer()
{
}
void SocketWorker::waitForTaskFinish()
{
while(!m_hasTaskFinished)
{
QCoreApplication::processEvents();
}
if (m_socket)
{
m_socket->deleteLater();
m_socket = NULL;
}
}
void SocketWorker::run()
{
while(!m_stop)
{
NetworkTask *task = m_taskQueue->popTask();
m_tempDataBuffer.clear();
m_hasTaskFinished = false;
m_incommingMessageSize = -1;
switch(task->getType())
{
case NetworkTask::SEND:
sendMessage((SendTask*) task);
break;
case NetworkTask::RECEIVE:
receiveMessage((ReceiveTask*)task);
break;
default:
qDebug() << "unimplemented network task:" << task->getType();
}
}
}
void SocketWorker::stop()
{
m_stop = true;
}
void SocketWorker::receiveMessage(ReceiveTask *task)
{
m_socket = new QTcpSocket(this);
connect(m_socket, SIGNAL(readyRead()), this, SLOT(onSocketReadyRead()));
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
connect(m_socket, SIGNAL(connected()), this, SLOT(onSocketConnected()));
connect(m_socket, SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
m_socket->setSocketDescriptor(task->getSocketId());
delete task;
waitForTaskFinish();
}
void SocketWorker::sendMessage(SendTask *task)
{
m_socket = new QTcpSocket(this);
m_socket->connectToHost(task->getIp(), task->getPort());
connect(m_socket, SIGNAL(readyRead()),this,SLOT(onSocketReadyRead()));
m_socket->waitForConnected();
if (m_socket->state() == QAbstractSocket::ConnectedState)
{
m_socket->write(task->getMessage());
}
delete task;
waitForTaskFinish();
}
void SocketWorker::onSocketReadyRead()
{
m_tempDataBuffer.append(m_socket->readAll());
if (m_incommingMessageSize == -1)
{
if (m_tempDataBuffer.size() > sizeof(quint32))
{
//little endian only for now
m_incommingMessageSize = *((quint32*) m_tempDataBuffer.data());
}
}
if (m_incommingMessageSize != -1 && m_tempDataBuffer.size() >= (sizeof(quint32) + m_incommingMessageSize))
{
handleIncommingMessage(m_tempDataBuffer.mid(sizeof(quint32)));
}
}
void SocketWorker::onSocketError(QAbstractSocket::SocketError error)
{
qDebug() << "Socket error:" << error;
m_hasTaskFinished = true;
}
void SocketWorker::onSocketConnected()
{
}
void SocketWorker::onSocketDisconnected()
{
}
void SocketWorker::handleIncommingMessage(QByteArray json)
{
QJsonParseError error;
QJsonDocument jdoc;
jdoc.fromJson(json, &error);
if (error.error == QJsonParseError::NoError)
{
if (jdoc.isObject())
{
QJsonObject obj = jdoc.object();
if (obj.contains("message"))
{
QString message = obj["message"].toString();
if(message == "PeerSync")
{
QJsonArray peers = obj["peers"].toArray();
foreach(QJsonValue val, peers)
{
QJsonObject pObj = val.toObject();
QHostAddress ip( pObj["ip"].toString());
unsigned short port = pObj["port"].toInt();
Peer p(ip,port);
m_networkEngine->addPeer(p);
}
}
else if(message == "Transaction")
{
Transaction t;
if (t.parseFromJsonObject(obj))
{
emit newTransaction(t);
}
}
else if(message == "Block")
{
Block b;
if (b.parseFromQJsonObject(obj))
{
emit newBlock(b);
}
}
else if(message == "QueryBlock")
{
int index = obj["index"].toInt();
emit queryBlock(index);
}
else if(message == "QueryBlockChainLength")
{
emit queryBlockChainLength();
}
else if(message == "BlockChainLength")
{
int length = obj["length"].toInt();
emit getBlockChainLength(length);
}
}
else
{
qDebug() << "ill-formed json message";
}
}
else
{
qDebug() << "ill-formed json message";
}
}
else
{
qDebug() << "ill-formed json message";
}
}