Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 000564ed

Přidáno uživatelem Oto Šťáva před téměř 4 roky(ů)

Re #8906 - Server - Add messages for incoming connections

Zobrazit rozdíly:

deltarobot/curvedataserver.cpp
146 146
    connect(clientSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
147 147
    connect(clientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onSocketStateChanged(QAbstractSocket::SocketState)));
148 148

  
149
    qDebug() << "Client" << clientSocket->peerAddress() << "is connecting...";
149 150
    sendPreamble(clientSocket);
150 151
    _sockets.push_back(Client(clientSocket));
151 152
}
......
265 266
void CurveDataServer::handleMessage(Client& client, uint16_t messageId, void *content, size_t contentSize) {
266 267
    switch (messageId) {
267 268
    case 0x3001:
268
        qDebug() << "Magic arrived!";
269 269
        handleProtocolMagic(client, (char *) content);
270 270
        break;
271 271
    case 0x2002:
272
        qDebug() << "Version arrived!";
273 272
        handleVersion(client, *((uint8_t*) content));
274 273
        break;
275 274
    case 0x3005:
......
289 288
    if (strncmp(content, correct, 8) == 0) {
290 289
        client.magic = true;
291 290
    }
291
    validateProtocol(client);
292 292
}
293 293

  
294 294
void CurveDataServer::handleVersion(Client& client, uint8_t content)
......
297 297
    {
298 298
        client.version = true;
299 299
    }
300
    validateProtocol(client);
300 301
}
301 302

  
302 303
void CurveDataServer::handlePong(Client& client, uint64_t* content)
......
325 326
}
326 327

  
327 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

  
328 337
void CurveDataServer::terminateConnection(Client& client, std::string&& reason)
329 338
{
330 339
    if (client.socket->isOpen())
......
446 455
    QByteArray message = message64(0x3001, *((uint64_t*) messageContent));
447 456

  
448 457
    socket->write(message);
449

  
450
    qDebug() << "Sent Protocol Magic to" << socket->peerAddress();
451 458
}
452 459

  
453 460
void CurveDataServer::sendVersion(QTcpSocket *socket)
......
455 462
    QByteArray message = message32(0x2002, 1);
456 463

  
457 464
    socket->write(message);
458

  
459
    qDebug() << "Sent Protocol Version to" << socket->peerAddress();
460 465
}
461 466

  
462 467
void CurveDataServer::sendEot(QTcpSocket *socket, std::string&& reason)
......
469 474
{
470 475
    std::lock_guard<decltype (_socketsLock)> l(_socketsLock);
471 476
    for (auto& client : _sockets) {
472
        if (client.magic && client.version)
477
        if (client.protocolValid)
473 478
        {
474 479
            client.socket->write(message);
475 480
        }
......
481 486
    std::lock_guard<decltype (_socketsLock)> l(_socketsLock);
482 487
    for (auto& client : _sockets) {
483 488

  
484
        if (client.magic && client.version)
489
        if (client.protocolValid)
485 490
        {
486 491
            if (client.lastPong  < currentTimeMillis() - 10000)
487 492
            {
deltarobot/curvedataserver.h
31 31
         */
32 32
        static const size_t BUF_SIZE = 4096;
33 33

  
34
        static const size_t MESSAGE_ID_SIZE = sizeof(uint16_t);      ///< Size of the protocol message identifier.
35
        static const size_t MESSAGE_LENGTH_SIZE = sizeof(uint32_t);  ///< Size of the variable-length message's content length.
34
        static const size_t MESSAGE_ID_SIZE = sizeof(uint16_t);      ///< Size of the protocol message identifier
35
        static const size_t MESSAGE_LENGTH_SIZE = sizeof(uint32_t);  ///< Size of the variable-length message's content length
36 36

  
37 37
        QTcpSocket* socket;                 ///< Communication socket
38 38
        std::map<uint64_t, uint64_t> ping;  ///< Key: ping ID;  Value: ping timestamp (msec);
......
42 42
        char buf[BUF_SIZE];                 ///< Message buffer
43 43
        size_t bufUsed = 0;                 ///< Number of bytes used up in the message buffer
44 44

  
45
        bool magic = false;                 ///< Whether the client has sent the protocol magic
46
        bool version = false;               ///< Whether the client has sent the protocol version
47
        bool initialized = false;           ///< Whether the client has successfully initialized its communication (by sending its magic and version)
45
        bool magic = false;                 ///< Whether the client has sent the correct protocol magic
46
        bool version = false;               ///< Whether the client has sent the correct protocol version
47
        bool protocolValid = false;         ///< Whether the client has sent both the correct protocol magic and version.
48

  
49
        bool initialized = false;           ///< Whether the server has initialized this client with the current curve
48 50

  
49 51
        Client(QTcpSocket* socket);
50 52
    };
......
88 90
    void handlePong(Client& client, uint64_t* content);
89 91
    void handleEOT(Client& client, char* content, size_t contentSize);
90 92

  
93
    /**
94
     * Checks whether the specified client has already sent both its protocol magic and version. If so, the appropriate
95
     * flag is set and a log is written out into qDebug.
96
     */
97
    void validateProtocol(Client& client);
98

  
91 99
    void removeSocket(QTcpSocket *socket);
92 100
    void terminateConnection(Client& client, std::string&& reason);
93 101
    void sendPing(Client& client);

Také k dispozici: Unified diff