Revize 04839796
Přidáno uživatelem Oto Šťáva před téměř 4 roky(ů)
deltarobot/curvedataserver.cpp | ||
---|---|---|
124 | 124 |
|
125 | 125 |
CurveDataServer::CurveDataServer() : |
126 | 126 |
_server(this), |
127 |
_pingThread([this](){threadFunction();}), |
|
128 | 127 |
_sockets() |
129 | 128 |
{ |
130 | 129 |
_server.listen(QHostAddress::Any, 4242); |
... | ... | |
139 | 138 |
terminateConnection(client, "Server is stopping"); |
140 | 139 |
} |
141 | 140 |
_sockets.clear(); |
142 |
|
|
143 |
_threadIsRunning = false; |
|
144 | 141 |
} |
145 | 142 |
|
146 | 143 |
void CurveDataServer::onNewConnection() |
... | ... | |
149 | 146 |
connect(clientSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead())); |
150 | 147 |
connect(clientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onSocketStateChanged(QAbstractSocket::SocketState))); |
151 | 148 |
|
149 |
qDebug() << "Client" << clientSocket->peerAddress() << "is connecting..."; |
|
152 | 150 |
sendPreamble(clientSocket); |
153 | 151 |
_sockets.push_back(Client(clientSocket)); |
154 | 152 |
} |
... | ... | |
268 | 266 |
void CurveDataServer::handleMessage(Client& client, uint16_t messageId, void *content, size_t contentSize) { |
269 | 267 |
switch (messageId) { |
270 | 268 |
case 0x3001: |
271 |
qDebug() << "Magic arrived!"; |
|
272 | 269 |
handleProtocolMagic(client, (char *) content); |
273 | 270 |
break; |
274 | 271 |
case 0x2002: |
275 |
qDebug() << "Version arrived!"; |
|
276 | 272 |
handleVersion(client, *((uint8_t*) content)); |
277 | 273 |
break; |
278 | 274 |
case 0x3005: |
279 |
qDebug() << "Pong " << *((uint64_t*) content) << " arrived!"; |
|
280 | 275 |
handlePong(client, (uint64_t*) content); |
281 | 276 |
break; |
282 | 277 |
case 0xF006: |
... | ... | |
293 | 288 |
if (strncmp(content, correct, 8) == 0) { |
294 | 289 |
client.magic = true; |
295 | 290 |
} |
291 |
validateProtocol(client); |
|
296 | 292 |
} |
297 | 293 |
|
298 | 294 |
void CurveDataServer::handleVersion(Client& client, uint8_t content) |
... | ... | |
301 | 297 |
{ |
302 | 298 |
client.version = true; |
303 | 299 |
} |
300 |
validateProtocol(client); |
|
304 | 301 |
} |
305 | 302 |
|
306 | 303 |
void CurveDataServer::handlePong(Client& client, uint64_t* content) |
... | ... | |
316 | 313 |
|
317 | 314 |
client.latency = time - timeSent; |
318 | 315 |
client.lastPong = time; |
319 |
//qDebug() << "prisel pong" << number; |
|
320 | 316 |
} |
321 | 317 |
|
322 | 318 |
void CurveDataServer::handleEOT(Client& client, char* content, size_t contentSize) |
... | ... | |
325 | 321 |
std::memcpy(cstrContent, content, contentSize); |
326 | 322 |
cstrContent[contentSize] = '\0'; |
327 | 323 |
|
328 |
qDebug() << "Client" << client.socket->peerName() << "disconnected:" << cstrContent;
|
|
324 |
qDebug() << "Client" << client.socket->peerAddress() << "disconnected:" << cstrContent;
|
|
329 | 325 |
terminateConnection(client, "Recieved EOT from client"); |
330 | 326 |
} |
331 | 327 |
|
332 | 328 |
|
329 |
void CurveDataServer::validateProtocol(Client &client) { |
|
330 |
if (!client.protocolValid && client.magic && client.version) { |
|
331 |
client.protocolValid = true; |
|
332 |
qDebug() << "Client" << client.socket->peerAddress() << "has successfully connected."; |
|
333 |
} |
|
334 |
} |
|
335 |
|
|
336 |
|
|
333 | 337 |
void CurveDataServer::terminateConnection(Client& client, std::string&& reason) |
334 | 338 |
{ |
335 | 339 |
if (client.socket->isOpen()) |
... | ... | |
339 | 343 |
client.socket->close(); |
340 | 344 |
} |
341 | 345 |
|
346 |
qDebug() << "Terminating connection to client" << client.socket->peerAddress() << "reason:" << reason.c_str(); |
|
342 | 347 |
removeSocket(client.socket); |
343 | 348 |
} |
344 | 349 |
|
... | ... | |
450 | 455 |
QByteArray message = message64(0x3001, *((uint64_t*) messageContent)); |
451 | 456 |
|
452 | 457 |
socket->write(message); |
453 |
|
|
454 |
qDebug() << "Sent Protocol Magic to" << socket->peerAddress(); |
|
455 | 458 |
} |
456 | 459 |
|
457 | 460 |
void CurveDataServer::sendVersion(QTcpSocket *socket) |
... | ... | |
459 | 462 |
QByteArray message = message32(0x2002, 1); |
460 | 463 |
|
461 | 464 |
socket->write(message); |
462 |
|
|
463 |
qDebug() << "Sent Protocol Version to" << socket->peerAddress(); |
|
464 | 465 |
} |
465 | 466 |
|
466 | 467 |
void CurveDataServer::sendEot(QTcpSocket *socket, std::string&& reason) |
... | ... | |
469 | 470 |
socket->write(message); |
470 | 471 |
} |
471 | 472 |
|
472 |
|
|
473 |
|
|
474 | 473 |
void CurveDataServer::sendMessageToAllConnected(QByteArray &message) |
475 | 474 |
{ |
476 | 475 |
std::lock_guard<decltype (_socketsLock)> l(_socketsLock); |
477 | 476 |
for (auto& client : _sockets) { |
478 |
if (client.magic && client.version)
|
|
477 |
if (client.protocolValid)
|
|
479 | 478 |
{ |
480 | 479 |
client.socket->write(message); |
481 | 480 |
} |
482 | 481 |
} |
483 | 482 |
} |
484 | 483 |
|
485 |
void CurveDataServer::threadFunction() |
|
486 |
{ |
|
487 |
while (_threadIsRunning) |
|
488 |
{ |
|
489 |
//processPings(); |
|
490 |
|
|
491 |
// TODO tim se zpravilo varovani se socketama |
|
492 |
|
|
493 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
|
494 |
} |
|
495 |
} |
|
496 |
|
|
497 | 484 |
void CurveDataServer::processPings() |
498 | 485 |
{ |
499 | 486 |
std::lock_guard<decltype (_socketsLock)> l(_socketsLock); |
500 | 487 |
for (auto& client : _sockets) { |
501 | 488 |
|
502 |
if (client.magic && client.version)
|
|
489 |
if (client.protocolValid)
|
|
503 | 490 |
{ |
504 | 491 |
if (client.lastPong < currentTimeMillis() - 10000) |
505 | 492 |
{ |
... | ... | |
539 | 526 |
{ |
540 | 527 |
uint64_t number = (((uint64_t) rand()) << 32) + (uint64_t) rand(); |
541 | 528 |
|
542 |
//qDebug() << "posilam ping" << ((qint64) number) << "."; |
|
543 |
|
|
544 | 529 |
client.ping.emplace(std::make_pair(number, currentTimeMillis())); |
545 |
//client.ping.insert({number, currentTimeMillis()}); |
|
546 | 530 |
|
547 | 531 |
client.socket->write(message64(0x3004, (uint64_t) number)); |
548 | 532 |
} |
Také k dispozici: Unified diff
Re #8906 - Server and client refactor