1 |
681d1a97
|
Jakub Hejman
|
#include "curvedataserver.h"
|
2 |
|
|
|
3 |
|
|
#include <QDebug>
|
4 |
|
|
#include <QHostAddress>
|
5 |
|
|
#include <QAbstractSocket>
|
6 |
f06d5ae1
|
Jakub Hejman
|
#include <QVector4D>
|
7 |
0cda8e8d
|
Oto Šťáva
|
#include <chrono>
|
8 |
|
|
|
9 |
9360266c
|
Jakub Hejman
|
#include "../element/trajectory.hpp"
|
10 |
0cda8e8d
|
Oto Šťáva
|
|
11 |
|
|
// Utility functions ///////////////////////////////////////////////////////////////////////////////////////////////////
|
12 |
681d1a97
|
Jakub Hejman
|
|
13 |
e6053a29
|
Oto Šťáva
|
static QByteArray message8(uint16_t messageId, uint8_t content = 0) {
|
14 |
|
|
QByteArray result(2 + 1, 0);
|
15 |
|
|
result[0] = ((uint8_t*) &messageId)[0];
|
16 |
|
|
result[1] = ((uint8_t*) &messageId)[1];
|
17 |
|
|
result[2] = content;
|
18 |
|
|
return result;
|
19 |
|
|
}
|
20 |
|
|
|
21 |
|
|
static QByteArray message16(uint16_t messageId, uint16_t content = 0) {
|
22 |
|
|
QByteArray result(2 + 2, 0);
|
23 |
|
|
result[0] = ((uint8_t*) &messageId)[0];
|
24 |
|
|
result[1] = ((uint8_t*) &messageId)[1];
|
25 |
|
|
result[2] = ((uint8_t*) &content)[0];
|
26 |
|
|
result[3] = ((uint8_t*) &content)[1];
|
27 |
|
|
return result;
|
28 |
|
|
}
|
29 |
|
|
|
30 |
|
|
static QByteArray message32(uint16_t messageId, uint32_t content = 0) {
|
31 |
|
|
QByteArray result(2 + 4, 0);
|
32 |
|
|
result[0] = ((uint8_t*) &messageId)[0];
|
33 |
|
|
result[1] = ((uint8_t*) &messageId)[1];
|
34 |
|
|
result[2] = ((uint8_t*) &content)[0];
|
35 |
|
|
result[3] = ((uint8_t*) &content)[1];
|
36 |
|
|
result[4] = ((uint8_t*) &content)[2];
|
37 |
|
|
result[5] = ((uint8_t*) &content)[3];
|
38 |
|
|
return result;
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
static QByteArray message64(uint16_t messageId, uint64_t content = 0) {
|
42 |
|
|
QByteArray result(2 + 8, 0);
|
43 |
|
|
result[0] = ((uint8_t*) &messageId)[0];
|
44 |
|
|
result[1] = ((uint8_t*) &messageId)[1];
|
45 |
|
|
result[2] = ((uint8_t*) &content)[0];
|
46 |
|
|
result[3] = ((uint8_t*) &content)[1];
|
47 |
|
|
result[4] = ((uint8_t*) &content)[2];
|
48 |
|
|
result[5] = ((uint8_t*) &content)[3];
|
49 |
|
|
result[6] = ((uint8_t*) &content)[4];
|
50 |
|
|
result[7] = ((uint8_t*) &content)[5];
|
51 |
|
|
result[8] = ((uint8_t*) &content)[6];
|
52 |
|
|
result[9] = ((uint8_t*) &content)[7];
|
53 |
|
|
return result;
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
static QByteArray message128(uint16_t messageId,
|
57 |
|
|
uint32_t contentX = 0,
|
58 |
|
|
uint32_t contentY = 0,
|
59 |
|
|
uint32_t contentZ = 0,
|
60 |
|
|
uint32_t contentW = 0)
|
61 |
|
|
{
|
62 |
|
|
QByteArray result(2 + 16, 0);
|
63 |
|
|
result[0] = ((uint8_t*) &messageId)[0];
|
64 |
|
|
result[1] = ((uint8_t*) &messageId)[1];
|
65 |
|
|
|
66 |
|
|
result[2] = ((uint8_t*) &contentX)[0];
|
67 |
|
|
result[3] = ((uint8_t*) &contentX)[1];
|
68 |
|
|
result[4] = ((uint8_t*) &contentX)[2];
|
69 |
|
|
result[5] = ((uint8_t*) &contentX)[3];
|
70 |
|
|
|
71 |
|
|
result[6] = ((uint8_t*) &contentY)[0];
|
72 |
|
|
result[7] = ((uint8_t*) &contentY)[1];
|
73 |
|
|
result[8] = ((uint8_t*) &contentY)[2];
|
74 |
|
|
result[9] = ((uint8_t*) &contentY)[3];
|
75 |
|
|
|
76 |
|
|
result[10] = ((uint8_t*) &contentZ)[0];
|
77 |
|
|
result[11] = ((uint8_t*) &contentZ)[1];
|
78 |
|
|
result[12] = ((uint8_t*) &contentZ)[2];
|
79 |
|
|
result[13] = ((uint8_t*) &contentZ)[3];
|
80 |
|
|
|
81 |
|
|
result[14] = ((uint8_t*) &contentW)[0];
|
82 |
|
|
result[15] = ((uint8_t*) &contentW)[1];
|
83 |
|
|
result[16] = ((uint8_t*) &contentW)[2];
|
84 |
|
|
result[17] = ((uint8_t*) &contentW)[3];
|
85 |
|
|
return result;
|
86 |
|
|
}
|
87 |
|
|
|
88 |
0cda8e8d
|
Oto Šťáva
|
static QByteArray messageVarLength(uint16_t messageId, uint32_t length, const char* content = nullptr) {
|
89 |
e6053a29
|
Oto Šťáva
|
QByteArray result(2 + 4 + length, 0);
|
90 |
|
|
result[0] = ((uint8_t*) &messageId)[0];
|
91 |
|
|
result[1] = ((uint8_t*) &messageId)[1];
|
92 |
|
|
result[2] = ((uint8_t*) &length)[0];
|
93 |
|
|
result[3] = ((uint8_t*) &length)[1];
|
94 |
|
|
result[4] = ((uint8_t*) &length)[2];
|
95 |
|
|
result[5] = ((uint8_t*) &length)[3];
|
96 |
0cda8e8d
|
Oto Šťáva
|
|
97 |
|
|
if (content) {
|
98 |
|
|
for (uint32_t i = 0; i < length; i++) {
|
99 |
|
|
result[6 + i] = content[i];
|
100 |
|
|
}
|
101 |
|
|
}
|
102 |
e6053a29
|
Oto Šťáva
|
return result;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
9360266c
|
Jakub Hejman
|
inline uint64_t currentTimeMillis() {
|
106 |
0cda8e8d
|
Oto Šťáva
|
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
107 |
|
|
std::chrono::steady_clock::now().time_since_epoch())
|
108 |
|
|
.count();
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
// Client //////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
113 |
|
|
|
114 |
|
|
CurveDataServer::Client::Client(QTcpSocket* socket) :
|
115 |
|
|
socket(socket),
|
116 |
|
|
lastPong(currentTimeMillis()),
|
117 |
|
|
bufUsed(0)
|
118 |
|
|
{}
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
// Server //////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
122 |
|
|
|
123 |
681d1a97
|
Jakub Hejman
|
CurveDataServer::CurveDataServer() :
|
124 |
|
|
_server(this),
|
125 |
9360266c
|
Jakub Hejman
|
_pingThread([this](){threadFunction();}),
|
126 |
681d1a97
|
Jakub Hejman
|
_sockets()
|
127 |
|
|
{
|
128 |
|
|
_server.listen(QHostAddress::Any, 4242);
|
129 |
|
|
connect(&_server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
|
130 |
|
|
}
|
131 |
|
|
|
132 |
0cda8e8d
|
Oto Šťáva
|
CurveDataServer::~CurveDataServer()
|
133 |
|
|
{
|
134 |
|
|
std::lock_guard<decltype (_socketsLock)> l(_socketsLock);
|
135 |
|
|
for (auto& client : _sockets)
|
136 |
|
|
{
|
137 |
9360266c
|
Jakub Hejman
|
terminateConnection(client, "Server is stopping");
|
138 |
0cda8e8d
|
Oto Šťáva
|
}
|
139 |
|
|
_sockets.clear();
|
140 |
9360266c
|
Jakub Hejman
|
|
141 |
|
|
_threadIsRunning = false;
|
142 |
0cda8e8d
|
Oto Šťáva
|
}
|
143 |
|
|
|
144 |
681d1a97
|
Jakub Hejman
|
void CurveDataServer::onNewConnection()
|
145 |
|
|
{
|
146 |
|
|
QTcpSocket *clientSocket = _server.nextPendingConnection();
|
147 |
|
|
connect(clientSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
|
148 |
|
|
connect(clientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onSocketStateChanged(QAbstractSocket::SocketState)));
|
149 |
|
|
|
150 |
e60ade62
|
Jakub Hejman
|
sendPreamble(clientSocket);
|
151 |
0cda8e8d
|
Oto Šťáva
|
_sockets.push_back(Client(clientSocket));
|
152 |
681d1a97
|
Jakub Hejman
|
}
|
153 |
|
|
|
154 |
|
|
void CurveDataServer::onSocketStateChanged(QAbstractSocket::SocketState socketState)
|
155 |
|
|
{
|
156 |
|
|
if (socketState == QAbstractSocket::UnconnectedState)
|
157 |
|
|
{
|
158 |
|
|
QTcpSocket* sender = static_cast<QTcpSocket*>(QObject::sender());
|
159 |
0cda8e8d
|
Oto Šťáva
|
|
160 |
9360266c
|
Jakub Hejman
|
removeSocket(sender);
|
161 |
681d1a97
|
Jakub Hejman
|
}
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
void CurveDataServer::onReadyRead()
|
165 |
|
|
{
|
166 |
0cda8e8d
|
Oto Šťáva
|
std::lock_guard<decltype (_socketsLock)> l(_socketsLock);
|
167 |
|
|
|
168 |
681d1a97
|
Jakub Hejman
|
QTcpSocket* sender = static_cast<QTcpSocket*>(QObject::sender());
|
169 |
0cda8e8d
|
Oto Šťáva
|
auto it = clientOf(sender);
|
170 |
|
|
if (it == _sockets.end()) {
|
171 |
|
|
return;
|
172 |
|
|
}
|
173 |
|
|
Client& client = *it;
|
174 |
|
|
|
175 |
|
|
while (sender->bytesAvailable()) {
|
176 |
|
|
if (client.bufUsed < Client::MESSAGE_ID_SIZE) {
|
177 |
|
|
qint64 toRead = qMin(qint64(Client::MESSAGE_ID_SIZE - client.bufUsed), sender->bytesAvailable());
|
178 |
|
|
client.bufUsed += sender->read(&client.buf[client.bufUsed], toRead);
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
if (client.bufUsed < 2) {
|
182 |
|
|
// no ID yet, wait for more data
|
183 |
|
|
return;
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
uint16_t messageId = *((uint16_t*) client.buf);
|
187 |
|
|
qint64 contentSize;
|
188 |
|
|
switch (messageId & 0xF000) {
|
189 |
|
|
case 0x0000:
|
190 |
|
|
contentSize = 1;
|
191 |
|
|
break;
|
192 |
|
|
case 0x1000:
|
193 |
|
|
contentSize = 2;
|
194 |
|
|
break;
|
195 |
|
|
case 0x2000:
|
196 |
|
|
contentSize = 4;
|
197 |
|
|
break;
|
198 |
|
|
case 0x3000:
|
199 |
|
|
contentSize = 8;
|
200 |
|
|
break;
|
201 |
|
|
case 0x4000:
|
202 |
|
|
contentSize = 16;
|
203 |
|
|
break;
|
204 |
|
|
case 0xF000:
|
205 |
|
|
// TODO - implement support for variable length messages
|
206 |
9360266c
|
Jakub Hejman
|
contentSize = 0;
|
207 |
|
|
break;
|
208 |
0cda8e8d
|
Oto Šťáva
|
default:
|
209 |
|
|
// unsupported message size
|
210 |
|
|
return;
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
qint64 toRead = qMin(qint64(contentSize - (client.bufUsed - Client::MESSAGE_ID_SIZE)), sender->bytesAvailable());
|
214 |
|
|
if (toRead > 0) {
|
215 |
|
|
client.bufUsed += sender->read(&client.buf[client.bufUsed], toRead);
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
if (client.bufUsed == (Client::MESSAGE_ID_SIZE + contentSize)) {
|
219 |
|
|
handleMessage(client, messageId, &client.buf[Client::MESSAGE_ID_SIZE]);
|
220 |
|
|
client.bufUsed = 0;
|
221 |
|
|
}
|
222 |
681d1a97
|
Jakub Hejman
|
}
|
223 |
|
|
}
|
224 |
|
|
|
225 |
0cda8e8d
|
Oto Šťáva
|
void CurveDataServer::handleMessage(Client& client, uint16_t messageId, void *content) {
|
226 |
|
|
switch (messageId) {
|
227 |
|
|
case 0x3001:
|
228 |
|
|
qDebug() << "Magic arrived!";
|
229 |
9360266c
|
Jakub Hejman
|
handleProtocolMagic(client, (char *) content);
|
230 |
0cda8e8d
|
Oto Šťáva
|
break;
|
231 |
|
|
case 0x2002:
|
232 |
|
|
qDebug() << "Version arrived!";
|
233 |
9360266c
|
Jakub Hejman
|
handleVersion(client, *((uint8_t*) content));
|
234 |
0cda8e8d
|
Oto Šťáva
|
break;
|
235 |
|
|
case 0x3005:
|
236 |
|
|
qDebug() << "Pong " << *((uint64_t*) content) << " arrived!";
|
237 |
9360266c
|
Jakub Hejman
|
handlePong(client, content);
|
238 |
|
|
break;
|
239 |
|
|
case 0xF006:
|
240 |
|
|
qDebug() << "EOT!";
|
241 |
|
|
handleEOT(client, content);
|
242 |
0cda8e8d
|
Oto Šťáva
|
break;
|
243 |
681d1a97
|
Jakub Hejman
|
}
|
244 |
|
|
}
|
245 |
|
|
|
246 |
9360266c
|
Jakub Hejman
|
void CurveDataServer::handleProtocolMagic(Client& client, char *content)
|
247 |
|
|
{
|
248 |
|
|
char *correct = "DeltaRVr";
|
249 |
|
|
|
250 |
|
|
if (strncmp(content, correct, 8) == 0) {
|
251 |
|
|
client.magic = true;
|
252 |
|
|
}
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
void CurveDataServer::handleVersion(Client& client, uint8_t content)
|
256 |
|
|
{
|
257 |
|
|
if (((uint8_t*) &content)[0] == 1)
|
258 |
|
|
{
|
259 |
|
|
client.version = true;
|
260 |
|
|
}
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
void CurveDataServer::handlePong(Client& client, void* content)
|
264 |
|
|
{
|
265 |
|
|
long time = currentTimeMillis();
|
266 |
|
|
|
267 |
|
|
long number = *((long*) content);
|
268 |
|
|
|
269 |
|
|
long timeSent = client.ping.at(number);
|
270 |
|
|
|
271 |
|
|
client.ping.erase(number);
|
272 |
|
|
|
273 |
|
|
client.latency = time - timeSent;
|
274 |
|
|
client.lastPong = time;
|
275 |
|
|
}
|
276 |
|
|
|
277 |
|
|
void CurveDataServer::handleEOT(Client& client, void* content)
|
278 |
|
|
{
|
279 |
|
|
qDebug() << "Client" << client.socket->peerName() << "disconnected:" << content;
|
280 |
|
|
terminateConnection(client, "Recieved EOT from client");
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
|
284 |
|
|
void CurveDataServer::terminateConnection(Client& client, std::string&& reason)
|
285 |
|
|
{
|
286 |
|
|
removeSocket(client.socket);
|
287 |
|
|
|
288 |
|
|
sendEot(client.socket, std::forward<std::string&&>(reason));
|
289 |
|
|
|
290 |
|
|
client.socket->close();
|
291 |
|
|
}
|
292 |
|
|
|
293 |
e6053a29
|
Oto Šťáva
|
void CurveDataServer::sendActuatorPosition(float x, float y, float z)
|
294 |
681d1a97
|
Jakub Hejman
|
{
|
295 |
e6053a29
|
Oto Šťáva
|
QByteArray message = message128(0x4003,
|
296 |
|
|
*((uint32_t*) &x),
|
297 |
|
|
*((uint32_t*) &y),
|
298 |
|
|
*((uint32_t*) &z));
|
299 |
681d1a97
|
Jakub Hejman
|
|
300 |
9360266c
|
Jakub Hejman
|
sendMessageToAllConnected(message);
|
301 |
e60ade62
|
Jakub Hejman
|
}
|
302 |
|
|
|
303 |
f06d5ae1
|
Jakub Hejman
|
void CurveDataServer::sendActualDirection(float x, float y, float z)
|
304 |
|
|
{
|
305 |
|
|
QByteArray message = message128(0x4007,
|
306 |
|
|
*((uint32_t*) &x),
|
307 |
|
|
*((uint32_t*) &y),
|
308 |
|
|
*((uint32_t*) &z));
|
309 |
|
|
|
310 |
9360266c
|
Jakub Hejman
|
sendMessageToAllConnected(message);
|
311 |
f06d5ae1
|
Jakub Hejman
|
}
|
312 |
|
|
|
313 |
|
|
void CurveDataServer::sendTargetDirection(float x, float y, float z)
|
314 |
|
|
{
|
315 |
|
|
QByteArray message = message128(0x4008,
|
316 |
|
|
*((uint32_t*) &x),
|
317 |
|
|
*((uint32_t*) &y),
|
318 |
|
|
*((uint32_t*) &z));
|
319 |
|
|
|
320 |
9360266c
|
Jakub Hejman
|
sendMessageToAllConnected(message);
|
321 |
f06d5ae1
|
Jakub Hejman
|
}
|
322 |
|
|
|
323 |
705930ed
|
Jakub Hejman
|
void CurveDataServer::sendNewCurve(QList<QVector3D> &points)
|
324 |
f06d5ae1
|
Jakub Hejman
|
{
|
325 |
|
|
int size = points.size();
|
326 |
|
|
|
327 |
|
|
QByteArray message = messageVarLength(0xF009, (uint32_t) size * 12);
|
328 |
|
|
|
329 |
|
|
for (int i = 0; i < size; i++)
|
330 |
|
|
{
|
331 |
|
|
float xf = points[i].x();
|
332 |
|
|
float yf = points[i].y();
|
333 |
|
|
float zf = points[i].z();
|
334 |
|
|
|
335 |
|
|
uint8_t *x = ((uint8_t*) &xf);
|
336 |
|
|
uint8_t *y = ((uint8_t*) &yf);
|
337 |
|
|
uint8_t *z = ((uint8_t*) &zf);
|
338 |
|
|
|
339 |
|
|
message[2 + 4 + i*12 + 0] = x[0];
|
340 |
|
|
message[2 + 4 + i*12 + 1] = x[1];
|
341 |
|
|
message[2 + 4 + i*12 + 2] = x[2];
|
342 |
|
|
message[2 + 4 + i*12 + 3] = x[3];
|
343 |
|
|
|
344 |
|
|
message[2 + 4 + i*12 + 4] = y[0];
|
345 |
|
|
message[2 + 4 + i*12 + 5] = y[1];
|
346 |
|
|
message[2 + 4 + i*12 + 6] = y[2];
|
347 |
|
|
message[2 + 4 + i*12 + 7] = y[3];
|
348 |
|
|
|
349 |
|
|
message[2 + 4 + i*12 + 8] = z[0];
|
350 |
|
|
message[2 + 4 + i*12 + 9] = z[1];
|
351 |
|
|
message[2 + 4 + i*12 + 10] = z[2];
|
352 |
|
|
message[2 + 4 + i*12 + 11] = z[3];
|
353 |
|
|
}
|
354 |
|
|
|
355 |
9360266c
|
Jakub Hejman
|
sendMessageToAllConnected(message);
|
356 |
|
|
|
357 |
|
|
|
358 |
|
|
std::lock_guard<decltype (_currentCurveLock)> l(_currentCurveLock);
|
359 |
|
|
_currentCurve.clear();
|
360 |
|
|
_currentCurve.append(points);
|
361 |
f06d5ae1
|
Jakub Hejman
|
}
|
362 |
|
|
|
363 |
705930ed
|
Jakub Hejman
|
void CurveDataServer::sendNewCurve(QList<QVector4D> &points)
|
364 |
f06d5ae1
|
Jakub Hejman
|
{
|
365 |
|
|
QList<QVector3D> curve3d;
|
366 |
|
|
int itemCount = points.size();
|
367 |
|
|
|
368 |
|
|
curve3d.clear();
|
369 |
|
|
if (itemCount > MAX_CURVE_SIZE)
|
370 |
|
|
itemCount = MAX_CURVE_SIZE;
|
371 |
|
|
for (int i = 0; i < itemCount; i++)
|
372 |
|
|
{
|
373 |
|
|
curve3d.append(points[i].toVector3D());
|
374 |
|
|
}
|
375 |
|
|
|
376 |
|
|
sendNewCurve(curve3d);
|
377 |
|
|
}
|
378 |
|
|
|
379 |
0cda8e8d
|
Oto Šťáva
|
QList<CurveDataServer::Client>::iterator CurveDataServer::clientOf(QTcpSocket *socket) {
|
380 |
|
|
std::lock_guard<decltype (_socketsLock)> l(_socketsLock);
|
381 |
|
|
for (auto it = _sockets.begin(); it != _sockets.end(); it++) {
|
382 |
|
|
if (it->socket == socket) {
|
383 |
|
|
return it;
|
384 |
|
|
}
|
385 |
|
|
}
|
386 |
|
|
return _sockets.end();
|
387 |
|
|
}
|
388 |
e60ade62
|
Jakub Hejman
|
|
389 |
|
|
void CurveDataServer::sendPreamble(QTcpSocket *socket)
|
390 |
|
|
{
|
391 |
|
|
sendProtocolMagic(socket);
|
392 |
|
|
sendVersion(socket);
|
393 |
|
|
}
|
394 |
|
|
|
395 |
|
|
void CurveDataServer::sendProtocolMagic(QTcpSocket *socket)
|
396 |
|
|
{
|
397 |
|
|
char *messageContent = "DeltaRVr";
|
398 |
e6053a29
|
Oto Šťáva
|
QByteArray message = message64(0x3001, *((uint64_t*) messageContent));
|
399 |
e60ade62
|
Jakub Hejman
|
|
400 |
|
|
socket->write(message);
|
401 |
|
|
|
402 |
|
|
qDebug() << "Sent Protocol Magic to" << socket->peerAddress();
|
403 |
|
|
}
|
404 |
|
|
|
405 |
|
|
void CurveDataServer::sendVersion(QTcpSocket *socket)
|
406 |
|
|
{
|
407 |
e6053a29
|
Oto Šťáva
|
QByteArray message = message32(0x2002, 1);
|
408 |
e60ade62
|
Jakub Hejman
|
|
409 |
|
|
socket->write(message);
|
410 |
|
|
|
411 |
|
|
qDebug() << "Sent Protocol Version to" << socket->peerAddress();
|
412 |
681d1a97
|
Jakub Hejman
|
}
|
413 |
0cda8e8d
|
Oto Šťáva
|
|
414 |
|
|
void CurveDataServer::sendEot(QTcpSocket *socket, std::string&& reason)
|
415 |
|
|
{
|
416 |
|
|
QByteArray message = messageVarLength(0xF006, ((uint32_t) reason.length()), reason.c_str());
|
417 |
|
|
socket->write(message);
|
418 |
|
|
}
|
419 |
9360266c
|
Jakub Hejman
|
|
420 |
|
|
|
421 |
|
|
|
422 |
|
|
void CurveDataServer::sendMessageToAllConnected(QByteArray &message)
|
423 |
|
|
{
|
424 |
|
|
std::lock_guard<decltype (_socketsLock)> l(_socketsLock);
|
425 |
|
|
for (auto& client : _sockets) {
|
426 |
|
|
if (client.magic && client.version)
|
427 |
|
|
{
|
428 |
|
|
client.socket->write(message);
|
429 |
|
|
}
|
430 |
|
|
}
|
431 |
|
|
}
|
432 |
|
|
|
433 |
|
|
void CurveDataServer::threadFunction()
|
434 |
|
|
{
|
435 |
|
|
while (_threadIsRunning)
|
436 |
|
|
{
|
437 |
|
|
// odeslat ping, popr krivku vsem, take kontrola odpojeni
|
438 |
|
|
{
|
439 |
|
|
std::lock_guard<decltype (_socketsLock)> l(_socketsLock);
|
440 |
|
|
for (auto& client : _sockets) {
|
441 |
|
|
if (client.magic && client.version)
|
442 |
|
|
{
|
443 |
|
|
if (client.lastPong < currentTimeMillis() - 10000)
|
444 |
|
|
{
|
445 |
|
|
terminateConnection(client, "Timeout");
|
446 |
|
|
}
|
447 |
|
|
|
448 |
|
|
sendPing(client);
|
449 |
|
|
|
450 |
|
|
std::lock_guard<decltype (_currentCurveLock)> l(_currentCurveLock);
|
451 |
|
|
if (!client.initialized && !_currentCurve.isEmpty())
|
452 |
|
|
{
|
453 |
|
|
sendCurve(client);
|
454 |
|
|
client.initialized = true;
|
455 |
|
|
}
|
456 |
|
|
}
|
457 |
|
|
else if (client.lastPong < currentTimeMillis() - 10000)
|
458 |
|
|
{
|
459 |
|
|
terminateConnection(client, "Preamble timeout");
|
460 |
|
|
}
|
461 |
|
|
}
|
462 |
|
|
}
|
463 |
|
|
|
464 |
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
465 |
|
|
}
|
466 |
|
|
}
|
467 |
|
|
|
468 |
|
|
void CurveDataServer::removeSocket(QTcpSocket *socket)
|
469 |
|
|
{
|
470 |
|
|
std::lock_guard<decltype (_socketsLock)> l(_socketsLock);
|
471 |
|
|
for (auto it = _sockets.begin(); it != _sockets.end(); it++) {
|
472 |
|
|
if (it->socket == socket) {
|
473 |
|
|
_sockets.erase(it);
|
474 |
|
|
break;
|
475 |
|
|
}
|
476 |
|
|
}
|
477 |
|
|
}
|
478 |
|
|
|
479 |
|
|
void CurveDataServer::sendPing(Client& client)
|
480 |
|
|
{
|
481 |
|
|
uint64_t number = (((uint64_t) rand()) << 32) + (uint64_t) rand();
|
482 |
|
|
|
483 |
|
|
client.ping.emplace(std::make_pair(number, currentTimeMillis()));
|
484 |
|
|
//client.ping.insert({number, currentTimeMillis()});
|
485 |
|
|
|
486 |
|
|
client.socket->write(message64(0x3004, (uint64_t) number));
|
487 |
|
|
}
|
488 |
|
|
|
489 |
|
|
void CurveDataServer::sendCurve(Client &client)
|
490 |
|
|
{
|
491 |
|
|
int size = _currentCurve.size();
|
492 |
|
|
|
493 |
|
|
QByteArray message = messageVarLength(0xF009, (uint32_t) size * 12);
|
494 |
|
|
|
495 |
|
|
for (int i = 0; i < size; i++)
|
496 |
|
|
{
|
497 |
|
|
float xf = _currentCurve[i].x();
|
498 |
|
|
float yf = _currentCurve[i].y();
|
499 |
|
|
float zf = _currentCurve[i].z();
|
500 |
|
|
|
501 |
|
|
uint8_t *x = ((uint8_t*) &xf);
|
502 |
|
|
uint8_t *y = ((uint8_t*) &yf);
|
503 |
|
|
uint8_t *z = ((uint8_t*) &zf);
|
504 |
|
|
|
505 |
|
|
message[2 + 4 + i*12 + 0] = x[0];
|
506 |
|
|
message[2 + 4 + i*12 + 1] = x[1];
|
507 |
|
|
message[2 + 4 + i*12 + 2] = x[2];
|
508 |
|
|
message[2 + 4 + i*12 + 3] = x[3];
|
509 |
|
|
|
510 |
|
|
message[2 + 4 + i*12 + 4] = y[0];
|
511 |
|
|
message[2 + 4 + i*12 + 5] = y[1];
|
512 |
|
|
message[2 + 4 + i*12 + 6] = y[2];
|
513 |
|
|
message[2 + 4 + i*12 + 7] = y[3];
|
514 |
|
|
|
515 |
|
|
message[2 + 4 + i*12 + 8] = z[0];
|
516 |
|
|
message[2 + 4 + i*12 + 9] = z[1];
|
517 |
|
|
message[2 + 4 + i*12 + 10] = z[2];
|
518 |
|
|
message[2 + 4 + i*12 + 11] = z[3];
|
519 |
|
|
}
|
520 |
|
|
|
521 |
|
|
client.socket->write(message);
|
522 |
|
|
}
|