* @brief Dispatches a validated JSON message to the appropriate handler. */
| 886 | * @brief Dispatches a validated JSON message to the appropriate handler. |
| 887 | */ |
| 888 | void API::Server::handleJsonMessage(QTcpSocket* socket, |
| 889 | ConnectionState& state, |
| 890 | const QByteArray& jsonBytes) |
| 891 | { |
| 892 | Q_ASSERT(socket); |
| 893 | Q_ASSERT(!jsonBytes.isEmpty()); |
| 894 | |
| 895 | if (!validateJsonMessage(socket, state, jsonBytes)) |
| 896 | return; |
| 897 | |
| 898 | if (MCP::isMCPMessage(jsonBytes)) { |
| 899 | auto& mcpHandler = API::MCPHandler::instance(); |
| 900 | const auto response = mcpHandler.processMessage(jsonBytes, state.sessionId); |
| 901 | |
| 902 | if (!response.isEmpty()) |
| 903 | sendResponseToSocket(socket, response); |
| 904 | |
| 905 | return; |
| 906 | } |
| 907 | |
| 908 | QString type; |
| 909 | QJsonObject json; |
| 910 | try { |
| 911 | if (!API::parseMessage(jsonBytes, type, json)) { |
| 912 | sendResponseToSocket( |
| 913 | socket, |
| 914 | CommandResponse::makeError( |
| 915 | QString(), ErrorCode::InvalidJson, QStringLiteral("Failed to parse JSON message")) |
| 916 | .toJsonBytes()); |
| 917 | return; |
| 918 | } |
| 919 | } catch (...) { |
| 920 | qWarning() << "[API] JSON parsing exception:" << state.peerAddress << ":" << state.peerPort |
| 921 | << "- Message size:" << jsonBytes.size() |
| 922 | << "- Disconnecting client (malformed or too deep JSON)"; |
| 923 | |
| 924 | disconnectClient(socket, |
| 925 | state, |
| 926 | ErrorCode::InvalidJson, |
| 927 | QStringLiteral("JSON parsing failed (malformed or too deep)")); |
| 928 | return; |
| 929 | } |
| 930 | |
| 931 | if (type == MessageType::Raw) { |
| 932 | processRawJsonCommand(socket, state, json); |
| 933 | return; |
| 934 | } |
| 935 | |
| 936 | auto& cmdHandler = API::CommandHandler::instance(); |
| 937 | sendResponseToSocket(socket, cmdHandler.processMessage(jsonBytes, CommandOrigin::Remote)); |
| 938 | } |
| 939 | |
| 940 | /** |
| 941 | * @brief Processes a JSON "raw" command that forwards base64 data to the device. |
nothing calls this directly
no test coverage detected