Revize 74adfd5f
Přidáno uživatelem Jakub Hejman 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); |
... | ... | |
133 | 132 |
|
134 | 133 |
CurveDataServer::~CurveDataServer() |
135 | 134 |
{ |
136 |
_threadIsRunning = false; |
|
137 |
_pingThread.join(); |
|
138 |
|
|
139 | 135 |
std::lock_guard<decltype (_socketsLock)> l(_socketsLock); |
140 | 136 |
for (auto& client : _sockets) |
141 | 137 |
{ |
... | ... | |
277 | 273 |
handleVersion(client, *((uint8_t*) content)); |
278 | 274 |
break; |
279 | 275 |
case 0x3005: |
280 |
qDebug() << "Pong " << *((uint64_t*) content) << " arrived!"; |
|
281 | 276 |
handlePong(client, (uint64_t*) content); |
282 | 277 |
break; |
283 | 278 |
case 0xF006: |
... | ... | |
317 | 312 |
|
318 | 313 |
client.latency = time - timeSent; |
319 | 314 |
client.lastPong = time; |
320 |
//qDebug() << "prisel pong" << number; |
|
321 | 315 |
} |
322 | 316 |
|
323 | 317 |
void CurveDataServer::handleEOT(Client& client, char* content, size_t contentSize) |
... | ... | |
326 | 320 |
std::memcpy(cstrContent, content, contentSize); |
327 | 321 |
cstrContent[contentSize] = '\0'; |
328 | 322 |
|
329 |
qDebug() << "Client" << client.socket->peerName() << "disconnected:" << cstrContent;
|
|
323 |
qDebug() << "Client" << client.socket->peerAddress() << "disconnected:" << cstrContent;
|
|
330 | 324 |
terminateConnection(client, "Recieved EOT from client"); |
331 | 325 |
} |
332 | 326 |
|
... | ... | |
340 | 334 |
client.socket->close(); |
341 | 335 |
} |
342 | 336 |
|
337 |
qDebug() << "Terminating connection to client" << client.socket->peerAddress() << "reason:" << reason.c_str(); |
|
343 | 338 |
removeSocket(client.socket); |
344 | 339 |
} |
345 | 340 |
|
... | ... | |
470 | 465 |
socket->write(message); |
471 | 466 |
} |
472 | 467 |
|
473 |
|
|
474 |
|
|
475 | 468 |
void CurveDataServer::sendMessageToAllConnected(QByteArray &message) |
476 | 469 |
{ |
477 | 470 |
std::lock_guard<decltype (_socketsLock)> l(_socketsLock); |
... | ... | |
483 | 476 |
} |
484 | 477 |
} |
485 | 478 |
|
486 |
void CurveDataServer::threadFunction() |
|
487 |
{ |
|
488 |
/* |
|
489 |
while (_threadIsRunning) |
|
490 |
{ |
|
491 |
//processPings(); |
|
492 |
|
|
493 |
// TODO tim se zpravilo varovani se socketama |
|
494 |
|
|
495 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
|
496 |
} |
|
497 |
*/ |
|
498 |
} |
|
499 |
|
|
500 | 479 |
void CurveDataServer::processPings() |
501 | 480 |
{ |
502 | 481 |
std::lock_guard<decltype (_socketsLock)> l(_socketsLock); |
... | ... | |
542 | 521 |
{ |
543 | 522 |
uint64_t number = (((uint64_t) rand()) << 32) + (uint64_t) rand(); |
544 | 523 |
|
545 |
//qDebug() << "posilam ping" << ((qint64) number) << "."; |
|
546 |
|
|
547 | 524 |
client.ping.emplace(std::make_pair(number, currentTimeMillis())); |
548 |
//client.ping.insert({number, currentTimeMillis()}); |
|
549 | 525 |
|
550 | 526 |
client.socket->write(message64(0x3004, (uint64_t) number)); |
551 | 527 |
} |
Také k dispozici: Unified diff
Server cleanup