commit 000564ed24ec122e699c6ff24e4149b23f3bcd6a
Author: Oto Šťáva <oto.stava@gmail.com>
Date:   Mon May 24 09:12:20 2021 +0200

    Re #8906 - Server - Add messages for incoming connections

diff --git a/deltarobot/curvedataserver.cpp b/deltarobot/curvedataserver.cpp
index 8ccaa63..51c1124 100644
--- a/deltarobot/curvedataserver.cpp
+++ b/deltarobot/curvedataserver.cpp
@@ -146,6 +146,7 @@ void CurveDataServer::onNewConnection()
     connect(clientSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
     connect(clientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onSocketStateChanged(QAbstractSocket::SocketState)));
 
+    qDebug() << "Client" << clientSocket->peerAddress() << "is connecting...";
     sendPreamble(clientSocket);
     _sockets.push_back(Client(clientSocket));
 }
@@ -265,11 +266,9 @@ void CurveDataServer::onReadyRead()
 void CurveDataServer::handleMessage(Client& client, uint16_t messageId, void *content, size_t contentSize) {
     switch (messageId) {
     case 0x3001:
-        qDebug() << "Magic arrived!";
         handleProtocolMagic(client, (char *) content);
         break;
     case 0x2002:
-        qDebug() << "Version arrived!";
         handleVersion(client, *((uint8_t*) content));
         break;
     case 0x3005:
@@ -289,6 +288,7 @@ void CurveDataServer::handleProtocolMagic(Client& client, char *content)
     if (strncmp(content, correct, 8) == 0) {
         client.magic = true;
     }
+    validateProtocol(client);
 }
 
 void CurveDataServer::handleVersion(Client& client, uint8_t content)
@@ -297,6 +297,7 @@ void CurveDataServer::handleVersion(Client& client, uint8_t content)
     {
         client.version = true;
     }
+    validateProtocol(client);
 }
 
 void CurveDataServer::handlePong(Client& client, uint64_t* content)
@@ -325,6 +326,14 @@ void CurveDataServer::handleEOT(Client& client, char* content, size_t contentSiz
 }
 
 
+void CurveDataServer::validateProtocol(Client &client) {
+    if (!client.protocolValid && client.magic && client.version) {
+        client.protocolValid = true;
+        qDebug() << "Client" << client.socket->peerAddress() << "has successfully connected.";
+    }
+}
+
+
 void CurveDataServer::terminateConnection(Client& client, std::string&& reason)
 {
     if (client.socket->isOpen())
@@ -446,8 +455,6 @@ void CurveDataServer::sendProtocolMagic(QTcpSocket *socket)
     QByteArray message = message64(0x3001, *((uint64_t*) messageContent));
 
     socket->write(message);
-
-    qDebug() << "Sent Protocol Magic to" << socket->peerAddress();
 }
 
 void CurveDataServer::sendVersion(QTcpSocket *socket)
@@ -455,8 +462,6 @@ void CurveDataServer::sendVersion(QTcpSocket *socket)
     QByteArray message = message32(0x2002, 1);
 
     socket->write(message);
-
-    qDebug() << "Sent Protocol Version to" << socket->peerAddress();
 }
 
 void CurveDataServer::sendEot(QTcpSocket *socket, std::string&& reason)
@@ -469,7 +474,7 @@ void CurveDataServer::sendMessageToAllConnected(QByteArray &message)
 {
     std::lock_guard<decltype (_socketsLock)> l(_socketsLock);
     for (auto& client : _sockets) {
-        if (client.magic && client.version)
+        if (client.protocolValid)
         {
             client.socket->write(message);
         }
@@ -481,7 +486,7 @@ void CurveDataServer::processPings()
     std::lock_guard<decltype (_socketsLock)> l(_socketsLock);
     for (auto& client : _sockets) {
 
-        if (client.magic && client.version)
+        if (client.protocolValid)
         {
             if (client.lastPong  < currentTimeMillis() - 10000)
             {
diff --git a/deltarobot/curvedataserver.h b/deltarobot/curvedataserver.h
index 6018438..586bae0 100644
--- a/deltarobot/curvedataserver.h
+++ b/deltarobot/curvedataserver.h
@@ -31,8 +31,8 @@ public:
          */
         static const size_t BUF_SIZE = 4096;
 
-        static const size_t MESSAGE_ID_SIZE = sizeof(uint16_t);      ///< Size of the protocol message identifier.
-        static const size_t MESSAGE_LENGTH_SIZE = sizeof(uint32_t);  ///< Size of the variable-length message's content length.
+        static const size_t MESSAGE_ID_SIZE = sizeof(uint16_t);      ///< Size of the protocol message identifier
+        static const size_t MESSAGE_LENGTH_SIZE = sizeof(uint32_t);  ///< Size of the variable-length message's content length
 
         QTcpSocket* socket;                 ///< Communication socket
         std::map<uint64_t, uint64_t> ping;  ///< Key: ping ID;  Value: ping timestamp (msec);
@@ -42,9 +42,11 @@ public:
         char buf[BUF_SIZE];                 ///< Message buffer
         size_t bufUsed = 0;                 ///< Number of bytes used up in the message buffer
 
-        bool magic = false;                 ///< Whether the client has sent the protocol magic
-        bool version = false;               ///< Whether the client has sent the protocol version
-        bool initialized = false;           ///< Whether the client has successfully initialized its communication (by sending its magic and version)
+        bool magic = false;                 ///< Whether the client has sent the correct protocol magic
+        bool version = false;               ///< Whether the client has sent the correct protocol version
+        bool protocolValid = false;         ///< Whether the client has sent both the correct protocol magic and version.
+
+        bool initialized = false;           ///< Whether the server has initialized this client with the current curve
 
         Client(QTcpSocket* socket);
     };
@@ -88,6 +90,12 @@ private:
     void handlePong(Client& client, uint64_t* content);
     void handleEOT(Client& client, char* content, size_t contentSize);
 
+    /**
+     * Checks whether the specified client has already sent both its protocol magic and version. If so, the appropriate
+     * flag is set and a log is written out into qDebug.
+     */
+    void validateProtocol(Client& client);
+
     void removeSocket(QTcpSocket *socket);
     void terminateConnection(Client& client, std::string&& reason);
     void sendPing(Client& client);
